reject null op/path/from fields in json_patch_apply

This commit is contained in:
Javid Khan
2026-07-16 12:55:41 +05:30
parent 0ea5f6c615
commit eed664e06a
3 changed files with 29 additions and 1 deletions
+12
View File
@@ -209,6 +209,10 @@ static int json_patch_apply_move_copy(struct json_object **res,
} }
from_s = json_object_get_string(jfrom); from_s = json_object_get_string(jfrom);
if (from_s == NULL) {
_set_err(EINVAL, "Patch object 'from' field is not a string");
return -1;
}
from_s_len = strlen(from_s); from_s_len = strlen(from_s);
if (strncmp(from_s, path, from_s_len) == 0) { if (strncmp(from_s, path, from_s_len) == 0) {
@@ -320,11 +324,19 @@ int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
return -1; return -1;
} }
op = json_object_get_string(jop); op = json_object_get_string(jop);
if (op == NULL) {
_set_err(EINVAL, "Patch object 'op' field is not a string");
return -1;
}
if (!json_object_object_get_ex(patch_elem, "path", &jpath)) { if (!json_object_object_get_ex(patch_elem, "path", &jpath)) {
_set_err(EINVAL, "Patch object does not contain 'path' field"); _set_err(EINVAL, "Patch object does not contain 'path' field");
return -1; return -1;
} }
path = json_object_get_string(jpath); // Note: empty string is ok! path = json_object_get_string(jpath); // Note: empty string is ok!
if (path == NULL) {
_set_err(EINVAL, "Patch object 'path' field is not a string");
return -1;
}
if (!strcmp(op, "test")) if (!strcmp(op, "test"))
rc = json_patch_apply_test(base, patch_elem, path, patch_error); rc = json_patch_apply_test(base, patch_elem, path, patch_error);
+12
View File
@@ -542,6 +542,18 @@
"patch": [{"op": "copy", "from": "/a", "path": "/b"}, "patch": [{"op": "copy", "from": "/a", "path": "/b"},
{"op": "copy", "from": "/b", "path": "/a/p/x"}], {"op": "copy", "from": "/b", "path": "/a/p/x"}],
"expected": {"a": {"p": {"x": {"p": {}}}}, "b": {"p": {}}} "expected": {"a": {"p": {"x": {"p": {}}}}, "b": {"p": {}}}
},
{ "comment": "null op field must be rejected, not dereferenced",
"doc": {"foo": "bar"},
"patch": [{"op": null, "path": "/foo"}],
"error": "a null op field should fail rather than crash"
},
{ "comment": "null from field must be rejected, not dereferenced",
"doc": {"foo": "bar"},
"patch": [{"op": "move", "from": null, "path": "/foo"}],
"error": "a null from field should fail rather than crash"
} }
] ]
+5 -1
View File
@@ -123,7 +123,7 @@ Testing 'test add with bad number should fail', doc '[ "foo", "sil" ]' patch '[
Testing 'missing 'path' parameter', doc '{ }' patch '[ { "op": "add", "value": "bar" } ]' : OK Testing 'missing 'path' parameter', doc '{ }' patch '[ { "op": "add", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain 'path' field => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object does not contain 'path' field
Testing ''path' parameter with null value', doc '{ }' patch '[ { "op": "add", "path": null, "value": "bar" } ]' : OK Testing ''path' parameter with null value', doc '{ }' patch '[ { "op": "add", "path": null, "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'path' field is not a string
Testing 'invalid JSON Pointer token', doc '{ }' patch '[ { "op": "add", "path": "foo", "value": "bar" } ]' : OK Testing 'invalid JSON Pointer token', doc '{ }' patch '[ { "op": "add", "path": "foo", "value": "bar" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Failed to set value at path referenced by 'path' field
Testing 'missing 'value' parameter to add', doc '[ 1 ]' patch '[ { "op": "add", "path": "\/-" } ]' : OK Testing 'missing 'value' parameter to add', doc '[ 1 ]' patch '[ { "op": "add", "path": "\/-" } ]' : OK
@@ -157,3 +157,7 @@ Testing 'Removing nonexistent index', doc '[ "foo", "bar" ]' patch '[ { "op": "r
=> json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field => json_patch_apply failed as expected: ERRNO=ENOENT at patch idx 0: Did not find element referenced by path field
Testing 'Patch with different capitalisation than doc', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/FOO", "value": "BAR" } ]' : OK Testing 'Patch with different capitalisation than doc', doc '{ "foo": "bar" }' patch '[ { "op": "add", "path": "\/FOO", "value": "BAR" } ]' : OK
Testing 'copy must duplicate the value, not alias it, so no cycle can form', doc '{ "a": { "p": { } } }' patch '[ { "op": "copy", "from": "\/a", "path": "\/b" }, { "op": "copy", "from": "\/b", "path": "\/a\/p\/x" } ]' : OK Testing 'copy must duplicate the value, not alias it, so no cycle can form', doc '{ "a": { "p": { } } }' patch '[ { "op": "copy", "from": "\/a", "path": "\/b" }, { "op": "copy", "from": "\/b", "path": "\/a\/p\/x" } ]' : OK
Testing 'null op field must be rejected, not dereferenced', doc '{ "foo": "bar" }' patch '[ { "op": null, "path": "\/foo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'op' field is not a string
Testing 'null from field must be rejected, not dereferenced', doc '{ "foo": "bar" }' patch '[ { "op": "move", "from": null, "path": "\/foo" } ]' : OK
=> json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'from' field is not a string