Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 31 additions & 7 deletions pkgs/id/src/world/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -205,6 +205,11 @@ impl WorldCore {
let mut program = None;
let mut replay = Vec::new();
let mut present: BTreeMap<u64, u64> = 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 }) => {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
74 changes: 74 additions & 0 deletions pkgs/id/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 —
Expand Down
Loading