From 4896d300677b500f5893a8881facd7dc5cf3ad28 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 20:11:26 -0600 Subject: [PATCH] fix(id/world): keep minted display names restorable at the 48-char boundary (#4711) sanitize_display_name now trims after the 48-char truncation, so every minted name is a fixed point of the sanitizer (previously a name whose 48th char was whitespace was minted with trailing whitespace and then refused by the #2960 restore check, bricking the journal). Restore also accepts names the pre-fix mint could have produced (is_pre_fix_mint_display_name) so journals already written with boundary names remain restorable, while still refusing empty, control-bearing, over-length, and otherwise unproducible names. Tests: sanitizer boundary shapes (space / double space / U+00A0 at the cap), pre-fix producibility predicate, and restore-level cases for pre-fix boundary journals and freshly minted boundary inputs. --- pkgs/id/src/world/core.rs | 12 ++++- pkgs/id/src/world/mod.rs | 1 + pkgs/id/src/world/tests.rs | 49 ++++++++++++++++++++ pkgs/id/src/world/util.rs | 94 +++++++++++++++++++++++++++++++++++++- 4 files changed, 153 insertions(+), 3 deletions(-) diff --git a/pkgs/id/src/world/core.rs b/pkgs/id/src/world/core.rs index 9b789b3621..5a6e5af7db 100644 --- a/pkgs/id/src/world/core.rs +++ b/pkgs/id/src/world/core.rs @@ -383,7 +383,12 @@ 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 @@ -391,7 +396,10 @@ impl WorldCore { || 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 ); diff --git a/pkgs/id/src/world/mod.rs b/pkgs/id/src/world/mod.rs index 0ca62eebe6..c72d180f92 100644 --- a/pkgs/id/src/world/mod.rs +++ b/pkgs/id/src/world/mod.rs @@ -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, }; diff --git a/pkgs/id/src/world/tests.rs b/pkgs/id/src/world/tests.rs index 452ce35bd7..b3b1120f9d 100644 --- a/pkgs/id/src/world/tests.rs +++ b/pkgs/id/src/world/tests.rs @@ -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), diff --git a/pkgs/id/src/world/util.rs b/pkgs/id/src/world/util.rs index a173bdc700..cb8c6070ed 100644 --- a/pkgs/id/src/world/util.rs +++ b/pkgs/id/src/world/util.rs @@ -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::() @@ -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| { @@ -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() { @@ -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");