Repository navigation
fix(cpworker): reject content after the top-level value in config JSON - #312
Merged
Merged
Conversation
cJSON was called with require_null_terminated=false, so a config file with trailing content (a stray comma, a second object, a truncated or concatenated write) was read up to its first complete value and accepted. Require only whitespace after the value. The error names the position of the trailing content. Duplicate keys and invalid \u escapes are left as they are: the config is generated by cpdaemon, which does not produce them. Fixes #238
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
cjson_parse()calledcJSON_ParseWithOpts(json, &end, false). Withrequire_null_terminated=false, cJSON stops after the first complete top-level value and ignores the rest. A config with trailing content was accepted and read only up to that value:{"tasks": []},(stray comma) was accepted as a config with zero tasks.cjson_parse()is used for the config file, the tasks file andparse_config_data(), so all three were affected.Change
cjson_parse()passesrequire_null_terminated=true. Only whitespace (including\r\n) may follow the top-level value.JSON parse error at line 1, column 14 near ','.Not changed, as discussed in #238:
\uXXXXescapes. The config is generated by cpdaemon withencoding/json, which produces neither.Tests
New cases in
cpworker/tests/unit/config_file.c:test_trailing_garbage_is_rejected: stray comma, exact error message.test_second_top_level_value_is_rejected: two concatenated configs.test_trailing_garbage_in_config_file_is_rejected: throughparse_config_fileandparse_tasks_file.test_trailing_whitespace_is_accepted: spaces, tabs and CRLF after the value still parse.The first three fail before the change. All 12 ctest targets and the integration suite (
run_test.sh all) pass. Every JSON file undercpworker/andcpdaemon/still parses, except the intentionally brokentestdata/reload/broken.json.Fixes #238