Normalize domains containing Unicode characters.

This commit is contained in:
Justus Winter 2019-03-07 14:15:01 +01:00
parent c0623ca419
commit d2f3f40cba
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
3 changed files with 9 additions and 0 deletions

View File

@ -19,6 +19,7 @@ url = "1.6"
hex = "0.3"
base64 = "0.10"
pathdiff = "0.1"
idna = "0.1"
[lib]
name = "hagrid_database"

View File

@ -12,6 +12,7 @@ use std::str::FromStr;
extern crate failure;
use failure::Error;
use failure::Fallible as Result;
extern crate idna;
#[macro_use] extern crate log;
extern crate parking_lot;
use parking_lot::MutexGuard;

View File

@ -52,6 +52,11 @@ impl FromStr for Email {
fn from_str(s: &str) -> Result<Email> {
let (localpart, domain) = parse2822address(s)?;
// Normalize Unicode in domains.
let domain = idna::domain_to_ascii(domain)
.map_err(|e| failure::format_err!(
"punycode conversion failed: {:?}", e))?;
Ok(Email(format!("{}@{}", localpart, domain)))
}
}
@ -167,5 +172,7 @@ mod tests {
assert_eq!(c("Foo Bar <foo@example.org>").as_str(), "foo@example.org");
assert_eq!(c("\"Foo Bar\" <foo@example.org>").as_str(),
"foo@example.org");
assert_eq!(c("foo@👍.example.org").as_str(),
"foo@xn--yp8h.example.org");
}
}