diff --git a/pkgs/id/src/petname.rs b/pkgs/id/src/petname.rs index cd9989e579..e5f620454f 100644 --- a/pkgs/id/src/petname.rs +++ b/pkgs/id/src/petname.rs @@ -119,7 +119,7 @@ impl Petnames { /// /// Fails with a [`Refusal::Conflict`] if another contact already has the /// name, or if it is confusable with another contact's name. Fails with a - /// plain error if the name is empty, too long, or has control characters. + /// plain error if the name is empty, too long, or has control or format characters. pub fn set<'a>( &mut self, owner: &str, @@ -231,12 +231,50 @@ fn looks_like(name: &str) -> anyhow::Error { Refusal::Conflict(format!("that name looks too much like {name}")).into() } +/// Whether `c` is a Unicode format character (general category Cf): +/// zero-width spaces and joiners, bidi controls and isolates, the word +/// joiner, soft hyphen, and the rest of the category. Rust's +/// `char::is_control` covers only category Cc, and Cf characters are +/// invisible or reorder the text around them, so a name carrying one +/// can render exactly like another name. Ranges are the Cf set of +/// Unicode 16. +fn is_format_char(c: char) -> bool { + matches!(c, + '\u{00AD}' + | '\u{0600}'..='\u{0605}' + | '\u{06DD}' + | '\u{070F}' + | '\u{0890}'..='\u{0891}' + | '\u{08E2}' + | '\u{180E}' + | '\u{200B}'..='\u{200F}' + | '\u{202A}'..='\u{202E}' + | '\u{2060}'..='\u{2064}' + | '\u{2066}'..='\u{206F}' + | '\u{FEFF}' + | '\u{FFF9}'..='\u{FFFB}' + | '\u{110BD}' + | '\u{110CD}' + | '\u{13430}'..='\u{1343F}' + | '\u{1BCA0}'..='\u{1BCA3}' + | '\u{1D173}'..='\u{1D182}' + | '\u{E0001}' + | '\u{E0020}'..='\u{E007F}') +} + +/// The name with every format character (category Cf) removed, for the +/// comparison path: a petname stored before Cf was refused, or an +/// alleged name carrying Cf, must compare by what it renders as. +fn strip_format_chars(name: &str) -> String { + name.chars().filter(|&c| !is_format_char(c)).collect() +} + /// The name, trimmed, if it is usable as a petname. /// /// # Errors /// /// Fails if the name is empty, longer than 64 characters, or has control -/// characters. +/// or format characters. pub fn normalize_name(name: &str) -> Result { let name = name.trim(); ensure!(!name.is_empty(), "a petname cannot be empty"); @@ -248,13 +286,20 @@ pub fn normalize_name(name: &str) -> Result { !name.chars().any(char::is_control), "a petname cannot contain control characters" ); + ensure!( + !name.chars().any(is_format_char), + "a petname cannot contain format characters" + ); Ok(name.to_owned()) } /// The name in lower case, for comparing two names as the same spelling. +/// +/// Format characters are stripped first, so a name stored with one +/// (before they were refused) cannot spoof by the comparison path. #[must_use] pub fn fold(name: &str) -> String { - name.to_lowercase() + strip_format_chars(name).to_lowercase() } /// The name with look-alike characters mapped to one form, so `PayPa1` and @@ -577,4 +622,62 @@ mod tests { assert_eq!(skeleton("Rnoon"), skeleton("moon")); assert_ne!(skeleton("Carol"), skeleton("Bob")); } + + #[test] + fn format_characters_are_refused_by_normalize() { + // Every codepoint the issue names, plus one per Cf range the + // category check must cover in full (task 1875: the full + // category, not only the named codepoints). + for bad in [ + "Pay\u{200B}Pal", + "Pay\u{202A}Pal", + "Pay\u{202E}Pal", + "Pay\u{2060}Pal", + "Pay\u{FEFF}Pal", + "Pay\u{00AD}Pal", + "Pay\u{0600}Pal", + "Pay\u{180E}Pal", + "Pay\u{FFF9}Pal", + "Pay\u{E0001}Pal", + "Pay\u{E0041}Pal", + ] { + assert!(normalize_name(bad).is_err(), "{bad:?} must be refused"); + } + // Ordinary punctuation and spaces are unaffected. + assert!(normalize_name("Bo b-Real_O'Neil!").is_ok()); + } + + #[test] + fn a_format_character_spoof_is_refused_by_set() { + let alleged = [(ALICE, "PayPal"), (BOB, "Bob")]; + let mut store = Petnames::new(); + for spoof in ["PayPal\u{200B}", "PayPal\u{2060}", "Pay\u{202E}Pal"] { + let error = store.set(OWNER, CAROL, spoof, alleged).unwrap_err(); + assert!( + matches!(refusal(&error), Some(Refusal::Conflict(_))) + || store.get(OWNER, CAROL).is_none(), + "{spoof:?} must not be stored" + ); + assert!(store.get(OWNER, CAROL).is_none()); + } + } + + #[test] + fn a_stored_format_character_name_cannot_spoof_via_comparison() { + // Pre-fix data: a petname stored with Cf in it. Inserted + // directly, bypassing normalize_name as the old code allowed. + let mut store = Petnames::new(); + store.names.insert( + (OWNER.to_owned(), ALICE.to_owned()), + "PayPal\u{200B}".to_owned(), + ); + // fold/skeleton strip Cf, so the stored name compares equal to + // the clean spelling and a second contact cannot take it, and + // the clean spelling cannot be set against it either. + assert_eq!(fold("PayPal\u{200B}"), fold("PayPal")); + assert_eq!(skeleton("PayPal\u{200B}"), skeleton("PayPal")); + let alleged = [(BOB, "Bob")]; + let error = store.set(OWNER, BOB, "PayPal", alleged).unwrap_err(); + assert!(matches!(refusal(&error), Some(Refusal::Conflict(_)))); + } }