Skip to content

Added JsonParseStrict() and JsonParseFileStrict() - #11

Open
djbclark wants to merge 2 commits into
masterfrom
fix/json-strict-parse
Open

djbclark wants to merge 2 commits into
masterfrom
fix/json-strict-parse

Conversation

@djbclark

Copy link
Copy Markdown
Owner

Adds JsonParseStrict() and JsonParseFileStrict(), an opt-in parse that
refuses a duplicate object member at any depth and any non-whitespace after
the value. A duplicate is detected when the second key is read, before
JsonObjectAppendElement() could replace the first member, so no member is
ever dropped; the new error is JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY.
Trailing text gives the existing JSON_PARSE_ERROR_INVALID_END. The file
variant keeps the size_max cap (JSON_PARSE_ERROR_TRUNCATED) and refuses
a null byte, which would hide the rest of the file from the parser.

JsonParse(), JsonParseAll(), JsonParseWithLookup(), JsonParseFile(),
JsonParseAnyFile() and JsonReadDataFile() are unchanged (duplicates
last-wins, valid prefix accepted); only a new reject_duplicates argument
is threaded through the static array/object parsers, passed as false by
the existing entry points.

Tests (tests/unit/json_test.c): valid documents and trailing whitespace;
duplicate member at top level, nested, inside an array, unquoted names;
an invalid earlier duplicate (null then string); a repeated top-level
section; trailing non-whitespace after object, array, number, bool, null
and string; file variant (valid, duplicate, trailing, null byte, size cap,
missing file, empty file); regression tests that the lenient functions
still resolve duplicates last-wins and accept a prefix. They are run by fork
CI; none were run on a Mac. git merge-tree against fork/tendcf-integration
is conflict-free.

Closes #10

Prior art / docs checked: RFC 8259 section 4 (duplicate names are
unpredictable; reporting an error is a permitted implementation choice);
Jansson JSON_REJECT_DUPLICATES and JSON_DISABLE_EOF_CHECK (opt-in
rejection, whole input by default); NorthernTechHQ/libntech and
djbclark/libntech issues and PRs for duplicate keys and trailing text, none
found.

Found by review of frdminc/tendcf#41 (A2).

🤖 Generated with Claude Code

JsonObjectAppendElement() removes an existing member of the same name
before appending, so a document with two members of one name loses the
earlier one while it is being parsed, before the caller can look at it.
JsonParse() and everything built on it (JsonParseFile(), JsonParseAnyFile(),
JsonReadDataFile()) also stop at the end of the first value and ignore
whatever follows.

A caller that must not act on a document it only half-read, such as a
loader that validates every entry, needs both to be refused. The two new
functions do that: a name that an object already holds is refused with the
new JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY before the earlier member is
replaced, at any depth, and anything but whitespace after the value is
refused with JSON_PARSE_ERROR_INVALID_END. The file variant keeps the
size cap and also refuses a null byte, which would hide the rest of the
file from the parser.

This is opt-in: the existing functions behave exactly as before, with
duplicates resolved last-wins and a valid prefix accepted. The same
choice is made by Jansson as JSON_REJECT_DUPLICATES, and RFC 8259
section 4 leaves duplicate names to the implementation.

Changelog: Title
Ticket: none

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Nov 14, 2026

Copy link
Copy Markdown

Automatic reviews are disabled for this repository. Configure in Code Review Settings.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T20:46:50.294589Z dc3fba2 PR opened
🔒 Security Review ✅ Completed 2026-11-14T20:52:06.370473Z dc3fba2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dc3fba212b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread libutils/json.c Outdated

const char *data = StringWriterData(contents);
JsonParseError error;
if (strlen(data) != StringWriterLength(contents))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Detect null bytes before FileRead discards them

For a file containing {}\0not-json, this check does not reject the hidden suffix: FileReadFromFd() passes each read buffer to WriterWriteLen(), whose StringWriterWriteLen() implementation uses strnlen() and discards the null byte and the remainder of that buffer. Consequently, strlen(data) still equals StringWriterLength(contents), and JsonParseFileStrict() successfully parses {} instead of returning JSON_PARSE_ERROR_INVALID_END. This also affects the embedded-null case already added to test_parse_file_strict(). Detect null bytes in the raw read buffers, or use a binary-preserving read path before validating the document.

Useful? React with 👍 / 👎.

FileRead() builds its Writer with strnlen(), so a null byte cuts the
text off there and the Writer's length still equals the length of the
string that is left: the check that compared the two could never fire,
and a file such as {"vars": {}}<NUL>not-json parsed as the valid
object. The function now reads through FileReadFromFd() and compares the
number of bytes it got with the size fstat() reports for the same
descriptor, so a null byte (or a file that changed while it was read) is
refused with JSON_PARSE_ERROR_INVALID_END.

Found by the unit tests in fork CI (test_parse_file_strict).

Changelog: None
Ticket: none

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

JSON parsing silently replaces duplicate object members and ignores trailing text

1 participant