Fix #1009, reject empty array reference token in decode_array_index_from_pointer#1032
Open
dalisyron wants to merge 1 commit into
Open
Fix #1009, reject empty array reference token in decode_array_index_from_pointer#1032dalisyron wants to merge 1 commit into
dalisyron wants to merge 1 commit into
Conversation
…ay_index_from_pointer An empty reference token (the path "/" applied to an array, or a trailing/empty token such as "/0/") was accepted by decode_array_index_from_pointer() and decoded as index 0, contrary to RFC 6901. Invalid JSON Pointers could therefore resolve to the first array element, so cJSONUtils_GetPointer() and the JSON Patch add/remove/replace/move/copy/test operations could read or modify array[0] for a path that is not a valid array location. Reject an empty reference token (leading '\0' or '/') before parsing the index. The valid empty-string object key (e.g. "/" on an object) is unaffected, since that path does not go through array index decoding. Add regression tests for cJSONUtils_GetPointer and JSON Patch.
ff09035 to
02fcc4e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
decode_array_index_from_pointer()accepts an empty reference token and decodes it as index0, which is not valid per RFC 6901, section 4: when the referenced value is an array, the token must be either a non-negative integer without leading zeroes or-. An empty string is not a valid array index.Because
get_item_from_pointer()(and the JSON Patch code) call this whenever the current value is an array, invalid pointers containing an empty array token silently resolve to the first element:The same empty token also made JSON Patch operate on
array[0]for paths that are not valid array locations, although RFC 6902 requires such operations to fail:[{ "op": "remove", "path": "/" }] // removed the first element [{ "op": "replace", "path": "/", "value": "x" }] // replaced the first element [{ "op": "move", "from": "/", "path": "/1" }] // detached the first elementFix
Reject an empty reference token (first character
'\0'or'/') at the top ofdecode_array_index_from_pointer(), before the index is parsed. It is a cheap, local guard, consistent with the existing leading-zero check just below it.This only affects array index decoding. The valid empty-string object key (e.g.
"/"applied to an object, or a trailing slash on a nested object) is intentionally left untouched, because that path never goes through array index decoding — the existingold_utils_tests.candtests.jsoncases for it still pass.Verification
json_pointer_should_reject_empty_array_indexintests/old_utils_tests.c, next to the existing JSON Pointer tests, plus two JSON Patch cases intests/json-patch-tests/cjson-utils-tests.json(remove/replacewith path"/"on an array must fail). All of them fail before this change and pass after it.-DENABLE_CJSON_UTILS=ON -DENABLE_SANITIZERS=ON(22/22, ASan/UBSan clean).cJSON_Utils.ccompiles cleanly under the strict warning set (-Werror -Wconversion -Wc++-compat -pedantic -std=c89).Fixes #1009