From 9e347a2dc313d03faaaed482e2df98f018aa801d Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 04:03:33 -0600 Subject: [PATCH] fix(id): parse_wants refuses duplicate JSON keys (#7621) The wants gate parsed with a bare serde_json::from_str, so duplicate object keys collapsed last-wins before the version check and the MAX_WANTS_ENTRIES bounds ran: {v:2,v:1} passed as v1, a 65-entry subscribe array hidden behind a duplicate key was never counted, and a duplicate cap inside one request object silently replaced the requested capability. Parse through the #6520 helper (parse_json_without_duplicate_keys), as parse_records already does, and pin it with rejects_duplicate_keys_at_any_depth. Probe-verified pre-fix against the real module (~/git/fleet/probe-wants-dupkeys); cargo test -p id-core 329 passed on the branch. --- pkgs/id/crates/id-core/src/world_caps.rs | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pkgs/id/crates/id-core/src/world_caps.rs b/pkgs/id/crates/id-core/src/world_caps.rs index a903763f76..3f0795ebc6 100644 --- a/pkgs/id/crates/id-core/src/world_caps.rs +++ b/pkgs/id/crates/id-core/src/world_caps.rs @@ -735,7 +735,11 @@ pub fn parse_wants(text: &str) -> Result { text.len() <= MAX_WANTS_BYTES, "wants exceeds {MAX_WANTS_BYTES} bytes" ); - let value: serde_json::Value = serde_json::from_str(text).context("wants is not JSON")?; + // A duplicate key is a parse error (#7621, the #6520 rule): the + // version check and the entry bounds below must see the document as + // written, never a last-wins collapse of it. + let value: serde_json::Value = + crate::json_unique::parse_json_without_duplicate_keys(text).context("wants is not JSON")?; let object = value.as_object().context("wants must be a JSON object")?; if let Some(version) = object.get("v") { ensure!( @@ -1520,6 +1524,32 @@ mod tests { assert!(parse_wants(&" ".repeat(MAX_WANTS_BYTES + 1)).is_err()); } + #[test] + fn rejects_duplicate_keys_at_any_depth() { + // #7621: every duplicate-key document below was accepted by the + // bare from_str gate (last-wins collapse) before this test existed. + for bad in [ + // The version check saw only the collapsed last "v". + r#"{"v":2,"v":1}"#, + r#"{"v":1,"v":2}"#, + // A duplicate inside one request object collapsed its cap. + r#"{"requests":[{"id":"r1","cap":"time.now","cap":"chat.say"}]}"#, + // A duplicate inside request args. + r#"{"requests":[{"id":"r1","cap":"chat.say","args":{"text":"a","text":"b"}}]}"#, + ] { + assert!(parse_wants(bad).is_err(), "{bad:?}"); + } + // The 64-entry bound counted only the collapsed second array. + let hidden = format!( + r#"{{"subscribe":[{}],"subscribe":["players"]}}"#, + (0..=MAX_WANTS_ENTRIES) + .map(|i| format!(r#""cap{i}""#)) + .collect::>() + .join(",") + ); + assert!(parse_wants(&hidden).is_err(), "{hidden:?}"); + } + #[test] fn subscription_specs_round_trip() { for spec in ["players", "time.tick:1000"] {