From 5f1c0163aef22962dad7d5f6f52616315f90d928 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 22:08:28 -0600 Subject: [PATCH] fix(id/world): reject non-object args in parse_wants (#7929) A request args that was present but neither a JSON object nor null collapsed to no arguments in every handler, silently widening records.read to every record. parse_wants now rejects number, string, array, and boolean args with an error naming the problem; absent, null, and object args are unchanged. Tests: parse-level rejection + controls in world_caps, and a world-level pin in world_dos (the request is never dispatched; the object-args control dispatches exactly once). --- pkgs/id/crates/id-core/src/world_caps.rs | 45 +++++++++++++++++++ pkgs/id/tests/world_dos.rs | 57 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/pkgs/id/crates/id-core/src/world_caps.rs b/pkgs/id/crates/id-core/src/world_caps.rs index 77779a95aa..97f169b35c 100644 --- a/pkgs/id/crates/id-core/src/world_caps.rs +++ b/pkgs/id/crates/id-core/src/world_caps.rs @@ -895,6 +895,21 @@ pub fn parse_wants(text: &str) -> Result { .get("args") .cloned() .unwrap_or(serde_json::Value::Null); + // A request's args are a JSON object or absent/null (#7929): + // handlers read named fields from them, so any other JSON + // value silently collapsed to "no arguments" — for + // records.read that widened the read to every record. + ensure!( + args.is_null() || args.is_object(), + "request args must be a JSON object or null, not {}", + match &args { + serde_json::Value::Number(_) => "a number", + serde_json::Value::String(_) => "a string", + serde_json::Value::Array(_) => "an array", + serde_json::Value::Bool(_) => "a boolean", + _ => "a non-object value", + } + ); ensure!( serde_json::to_vec(&args).map_or(usize::MAX, |bytes| bytes.len()) <= MAX_ARGS_BYTES, "request args exceed {MAX_ARGS_BYTES} bytes" @@ -1711,6 +1726,36 @@ mod tests { ); } + #[test] + fn non_object_request_args_are_rejected() { + // #7929: a present args that is neither an object nor null used + // to collapse to no arguments in every handler, silently + // widening records.read to every record. + for bad in ["42", "\"text\"", "[1,2]", "true"] { + let doc = format!( + r#"{{"requests":[{{"id":"r1","cap":"records.read","args":{bad}}}]}}"# + ); + let error = parse_wants(&doc).unwrap_err(); + assert!( + format!("{error:#}").contains("args must be a JSON object"), + "{error:#}" + ); + } + // Absent args and args:null behave exactly as before. + parse_wants(r#"{"requests":[{"id":"r1","cap":"records.read"}]}"#).unwrap(); + parse_wants(r#"{"requests":[{"id":"r1","cap":"records.read","args":null}]}"#) + .unwrap(); + // An object args is carried through unchanged. + let wants = parse_wants( + r#"{"requests":[{"id":"r1","cap":"records.read","args":{"prefix":"co"}}]}"#, + ) + .unwrap(); + assert_eq!( + wants.requests[0].args, + serde_json::json!({"prefix": "co"}) + ); + } + #[test] fn an_empty_document_wants_nothing() { assert_eq!(parse_wants("{}").unwrap(), Wants::default()); diff --git a/pkgs/id/tests/world_dos.rs b/pkgs/id/tests/world_dos.rs index 569f0256bb..1285308014 100644 --- a/pkgs/id/tests/world_dos.rs +++ b/pkgs/id/tests/world_dos.rs @@ -139,6 +139,63 @@ fn schedule_ids_are_refused_when_out_of_bounds() { assert!(validate_schedule_id(&"x".repeat(65)).is_err()); } +/// A program whose records.read request carries a non-object args (#7929). +struct NonObjectArgsProgram; + +impl WorldProgram for NonObjectArgsProgram { + fn wants(&mut self) -> anyhow::Result> { + Ok(Some( + r#"{"v":1,"requests":[{"id":"r0","cap":"records.read","args":42}]}"#.to_owned(), + )) + } +} + +/// The same request with a valid object args: the control. +struct ObjectArgsProgram; + +impl WorldProgram for ObjectArgsProgram { + fn wants(&mut self) -> anyhow::Result> { + Ok(Some( + r#"{"v":1,"requests":[{"id":"r0","cap":"records.read","args":{"prefix":"co"}}]}"# + .to_owned(), + )) + } +} + +#[tokio::test] +async fn non_object_args_never_reach_the_world() { + // #7929: before the parse_wants rejection, a non-object args was + // dispatched and collapsed to no arguments in the handler. Now the + // wants document fails to parse, so the request is never dispatched: + // no records.read usage is recorded, and the world stays responsive. + let core = WorldCore::new("dos-bad-args", WorldLimits::default()).unwrap(); + let handle = WorldHandle::spawn_with_program(core, Box::new(NonObjectArgsProgram)); + let (_participant, token) = handle.issue("probe", WorldScopes::GUEST).await.unwrap(); + handle.chat(token.clone(), "still here").await.unwrap(); + let report = handle.caps_report().await.unwrap(); + assert!( + report.usage.get("records.read").is_none_or(|usage| usage.allowed == 0 && usage.denied == 0), + "the non-object-args request must never be dispatched: {:?}", + report.usage.get("records.read") + ); + handle.chat(token, "and again").await.unwrap(); + handle.shutdown().await.unwrap(); + + // Control: the identical request with an object args IS dispatched + // (and refused here only because nothing is granted: cap_denied). + let core = WorldCore::new("dos-good-args", WorldLimits::default()).unwrap(); + let handle = WorldHandle::spawn_with_program(core, Box::new(ObjectArgsProgram)); + let (_participant, token) = handle.issue("probe", WorldScopes::GUEST).await.unwrap(); + handle.chat(token, "still here").await.unwrap(); + let report = handle.caps_report().await.unwrap(); + assert_eq!( + report.usage.get("records.read").map(|usage| usage.denied), + Some(1), + "the object-args control request must be dispatched exactly once" + ); + handle.shutdown().await.unwrap(); +} + #[tokio::test] async fn a_hostile_program_is_refused_without_taking_the_node_down() { let core = WorldCore::new("dos", WorldLimits::default()).unwrap();