From e248056aa170783f7bf4cc88dcbb1664096449a3 Mon Sep 17 00:00:00 2001 From: Javid Khan Date: Sat, 18 Jul 2026 21:08:17 +0530 Subject: [PATCH] require string type for patch op/path/from fields --- json_patch.c | 8 +++++--- tests/json_patch_tests.json | 6 ++++++ tests/test_json_patch.expected | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/json_patch.c b/json_patch.c index 6b061ff..90e9d19 100644 --- a/json_patch.c +++ b/json_patch.c @@ -208,7 +208,7 @@ static int json_patch_apply_move_copy(struct json_object **res, return -1; } - from_s = json_object_get_string(jfrom); + from_s = json_object_get_type(jfrom) == json_type_string ? json_object_get_string(jfrom) : NULL; if (from_s == NULL) { _set_err(EINVAL, "Patch object 'from' field is not a string"); return -1; @@ -323,7 +323,7 @@ int json_patch_apply(struct json_object *copy_from, struct json_object *patch, _set_err(EINVAL, "Patch object does not contain 'op' field"); return -1; } - op = json_object_get_string(jop); + op = json_object_get_type(jop) == json_type_string ? json_object_get_string(jop) : NULL; if (op == NULL) { _set_err(EINVAL, "Patch object 'op' field is not a string"); return -1; @@ -332,7 +332,9 @@ int json_patch_apply(struct json_object *copy_from, struct json_object *patch, _set_err(EINVAL, "Patch object does not contain 'path' field"); return -1; } - path = json_object_get_string(jpath); // Note: empty string is ok! + // Note: empty string is ok! + path = json_object_get_type(jpath) == json_type_string ? json_object_get_string(jpath) + : NULL; if (path == NULL) { _set_err(EINVAL, "Patch object 'path' field is not a string"); return -1; diff --git a/tests/json_patch_tests.json b/tests/json_patch_tests.json index 10fad98..80103c0 100644 --- a/tests/json_patch_tests.json +++ b/tests/json_patch_tests.json @@ -554,6 +554,12 @@ "doc": {"foo": "bar"}, "patch": [{"op": "move", "from": null, "path": "/foo"}], "error": "a null from field should fail rather than crash" + }, + + { "comment": "null path field must be rejected, not dereferenced", + "doc": {"foo": "bar"}, + "patch": [{"op": "remove", "path": null}], + "error": "a null path field should fail rather than crash" } ] diff --git a/tests/test_json_patch.expected b/tests/test_json_patch.expected index 9f065e0..8882779 100644 --- a/tests/test_json_patch.expected +++ b/tests/test_json_patch.expected @@ -161,3 +161,5 @@ Testing 'null op field must be rejected, not dereferenced', doc '{ "foo": "bar" => 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 +Testing 'null path field must be rejected, not dereferenced', doc '{ "foo": "bar" }' patch '[ { "op": "remove", "path": null } ]' : OK + => json_patch_apply failed as expected: ERRNO=EINVAL at patch idx 0: Patch object 'path' field is not a string -- 2.47.3