Fix: panic when a Received header ends with a folded line (closes #155)

This commit is contained in:
Maurus Decimus
2026-08-10 19:22:45 +02:00
parent 339a4306d3
commit f9a580c551
8 changed files with 147 additions and 10 deletions
+2
View File
@@ -1,6 +1,8 @@
mail-parser 0.11.6
================================
- Fix: Missing whitespace between a quoted name and a following encoded word (#150)
- Fix: `panic` when a `Received` header ends with a folded line (#155)
- Fix: `Received` header tokens losing their last character at the end of the input, retaining folding characters, and failing to parse a clause folded before its value (#155)
mail-parser 0.11.5
================================
+2 -2
View File
@@ -200,7 +200,7 @@
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "SAA22453\n",
"id": "SAA22453",
"ident": null,
"helo": null,
"helo_cmd": null,
@@ -238,7 +238,7 @@
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "RAA20366\n",
"id": "RAA20366",
"ident": null,
"helo": null,
"helo_cmd": null,
+2 -2
View File
@@ -32,7 +32,7 @@
"value": {
"Received": {
"from": {
"Name": "nm2-vm1.bullet.mail.ukl.yahoo.com\n"
"Name": "nm2-vm1.bullet.mail.ukl.yahoo.com"
},
"from_ip": "217.146.183.227",
"from_iprev": "nm2-vm1.bullet.mail.ukl.yahoo.com",
@@ -43,7 +43,7 @@
"with": "SMTP",
"tls_version": null,
"tls_cipher": null,
"id": "BC0C186A2A\n",
"id": "BC0C186A2A",
"ident": null,
"helo": null,
"helo_cmd": null,
+1 -1
View File
@@ -54,7 +54,7 @@
"by": {
"Name": "mx-ha.gmx.net"
},
"for_": null,
"for_": "jonas.sterr@gmx.net",
"with": "ESMTPS",
"tls_version": null,
"tls_cipher": null,
+1 -1
View File
@@ -484,7 +484,7 @@
"with": "ESMTP",
"tls_version": null,
"tls_cipher": null,
"id": "1C36444F59\n",
"id": "1C36444F59",
"ident": null,
"helo": null,
"helo_cmd": null,
+99
View File
@@ -4358,5 +4358,104 @@
"tz_minute": 0
}
}
},
{
"header": "from mx.example.org by srv.example.com",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com\r\n ",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com\r\n\t",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com\r\n ",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com\r\n \t ",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com\n\t",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com;\r\n ",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
}
}
},
{
"header": "from mx.example.org by srv.example.com; Wed, 01 Jan 2025 00:00:00 +0000\r\n ",
"expected": {
"from": {
"Name": "mx.example.org"
},
"by": {
"Name": "srv.example.com"
},
"date": {
"year": 2025,
"month": 1,
"day": 1,
"hour": 0,
"minute": 0,
"second": 0,
"tz_before_gmt": false,
"tz_hour": 0,
"tz_minute": 0
}
}
}
]
+39 -3
View File
@@ -379,6 +379,7 @@ impl<'x> Iterator for Tokenizer<'x, '_> {
let bracket_depth = self.bracket_depth;
let mut start_pos = self.stream.offset();
let mut end_pos = start_pos;
while let Some(ch) = self.stream.next() {
match ch {
@@ -439,6 +440,8 @@ impl<'x> Iterator for Tokenizer<'x, '_> {
break;
} else {
start_pos = self.stream.offset();
end_pos = start_pos;
continue;
}
}
b'(' => {
@@ -524,6 +527,7 @@ impl<'x> Iterator for Tokenizer<'x, '_> {
break;
} else {
start_pos += 1;
end_pos = start_pos;
continue;
}
}
@@ -540,14 +544,14 @@ impl<'x> Iterator for Tokenizer<'x, '_> {
}
n_total += 1;
end_pos = self.stream.offset();
}
if n_total == 0 {
return self.next_token.take();
}
let text = std::str::from_utf8(self.stream.bytes(start_pos..self.stream.offset() - 1))
.unwrap_or_default();
let text = std::str::from_utf8(self.stream.bytes(start_pos..end_pos)).unwrap_or_default();
let token = match (
n_alpha, n_digit, n_hex, n_dot, n_at, n_other, n_colon, n_plus, n_minus, n_utf, hash,
@@ -804,7 +808,10 @@ impl Token {
#[cfg(test)]
mod tests {
use crate::parsers::{MessageStream, fields::load_tests};
use crate::{
MessageParser,
parsers::{MessageStream, fields::load_tests},
};
#[test]
fn parse_received() {
@@ -819,4 +826,33 @@ mod tests {
);
}
}
#[test]
fn parse_received_truncated_fold() {
for message in [
&b"Received:\n\t"[..],
b"Received:\r\n ",
b"Received: x\r\n ",
b"Received: x\r\n\t",
b"Received: x\n ",
b"Received: x\n\t",
b"Received: x\r\n ",
b"Received: x\r\n \t",
b"Received: x\r\n \t ",
b"Received: x\r\n \r",
b"Received: x\r\n \r\n",
b"Received: x\r\n \r\nbody",
b"Received: x\r\n y\r\n ",
b"Received: x;\r\n ",
b"To: a@b\r\nReceived: x\r\n ",
b"Received: from x (y)\r\n\t",
b"Received: from x by y;\r\n\t",
b"Received: ",
b"Received:",
b"Received: x",
] {
MessageParser::default().parse(message);
MessageParser::default().parse_headers(message);
}
}
}
+1 -1
View File
@@ -100,7 +100,7 @@ impl<'x> MessageStream<'x> {
#[inline(always)]
pub fn bytes(&self, range: Range<usize>) -> &'x [u8] {
&self.data[range]
self.data.get(range).unwrap_or_default()
}
#[inline(always)]