From 62377a73ee3acafa06158c84fac9188aa2a8a19d Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:21:46 -0600 Subject: [PATCH] fix(id): reject world_created replies with a missing or mistyped created flag (#6632) --- pkgs/id/src/world_net.rs | 77 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/world_net.rs b/pkgs/id/src/world_net.rs index 5d33299c2f..a09e2d2b08 100644 --- a/pkgs/id/src/world_net.rs +++ b/pkgs/id/src/world_net.rs @@ -1242,6 +1242,17 @@ impl WorldClient { .recv_json() .await? .context("host closed without replying")?; + Self::parse_create_world_reply(&reply) + } + + /// Parse a `world_created` reply frame into the `created` flag. + /// + /// Factored out of [`WorldClient::create_world`] so the reply + /// validation can be unit-tested without a live host. `false` is a + /// real answer (the world already existed), so a missing or + /// mistyped `created` field must never default to it: that shape + /// is corruption or version skew and errors instead (issue #6632). + fn parse_create_world_reply(reply: &serde_json::Value) -> Result { if reply["type"] != "world_created" { bail!( "{}", @@ -1250,7 +1261,13 @@ impl WorldClient { .unwrap_or("host refused to create the world") ); } - Ok(reply["created"].as_bool().unwrap_or(false)) + match reply.get("created") { + Some(serde_json::Value::Bool(created)) => Ok(*created), + Some(_) => { + bail!("host sent a malformed world_created reply: `created` is not a boolean") + } + None => bail!("host sent no created flag in world_created reply"), + } } /// Receive one JSON frame; `None` when the host ended the session. @@ -3489,4 +3506,62 @@ mod tests { ); } } + + #[test] + fn create_world_reply_requires_created_flag() { + // Issue #6632: `false` is a real answer ("the world already + // existed"), so a `world_created` reply whose `created` field + // is missing or mistyped must error, never read as Ok(false). + // Oracle: the sibling strictness of parse_module_info_reply — + // a present-but-wrong-shape field is corruption, not a default. + let parse = |reply: serde_json::Value| WorldClient::parse_create_world_reply(&reply); + + // Well-formed controls keep their meaning. + assert!( + parse(serde_json::json!({"type": "world_created", "world": "arena", "created": true})) + .unwrap() + ); + assert!( + !parse( + serde_json::json!({"type": "world_created", "world": "arena", "created": false}) + ) + .unwrap() + ); + + // Missing `created` -> an error naming the flag. + let result = parse(serde_json::json!({"type": "world_created", "world": "arena"})); + assert!( + result.is_err(), + "missing created must error, got {result:?}" + ); + assert!( + result.err().unwrap().to_string().contains("created"), + "error must name the created flag" + ); + + // Mistyped `created` shapes -> an error, never Ok(false). + for bad in [ + serde_json::json!("true"), + serde_json::json!(1), + serde_json::Value::Null, + serde_json::json!(["true"]), + ] { + let result = parse( + serde_json::json!({"type": "world_created", "world": "arena", "created": bad}), + ); + assert!( + result.is_err(), + "mistyped created must error, got {result:?}" + ); + } + + // The refusal path still surfaces the host's message. + let result = parse(serde_json::json!({"type": "error", "message": "bad admin token"})); + assert_eq!(result.err().unwrap().to_string(), "bad admin token"); + let result = parse(serde_json::json!({"type": "error"})); + assert_eq!( + result.err().unwrap().to_string(), + "host refused to create the world" + ); + } }