Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkgs/id/crates/id-core/src/world_caps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,21 @@ pub fn parse_wants(text: &str) -> Result<Wants> {
.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"
Expand Down Expand Up @@ -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());
Expand Down
57 changes: 57 additions & 0 deletions pkgs/id/tests/world_dos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>> {
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<Option<String>> {
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();
Expand Down
Loading