diff --git a/pkgs/id/src/world_manifest.rs b/pkgs/id/src/world_manifest.rs index cfeb7be915..165a6feab7 100644 --- a/pkgs/id/src/world_manifest.rs +++ b/pkgs/id/src/world_manifest.rs @@ -368,14 +368,27 @@ pub fn plan(desired: &WorldManifest, current: &WorldState) -> Vec { /// /// # Errors /// -/// A step that names an unknown capability (a manifest should be validated -/// first; this is a defensive re-check). +/// A step that names an unknown capability, or that would fold a value +/// `WorldManifest::validate` refuses — a schedule firing at time zero or +/// past the pending-schedule cap, a profile outside its bounds, or a +/// module hash that is not 1 to `MAX_NAME_BYTES` hex digits (a manifest +/// should be validated first; this is a defensive re-check, #4769). pub fn apply(steps: &[ApplyStep], state: &mut WorldState) -> Result { let mut changed = 0; for step in steps { match step { ApplyStep::InstallModule { hash, source, seed } => { if let Some(hash) = hash { + // Same hash form validate() enforces (lines above): + // a non-hex, empty, or over-long hash must not fold + // into state as the live module hash. + let valid = !hash.is_empty() + && hash.len() <= MAX_NAME_BYTES + && hash.bytes().all(|b| b.is_ascii_hexdigit()); + ensure!( + valid, + "module hash must be 1 to {MAX_NAME_BYTES} hex digits" + ); state.module_hash = Some(hash.clone()); } state.seed = *seed; @@ -399,6 +412,7 @@ pub fn apply(steps: &[ApplyStep], state: &mut WorldState) -> Result { } } ApplyStep::SetProfile { profile } => { + profile.validate()?; if state.profile != *profile { state.profile = *profile; changed += 1; @@ -406,6 +420,17 @@ pub fn apply(steps: &[ApplyStep], state: &mut WorldState) -> Result { } ApplyStep::ScheduleAt { id, at_ms } => { crate::world_caps::validate_schedule_id(id)?; + ensure!(*at_ms > 0, "schedule {id:?} must fire in the future"); + // Replacing an existing schedule never grows the map; a + // new id must not push it past the pending-schedule cap + // validate() enforces. + if !state.schedules.contains_key(id) { + ensure!( + state.schedules.len() < MAX_PENDING_SCHEDULES, + "too many schedules: {} exceeds the maximum of {MAX_PENDING_SCHEDULES} pending schedules", + state.schedules.len() + 1 + ); + } if state.schedules.get(id) != Some(at_ms) { state.schedules.insert(id.clone(), *at_ms); changed += 1; @@ -708,6 +733,85 @@ native_memory_bytes = 16777216") assert!(plan(&manifest, &state).is_empty()); } + #[test] + fn apply_refuses_schedule_at_time_zero() { + // #4769: validate() refuses at_ms == 0; apply()'s re-check must too, + // naming the schedule id, and must not store the schedule. + let mut state = WorldState::default(); + let steps = [ApplyStep::ScheduleAt { + id: "wake".to_owned(), + at_ms: 0, + }]; + let err = apply(&steps, &mut state).unwrap_err(); + assert!(format!("{err}").contains("wake"), "{err}"); + assert!(state.schedules.is_empty()); + } + + #[test] + fn apply_refuses_schedules_past_the_cap() { + // #4769: a step list that would leave more than + // MAX_PENDING_SCHEDULES schedules in state is refused. + let steps: Vec = (0..=MAX_PENDING_SCHEDULES) + .map(|i| ApplyStep::ScheduleAt { + id: format!("s{i}"), + at_ms: 1, + }) + .collect(); + let mut state = WorldState::default(); + let err = apply(&steps, &mut state).unwrap_err(); + assert!(format!("{err}").contains("too many schedules"), "{err}"); + assert_eq!(state.schedules.len(), MAX_PENDING_SCHEDULES); + // Replacing an existing schedule at the cap is not growth and + // still folds. + let replace = [ApplyStep::ScheduleAt { + id: "s0".to_owned(), + at_ms: 2, + }]; + assert_eq!(apply(&replace, &mut state).unwrap(), 1); + assert_eq!(state.schedules.len(), MAX_PENDING_SCHEDULES); + } + + #[test] + fn apply_refuses_a_profile_that_fails_validation() { + // #4769: SetProfile must run WorldExecutionProfile::validate(), + // which refuses a zero wasm_fuel bound. + let mut state = WorldState::default(); + let steps = [ApplyStep::SetProfile { + profile: WorldExecutionProfile { + wasm_fuel: Some(0), + ..WorldExecutionProfile::default() + }, + }]; + assert!(apply(&steps, &mut state).is_err()); + assert_eq!(state.profile, WorldExecutionProfile::default()); + } + + #[test] + fn apply_refuses_a_malformed_module_hash() { + // #4769: InstallModule must enforce the same 1..=64 hex form + // validate() enforces — non-hex, empty, and over-long hashes are + // refused and never stored; a valid hash folds as before. + for hash in ["", "zz!", &"a".repeat(MAX_NAME_BYTES + 1)] { + let mut state = WorldState::default(); + let steps = [ApplyStep::InstallModule { + hash: Some(hash.to_owned()), + source: None, + seed: 1, + }]; + let err = apply(&steps, &mut state).unwrap_err(); + assert!(format!("{err}").contains("module hash"), "{err}"); + assert_eq!(state.module_hash, None, "hash {hash:?} was stored"); + } + let mut state = WorldState::default(); + let steps = [ApplyStep::InstallModule { + hash: Some("abc123".to_owned()), + source: None, + seed: 1, + }]; + assert_eq!(apply(&steps, &mut state).unwrap(), 1); + assert_eq!(state.module_hash.as_deref(), Some("abc123")); + } + #[test] fn drift_is_detected_after_a_manual_change() { let manifest = parse(manifest_toml()).unwrap();