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
17 changes: 16 additions & 1 deletion pkgs/id/src/world/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ impl WorldCore {
participant.id
);
}
// #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
// a capability mint could never have issued.
let Some(validated_scopes) = WorldScopes::from_bits(scopes) else {
anyhow::bail!(
"participant {} journaled scopes {scopes:#04x} name no scope",
participant.id
);
};
ensure!(
validated_scopes.contains(WorldScopes::JOIN),
"participant {} journaled scopes must include join scope",
participant.id
);
core.next_participant_id = core.next_participant_id.max(
participant
.id
Expand All @@ -264,7 +279,7 @@ impl WorldCore {
CapabilityRecord {
digest,
participant,
scopes: WorldScopes(scopes),
scopes: validated_scopes,
parent,
revoked: false,
bounds: CapabilityBounds { expires_at, uses },
Expand Down
70 changes: 70 additions & 0 deletions pkgs/id/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,76 @@ mod tests {
);
}

fn issued_entry(scopes: u8) -> JournalEntry {
let secret = "ef".repeat(32);
let digest = hex_encode(&token_digest(&format!("1.{secret}")));
JournalEntry::Issued {
participant: Participant {
id: 1,
display_name: "ann".to_owned(),
},
digest,
scopes,
parent: None,
expires_at: None,
uses: None,
used: 0,
subject: None,
}
}

fn restore_with_scopes(scopes: u8) -> anyhow::Result<RestoredWorld> {
WorldCore::restore(
"scopes",
WorldLimits::default(),
vec![
JournalEntry::Created {
world_id: "scopes".to_owned(),
version: JOURNAL_VERSION,
},
issued_entry(scopes),
],
)
}

#[test]
fn test_restore_refuses_issued_scopes_naming_no_scope() {
// 0x80 names no scope; from_bits rejects the whole byte.
let error = restore_with_scopes(0x87).unwrap_err();
assert!(
error.to_string().contains("participant 1"),
"error names the participant: {error}"
);
assert!(
error.to_string().contains("name no scope"),
"error explains the refusal: {error}"
);
}

#[test]
fn test_restore_refuses_issued_scopes_without_join() {
for scopes in [0, WorldScopes::CHAT.0, WorldScopes::INPUT.0] {
let error = restore_with_scopes(scopes).unwrap_err();
assert!(
error.to_string().contains("join scope"),
"scopes {scopes:#04x} refused for missing JOIN: {error}"
);
}
}

#[test]
fn test_restore_accepts_guest_and_admin_bit_scopes() {
for scopes in [
WorldScopes::GUEST.0,
WorldScopes::JOIN.0 | WorldScopes::BUILDER.0,
WorldScopes::JOIN.0 | WorldScopes::MODERATOR.0,
] {
let restored = restore_with_scopes(scopes).unwrap();
let record = restored.core.capabilities.get(&1).unwrap();
assert_eq!(record.scopes.bits(), scopes, "scopes round-trip");
}
}

#[test]
fn a_restore_closes_presence_the_journal_left_open() {
let secret = "cd".repeat(32);
Expand Down
Loading