diff --git a/pkgs/id/src/world/core.rs b/pkgs/id/src/world/core.rs index 465f425f1b..6d61bea651 100644 --- a/pkgs/id/src/world/core.rs +++ b/pkgs/id/src/world/core.rs @@ -10,7 +10,7 @@ use super::*; use anyhow::{Context, Result, ensure}; -use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use std::sync::Arc; /// Mutable world state. Keep it behind one actor/mutex so event sequencing is serialized. @@ -205,6 +205,11 @@ impl WorldCore { let mut program = None; let mut replay = Vec::new(); let mut present: BTreeMap = BTreeMap::new(); + // A compacted journal writes a revoked parent before its descendants' + // Issued/Revoked pairs. Remember descendants made unusable by that + // earlier revocation so their later explicit Revoked entries remain + // valid without allowing duplicate revocation entries. + let mut inherited_revocations = HashSet::new(); let mut entries = entries.into_iter(); match entries.next() { Some(JournalEntry::Created { world_id, version }) => { @@ -298,6 +303,19 @@ impl WorldCore { participant.id ); } + let mut ancestor = parent; + let mut revoked_by_ancestor = false; + while let Some(ancestor_id) = ancestor { + let ancestor_record = core + .capabilities + .get(&ancestor_id) + .context("delegated capability ancestor disappeared during restore")?; + if ancestor_record.revoked { + revoked_by_ancestor = true; + break; + } + ancestor = ancestor_record.parent; + } // #2890: mint refuses scope bits from_bits rejects // and scopes without JOIN; a journaled Issued entry // must meet the same bar, or restore would resurrect @@ -319,25 +337,31 @@ impl WorldCore { .checked_add(1) .context("participant ID exhausted")?, ); + let issued_participant_id = participant.id; core.capabilities.insert( - participant.id, + issued_participant_id, CapabilityRecord { digest, participant, scopes: validated_scopes, parent, - revoked: false, + revoked: revoked_by_ancestor, bounds: CapabilityBounds { expires_at, uses }, used, subject, }, ); + if revoked_by_ancestor { + inherited_revocations.insert(issued_participant_id); + } } JournalEntry::Revoked { participant_id, .. } => { - ensure!( - !core.revoke_tree(participant_id).is_empty(), - "journal revokes unknown or revoked participant {participant_id}" - ); + if !inherited_revocations.remove(&participant_id) { + ensure!( + !core.revoke_tree(participant_id).is_empty(), + "journal revokes unknown or revoked participant {participant_id}" + ); + } } JournalEntry::CapsGranted { granted } => { for name in &granted { diff --git a/pkgs/id/src/world/tests.rs b/pkgs/id/src/world/tests.rs index c699d5c9c1..8ca4f532c7 100644 --- a/pkgs/id/src/world/tests.rs +++ b/pkgs/id/src/world/tests.rs @@ -1901,6 +1901,80 @@ mod tests { assert!(WorldCore::restore("test-world", WorldLimits::default(), entries).is_err()); } + #[test] + fn restore_does_not_resurrect_a_delegate_issued_after_parent_revocation() { + let mut source = world(8); + let (owner, owner_token) = source + .issue_capability( + "Ada", + WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()), + CapabilityBounds::default(), + ) + .unwrap(); + let (child, child_token) = source + .attenuate( + &owner_token, + WorldScopes::JOIN, + CapabilityBounds::default(), + "Bo", + ) + .unwrap(); + let created = JournalEntry::Created { + world_id: "test-world".to_owned(), + version: JOURNAL_VERSION, + }; + let owner_issued = source.issued_entry(owner.id).unwrap(); + let parent_revoked = JournalEntry::Revoked { + participant_id: owner.id, + by: None, + }; + let child_issued = source.issued_entry(child.id).unwrap(); + + // A hand-written journal can omit the child's redundant Revoked entry. + // The earlier parent revocation must still keep the child unusable. + let restored = WorldCore::restore( + "test-world", + WorldLimits::default(), + vec![ + created.clone(), + owner_issued.clone(), + parent_revoked.clone(), + child_issued.clone(), + ], + ) + .unwrap(); + assert!( + restored + .core + .authorize(&child_token, WorldScopes::JOIN) + .is_err() + ); + + // Compaction emits the descendant's own Issued/Revoked pair after its + // already-revoked parent; that legitimate order must also restore. + let compacted = WorldCore::restore( + "test-world", + WorldLimits::default(), + vec![ + created, + owner_issued, + parent_revoked, + child_issued, + JournalEntry::Revoked { + participant_id: child.id, + by: None, + }, + ], + ) + .unwrap(); + assert!( + compacted + .core + .authorize(&child_token, WorldScopes::JOIN) + .is_err() + ); + } + #[test] fn restore_refuses_an_issued_participant_zero() { // #2972: participant 0 is the reserved host/system identity —