From 37aa6ebea1bf1b77738032cc7413359177ca8d39 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 13:13:16 -0600 Subject: [PATCH 1/3] fix(id): reject format characters in directory names --- pkgs/id/src/directory.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pkgs/id/src/directory.rs b/pkgs/id/src/directory.rs index 9d70a8c811..87eb255507 100644 --- a/pkgs/id/src/directory.rs +++ b/pkgs/id/src/directory.rs @@ -1838,8 +1838,10 @@ fn clean_name(name: &str) -> Result { "name must be at most {MAX_NAME_CHARS} characters" ); ensure!( - !name.chars().any(char::is_control), - "name must not contain control characters" + !name + .chars() + .any(|c| c.is_control() || crate::petname::is_format_char(c)), + "name must not contain control or format characters" ); Ok(name.to_owned()) } @@ -1851,8 +1853,10 @@ fn clean_description(description: &str) -> Result { "description must be at most {MAX_DESCRIPTION_CHARS} characters" ); ensure!( - !description.chars().any(|c| c.is_control() && c != '\n'), - "description must not contain control characters" + !description + .chars() + .any(|c| { (c.is_control() && c != '\n') || crate::petname::is_format_char(c) }), + "description must not contain control or format characters" ); Ok(description.to_owned()) } @@ -1891,6 +1895,19 @@ mod tests { (id, credential) } + #[test] + fn cleaners_reject_format_characters_and_accept_plain_text() { + assert!(clean_name("Al\u{200B}ice").is_err()); + assert!(clean_name("Alice\u{202E}Admin").is_err()); + assert!(clean_description("Group\u{202E} description").is_err()); + + assert_eq!(clean_name("Alice").unwrap(), "Alice"); + assert_eq!( + clean_description("Group description\nwith a newline").unwrap(), + "Group description\nwith a newline" + ); + } + #[test] fn a_credential_authenticates_only_its_own_account() { let mut directory = Directory::new(); From 4a0826d92f5d6d8af07d598b1692acaac7dbddf2 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 13:15:13 -0600 Subject: [PATCH 2/3] test(id): follow directory test naming convention --- pkgs/id/src/directory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/id/src/directory.rs b/pkgs/id/src/directory.rs index 87eb255507..ad34888f71 100644 --- a/pkgs/id/src/directory.rs +++ b/pkgs/id/src/directory.rs @@ -1896,7 +1896,7 @@ mod tests { } #[test] - fn cleaners_reject_format_characters_and_accept_plain_text() { + fn test_cleaners_reject_format_characters_and_accept_plain_text() { assert!(clean_name("Al\u{200B}ice").is_err()); assert!(clean_name("Alice\u{202E}Admin").is_err()); assert!(clean_description("Group\u{202E} description").is_err()); From c1d3d68408ff5e370e7aeaca9fcc5dfe0df5f7d1 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 16:06:06 -0600 Subject: [PATCH 3/3] fix(id): preserve replay of pre-#1778 directory entries with format characters Codex P1 on PR #3945: Directory::replay passes every historical entry through apply -> checked -> change, which invokes the same cleaners the #1778 fix just tightened. A journal written before the fix can legitimately contain a category-Cf character in an account/group name or group description, and replaying it would refuse, so the world could never reopen. Split the cleaners into strict/mode variants and thread a refuse_format flag from apply: new writes (apply, checked, change, store_artifact effects) refuse Cf exactly as before, while replay (apply_replay) keeps every other check - Cc refusal, length, trim - but tolerates format characters in historical entries. Regression test: replay_tolerates_pre_fix_format_characters_in_group_fields crafts a journal whose GroupUpdated entry carries U+202E/U+200B, asserts replay reopens with the historical values intact, and asserts create_group still refuses a Cf name. Tests: cargo test -p id --lib --no-default-features --features world directory::tests 44/44 pass; petname::tests 3/3 pass; rustfmt clean. --- pkgs/id/src/directory.rs | 116 ++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 19 deletions(-) diff --git a/pkgs/id/src/directory.rs b/pkgs/id/src/directory.rs index ad34888f71..da19046cfc 100644 --- a/pkgs/id/src/directory.rs +++ b/pkgs/id/src/directory.rs @@ -525,7 +525,7 @@ impl Directory { let entry: DirectoryEntry = serde_json::from_str(line) .with_context(|| format!("directory journal line {} is malformed", number + 1))?; directory - .apply(&entry) + .apply_replay(&entry) .with_context(|| format!("directory journal line {} is refused", number + 1))?; } Ok(directory) @@ -1442,12 +1442,12 @@ impl Directory { Ok((id.to_owned(), key)) } - fn insert_account(&mut self, id: &str, name: &str) -> Result<()> { + fn insert_account(&mut self, id: &str, name: &str, refuse_format: bool) -> Result<()> { self.accounts.insert( id.to_owned(), Account { id: id.to_owned(), - name: clean_name(name)?, + name: clean_name_mode(name, refuse_format)?, verified: false, keys: BTreeSet::new(), emails: BTreeSet::new(), @@ -1466,7 +1466,19 @@ impl Directory { } pub(crate) fn apply(&mut self, entry: &DirectoryEntry) -> Result<()> { - let next = self.checked(entry)?; + self.apply_mode(entry, true) + } + + /// Replay a historical journal entry. Entries accepted before the + /// category-Cf refusal (#1778) may carry format characters; replaying + /// them must not refuse, or a world whose journal predates the fix + /// can never reopen. New writes still go through `apply` and refuse. + fn apply_replay(&mut self, entry: &DirectoryEntry) -> Result<()> { + self.apply_mode(entry, false) + } + + fn apply_mode(&mut self, entry: &DirectoryEntry, refuse_format: bool) -> Result<()> { + let next = self.checked_mode(entry, refuse_format)?; if let Some(path) = &self.journal { Self::append(path, entry)?; } @@ -1475,13 +1487,21 @@ impl Directory { } fn checked(&self, entry: &DirectoryEntry) -> Result { + self.checked_mode(entry, true) + } + + fn checked_mode(&self, entry: &DirectoryEntry, refuse_format: bool) -> Result { let mut next = self.clone(); - next.change(entry)?; + next.change_mode(entry, refuse_format)?; next.ensure_admins()?; Ok(next) } fn change(&mut self, entry: &DirectoryEntry) -> Result<()> { + self.change_mode(entry, true) + } + + fn change_mode(&mut self, entry: &DirectoryEntry, refuse_format: bool) -> Result<()> { match entry { DirectoryEntry::AccountSignedUp { id, name, digest } => { ensure_account_id(id)?; @@ -1491,7 +1511,7 @@ impl Directory { "account already exists" ); let digest = hex_to_array::<32>(digest).context("malformed account digest")?; - self.insert_account(id, name)?; + self.insert_account(id, name, refuse_format)?; self.digests.insert(id.clone(), digest); } DirectoryEntry::AccountKeyed { id, name } => { @@ -1501,7 +1521,7 @@ impl Directory { Conflict, "that key already belongs to an account" ); - self.insert_account(id, name)?; + self.insert_account(id, name, refuse_format)?; } DirectoryEntry::AccountVerified { id } => { self.accounts @@ -1572,8 +1592,8 @@ impl Directory { *id, Group { id: *id, - name: clean_name(name)?, - description: clean_description(description)?, + name: clean_name_mode(name, refuse_format)?, + description: clean_description_mode(description, refuse_format)?, scopes: permissions(*scopes)?, public: false, }, @@ -1595,8 +1615,8 @@ impl Directory { description, scopes, } => { - let name = clean_name(name)?; - let description = clean_description(description)?; + let name = clean_name_mode(name, refuse_format)?; + let description = clean_description_mode(description, refuse_format)?; let scopes = permissions(*scopes)?; let group = self.groups.get_mut(id).context("no such group")?; group.name = name; @@ -1831,6 +1851,10 @@ fn permissions(bits: u8) -> Result { } fn clean_name(name: &str) -> Result { + clean_name_mode(name, true) +} + +fn clean_name_mode(name: &str, refuse_format: bool) -> Result { let name = name.trim(); ensure!(!name.is_empty(), "name must not be empty"); ensure!( @@ -1838,26 +1862,38 @@ fn clean_name(name: &str) -> Result { "name must be at most {MAX_NAME_CHARS} characters" ); ensure!( - !name - .chars() - .any(|c| c.is_control() || crate::petname::is_format_char(c)), - "name must not contain control or format characters" + !name.chars().any(char::is_control), + "name must not contain control characters" ); + if refuse_format { + ensure!( + !name.chars().any(crate::petname::is_format_char), + "name must not contain format characters" + ); + } Ok(name.to_owned()) } fn clean_description(description: &str) -> Result { + clean_description_mode(description, true) +} + +fn clean_description_mode(description: &str, refuse_format: bool) -> Result { let description = description.trim(); ensure!( description.chars().count() <= MAX_DESCRIPTION_CHARS, "description must be at most {MAX_DESCRIPTION_CHARS} characters" ); ensure!( - !description - .chars() - .any(|c| { (c.is_control() && c != '\n') || crate::petname::is_format_char(c) }), - "description must not contain control or format characters" + !description.chars().any(|c| c.is_control() && c != '\n'), + "description must not contain control characters" ); + if refuse_format { + ensure!( + !description.chars().any(crate::petname::is_format_char), + "description must not contain format characters" + ); + } Ok(description.to_owned()) } @@ -1908,6 +1944,48 @@ mod tests { ); } + #[test] + fn replay_tolerates_pre_fix_format_characters_in_group_fields() { + // #1778 Codex P1: a journal written before the Cf refusal can + // legitimately contain format characters in a group name or + // description. Replay must reopen such a world, while new + // writes through the public path still refuse Cf. + let dir = TempDir::new().unwrap(); + let path = dir.path().join("directory.jsonl"); + let mut directory = Directory::new(); + let (ann, _) = account_into(&mut directory, &path, "Ann"); + let (group, entry) = directory + .create_group(&ann, "Crew", "hello", all()) + .unwrap(); + Directory::append(&path, &entry).unwrap(); + + let historical = DirectoryEntry::GroupUpdated { + id: group, + name: "Crew\u{202E}X".to_owned(), + description: "hi\u{200B} there".to_owned(), + scopes: all().bits(), + }; + let mut line = serde_json::to_string(&historical).unwrap(); + line.push('\n'); + let mut text = std::fs::read_to_string(&path).unwrap(); + text.push_str(&line); + std::fs::write(&path, text).unwrap(); + + let replayed = Directory::replay(&path).unwrap(); + assert_eq!(replayed.group(group).unwrap().name, "Crew\u{202E}X"); + assert_eq!( + replayed.group(group).unwrap().description, + "hi\u{200B} there" + ); + + assert!( + directory + .create_group(&ann, "Bad\u{200B}Name", "hello", all()) + .is_err(), + "new writes must still refuse format characters" + ); + } + #[test] fn a_credential_authenticates_only_its_own_account() { let mut directory = Directory::new();