Skip to content

Commit 547524f

Browse files
wezMHillyer
authored andcommitted
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 servo/rust-url#489
1 parent c70530a commit 547524f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

crates/rfc5321/src/parser.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl Domain {
355355
pub fn name(name: &str) -> Result<Self, String> {
356356
match idna::domain_to_ascii(name) {
357357
Ok(name) => Ok(Self::Name(name)),
358-
Err(err) => Err(format!("{err:#}")),
358+
Err(_empty_error_type) => Err(format!("invalid IDNA domain {name}")),
359359
}
360360
}
361361
}
@@ -588,6 +588,16 @@ mod test {
588588
);
589589
}
590590

591+
#[test]
592+
fn parse_rcpt_to_punycode() {
593+
assert_eq!(
594+
Parser::parse_command("Rcpt To:<[email protected]>")
595+
.unwrap_err()
596+
.to_string(),
597+
"invalid IDNA domain 4bed.xn--5dbhlacyps5bf4a.com"
598+
);
599+
}
600+
591601
#[test]
592602
fn parse_rcpt_to() {
593603
assert_eq!(

0 commit comments

Comments
 (0)