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
12 changes: 10 additions & 2 deletions pkgs/id/src/world/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,23 @@ impl WorldCore {
// 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.
// check existed. The pre-#4711 sanitizer (no trim
// after the 48-char cap) is also accepted: journals
// minted before the boundary fix can legitimately
// carry 48-char names ending in whitespace, and
// refusing those would brick worlds that minted
// fine under the rules of their time.
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
)),
)
|| is_pre_fix_mint_display_name(
&participant.display_name
)),
"participant {} has a display name mint could not have produced",
participant.id
);
Expand Down
1 change: 1 addition & 0 deletions pkgs/id/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub use replication_receiver::{
pub use util::{EventPage, hex_encode, publish_records};
pub(crate) use util::{
constant_time_eq, hex_decode_32, legacy_sanitize_display_name, sanitize_chat,
is_pre_fix_mint_display_name, pre_fix_sanitize_display_name,
sanitize_display_name, token_digest,
};

Expand Down
49 changes: 49 additions & 0 deletions pkgs/id/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,55 @@ mod tests {
[header.clone(), legacy],
)
.unwrap();
// A name minted before #4711 — 48 chars ending in a space, the
// exact output of the pre-fix sanitizer for a longer input —
// also restores fine: that journal was written by a live mint
// path, and refusing it bricks the world.
let boundary = JournalEntry::Issued {
participant: Participant {
id: 1,
display_name: format!("{} ", "a".repeat(47)),
},
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(), boundary],
)
.unwrap();
// And a name minted now from a boundary input (>48 chars,
// whitespace at position 48) is trimmed at mint and restores.
let (minted, minted_token) = source
.issue_capability(
&format!("{} {}", "a".repeat(47), "b".repeat(10)),
WorldScopes::GUEST,
CapabilityBounds::default(),
)
.unwrap();
assert_eq!(minted.display_name, "a".repeat(47));
let minted_entry = JournalEntry::Issued {
participant: minted.clone(),
digest: hex_encode(&token_digest(minted_token.expose())),
scopes: WorldScopes::GUEST.bits(),
parent: None,
expires_at: None,
uses: None,
used: 0,
subject: None,
};
WorldCore::restore(
"test-world",
WorldLimits::default(),
[header.clone(), minted_entry],
)
.unwrap();
for bad_name in [
String::new(),
"a\u{7}\n".to_owned() + &"x".repeat(98),
Expand Down
94 changes: 93 additions & 1 deletion pkgs/id/src/world/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ pub(crate) fn hex_decode_32(encoded: &str) -> Option<[u8; 32]> {
}

pub(crate) fn sanitize_display_name(name: &str) -> String {
// #4711: trim again after the 48-char cap. The pre-cap trim alone
// let truncation land on a whitespace character, minting a 48-char
// name ending in whitespace — a fixed point of neither sanitizer,
// which the #2960 restore check then refused, leaving the journal
// unrestorable. The final trim makes every minted name a fixed
// point of this function.
pre_fix_sanitize_display_name(name).trim().to_owned()
}

/// The display-name sanitizer as it existed before #4711 added the
/// post-truncation trim: control and format characters stripped,
/// trimmed, then capped at 48 chars — with no trim after the cap, so a
/// name whose 48th character was whitespace was minted with trailing
/// whitespace. Journals written by that mint can legitimately carry
/// such names, and restore accepts those for journal compatibility
/// (see the #2960 check in `WorldCore::restore`).
pub(crate) fn pre_fix_sanitize_display_name(name: &str) -> String {
name.chars()
.filter(|ch| !ch.is_control() && !crate::petname::is_format_char(*ch))
.collect::<String>()
Expand All @@ -92,6 +109,30 @@ pub(crate) fn legacy_sanitize_display_name(name: &str) -> String {
.to_owned()
}

/// Whether `name` is a display name the pre-#4711 mint could have
/// produced. That mint returned the first 48 chars of a control- and
/// format-stripped, trimmed string, so a producible name contains no
/// stripped characters, never starts with whitespace, is at most 48
/// chars, and — unless it fills the whole 48-char budget — is fully
/// trimmed: trailing whitespace was only possible when truncation
/// landed on it at exactly 48 chars. (Such names are not fixed points
/// of the pre-fix sanitizer — re-applying it trims them — so restore
/// cannot recognize them by equality; see the #2960 check in
/// `WorldCore::restore`.)
pub(crate) fn is_pre_fix_mint_display_name(name: &str) -> bool {
if name.chars().next().is_none_or(|first| first.is_whitespace()) {
return false;
}
if name
.chars()
.any(|ch| ch.is_control() || crate::petname::is_format_char(ch))
{
return false;
}
let count = name.chars().count();
count == 48 || (count < 48 && name.trim() == name)
}

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

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

#[test]
fn display_name_strips_bidi_override() {
Expand Down Expand Up @@ -151,6 +195,54 @@ mod tests {
);
}

#[test]
fn display_name_truncation_never_leaves_trailing_whitespace() {
// #4711 boundary shapes: whitespace landing exactly on the
// 48-char cap is trimmed after truncation, and every minted
// name is a fixed point of the sanitizer.
let cases = [
(format!("{} {}", "a".repeat(47), "b".repeat(10)), "a".repeat(47)),
(format!("{} {}", "a".repeat(46), "b".repeat(10)), "a".repeat(46)),
(
format!("{}\u{a0}{}", "a".repeat(47), "b".repeat(10)),
"a".repeat(47),
),
];
for (input, expected) in cases {
let minted = sanitize_display_name(&input);
assert_eq!(minted, expected, "input: {input:?}");
assert_eq!(sanitize_display_name(&minted), minted);
}
// A full 48-char name with no boundary whitespace is untouched.
let exactly_48 = "x".repeat(48);
assert_eq!(sanitize_display_name(&exactly_48), exactly_48);
}

#[test]
fn pre_fix_sanitizer_reproduces_boundary_names_restore_must_accept() {
// The pre-#4711 mint produced exactly this 48-char name ending
// in a space; journals carrying it must remain restorable.
let boundary = format!("{} ", "a".repeat(47));
assert_eq!(
pre_fix_sanitize_display_name(&format!("{} {}", "a".repeat(47), "b".repeat(10))),
boundary
);
// It is recognizable as a pre-fix mint output even though it
// is not a fixed point of the pre-fix sanitizer (re-applying
// that sanitizer trims it), and the current sanitizer maps it
// to its trim.
assert!(is_pre_fix_mint_display_name(&boundary));
assert_eq!(sanitize_display_name(&boundary), "a".repeat(47));
// Names no mint path could produce are not pre-fix outputs:
// short-but-untrimmed, over-length, control-bearing, or
// leading-whitespace names.
assert!(!is_pre_fix_mint_display_name("Ada "));
assert!(!is_pre_fix_mint_display_name(&"a".repeat(49)));
assert!(!is_pre_fix_mint_display_name("a\u{7}b"));
assert!(!is_pre_fix_mint_display_name(" Ada"));
assert!(is_pre_fix_mint_display_name("Ada"));
}

#[test]
fn chat_strips_bidi_isolates() {
assert_eq!(sanitize_chat("a\u{2066}b\u{2069}c"), "abc");
Expand Down