Skip to content

fix(cpworker): reject content after the top-level value in config JSON - #312

Merged
timmy21 merged 1 commit into
0.9.xfrom
fix/238-json-trailing-garbage
Oct 9, 2026
Merged

timmy21 merged 1 commit into
0.9.xfrom
fix/238-json-trailing-garbage

Conversation

@timmy21

@timmy21 timmy21 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Problem

cjson_parse() called cJSON_ParseWithOpts(json, &end, false). With require_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.
  • A file holding two concatenated configs ran the first one.
  • A truncated or doubled write that still started with a complete object passed without an error.

cjson_parse() is used for the config file, the tasks file and parse_config_data(), so all three were affected.

Change

  • cjson_parse() passes require_null_terminated=true. Only whitespace (including \r\n) may follow the top-level value.
  • The existing error path reports the position of the trailing content, for example JSON parse error at line 1, column 14 near ','.

Not changed, as discussed in #238:

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: through parse_config_file and parse_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 under cpworker/ and cpdaemon/ still parses, except the intentionally broken testdata/reload/broken.json.

Fixes #238

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
@timmy21
timmy21 merged commit 33d9237 into 0.9.x Oct 9, 2026
1 check passed
@timmy21
timmy21 deleted the fix/238-json-trailing-garbage branch October 9, 2026 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cpworker] cJSON leniency: duplicate keys, trailing garbage, invalid \uXXXX (by design?)

1 participant