Skip to content

JSON parsing silently replaces duplicate object members and ignores trailing text #10

Open

Description

@djbclark

A caller that has to refuse a whole document it cannot read unambiguously
has no way to do so with the JSON parser. Two behaviours hide the problem
before the caller sees the parsed tree.

  1. JsonObjectAppendElement() calls JsonObjectRemoveKey() first
    (libutils/json.c), so while parsing, a second member of the same name
    replaces the first. Counterexample, parsed with JsonParse():

    {"vars": {"good": null, "good": "ok"}}
    

    returns JSON_PARSE_OK with vars.good == "ok". The null is gone
    before any validation of the tree can reject it. Likewise
    {"vars": {"a": "1"}, "classes": {}, "vars": {"b": "2"}} loses the
    whole first vars section.

  2. JsonParse() stops at the end of the first value, and
    JsonParseAnyFile(), JsonParseFile() and JsonReadDataFile() call it.
    {"vars": {"good": "ok"}} not-json is accepted as the valid prefix.
    JsonParseAll() refuses trailing text but still replaces duplicates.

RFC 8259 section 4 leaves the behaviour with duplicate names unpredictable
and lists "report an error" as one implementation choice; Jansson offers it
as the opt-in flag JSON_REJECT_DUPLICATES, and requires the whole input
to be consumed unless JSON_DISABLE_EOF_CHECK is set.

Changing the default behaviour would change what every policy reading JSON
sees, so the proposal is opt-in: new JsonParseStrict() and
JsonParseFileStrict() that refuse a duplicate member at any depth before
it replaces anything (new JSON_PARSE_ERROR_OBJECT_DUPLICATE_KEY) and
trailing non-whitespace (JSON_PARSE_ERROR_INVALID_END), keeping the size
cap. Every existing function stays as it is.

Found by review of frdminc/tendcf#41 (finding A2):
the CMDB loader of the pinned engine validates the parsed tree, so it cannot
see what the parser already discarded.

Activity

  1. linear-code commented on Nov 14, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions