diff --git a/pkgs/id/src/directory.rs b/pkgs/id/src/directory.rs index 9d70a8c811..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!( @@ -1841,10 +1865,20 @@ fn clean_name(name: &str) -> Result { !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, @@ -1854,6 +1888,12 @@ fn clean_description(description: &str) -> Result { !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()) } @@ -1891,6 +1931,61 @@ mod tests { (id, credential) } + #[test] + 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()); + + assert_eq!(clean_name("Alice").unwrap(), "Alice"); + assert_eq!( + clean_description("Group description\nwith a newline").unwrap(), + "Group description\nwith a newline" + ); + } + + #[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();