Fix IDN: sanitize_email rejects valid Punycode domains

This commit is contained in:
Maurus Decimus
2026-07-05 09:58:54 +02:00
parent 4b8ea03e7e
commit 03c55362ef
2 changed files with 20 additions and 1 deletions

View File

@@ -285,10 +285,16 @@ pub fn sanitize_email(email: &str) -> Option<String> {
for ch in chars {
match ch {
'.' | '-' | '_' => {
'.' => {
if !last_ch.is_alphanumeric() {
return None;
}
result.push('.');
}
'-' | '_' => {
if last_ch == NIL_CHAR || last_ch == '.' {
return None;
}
result.push(ch);
}
' ' | '\x09'..='\x0d' => continue,
@@ -477,6 +483,18 @@ mod tests {
);
}
#[test]
fn a_label_email_domains_are_accepted_and_idempotent() {
assert_eq!(
sanitize_email("user@xn--fsqu00a.com").as_deref(),
Some("user@xn--fsqu00a.com")
);
assert_eq!(
sanitize_email("User@例子.com").as_deref(),
sanitize_email("user@xn--fsqu00a.com").as_deref()
);
}
#[test]
fn to_ascii_domain_borrows_ascii_owns_idn() {
assert!(matches!(