From 9aff3ee9d8edfdccb03139e1b4ed9772fe81dcd5 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 21 Mar 2025 14:04:07 -0700 Subject: [PATCH] rfc5321: improve error message for bad IDNA domains Previously, we'd use the default presentation for the Error type reported by the idna crate. Unfortunately, that type holds no context: it is a zero-sized empty struct type, and it chooses to render itself as `Errors`, which isn't super helpful, but it is hard to convey every possible error clause from the underlying spec, so I understand why this is this way. This commit handles that error case to present a slightly more informative error message. It doesn't provide context on what specifically is bad about the input, but it at least helps to characterize that the domain is bad, rather than imply that we don't know how to deal with punycode at all. The specific problem with the domain in the included test case is most likely BIDI related per discussion in https://github.com/servo/rust-url/issues/489 --- crates/rfc5321/src/parser.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/rfc5321/src/parser.rs b/crates/rfc5321/src/parser.rs index ca52a1b5..d8be0f5e 100644 --- a/crates/rfc5321/src/parser.rs +++ b/crates/rfc5321/src/parser.rs @@ -355,7 +355,7 @@ impl Domain { pub fn name(name: &str) -> Result { match idna::domain_to_ascii(name) { Ok(name) => Ok(Self::Name(name)), - Err(err) => Err(format!("{err:#}")), + Err(_empty_error_type) => Err(format!("invalid IDNA domain {name}")), } } } @@ -588,6 +588,16 @@ mod test { ); } + #[test] + fn parse_rcpt_to_punycode() { + assert_eq!( + Parser::parse_command("Rcpt To:") + .unwrap_err() + .to_string(), + "invalid IDNA domain 4bed.xn--5dbhlacyps5bf4a.com" + ); + } + #[test] fn parse_rcpt_to() { assert_eq!(