Skip to content
Merged
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
89 changes: 87 additions & 2 deletions pkgs/id/src/world_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,8 @@ impl WorldClient {
///
/// # Errors
///
/// Fails with the host's message if the admin token or profile is refused.
/// Fails with the host's message if the admin token or profile is refused,
/// or if the reply does not carry a valid `update.requested` profile.
pub async fn set_execution_profile(
&mut self,
admin_token: &str,
Expand All @@ -1149,6 +1150,26 @@ impl WorldClient {
.recv_json()
.await?
.context("host closed without replying")?;
Self::parse_set_execution_profile_reply(&reply)
}

/// Validate an `execution_profile` reply to a set request: the
/// host's typed contract (`ServerFrame::ExecutionProfile`) always
/// carries an `update` whose `requested` is a real
/// [`crate::world_limits::WorldExecutionProfile`], so a reply
/// whose `update.requested` is missing or malformed is not a
/// success — it is a host that did not confirm the profile change
/// (#6777, the setter leg of #6635: the getter already extracts
/// `update.requested` with this same refusal). On success the
/// frame is returned unchanged: consumers print it verbatim and
/// manifest apply counts the step on `Ok`, so a byte-identical
/// return keeps both honest. Factored out of
/// [`WorldClient::set_execution_profile`] so the validation can be
/// unit-tested without a live host sending frames no real host
/// sends.
fn parse_set_execution_profile_reply(
reply: &serde_json::Value,
) -> Result<serde_json::Value> {
if reply["type"] != "execution_profile" {
bail!(
"{}",
Expand All @@ -1157,7 +1178,14 @@ impl WorldClient {
.unwrap_or("host refused the execution profile change")
);
}
Ok(reply)
let requested = reply
.get("update")
.and_then(|update| update.get("requested"))
.context("host returned a malformed execution profile")?;
let _validated: crate::world_limits::WorldExecutionProfile =
serde_json::from_value(requested.clone())
.context("host returned a malformed execution profile")?;
Ok(reply.clone())
}

/// Read the world's requested execution profile.
Expand Down Expand Up @@ -3123,6 +3151,63 @@ mod tests {
);
}

#[test]
fn set_execution_profile_reply_requires_a_valid_requested() {
// Issue #6777: an `execution_profile` frame carrying no valid
// `update.requested` must not read as a successful profile
// change. Driven through the factored reply parser. Oracle:
// the host's typed `ServerFrame::ExecutionProfile` always
// carries a real profile, and the getter in this file extracts
// `update.requested` with the same refusal (#6635).
let requested = serde_json::to_value(crate::world_limits::WorldExecutionProfile {
wasm_fuel: Some(424242),
..Default::default()
})
.unwrap();
let frame = serde_json::json!({
"type": "execution_profile",
"update": {
"requested": requested,
"applies_on_next_program_install_or_process_restart": false,
},
"message": "current persisted execution profile",
});
let ok = WorldClient::parse_set_execution_profile_reply(&frame).unwrap();
// Success returns the frame unchanged (byte-identical Value).
assert_eq!(ok, frame);

// Missing update, missing/null requested, and a mistyped
// requested all refuse with the malformed-profile error.
for bad_reply in [
serde_json::json!({"type": "execution_profile", "message": "ok"}),
serde_json::json!({"type": "execution_profile", "update": {}, "message": "ok"}),
serde_json::json!({"type": "execution_profile", "update": {"requested": null}, "message": "ok"}),
serde_json::json!({"type": "execution_profile", "update": {"requested": {"wasm_fuel": "lots"}}, "message": "ok"}),
] {
let result = WorldClient::parse_set_execution_profile_reply(&bad_reply);
assert!(result.is_err(), "reply {bad_reply} must be refused");
let err = result.err().unwrap();
assert!(
err.to_string().contains("malformed execution profile"),
"unexpected error: {err}"
);
}

// A non-execution_profile frame still bails with the host's
// message.
let refused = WorldClient::parse_set_execution_profile_reply(&serde_json::json!({
"type": "error",
"message": "admin token refused",
}));
assert!(
refused
.err()
.unwrap()
.to_string()
.contains("admin token refused")
);
}

#[test]
fn policy_reply_requires_a_valid_policy() {
// A well-formed policy is returned verbatim for Value consumers.
Expand Down
Loading