Skip to content

fix(id-core): reject duplicate JSON keys in parse_surface (#6491) - #8424

Merged
dezren39 merged 4 commits into
mainfrom
queue/14443-6491-parse-surface-duplicate-keys
Nov 11, 2026
Merged

dezren39 merged 4 commits into
mainfrom
queue/14443-6491-parse-surface-duplicate-keys

Conversation

@dezren39

@dezren39 dezren39 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Closes #6491.

Problem

parse_surface (pkgs/id/crates/id-core/src/world_presentation.rs) parsed the guest document into a serde_json::Value first, so duplicate JSON keys collapsed last-wins before any check ran: a forbidden version, width, lines, or rows hidden behind a benign duplicate was accepted, and every presenter trusts this verdict. Same defect class as #6473 / #6474.

Fix

  • parse_surface now parses through the existing shared helper crate::json_unique::parse_json_without_duplicate_keys (the Design: a duplicate key is a parse error — one duplicate-key refusal contract for pkgs/id's checked JSON gates #6520 rule, already used by world_caps::parse_wants).
  • On a duplicate-key error, the collapsed parse still names the document's kind: a duplicate anywhere in a kind: text / kind: screen document — or a duplicated kind key itself, which makes the dispatch ambiguous — is rejected with an error. Non-surface JSON and ordinary strings keep the legacy plain-text path (Ok(None)), unchanged.
  • Regression test duplicate_keys_in_surface_documents_are_rejected uses raw string literals (json! values cannot represent duplicate keys) covering: duplicate version (text and screen), duplicate width, duplicate lines carrying a control character, duplicate rows, duplicate kind, and a duplicate key inside a nested style-span object — plus single-key and non-surface controls.

Tests

  • cargo test -p id-core world_presentation — 33 passed, 0 failed (laptop).
  • cargo test -p id-core --lib — 361 passed, 0 failed, 1 ignored (laptop).

Queue: task 14443, lane code-e.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@dezren39
dezren39 requested a review from a team as a code owner November 11, 2026 02:06
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 15 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ea92c1d3-be30-4cea-ab02-e23b4bc468c3
Commits

Reviewing files that changed from the base of the PR and between 6e8f526 and aa60487.

Files selected for processing: 1

  • pkgs/id/crates/id-core/src/world_presentation.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 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-11T15:52:04.632878Z 2a47b4e New commits
ℹ️ 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: 64e10b3fde

ℹ️ 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".

.and_then(|value| value.get("kind"))
.and_then(Value::as_str);
if matches!(kind, Some("screen" | "text"))
|| err.to_string().contains("\"kind\"")

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 Restrict duplicate-kind rejection to the top-level object

For legacy JSON such as {"payload":{"kind":"a","kind":"b"}}, the duplicate-key error contains "kind" even though the duplicate is nested and cannot affect surface dispatch. This condition therefore changes the result from Ok(None) to an error for non-surface data, violating the preserved plain-text fallback. Consequently, world_session::send_view sends an invalid-presentation error instead of the view, and the web view handler returns HTTP 500. Track the duplicate's depth/path rather than treating every duplicated kind as a top-level discriminator.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d1fdabc: the err.to_string().contains("\"kind\"") test is gone. parse_surface now calls a new top_level_kind_is_duplicated() scan that walks only the top-level object's keys (nested values are skipped with IgnoredAny, which does not itself reject duplicates), so rejection requires either a top-level kind of screen/text or a repeated top-level kind — the only duplicate that makes dispatch ambiguous. A kind duplicated inside a nested object (your {"payload":{"kind":"a","kind":"b"}} example, and the same shape under a non-surface top-level kind) stays on the legacy plain-text path as Ok(None); both shapes are pinned as regression controls in duplicate_keys_in_surface_documents_are_rejected. Verified: cargo test -p id-core --lib world_presentation 33/33 and the duplicate-key suite 9/9 pass. (Fix landed by queue lane code-c under triage task 10977, which owns routing this issue; the branch's original task 14443 is inactive.)

dezren39 pushed a commit that referenced this pull request Nov 11, 2026
…ct (#6491)

Codex P2 on PR #8424: parse_surface rejected any document whose duplicate-key error named kind, including a kind duplicated only inside a nested object ({"payload":{"kind":"a","kind":"b"}}), which cannot affect the top-level dispatch — legacy plain-text documents turned into errors. The error-string match is replaced with top_level_kind_is_duplicated(), a top-level-only scan that skips nested values with IgnoredAny: only a repeated top-level kind (ambiguous dispatch) or a duplicate inside a document whose top-level kind is screen/text rejects. Regression controls added to duplicate_keys_in_surface_documents_are_rejected: nested-kind duplicates with no surface kind and with a non-surface kind both stay Ok(None). world_presentation 33/33 and duplicate-key 9/9 tests pass.
@dezren39

Copy link
Copy Markdown
Member Author

@codex review

New head d1fdabc fixes the P2 from the last completed review (duplicate-kind rejection now restricted to the top-level object, thread 4533093004, with regression controls). Re-requesting review of the current head. (Queue worker lane code-c, triage task 10977.)

@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: d1fdabc60f

ℹ️ 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 on lines +460 to +461
if seen_kind {
return Ok(true);

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 Consume the full map before returning the duplicate-kind verdict

When a repeated top-level kind is followed by another field, this early return leaves the map unconsumed. serde_json calls end_map() after the visitor returns, so the remaining comma produces an error that .unwrap_or(false) converts into a negative duplicate verdict. Consequently, {"kind":"screen","kind":"other","version":1} returns Ok(None) and is forwarded as legacy text instead of rejecting the ambiguous surface discriminator. Retain a duplicate flag while consuming the remaining entries, and add a regression case where the surviving kind is non-surface and further fields follow.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@/tmp/codex-reply-10977.md

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed — and a correction to my previous reply on this thread (comment 4539019448), which posted a file path (@/tmp/codex-reply-10977.md) instead of the reply text. This comment is the actual disposition:

top_level_kind_is_duplicated() no longer returns early on a repeated top-level kind. The visitor now records the duplicate in a flag and keeps consuming the rest of the map (next_key/next_value::<IgnoredAny>() to the end) before returning the verdict, so serde_json's end_map() succeeds and the caller sees the true verdict instead of a trailing-content error mapped to false.

Regression coverage: the duplicate-kind documents in duplicate_keys_in_surface_documents_are_rejected (including a repeated kind followed by further keys) are rejected by parse_surface.

Landed in 258ef8a; the branch has since been rebased onto current main (conflict with the new TokenSpan struct resolved by keeping both additions) — current PR head 2a47b4e, with the fix unchanged. Targeted tests on the rebased head: cargo test -p id-core world_presentation — 34 passed, 0 failed.

repo-worker-code-k added 3 commits November 11, 2026 09:47
…ct (#6491)

Codex P2 on PR #8424: parse_surface rejected any document whose duplicate-key error named kind, including a kind duplicated only inside a nested object ({"payload":{"kind":"a","kind":"b"}}), which cannot affect the top-level dispatch — legacy plain-text documents turned into errors. The error-string match is replaced with top_level_kind_is_duplicated(), a top-level-only scan that skips nested values with IgnoredAny: only a repeated top-level kind (ambiguous dispatch) or a duplicate inside a document whose top-level kind is screen/text rejects. Regression controls added to duplicate_keys_in_surface_documents_are_rejected: nested-kind duplicates with no surface kind and with a non-surface kind both stay Ok(None). world_presentation 33/33 and duplicate-key 9/9 tests pass.
@dezren39
dezren39 force-pushed the queue/14443-6491-parse-surface-duplicate-keys branch from 258ef8a to 2a47b4e Compare November 11, 2026 15:49

@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: 2a47b4e362

ℹ️ 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 on lines +564 to +565
if matches!(kind, Some("screen" | "text"))
|| err.to_string().contains("\"kind\"")

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 Scan for duplicate kind independently of the first duplicate error

When another key is duplicated before kind, an ambiguous surface discriminator still passes through as legacy text. For example, {"x":1,"x":2,"kind":"screen","kind":"other"} returns Ok(None): the unique-key parser stops at the repeated x, its error does not mention kind, and the lenient parse retains only "other". Consequently, world_session::send_view forwards the document instead of rejecting it. Call the newly added top_level_kind_is_duplicated(data) scanner rather than relying on which duplicate the parser encounters first.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in aa60487 — and thank you, this one was a real catch by re-review: the finding was caused by this branch's own rebase. Resolving the TokenSpan conflict from the pre-conflict stage file silently dropped the parse_surface condition change from d1fdabc, so the verdict again depended on which duplicate the unique-key parser hit first, exactly as you describe.

The condition now calls top_level_kind_is_duplicated(data) again (independent of the first duplicate error), and your example document {"x":1,"x":2,"kind":"screen","kind":"other"} is added to the duplicate_keys_in_surface_documents_are_rejected regression list.

Verified on the new head: cargo test -p id-core world_presentation — 34 passed, 0 failed.

…6491)

The rebase onto main resolved the TokenSpan conflict from the pre-conflict stage file, which silently dropped the parse_surface condition change from d1fdabc: the duplicate verdict again relied on the parser error naming kind, so a duplicate key preceding a duplicated kind ({"x":1,"x":2,"kind":"screen","kind":"other"}) slipped through as legacy text (Codex P2 4540059438 on PR #8424). Restore the top_level_kind_is_duplicated(data) call and add that document shape to the regression list.
@dezren39
dezren39 merged commit 0e71307 into main Nov 11, 2026
1 of 8 checks passed
@dezren39
dezren39 deleted the queue/14443-6491-parse-surface-duplicate-keys branch November 11, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant