Skip to content

Strict JSON parse: accepted non-JSON forms, O(n^2) duplicate check, no depth cap #67

Open

Description

@djbclark

What is wrong

Two low-severity gaps in the new strict JSON parse (JsonParseStrict / JsonParseFileStrict). The author of the file is trusted, hence low.

A. Accepted non-JSON forms

The strict parse still accepts non-JSON forms, so it is not a full conformance gate. Where: overlay/libntech/0005-Added-JsonParseStrict-and-JsonParseFileStrict.patch:315-329 (the json.h doc: "refusing input a lenient parse would repair"). Not strictly refused:

  • Unquoted keys (the patch's own test at 0005:379 accepts plain: true).
  • A missing colon, since JsonParseAsObject appends a value whenever property_name != NULL without checking prev_char == ':' (so {"a" "b"} parses as {"a":"b"}).
  • \u0000, which Utf8EncodeCodePointWriter writes as a NUL that StringWriterWriteLen's strnlen drops ("a\u0000b" becomes "ab").

Scenario: a parser differential with any validator that uses a conformant parser, such as Python json in tendcf's projector or lint. A key "tendcf_interlock\u0000" is a different key to Python but tendcf_interlock to CFEngine. Inside one file a collision is refused as a duplicate, so the outcome is fail-closed, but a validator and the loader can disagree on the name of a lone key.

Suggested fix: document these accepted forms next to JsonParseStrict, or refuse missing colons and \u0000 under reject_duplicates. Separately, have the projector refuse \u0000 in keys and values.

B. O(n^2) duplicate check, no depth cap

Where: 0005:102-108

return reject_duplicates && JsonObjectGet(object, key) != NULL;

This is a linear lookup for every member. Nesting depth is pre-existing.

Scenario: a 5 MiB host_specific.json with one flat object of ~400k short keys means on the order of 10^11 comparisons. Root cf-agent then spins until agent_expireafter kills it, and nothing is installed. A deeply nested [[[[... overflows the stack, which is pre-existing.

Suggested fix: a hash set of seen keys per object under reject_duplicates, or a member-count cap in JsonParseFileStrict. Add a nesting cap for the strict variant.

All locations are at 75db441.

Found in review of #65 (head 75db441).

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions