Keep quoted-pairs in a quoted local part inside angle brackets (#167)

This commit is contained in:
Luca Cappelletti
2026-11-15 15:53:49 +01:00
committed by GitHub
parent 70bba6b551
commit 8e149db324
3 changed files with 66 additions and 2 deletions
+1
View File
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This projec
### Fixed
- An address without angle brackets whose local part is a quoted string, such as `"john smith"@example.com`, was read as the display name `john smith` and the address `@example.com` (#166).
- A quoted local part inside angle brackets lost its backslashes, so `<"a\"b"@example.com>` gave the address `"a"b"@example.com`. A quoted-pair now stays in the address as written (#167).
## [1.0.1] - 2026-11-02
+50
View File
@@ -1971,5 +1971,55 @@
}
}
]
},
{
"header": "<\"a\\\"b\"@example.com>\n",
"expected": [
{
"mailbox": {
"name": null,
"address": "\"a\\\"b\"@example.com"
}
}
]
},
{
"header": "Name <\"a\\\"b\"@example.com>\n",
"expected": [
{
"mailbox": {
"name": "Name",
"address": "\"a\\\"b\"@example.com"
}
}
]
},
{
"header": "<\"a\\\\b\"@example.com>\n",
"expected": [
{
"mailbox": {
"name": null,
"address": "\"a\\\\b\"@example.com"
}
}
]
},
{
"header": "<\"a\\\"b@c.d>, <e\\f@g.h>\n",
"expected": [
{
"mailbox": {
"name": null,
"address": "\"a\\\"b@c.d"
}
},
{
"mailbox": {
"name": null,
"address": "ef@g.h"
}
}
]
}
]
+15 -2
View File
@@ -35,7 +35,7 @@ static STOPS: [u8; 256] = {
table[b' ' as usize] = EVERYWHERE;
table[b'\t' as usize] = EVERYWHERE;
table[b'=' as usize] = EVERYWHERE;
table[b'"' as usize] = IN_NAME | IN_QUOTE;
table[b'"' as usize] = IN_NAME | IN_ADDRESS | IN_QUOTE;
table[b'(' as usize] = IN_NAME | IN_ADDRESS | IN_COMMENT;
table[b'\\' as usize] = EVERYWHERE;
table[b',' as usize] = IN_NAME;
@@ -287,6 +287,7 @@ struct Parser<'x> {
email: bool,
token_start: bool,
escaped: bool,
address_quoted: bool,
glued: bool,
last_encoded: bool,
name: Acc,
@@ -313,6 +314,7 @@ impl FieldCtx<'_> {
email: false,
token_start: true,
escaped: false,
address_quoted: false,
glued: false,
last_encoded: true,
name: Acc::EMPTY,
@@ -391,6 +393,11 @@ impl<'x> Parser<'x> {
pos += blanks;
}
b'\r' => pos += 1,
b'\\' if !self.escaped && self.address_quoted && self.state == State::Address => {
self.extend(pos, pos + 1);
self.escaped = true;
pos += 1;
}
b'\\' if !self.escaped => {
let glued = !self.token_start;
if let Some((first, _)) = self.token {
@@ -425,6 +432,7 @@ impl<'x> Parser<'x> {
pos += 1;
}
b'>' => {
self.address_quoted = false;
self.add_token();
self.state = State::Name;
pos += 1;
@@ -448,6 +456,11 @@ impl<'x> Parser<'x> {
}
}
}
b'"' if !self.escaped && self.state == State::Address => {
self.address_quoted = !self.address_quoted;
self.extend(pos, pos + 1);
pos += 1;
}
b'"' if !self.escaped => {
self.add_token();
self.state = State::Name;
@@ -887,7 +900,7 @@ mod tests {
#[test]
fn address_fixtures() {
let tests = load_tests("address.json");
assert_eq!(tests.len(), 137);
assert_eq!(tests.len(), 141);
for (header, expected) in tests {
assert_eq!(parse(&header), expected, "{header:?}");
let crlf = header.replace("\r\n", "\n").replace('\n', "\r\n");