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
23 changes: 23 additions & 0 deletions pkgs/id/src/world/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,29 @@ impl WorldCore {
participant.id != 0,
"participant 0 is the reserved host identity and cannot be issued"
);
// #2960: the journaled display name must be one mint
// could have produced. Mint sanitizes every name
// (control and format characters stripped, capped at
// 48 chars, trimmed) and refuses an empty result, so a
// name that is a fixed point of neither sanitization
// — or is empty — comes from a journal no live path
// can write; refuse the whole restore. The pre-#2112
// sanitizer (control characters only) is also accepted:
// journals minted before format stripping landed can
// legitimately carry ZWJ/ZWNJ names such as family
// emoji, and those worlds restored fine before this
// check existed.
ensure!(
!participant.display_name.is_empty()
&& (participant.display_name
== sanitize_display_name(&participant.display_name)
|| participant.display_name
== legacy_sanitize_display_name(
&participant.display_name
)),
"participant {} has a display name mint could not have produced",
participant.id
);
ensure!(
!core.capabilities.contains_key(&participant.id),
"participant {} issued twice",
Expand Down
3 changes: 2 additions & 1 deletion pkgs/id/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ pub use handle::WorldHandle;
pub use metrics::{HistogramSnapshot, MetricsSnapshot, WorldMetrics};
pub use util::{EventPage, hex_encode, publish_records};
pub(crate) use util::{
constant_time_eq, hex_decode_32, sanitize_chat, sanitize_display_name, token_digest,
constant_time_eq, hex_decode_32, legacy_sanitize_display_name, sanitize_chat,
sanitize_display_name, token_digest,
};

/// A chat participant may send at most this many lines during the rolling window.
Expand Down
92 changes: 92 additions & 0 deletions pkgs/id/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,98 @@ mod tests {
}
}

#[test]
fn restore_refuses_issued_display_names_mint_could_not_produce() {
// #2960: restore inserts journaled participants verbatim, but
// mint sanitizes every display name (control/format characters
// stripped, 48-char cap, trimmed) and refuses an empty result.
// A journaled name mint could never have produced — empty,
// control-bearing, over-length, or untrimmed — must refuse the
// whole restore, while a legitimately minted journal restores
// with byte-identical names.
let mut source = world(8);
let (owner, owner_token) = source
.issue_capability("Ada", WorldScopes::GUEST, CapabilityBounds::default())
.unwrap();
let legitimate = JournalEntry::Issued {
participant: owner.clone(),
digest: hex_encode(&token_digest(owner_token.expose())),
scopes: WorldScopes::GUEST.bits(),
parent: None,
expires_at: None,
uses: None,
used: 0,
subject: None,
};
let header = JournalEntry::Created {
world_id: "test-world".to_owned(),
version: JOURNAL_VERSION,
};
// A legitimately minted name restores fine.
WorldCore::restore(
"test-world",
WorldLimits::default(),
[header.clone(), legitimate],
)
.unwrap();
// A name minted before #2112's format stripping — a fixed point
// of the legacy sanitizer only, here a ZWJ family emoji — also
// restores fine: those journals predate the current sanitizer
// and restored successfully before this check existed.
let legacy = JournalEntry::Issued {
participant: Participant {
id: 1,
display_name: "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}".to_owned(),
},
digest: "ab".repeat(32),
scopes: WorldScopes::GUEST.bits(),
parent: None,
expires_at: None,
uses: None,
used: 0,
subject: None,
};
WorldCore::restore(
"test-world",
WorldLimits::default(),
[header.clone(), legacy],
)
.unwrap();
for bad_name in [
String::new(),
"a\u{7}\n".to_owned() + &"x".repeat(98),
"a".repeat(49),
"Ada ".to_owned(),
"\u{1}\u{2}".to_owned(),
] {
let forged = JournalEntry::Issued {
participant: Participant {
id: 1,
display_name: bad_name.clone(),
},
digest: "ab".repeat(32),
scopes: WorldScopes::GUEST.bits(),
parent: None,
expires_at: None,
uses: None,
used: 0,
subject: None,
};
let err = match WorldCore::restore(
"test-world",
WorldLimits::default(),
[header.clone(), forged],
) {
Ok(_) => panic!("restore must refuse display name {bad_name:?}"),
Err(err) => err,
};
assert!(
err.to_string().contains("display name"),
"error must name the display name rule: {err}"
);
}
}

#[test]
fn events_are_sequenced_and_reconnect_can_catch_up() {
let mut core = world(8);
Expand Down
26 changes: 25 additions & 1 deletion pkgs/id/src/world/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ pub(crate) fn sanitize_display_name(name: &str) -> String {
.to_owned()
}

/// The display-name sanitizer as it existed before #2112 (commit
/// 872aa09f) added format-character stripping: control characters
/// stripped, 48-char cap, trimmed. Journals written by that earlier
/// mint can legitimately carry names that are fixed points of this
/// version but not of the current one — ZWJ/ZWNJ sequences such as
/// family emoji are the common case — and restore accepts those for
/// journal compatibility (see the #2960 check in `WorldCore::restore`).
pub(crate) fn legacy_sanitize_display_name(name: &str) -> String {
name.chars()
.filter(|ch| !ch.is_control())
.take(48)
.collect::<String>()
.trim()
.to_owned()
}

pub(crate) fn sanitize_chat(text: &str) -> String {
text.chars()
.filter(|ch| {
Expand All @@ -88,7 +104,7 @@ pub(crate) fn sanitize_chat(text: &str) -> String {

#[cfg(test)]
mod tests {
use super::{sanitize_chat, sanitize_display_name};
use super::{legacy_sanitize_display_name, sanitize_chat, sanitize_display_name};

#[test]
fn display_name_strips_bidi_override() {
Expand All @@ -106,6 +122,14 @@ mod tests {
assert_eq!(sanitize_display_name("a\u{200c}b"), "ab");
}

#[test]
fn legacy_display_name_keeps_joiners_but_strips_controls() {
let family = "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}";
assert_eq!(legacy_sanitize_display_name(family), family);
assert_eq!(legacy_sanitize_display_name("a\u{7}b"), "ab");
assert_eq!(legacy_sanitize_display_name(" Ada "), "Ada");
}

#[test]
fn display_name_strips_control_chars() {
assert_eq!(sanitize_display_name("a\u{7}b"), "ab");
Expand Down
Loading