Skip to content

Commit ac86e96

Browse files
committed
Reject invalid patch document containers
1 parent c39058e commit ac86e96

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎jsonpatch.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,18 @@ def __init__(self, patch, pointer_cls=JsonPointer):
518518
self.patch = patch
519519
self.pointer_cls = pointer_cls
520520

521+
if isinstance(patch, (str, bytes, Mapping)):
522+
raise InvalidJsonPatch("Document is expected to be sequence of operations")
523+
try:
524+
patch_iter = iter(patch)
525+
except TypeError:
526+
raise InvalidJsonPatch("Document is expected to be sequence of operations")
527+
521528
# Verify that the structure of the patch document
522529
# is correct by retrieving each patch element.
523530
# Much of the validation is done in the initializer
524531
# though some is delayed until the patch is applied.
525-
for op in self.patch:
532+
for op in patch_iter:
526533
# We're only checking for strings in the following check
527534
# for two reasons:
528535
#

‎property_tests.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ def test_result_shares_nothing_with_dst(self, docs):
324324

325325
class ApplyPatchProperties(unittest.TestCase):
326326

327+
@given(st.one_of(json_scalars, st.dictionaries(json_keys, json_docs)))
328+
@example(patch={})
329+
@example(patch='')
330+
def test_non_array_patch_documents_are_rejected(self, patch):
331+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
332+
jsonpatch.JsonPatch(patch)
333+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
334+
jsonpatch.JsonPatch.from_string(json.dumps(patch))
335+
327336
@given(docs_with_patches())
328337
def test_does_not_modify_document(self, case):
329338
doc, patch = case

‎tests.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,35 @@ def test_copy_from_pointer(self):
15331533

15341534
class JsonPatchCreationTest(unittest.TestCase):
15351535

1536+
def test_creation_rejects_non_sequence_documents(self):
1537+
for patch in ({}, {'op': 'add', 'path': '/a', 'value': 1},
1538+
MappingProxyType({}), '', b'', None, True, False,
1539+
0, 1, 1.5):
1540+
with self.subTest(patch=patch):
1541+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
1542+
jsonpatch.JsonPatch(patch)
1543+
1544+
def test_serialized_patch_rejects_non_array_documents(self):
1545+
for patch in ({}, {'op': 'add', 'path': '/a', 'value': 1},
1546+
'', 'text', None, True, False, 0, 1, 1.5):
1547+
for serialized in (json.dumps(patch), json.dumps(patch).encode()):
1548+
with self.subTest(patch=serialized):
1549+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
1550+
jsonpatch.JsonPatch.from_string(serialized)
1551+
doc = {'a': 'unchanged'}
1552+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
1553+
jsonpatch.apply_patch(doc, serialized, in_place=True)
1554+
self.assertEqual(doc, {'a': 'unchanged'})
1555+
1556+
def test_creation_preserves_operation_sequences(self):
1557+
for operations in ([], (),
1558+
[{'op': 'add', 'path': '/a', 'value': 1}],
1559+
({'op': 'add', 'path': '/a', 'value': 1},)):
1560+
with self.subTest(operations=operations):
1561+
patch = jsonpatch.JsonPatch(operations)
1562+
self.assertIs(patch.patch, operations)
1563+
self.assertEqual(patch.apply({}), {'a': 1} if operations else {})
1564+
15361565
def test_creation_fails_with_invalid_patch(self):
15371566
invalid_patches = [
15381567
{ 'path': '/foo', 'value': 'bar'},

0 commit comments

Comments
 (0)