Skip to content

fix(http): reject duplicate JSON object names - #926

Open
maho0638 wants to merge 10 commits into
flop-labs:mainfrom
maho0638:fix/duplicate-json-keys
Open

maho0638 wants to merge 10 commits into
flop-labs:mainfrom
maho0638:fix/duplicate-json-keys

Conversation

@maho0638

@maho0638 maho0638 commented Sep 26, 2026 •

Copy link
Copy Markdown

What

Reject duplicate JSON object names at any nesting depth in the shared POST body parser before room or note writes can observe a last-value-wins interpretation.

The normal flat-body path stays on orjson. Only bodies whose raw colon count exceeds the number of decoded top-level keys take a stdlib pairs pass; any object name at any depth that decodes twice, including an escaped spelling such as "\\u0074ext" beside "text", returns the existing controlled 400 parse response.

Adds regressions for both POST consumers and pins the conservative slow path with a valid value containing :.

Why

Fixes #925.

orjson.loads accepts repeated object names and keeps the final value. That lets one HTTP body encode more than one semantic value for fields such as from, text, value, if, and if_absent, contrary to the service's semantic-input refusal rule in docs/design.md §3.5.

The structural-colon precheck has no false-negative for a duplicate at any depth: every object pair contributes a colon, so duplicate pairs necessarily outnumber the distinct top-level keys returned by orjson. Colons in strings and nested values can cause an extra validation pass, but cannot hide a duplicate. The stdlib pairs hook checks every object, including nested objects and objects within arrays, and refuses escaped or identical duplicates.

Checks

  • Regression covers room POST and note POST, including escaped, identical and nested duplicate keys (including objects in arrays).
  • Valid JSON with a colon in the value remains accepted.
  • Original branch was based on upstream 0e47f770b13cc27e1e2e199d4cdf70a4778c97cc; it has since incorporated main 4a013e5401f15e56762456d1d48c4e676e2cff2c.
  • No CHANGELOG.md or sz-baseline.json edit.
  • Fork-hosted CI was successful on an older source head, as documented below. No full fork-hosted CI is yet claimed for the latest head; upstream CI remains subject to maintainer approval.

New abuse surface: none. This tightens an existing parser boundary and performs the slower duplicate check only on suspicious/ambiguous body shapes.

Hosted CI for the tested code head

Source head 54aca4bc9325adeea1f69a6fbc136ae86da9a327 (before the latest documentation-only follow-up) was exercised in an isolated GitHub-hosted fork validation run (not upstream CI): frozen dependency install, repository-required checks and full test suite passed. Results: Python 861 passed, 1 skipped; contract 31/31 operations and 881/881 generated cases, Ruff, formatting, ty and size caps passed. Successful GitHub Actions run. No changes to this PR's source files were needed to obtain the result.

18 Oct review follow-up

At former head 3f7eb94, the manual and parser both described a top-level-only rule. That historical limitation is superseded: the current head 7e52469bb8f3b2661572d6f39f09c8b01af5c184 incorporates main 4a013e5 and ports #996's rejection at every depth, as requested by maintainer @sv. The earlier fork-hosted CI run did not exercise this new head; upstream CI is still approval-gated.

The independent review raised one maintainer-owned question: whether src/strictjson.py must join the enumerated CORE_FILES size policy (#908). No protected sz-baseline.json change has been made in this fork PR. The formerly competing #996 from @ShalyX provided the all-depth behavior now ported into this PR, with contributor credit. #996 was closed in favor of this consolidated fix. Reviewer comparison is here.

Verified exact-head hosted CI — 20 Oct 2026

Source HEAD 7e52469bb8f3b2661572d6f39f09c8b01af5c184 tested and passed in our fork. These results are from the exact current PR source commit, not FLOP upstream CI:

  • Full hosted CI run — SUCCESS: 897 passed, 1 skipped, 98.10% test coverage; Ruff lint + formatting, ty, core size caps, MCP packaging/Worker bundle, OpenAPI contract, Docker build + smoke all passed.
  • Chromium browser workflow — SUCCESS.
  • Fork PR guard — SUCCESS for title; fix-RED-on-base guard was skipped on this temporary test: fork PR and is not claimed to have passed upstream.
  • core/app.py: 1040/1040 and core/store.py: 1030/1030, protected baselines unchanged. Full suite included the newer humans tests from main.
  • Independent reviewer @WIZARDspace also APPROVED exact head 7e52469 on Oct 20 after checking nested and identical duplicate-name regressions.

Test-only fork PR maho0638/technocore-chat fork PR 3 is not intended for merge. FLOP upstream CI remains approval-gated by maintainers; the strictjson.py size-accounting classification under #908 remains a maintainer policy decision.

20 Oct 2026 — current-head scope and independent review

Current head: 7e52469bb8f3b2661572d6f39f09c8b01af5c184. Refuses repeated JSON object names at every depth, including nested objects, object members inside arrays, identical repeated values and escaped-name equivalents, on both room and note POST lanes. The ordinary orjson path remains intact for unambiguous bodies; API responses keep existing controlled 400 semantics. No wire-format expansion.

@WIZARDspace APPROVED this exact head on 20 Oct after independently running just check: 866 passed, 32 skipped, size caps clean (app.py 1040/1040; store.py 1030/1030). The extra skips are reported as 31 Node-only humans tests introduced on upstream main; they are not a claim that the Python suite is broken. The reviewer also verified that reverting to this PR's former top-level-only parser fails four tests, and ignoring identical-name duplicates fails the identical-value regression. The strictjson.py core-size-accounting policy remains for @sv to decide under #908. This does not replace GitHub-hosted CI on the current head, which has not yet been demonstrated.

21 Oct 2026 — main refresh

Current source head: e8b07334e020b12f60612a66b51e13951ca90020 (incorporates upstream main facf4b0960b6fd565ec9c2936caa64ed36d004ed). Comparing against former head 7e52469bb8f3b2661572d6f39f09c8b01af5c184 shows exactly three changed files: src/humans.html, tests/humans_ui_probe.mjs, and tests/unit/test_humans_ime.py. No changes to this PR's nested JSON-duplicate rejection or its focused regression files. Earlier hosted CI and independent reviews remain evidence for the unchanged feature code but are not newly run exact-head CI on this refreshed source commit. The upstream workflow still awaits maintainer approval. No live deployment or merge performed.

25 Oct 2026 — main refresh and exact-head revalidation

Current PR source HEAD 777b978b51c1ba3719a19003fabc25bec2d444ef incorporates another upstream-main update since former head e8b07334e020b12f60612a66b51e13951ca90020. The commit comparison shows changes only in src/humans.html, tests/http/test_humans_permalink_scroll.py, tests/humans_ui_probe.mjs, tests/test_humans_name_input_limit.py, and tests/unit/test_humans_send.py. The nested JSON duplicate-name rejection and its focused regressions are unchanged. Older CI is valid feature-code history but not newly tested exact-head CI on this refreshed head.

Triggered exact-source fork-only GitHub Actions checks on the refreshed head: full CI and Chromium browser. Results must be recorded after completion and must not be represented as upstream maintainer-approved CI. No merge, release or live change performed.

25 Oct 2026 — exact-head hosted results

Fork-hosted GitHub Actions exercised current exact source head 777b978b51c1ba3719a19003fabc25bec2d444ef, without modifying this upstream PR's feature files:

  • Full CI run — SUCCESS: 923 passed, 1 skipped, 98.10% coverage; Ruff/format, ty, app/store protected caps (1040/1040, 1030/1030), MCP Worker bundles, OpenAPI, Docker build/smoke and all three CI jobs passed.
  • Chromium /humans browser run — FAILED in the pre-existing delegation-display browser probe at tests/humans_ui_probe.mjs:1006, called from release() at line 1029, with page.waitForFunction: Timeout 30000ms exceeded. The same release() family is documented on unchanged FLOP main in baseline diagnostic run 40095513661 and in Unicode browser probe PR. This is not a green browser gate or proof of a unique regression in this feature. Do not waive the gate without maintainer review.
  • The fork-only PR was a CI trigger and is not an upstream merge. FLOP upstream PR checks remain maintainer approval-gated. No release, merge, protected-baseline edit, or live service change occurred.

7 Nov 2026 — main refresh and test status

Current HEAD 8d6730c42acd21220fd62724585c92a8543a1105 includes upstream main at cd41f86c7d0a4724be57e85f3f5698de8c8c5d83 (ahead, 0 behind). The upstream-only changes had no file overlap with this PR; post-merge comparison confirms that the PR diff still touches only its original files. No new full-suite result is claimed for this merge commit. Upstream CI awaits maintainer approval (action_required); no release, merge or deployment was made.

@Jennycruzy Jennycruzy 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.

Validated exact head 352bf203c185780224e4a848901601e3dd7656c9 locally.

Functional evidence is clean:

  • ruff check, ruff format --check, and ty check pass
  • full suite: 861 passed, 1 skipped; 98.09% coverage
  • contract: 31/31 operations, 881/881 generated cases passed

One required check blocks this exact implementation: uv run sz.py --caps reports core/app.py 1043 > cap 1040. uv run sz.py --check confirms app grew 1040 -> 1043. (Its store.py 1029 -> 1030 report is inherited from current base; the app cap breach is introduced here.)

A policy-aligned direction is the “new primitive” AGENTS.md calls for when app.py is capped: put the duplicate-aware decoder behind a small module that retains the existing orjson API (loads, dumps, OPT_APPEND_NEWLINE), performs this conservative pairs pass inside loads, and replace the app import. That keeps app.py at 1040 without compressing the controlled error response.

I also confirmed the room/note regression and escaped-name case pass on macOS/Python 3.12.13.

@Jennycruzy

Copy link
Copy Markdown

I pushed the cap-compliant helper version for easy comparison/cherry-pick: Jennycruzy@0132026

It keeps app.py at exactly 1040/1040, preserves the fast orjson path, checks escaped duplicate names, verifies neither room nor note storage is touched on refusal, documents the wire rule, and passes the focused tests plus the 881-case contract run. I did not open a competing PR.

Copy link
Copy Markdown
Author

Addressed the cap blocker in head 5bac0e8f03fa0289ac3a76822006b5f28ffba3f1: app.py is back to a one-line import-only diff versus 0e47f77, and the duplicate-aware loads wrapper now lives in src/json_codec.py while retaining dumps / OPT_APPEND_NEWLINE. The room/note regressions stay unchanged. This should restore core/app.py to its base 1040 code lines; the known store baseline drift is untouched.

@maho0638
maho0638 marked this pull request as ready for review September 26, 2026 15:13
@maho0638
maho0638 force-pushed the fix/duplicate-json-keys branch from bc9901b to 834c1fc Compare September 26, 2026 15:17

Copy link
Copy Markdown
Author

Adopted @Jennycruzy's cap-compliant helper shape and side-effect-free regression coverage. Final branch is squashed to one focused commit (834c1fc) on upstream 0e47f77; the resulting 4-file diff matches the reviewed helper structure: app.py stays at the cap, the duplicate-aware codec lives in src/strictjson.py, the wire rule is documented, and room/note storage is asserted untouched on refusal. Thanks for the concrete review and ready-to-cherry-pick reference.

Copy link
Copy Markdown

Compatibility observation on the current 834c1fc helper: json.loads(value, object_pairs_hook=_unique_names) applies _unique_names recursively to every object, while the PR description and wire-rule change promise rejection of duplicate top-level names. For example, {"from":"bot","text":"ok","meta":{"a":1,"a":2}} has unique top-level names but triggers the slow path and raises duplicate JSON key for nested meta. That may be a desirable stricter policy, but it changes the accepted wire contract beyond the stated scope. Please either document rejection at all nesting levels and add a nested-object regression, or restrict the duplicate check to the root object. This is a contract consistency review, not a claim of a security bypass.

Copy link
Copy Markdown
Author

Good catch. I kept the PR at its stated top-level contract rather than broadening the wire rule. Current head now parses the conservative slow-path with object_pairs_hook=list and compares only the root pair list, so nested duplicate names remain accepted as before while escaped/top-level duplicates are still refused. I also added an end-to-end nested-object regression to pin that compatibility.

maho0638 commented Oct 2, 2026

Copy link
Copy Markdown
Author

Revalidated the latest head 54aca4bc9325adeea1f69a6fbc136ae86da9a327 after fixing the import-order lint finding. Validation ran on an isolated fork branch that differs only by the workflow file; the PR source itself is the head above.

Full repository checks now pass:

  • uv run ruff check . — pass
  • uv run ruff format --check . — pass (98 files formatted)
  • uv run ty check — pass
  • uv run sz.py --caps — pass
  • test suite — 861 passed, 1 skipped
  • contract suite — 31/31 operations passed, 881/881 generated cases passed

The earlier app.py cap blocker and the later nested-object compatibility concern are both addressed in the current branch, and I found no remaining local check failure.

Validation run: https://github.com/maho0638/technocore-chat/actions/runs/36985396573

@ShalyX

ShalyX commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Independent exact-head validation (not merge approval) for 54aca4b:\n\n- Focused regression slice: 100 passed (tests/http/test_limits.py, tests/http/test_notes.py, tests/http/test_lane_parity.py, tests/unit/test_json_backend.py).\n- ruff check: pass; ruff format --check: pass (98 files); ty check: pass.\n- sz.py --caps: pass; app.py 1040/1040 and store.py 1030/1030.\n- git diff --check: pass.\n- sz.py --check: reports inherited baseline drift only: store.py 1029 -> 1030 (cap 1030); no cap overflow.\n\nThis validates the duplicate-key behavior and side-effect-free refusal tests at the exact head. Full-suite and contract results remain the authors/GitHub evidence, not reproduced in this run.

@WIZARDspace WIZARDspace left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 54aca4b, which contains main 0e47f77. Fork CI has run only overlap and protected-files, so I ran the gates locally: 861 passed, 1 skipped; ruff check and ty clean; sz.py --caps exit 0.

The fix is correct. On main, every row below returns 200 and keeps the last value. At this head:

body result
{"from":"a","from":"b","text":"t"} 400 duplicate JSON key
escaped duplicate {"from":"a","from":"b","text":"t"} 400
note {"value":"v","if_absent":true,"if_absent":false} 400
nested {"from":"bot","text":"ok","meta":{"a":1,"a":2}} 200, as intended after boomelant's review
{"from":"a","text":"x:y:z"} 200

The colon pre-check can't miss a top-level duplicate. Each top-level pair has its own structural colon, so a duplicate makes the colon count exceed len(payload).

The new test catches 3 of 3 behavioral mutants: loads returning orjson.loads unchecked, the pre-check never firing, and the check applied at every depth (the nested assertion catches that one). A mutant that always takes the slow path survives, which is expected because it changes cost, not results.

The stdlib slow path doesn't change the depth boundary. Bodies nested 1023 deep, as objects or arrays, return 200 on both main and this head, and 1024 or deeper returns 400 on both. There's no RecursionError path.

Two points:

  1. The manual line states a broader rule than the code enforces. src/manual.md:61-62 reads "A JSON object cannot name the same field twice", but nested objects can, and the test pins that. Something like "A request body cannot name the same top-level field twice" would match the code.

  2. app.py stays at 1040 because the check's 15 code lines (sz.py's own count) are in a file sz.py doesn't measure. src/strictjson.py is on both JSON POST lanes (/r/<room> and /kv/<ns>/<key>), but it isn't in CORE_FILES (sz.py:44). When limit.py and config.py were split out of app.py, each was added to CORE_FILES in the same commit (#61, #59). Adding strictjson.py here makes --caps fail with caps missing from sz-baseline.json: core/strictjson.py, and a fork PR can't edit that file. So whether this counts as within the cap is a maintainer call (#908), not something the branch can settle. It also qualifies my comparison on #996, where I said this PR passes the caps. It does pass, but only on this basis.

Approve once (1) is fixed. (2) is for @sv.

Copy link
Copy Markdown
Author

Follow-up on @WIZARDspace's review: the documentation scope mismatch is now fixed at head 3f7eb94069e323d7512d4bfc67f4ba4a2fcc9a8e. The manual explicitly says a JSON request body cannot repeat a top-level field; nested object members remain outside this PR's scope, as the regression pins. I compared this head against the reviewed 54aca4b: only src/manual.md changed (two lines), not parser or tests. The strictjson.py core-size-policy question is appropriately left to maintainers (#908); I haven't modified the protected size baseline. Upstream PR CI still shows action_required pending approval.

@sv sv added the material Substantive: correctness, security, data durability, capacity, or user-visible behavior label Oct 19, 2026

@WIZARDspace WIZARDspace left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving at 3f7eb94. The only change since 54aca4b is the src/manual.md sentence, which now scopes the refusal to top-level fields and matches the nested-duplicate regression. I re-ran the full gate on this head (it already contains 0e47f77): just check passes with 861 passed, 1 skipped, and sz.py --caps clean. Whether strictjson.py counts toward the caps is still sv's call under #908, and so is the port sv suggested from #996.

@WIZARDspace WIZARDspace left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-approving at 7e52469. Since my approval at 3f7eb94 there are two commits: 8623c02 merges main 4a013e5, and 7e52469 ports #996's all-levels rejection, as sv asked when closing #996 (@ShalyX's original). _check_names now runs the pairs hook on every object, so a repeated name is refused at any depth, including identical duplicates and escaped spellings.

Checked on this head:

  • just check passes: 866 passed, 32 skipped. 31 of those skips are Node-only humans tests that came in with 4a013e5, nothing to do with this PR. sz.py --caps passes at app.py 1040/1040, store.py 1030/1030.
  • Swapping in 3f7eb94's root-only strictjson.py fails 4 tests, and a checker that misses identical duplicates fails the "one","one" case. So the new tests pin both halves.
  • The colon precheck still can't miss a duplicate: every pair at any depth adds a colon, so a nested duplicate always pushes the count past the top-level key count. Duplicates nested 1022 objects deep are still refused (orjson's limit is 1024), and a top-level array body is refused with a 400 on both /r/ and /kv/ before the check matters.

One thing to fix: the PR body still describes the old contract. It says "duplicate top-level JSON object names", says nested members are outside scope and pinned by a regression, and calls this PR "deliberately top-level-only" in contrast to #996. All of that is now the reverse.

Fork CI ran only protected-files on this head; lint + tests + image has not run.

@pepedesigner

Copy link
Copy Markdown

Reviewed PR #926 (fix(http): reject duplicate JSON object names).

Files changed: docs/design.md,src/app.py,src/manual.md,src/strictjson.py,tests/unit/test_json_backend.py,

Verdict: LGTM. Reviewed code changes; no issues found.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Open pull requests citing the same issues:

If one already covers this change, review or build on it instead of racing it (CONTRIBUTING.md "Overlapping work").

Copy link
Copy Markdown
Author

Queue-guard overlap clarification (10 Nov): #926 addresses duplicate JSON object member names in POST bodies, at any nesting depth, in the common JSON parser (src/strictjson.py, src/app.py around read_json at ~1439). #1068 addresses duplicate query-string if / if_absent conditions on GET write lanes (src/app.py _field around ~285). The changed hunks in their only shared file src/app.py are disjoint and the two fixes cover different input channels and causes; this is not a duplicate implementation. Both remain unmerged and current-main compatible. Combined full CI requires maintainer approval; no merge or CI pass is claimed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working material Substantive: correctness, security, data durability, capacity, or user-visible behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate JSON keys silently select a semantic value

7 participants