From 6d505cf4bdc1e2da096e250230f49e0be435b26b Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 12:17:21 -0600 Subject: [PATCH 1/2] id(world): validate event authors on journal restore (#2894) --- pkgs/id/src/world/core.rs | 51 ++++++++++ pkgs/id/src/world/tests.rs | 201 ++++++++++++++++++++++++++++++++++++- 2 files changed, 248 insertions(+), 4 deletions(-) diff --git a/pkgs/id/src/world/core.rs b/pkgs/id/src/world/core.rs index 4bb0d43214..234d9ca004 100644 --- a/pkgs/id/src/world/core.rs +++ b/pkgs/id/src/world/core.rs @@ -938,6 +938,7 @@ impl WorldCore { core.sequence, event.sequence ); + core.validate_restored_event_author(&event, false)?; match &event.kind { WorldEventKind::ProgramInstalled { module_hash, @@ -1019,6 +1020,9 @@ impl WorldCore { }), "journal checkpoint contains a host system event" ); + for event in &events { + core.validate_restored_event_author(event, true)?; + } core.sequence = sequence; core.events = events.into(); while core.events.len() > core.limits.retained_events { @@ -1193,6 +1197,53 @@ impl WorldCore { }) } + /// Check that a journaled event could have been authored by the + /// participant state rebuilt so far. For compacted checkpoint events, + /// revocation timing is no longer represented: `checkpoint_entries` + /// writes the current revoked flags before the retained historical events. + /// Keep those events valid when their participant and scope are present; + /// ordinary `Committed` entries retain enough journal order to reject an + /// author revoked before that event. + fn validate_restored_event_author( + &self, + event: &WorldEvent, + checkpoint_event: bool, + ) -> Result<()> { + let participant_id = event.participant_id; + if participant_id == 0 { + return Ok(()); + } + + let record = self.capabilities.get(&participant_id).with_context(|| { + format!( + "journal event at sequence {} uses participant {participant_id} before it was issued", + event.sequence + ) + })?; + ensure!( + checkpoint_event || !record.revoked, + "journal event at sequence {} is authored by participant {participant_id} after revocation", + event.sequence + ); + + let required_scope = match &event.kind { + WorldEventKind::Input(_) | WorldEventKind::EntityInput { .. } => { + Some((WorldScopes::INPUT, "INPUT")) + } + WorldEventKind::Chat(_) => Some((WorldScopes::CHAT, "CHAT")), + WorldEventKind::System { .. } | WorldEventKind::ProgramInstalled { .. } => None, + }; + if let Some((scope, name)) = required_scope { + ensure!( + self.effective_scopes(participant_id) + .is_some_and(|scopes| scopes.contains(scope)), + "journal event at sequence {} uses participant {participant_id} without {name} scope", + event.sequence + ); + } + Ok(()) + } + /// Whether enough events piled up behind the last checkpoint to trim. pub(crate) const fn checkpoint_due(&self) -> bool { self.limits.checkpoint_every > 0 diff --git a/pkgs/id/src/world/tests.rs b/pkgs/id/src/world/tests.rs index 0212305181..568a0948e6 100644 --- a/pkgs/id/src/world/tests.rs +++ b/pkgs/id/src/world/tests.rs @@ -2079,13 +2079,13 @@ mod tests { ); } - fn issued_entry(scopes: u8) -> JournalEntry { + fn issued_entry_for(participant_id: u64, scopes: u8) -> JournalEntry { let secret = "ef".repeat(32); - let digest = hex_encode(&token_digest(&format!("1.{secret}"))); + let digest = hex_encode(&token_digest(&format!("{participant_id}.{secret}"))); JournalEntry::Issued { participant: Participant { - id: 1, - display_name: "ann".to_owned(), + id: participant_id, + display_name: format!("ann-{participant_id}"), }, digest, scopes, @@ -2097,6 +2097,199 @@ mod tests { } } + fn issued_entry(scopes: u8) -> JournalEntry { + issued_entry_for(1, scopes) + } + + fn restore_author_entries(entries: Vec) -> anyhow::Result { + WorldCore::restore( + "restore-authors", + WorldLimits::default(), + std::iter::once(JournalEntry::Created { + world_id: "restore-authors".to_owned(), + version: JOURNAL_VERSION, + }) + .chain(entries), + ) + } + + fn committed_event(sequence: u64, participant_id: u64, kind: WorldEventKind) -> JournalEntry { + JournalEntry::Committed { + event: WorldEvent { + sequence, + participant_id, + kind, + }, + } + } + + fn checkpoint_event(sequence: u64, events: Vec) -> JournalEntry { + JournalEntry::Checkpoint { + sequence, + module_hash: "module".to_owned(), + seed: 0, + provenance: None, + execution_profile: None, + caps_ceiling: None, + schedules: None, + transfers: None, + snapshot: "snapshot".to_owned(), + events, + } + } + + #[test] + fn restore_refuses_committed_events_from_never_issued_participants() { + for kind in [ + WorldEventKind::Input(b"ghost".to_vec()), + WorldEventKind::Chat("ghost".to_owned()), + ] { + let error = restore_author_entries(vec![committed_event(1, 99, kind)]).unwrap_err(); + assert!( + error.to_string().contains("before it was issued"), + "error explains the missing prior issuance: {error}" + ); + } + } + + #[test] + fn restore_refuses_checkpoint_events_from_never_issued_participants() { + let error = restore_author_entries(vec![checkpoint_event( + 1, + vec![WorldEvent { + sequence: 1, + participant_id: 99, + kind: WorldEventKind::Input(b"ghost".to_vec()), + }], + )]) + .unwrap_err(); + assert!( + error.to_string().contains("before it was issued"), + "error explains the missing prior issuance: {error}" + ); + } + + #[test] + fn restore_refuses_events_from_participants_issued_later() { + let error = restore_author_entries(vec![ + committed_event(1, 2, WorldEventKind::Input(b"late".to_vec())), + issued_entry_for(2, WorldScopes::GUEST.bits()), + ]) + .unwrap_err(); + assert!( + error.to_string().contains("before it was issued"), + "error explains the missing prior issuance: {error}" + ); + } + + #[test] + fn restore_refuses_committed_events_after_author_revocation() { + let error = restore_author_entries(vec![ + issued_entry(WorldScopes::GUEST.bits()), + JournalEntry::Revoked { + participant_id: 1, + by: None, + }, + committed_event(1, 1, WorldEventKind::Chat("after revoke".to_owned())), + ]) + .unwrap_err(); + assert!( + error.to_string().contains("after revocation"), + "error explains the earlier revocation: {error}" + ); + } + + #[test] + fn restore_refuses_input_and_chat_without_the_matching_scope() { + for (kind, scope_name) in [ + (WorldEventKind::Input(b"move".to_vec()), "INPUT"), + (WorldEventKind::Chat("hello".to_owned()), "CHAT"), + ] { + let error = restore_author_entries(vec![ + issued_entry(WorldScopes::JOIN.bits()), + committed_event(1, 1, kind), + ]) + .unwrap_err(); + assert!( + error + .to_string() + .contains(&format!("without {scope_name} scope")), + "error explains the missing scope: {error}" + ); + } + } + + #[test] + fn restore_refuses_checkpoint_events_without_the_matching_scope() { + let error = restore_author_entries(vec![ + issued_entry(WorldScopes::JOIN.bits()), + checkpoint_event( + 1, + vec![WorldEvent { + sequence: 1, + participant_id: 1, + kind: WorldEventKind::Chat("unauthorized".to_owned()), + }], + ), + ]) + .unwrap_err(); + assert!( + error.to_string().contains("without CHAT scope"), + "error explains the missing scope: {error}" + ); + } + + #[test] + fn restore_accepts_host_chat_and_committed_content_before_later_revocation() { + let host_chat = restore_author_entries(vec![committed_event( + 1, + 0, + WorldEventKind::Chat("host".to_owned()), + )]) + .unwrap(); + assert!(matches!( + host_chat.core.events.back().map(|event| &event.kind), + Some(WorldEventKind::Chat(text)) if text == "host" + )); + + let restored = restore_author_entries(vec![ + issued_entry(WorldScopes::GUEST.bits()), + committed_event(1, 1, WorldEventKind::Chat("before revoke".to_owned())), + JournalEntry::Revoked { + participant_id: 1, + by: None, + }, + ]) + .unwrap(); + assert!(matches!( + restored.core.events.back().map(|event| &event.kind), + Some(WorldEventKind::Chat(text)) if text == "before revoke" + )); + } + + #[test] + fn checkpoint_restore_keeps_valid_history_from_later_revoked_participant() { + let mut core = world(8); + let (participant, token) = core + .issue_capability("Ann", WorldScopes::GUEST, CapabilityBounds::default()) + .unwrap(); + core.input(&token, b"before revoke").unwrap(); + assert_eq!(core.revoke_tree(participant.id), vec![participant.id]); + core.active_module_hash = Some("module".to_owned()); + + let entries = core.checkpoint_entries("snapshot".to_owned()).unwrap(); + let restored = WorldCore::restore("test-world", WorldLimits::default(), entries).unwrap(); + assert!(matches!( + restored.core.events.back(), + Some(WorldEvent { + participant_id, + kind: WorldEventKind::Input(input), + .. + }) if *participant_id == participant.id && input.as_slice() == b"before revoke" + )); + } + + fn restore_with_scopes(scopes: u8) -> anyhow::Result { WorldCore::restore( "scopes", From 44a985a12db7c05f81c5388ead099a254ee9a41e Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 00:14:51 -0600 Subject: [PATCH 2/2] id(world): validate restored event scopes from the journaled capability chain Codex P1 on PR #4518: validate_restored_event_author used effective_scopes, which consults the world directory (friendship and ceilings). Restore runs before the directory loads, so a friend-delegated participant had zero effective scopes and its valid journal was rejected on reopen. Historical validation now intersects the issued scopes recorded along the capability chain (journaled_scopes); live paths such as viewer_scopes keep using effective_scopes. Adds restore_accepts_friend_delegated_history_without_directory_state, which fails with the pre-fix call (participant without INPUT scope) and passes now; the checkpoint_event test helper gains the admin_history/lease/open_presence fields main added to the Checkpoint entry. --- pkgs/id/src/world/core.rs | 20 ++++++++++++++- pkgs/id/src/world/tests.rs | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/world/core.rs b/pkgs/id/src/world/core.rs index 234d9ca004..ee442a6e59 100644 --- a/pkgs/id/src/world/core.rs +++ b/pkgs/id/src/world/core.rs @@ -1197,6 +1197,24 @@ impl WorldCore { }) } + /// Scopes a participant capability chain records in the journal + /// itself: the intersection of every ancestor issued scope. + /// Unlike `effective_scopes`, this never consults the world + /// directory: friendship and ceilings are mutable current state, + /// and restore runs before the directory is loaded, so historical + /// events are judged against what the journal recorded when they + /// were committed (Codex P1 finding on PR #4518). + fn journaled_scopes(&self, participant_id: u64) -> Option { + self.capabilities.get(&participant_id)?; + let mut scopes = WorldScopes::ALL; + let mut current = Some(participant_id); + while let Some(record) = current.and_then(|id| self.capabilities.get(&id)) { + scopes = scopes.intersect(record.scopes.implied()); + current = record.parent; + } + Some(scopes) + } + /// Check that a journaled event could have been authored by the /// participant state rebuilt so far. For compacted checkpoint events, /// revocation timing is no longer represented: `checkpoint_entries` @@ -1235,7 +1253,7 @@ impl WorldCore { }; if let Some((scope, name)) = required_scope { ensure!( - self.effective_scopes(participant_id) + self.journaled_scopes(participant_id) .is_some_and(|scopes| scopes.contains(scope)), "journal event at sequence {} uses participant {participant_id} without {name} scope", event.sequence diff --git a/pkgs/id/src/world/tests.rs b/pkgs/id/src/world/tests.rs index 568a0948e6..56ebe2ccbd 100644 --- a/pkgs/id/src/world/tests.rs +++ b/pkgs/id/src/world/tests.rs @@ -2135,6 +2135,9 @@ mod tests { transfers: None, snapshot: "snapshot".to_owned(), events, + admin_history: None, + lease: None, + open_presence: None, } } @@ -2289,6 +2292,55 @@ mod tests { )); } + #[test] + fn restore_accepts_friend_delegated_history_without_directory_state() { + // Codex P1 on PR #4518: restore runs before the directory loads, + // so scope validation must use the recorded capability chain. + // A participant delegated by a parent with a different subject + // (the friend-delegation shape) has zero effective scopes under + // an empty directory; its valid history must still restore. + let secret = "ef".repeat(32); + let issued_with = + |id: u64, scopes: u8, parent: Option, subject: &str| JournalEntry::Issued { + participant: Participant { + id, + display_name: format!("delegate-{id}"), + }, + digest: hex_encode(&token_digest(&format!("{id}.{secret}"))), + scopes, + parent, + expires_at: None, + uses: None, + used: 0, + subject: Some(subject.to_owned()), + }; + let delegated_scopes = WorldScopes::JOIN.0 | WorldScopes::INPUT.0 | WorldScopes::CHAT.0; + let entries = vec![ + issued_with(1, WorldScopes::ALL.bits(), None, "subject-a"), + issued_with(2, delegated_scopes, Some(1), "subject-b"), + committed_event(1, 2, WorldEventKind::Input(b"hello".to_vec())), + committed_event(2, 2, WorldEventKind::Chat("hi".to_owned())), + ]; + let restored = restore_author_entries(entries).unwrap(); + assert!( + restored.core.capabilities.get(&2).is_some(), + "delegated participant restores" + ); + + let checkpoint = vec![ + issued_with(1, WorldScopes::ALL.bits(), None, "subject-a"), + issued_with(2, delegated_scopes, Some(1), "subject-b"), + checkpoint_event( + 1, + vec![WorldEvent { + sequence: 1, + participant_id: 2, + kind: WorldEventKind::Input(b"kept".to_vec()), + }], + ), + ]; + restore_author_entries(checkpoint).unwrap(); + } fn restore_with_scopes(scopes: u8) -> anyhow::Result { WorldCore::restore(