From f9a580c5511014e83181ef1ccb1cc06c011c59aa Mon Sep 17 00:00:00 2001 From: Maurus Decimus <11444311+mdecimus@users.noreply.github.com> Date: Mon, 10 Aug 2026 19:22:45 +0200 Subject: [PATCH] Fix: `panic` when a `Received` header ends with a folded line (closes #155) --- CHANGELOG.md | 2 + resources/eml/rfc/002.json | 4 +- resources/eml/thirdparty/014.json | 4 +- resources/eml/thirdparty/015.crlf.json | 2 +- resources/eml/thirdparty/015.json | 2 +- resources/received.json | 99 ++++++++++++++++++++++++++ src/parsers/fields/received.rs | 42 ++++++++++- src/parsers/mod.rs | 2 +- 8 files changed, 147 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89a81ef..ca5ded6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ================================ diff --git a/resources/eml/rfc/002.json b/resources/eml/rfc/002.json index fa4daf2..8b1bace 100644 --- a/resources/eml/rfc/002.json +++ b/resources/eml/rfc/002.json @@ -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, diff --git a/resources/eml/thirdparty/014.json b/resources/eml/thirdparty/014.json index b6c519f..3e9dd84 100644 --- a/resources/eml/thirdparty/014.json +++ b/resources/eml/thirdparty/014.json @@ -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, diff --git a/resources/eml/thirdparty/015.crlf.json b/resources/eml/thirdparty/015.crlf.json index 94db7af..1acd817 100644 --- a/resources/eml/thirdparty/015.crlf.json +++ b/resources/eml/thirdparty/015.crlf.json @@ -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, diff --git a/resources/eml/thirdparty/015.json b/resources/eml/thirdparty/015.json index fda7890..3e83c4c 100644 --- a/resources/eml/thirdparty/015.json +++ b/resources/eml/thirdparty/015.json @@ -484,7 +484,7 @@ "with": "ESMTP", "tls_version": null, "tls_cipher": null, - "id": "1C36444F59\n", + "id": "1C36444F59", "ident": null, "helo": null, "helo_cmd": null, diff --git a/resources/received.json b/resources/received.json index d0d328f..c544493 100644 --- a/resources/received.json +++ b/resources/received.json @@ -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 + } + } } ] diff --git a/src/parsers/fields/received.rs b/src/parsers/fields/received.rs index 38c40f8..1f0f7c4 100644 --- a/src/parsers/fields/received.rs +++ b/src/parsers/fields/received.rs @@ -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); + } + } } diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs index 0f2574a..2c5879f 100644 --- a/src/parsers/mod.rs +++ b/src/parsers/mod.rs @@ -100,7 +100,7 @@ impl<'x> MessageStream<'x> { #[inline(always)] pub fn bytes(&self, range: Range) -> &'x [u8] { - &self.data[range] + self.data.get(range).unwrap_or_default() } #[inline(always)]