Skip to main content
Menu
Advisories

Mailbox::from_str unescapes a quoted local part, so mail goes to a different address than the one parsed

GHSA-62W5-58XQ-P27JPublished Updated GitHub

Description

Mailbox::from_str (and Mailboxes::from_str) strip the quotes and quoted-pairs of a quoted local part and drop whitespace before the closing quote, then build the Address from the result. Address::from_str keeps the same input verbatim. As a result the two parsers disagree on the recipient, and the address that Mailbox sends to differs from the one that was written.

Details

local_part in src/message/mailbox/parsers/rfc2822.rs returns the semantic content of quoted_string (and of obs_local_part), and Mailbox::from_str passes it to Address::new:

inputAddress::from_strMailbox::from_str(..).email
"d "@d"d "@dd@d
"\"x\""@y"\"x\""@y"x"@y (a server reads it as x@y)
"\".\""@A"\".\""@A"."@A

The domain never changes (a local part that gains @ after unescaping is rejected by validation), so the effect is a different mailbox on the same domain.

In the last case the result is not even re-parsable: Mailbox::from_str(&mailbox.to_string()) fails with InvalidUser, and Message::builder().to(mailbox) returns MissingTo, i.e. an address that passed validation silently fails to be used.

PoC

use lettre::{message::Mailbox, Address};

let src = r#""d "@d"#;
assert_eq!(src.parse::<Address>().unwrap().to_string(), r#""d "@d"#);
assert_eq!(src.parse::<Mailbox>().unwrap().email.to_string(), "d@d"); // different recipient

Found by differential fuzzing of Address::from_str vs Mailbox::from_str (round-trip through Display) with cargo-fuzz. Confirmed on 0.11.0, 0.11.18, 0.11.22, 0.11.23 and current master (94b63ac).

Impact

Mis-delivery: an application that validates/stores a recipient with one parser and sends with Mailbox (or an attacker who controls a recipient string that a reviewer sees in its quoted form) gets the message delivered to a different local part on the same domain, without any error. Some quoted addresses that pass parsing make message building fail (MissingTo).

Suggested fix

Keep quoted forms of the local part verbatim, so that Mailbox::from_str yields the same Address as Address::from_str (patch below, with regression tests; all existing tests pass). We are happy to open a PR once you are ready.

diff --git a/src/message/mailbox/parsers/rfc2822.rs b/src/message/mailbox/parsers/rfc2822.rs
index ac9e7a1..f122f60 100644
--- a/src/message/mailbox/parsers/rfc2822.rs
+++ b/src/message/mailbox/parsers/rfc2822.rs
@@ -210,8 +210,16 @@ pub(super) fn addr_spec(input: &str) -> IResult<&str, (String, String)> {
 }
 
 // local-part      =       dot-atom / quoted-string / obs-local-part
+//
+// Quoted forms are kept verbatim: unescaping them here would produce a
+// different local part than `Address::from_str` sees for the same input.
 pub(super) fn local_part(input: &str) -> IResult<&str, String> {
-    alt((dot_atom, quoted_string, obs_local_part)).parse(input)
+    alt((
+        dot_atom,
+        map(recognize(quoted_string), str::to_owned),
+        map(preceded(cfws, recognize(obs_local_part)), str::to_owned),
+    ))
+    .parse(input)
 }
 
 // domain          =       dot-atom / domain-literal / obs-domain
diff --git a/src/message/mailbox/types.rs b/src/message/mailbox/types.rs
index 76f2970..9258d3e 100644
--- a/src/message/mailbox/types.rs
+++ b/src/message/mailbox/types.rs
@@ -445,7 +445,7 @@ fn write_quoted_string_char(f: &mut Formatter<'_>, c: char) -> FmtResult {
 mod test {
     use pretty_assertions::assert_eq;
 
-    use super::Mailbox;
+    use super::{Address, Mailbox};
 
     #[test]
     fn mailbox_format_address_only() {
@@ -622,4 +622,29 @@ mod test {
             ))
         );
     }
+
+    #[test]
+    fn parse_quoted_local_part_matches_address() {
+        for src in [
+            r#""d "@d"#,
+            r#""\"x\""@y"#,
+            r#""\".\""@A"#,
+            r#""john..doe"@example.com"#,
+        ] {
+            let mailbox: Mailbox = src.parse().unwrap();
+            assert_eq!(mailbox.email, src.parse::<Address>().unwrap(), "{src}");
+            assert_eq!(mailbox.to_string().parse::<Mailbox>(), Ok(mailbox), "{src}");
+        }
+    }
+
+    #[test]
+    fn parse_quoted_local_part_with_name() {
+        assert_eq!(
+            r#"K. <"d "@example.com>"#.parse(),
+            Ok(Mailbox::new(
+                Some("K.".into()),
+                r#""d "@example.com"#.parse().unwrap()
+            ))
+        );
+    }
 }

Affected versions

References

GitHub · lettre/lettre