🔒 auth: email normalization touches ASCII only, so look-alikes cannot fold into an address
Browse files at this commit699ed4cb5375186ab07abc0b2c1582f2684e5a76Parent: e9d46871 changed file +19 -2
- crates/im-core/src/names.rs +19 -2
crates/im-core/src/names.rsmodified+19 -2
| @@ -27,9 +27,12 @@ pub const MAX_LOCAL_LEN: usize = 64; | |||
| 27 | /// Longest domain label, in bytes (RFC 1035). | 27 | /// Longest domain label, in bytes (RFC 1035). |
| 28 | const MAX_LABEL_LEN: usize = 63; | 28 | const MAX_LABEL_LEN: usize = 63; |
| 29 | 29 | ||
| 30 | -/// The stored form of an address: surrounding whitespace removed, then lowercased. | 30 | +/// The stored form of an address: surrounding ASCII whitespace removed, then ASCII lowercased. |
| 31 | +/// | ||
| 32 | +/// Only ASCII is touched. Unicode trimming or case folding would turn look-alikes (a no-break space, | ||
| 33 | +/// the Kelvin sign) into an existing address; left alone, they fail the ASCII shape check instead. | ||
| 31 | pub fn normalize_email(input: &str) -> String { | 34 | pub fn normalize_email(input: &str) -> String { |
| 32 | - input.trim().to_lowercase() | 35 | + input.trim_ascii().to_ascii_lowercase() |
| 33 | } | 36 | } |
| 34 | 37 | ||
| 35 | /// Normalises `input` and checks its shape. Returns the stored form. | 38 | /// Normalises `input` and checks its shape. Returns the stored form. |
| @@ -155,6 +158,20 @@ mod tests { | |||
| 155 | ); | 158 | ); |
| 156 | } | 159 | } |
| 157 | 160 | ||
| 161 | + #[test] | ||
| 162 | + fn look_alikes_do_not_fold_into_an_address() { | ||
| 163 | + assert_ne!( | ||
| 164 | + normalize_email("\u{a0}admin@example.test"), | ||
| 165 | + "admin@example.test" | ||
| 166 | + ); | ||
| 167 | + assert_ne!( | ||
| 168 | + normalize_email("\u{212a}elvin@example.test"), | ||
| 169 | + "kelvin@example.test" | ||
| 170 | + ); | ||
| 171 | + assert!(parse_email("\u{a0}admin@example.test").is_err()); | ||
| 172 | + assert!(parse_email("\u{212a}elvin@example.test").is_err()); | ||
| 173 | + } | ||
| 174 | + | ||
| 158 | #[test] | 175 | #[test] |
| 159 | fn display_names_are_trimmed_and_checked() { | 176 | fn display_names_are_trimmed_and_checked() { |
| 160 | assert_eq!( | 177 | assert_eq!( |