diff --git a/crates/im-core/src/names.rs b/crates/im-core/src/names.rs index 9149373..64ff074 100644 --- a/crates/im-core/src/names.rs +++ b/crates/im-core/src/names.rs @@ -27,9 +27,12 @@ pub const MAX_LOCAL_LEN: usize = 64; /// Longest domain label, in bytes (RFC 1035). const MAX_LABEL_LEN: usize = 63; -/// The stored form of an address: surrounding whitespace removed, then lowercased. +/// The stored form of an address: surrounding ASCII whitespace removed, then ASCII lowercased. +/// +/// Only ASCII is touched. Unicode trimming or case folding would turn look-alikes (a no-break space, +/// the Kelvin sign) into an existing address; left alone, they fail the ASCII shape check instead. pub fn normalize_email(input: &str) -> String { - input.trim().to_lowercase() + input.trim_ascii().to_ascii_lowercase() } /// Normalises `input` and checks its shape. Returns the stored form. @@ -155,6 +158,20 @@ mod tests { ); } + #[test] + fn look_alikes_do_not_fold_into_an_address() { + assert_ne!( + normalize_email("\u{a0}admin@example.test"), + "admin@example.test" + ); + assert_ne!( + normalize_email("\u{212a}elvin@example.test"), + "kelvin@example.test" + ); + assert!(parse_email("\u{a0}admin@example.test").is_err()); + assert!(parse_email("\u{212a}elvin@example.test").is_err()); + } + #[test] fn display_names_are_trimmed_and_checked() { assert_eq!(