diff --git a/pkgs/id/src/world_manifest.rs b/pkgs/id/src/world_manifest.rs index 98baa61560..aaf8940817 100644 --- a/pkgs/id/src/world_manifest.rs +++ b/pkgs/id/src/world_manifest.rs @@ -24,7 +24,7 @@ use crate::world_limits::WorldExecutionProfile; /// alone by [`plan`], so a minimal manifest drifts nothing; a field that is /// present — even empty, like `grants = []` — is authoritative. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(default, rename_all = "snake_case")] +#[serde(default, rename_all = "snake_case", deny_unknown_fields)] pub struct WorldManifest { /// A content hash of an already-installed module, encoded as 1 to 64 plain /// hexadecimal digits (no `blake3:` prefix). Mutually exclusive with @@ -148,13 +148,60 @@ impl ApplyStep { /// /// # Errors /// -/// Invalid TOML, or fields that name an unknown capability. +/// Invalid TOML, an unknown key (top-level, or inside `[profile]`), or +/// fields that name an unknown capability. pub fn parse(text: &str) -> Result { + reject_unknown_profile_keys(text)?; let manifest: WorldManifest = toml::from_str(text).map_err(anyhow::Error::new)?; manifest.validate()?; Ok(manifest) } +/// Reject unknown keys in a manifest's `[profile]` table, naming the key. +/// +/// `WorldManifest` denies unknown top-level keys via serde, but the +/// nested profile cannot use `deny_unknown_fields` on +/// [`WorldExecutionProfile`] itself: that struct must stay lenient +/// because it is also deserialized from host-returned JSON (see +/// `current_world_state` in `commands::world`), where a newer host may +/// send fields this build does not know. So [`parse`] runs this probe +/// instead: a typo'd limit key (`wasm_fuell = 1`) must fail loudly +/// instead of silently leaving the live profile alone (#2876). +/// +/// The probe deserializes through the typed TOML deserializer, never +/// `toml::Value` — whose integers are `i64`-capped — so full-width +/// `u64` values the real types accept (a `seed` or `wasm_fuel` above +/// `i64::MAX`) pass through untouched. The known fields' values are +/// irrelevant here (the real parse validates them), so they are read +/// as [`serde::de::IgnoredAny`]. +fn reject_unknown_profile_keys(text: &str) -> Result<()> { + #[derive(Deserialize)] + struct ProfileProbe { + #[serde(default)] + profile: Option, + } + + #[derive(Deserialize)] + #[serde(deny_unknown_fields)] + struct ProfileKeysProbe { + #[serde(default)] + wasm_fuel: Option, + #[serde(default)] + wasm_memory_bytes: Option, + #[serde(default)] + wasm_message_bytes: Option, + #[serde(default)] + native_memory_bytes: Option, + #[serde(default)] + native_cpu_seconds: Option, + #[serde(default)] + native_call_deadline_ms: Option, + } + + let _: ProfileProbe = toml::from_str(text).map_err(anyhow::Error::new)?; + Ok(()) +} + impl WorldManifest { /// Validate the manifest: grants and ceiling name only known capabilities, /// schedule ids are bounded printable ASCII, and fire times are positive. @@ -463,6 +510,45 @@ wasm_fuel = 5_000_000 assert_eq!(round_trip(&manifest).unwrap(), manifest); } + #[test] + fn unknown_top_level_keys_are_rejected() { + // #2876: a typo'd or bogus top-level key must fail parse, naming + // the key — never silently drop a revocation the admin wrote. + let err = parse("grant = []").unwrap_err(); + assert!(format!("{err}").contains("grant"), "{err}"); + let err = parse("bogus_key = 1").unwrap_err(); + assert!(format!("{err}").contains("bogus_key"), "{err}"); + } + + #[test] + fn typoed_field_keys_are_rejected() { + // The probe matrix from #2876: each of these used to parse clean + // and plan zero steps. + for text in ["ceilin = []", "schedule = {}", "seeds = 99"] { + assert!(parse(text).is_err(), "parsed clean: {text}"); + } + } + + #[test] + fn unknown_profile_keys_are_rejected() { + // #2876: same strictness inside [profile], enforced by parse + // itself because the profile struct stays lenient for host JSON. + let err = parse("[profile]\nwasm_fuell = 1").unwrap_err(); + assert!(format!("{err}").contains("wasm_fuell"), "{err}"); + } + + #[test] + fn full_width_u64_values_still_parse() { + // Regression (Codex review of the fix for this issue): the + // [profile] key check must not route the document through + // toml::Value, whose i64 integers would reject u64 values the + // typed manifest accepts. + let manifest = parse("seed = 18446744073709551615").unwrap(); + assert_eq!(manifest.seed, Some(u64::MAX)); + let manifest = parse("[profile]\nwasm_fuel = 18446744073709551615").unwrap(); + assert_eq!(manifest.profile.wasm_fuel, Some(u64::MAX)); + } + #[test] fn an_empty_manifest_drifts_nothing() { let manifest = WorldManifest::default();