Keep parameter values after a fold following = (fixes #165)

This commit is contained in:
Maurus Decimus
2026-10-29 12:05:17 +01:00
parent 81ceb39699
commit 82a87a063d
11 changed files with 383 additions and 5 deletions
+5
View File
@@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/).
## [1.0.1] - Unreleased
### Fixed
- A parameter value on a folded line after `=` was read as a new parameter name, losing `name`, `filename` and `boundary` (#165).
## [1.0.0] - 2026-10-24
A rewrite of the parser for performance, with a new read-only API. Breaking changes are listed under Changed and Removed; the table at the end of this entry maps the 0.11 API to 1.0.
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "mail-parser"
description = "Fast and robust e-mail parsing library for Rust"
version = "1.0.0"
version = "1.0.1"
edition = "2024"
rust-version = "1.98"
authors = [ "Stalwart Labs <hello@stalw.art>"]
+1 -1
View File
@@ -118,7 +118,7 @@ dependencies = [
[[package]]
name = "mail-parser"
version = "1.0.0"
version = "1.0.1"
dependencies = [
"encodify",
"encoding_rs",
+82
View File
@@ -1452,5 +1452,87 @@
]
]
}
},
{
"header": " image/png; name=\r\n\t\"logo.png\"\r\n",
"expected": {
"c_type": "image",
"c_subtype": "png",
"attributes": [
[
"name",
"logo.png"
]
]
}
},
{
"header": " attachment; filename=\n logo.png\n",
"expected": {
"c_type": "attachment",
"c_subtype": null,
"attributes": [
[
"filename",
"logo.png"
]
]
}
},
{
"header": " multipart/mixed; boundary=\r\n\t\"X\"; type=text/plain\r\n",
"expected": {
"c_type": "multipart",
"c_subtype": "mixed",
"attributes": [
[
"boundary",
"X"
],
[
"type",
"text/plain"
]
]
}
},
{
"header": " attachment; filename*0=\"long \"; filename*1*=\n name.txt\n",
"expected": {
"c_type": "attachment",
"c_subtype": null,
"attributes": [
[
"filename",
"long name.txt"
]
]
}
},
{
"header": " text/plain; name=\n =?utf-8?q?caf=C3=A9?=\n",
"expected": {
"c_type": "text",
"c_subtype": "plain",
"attributes": [
[
"name",
"café"
]
]
}
},
{
"header": " text/plain; name*=\n utf-8''caf%C3%A9\n",
"expected": {
"c_type": "text",
"c_subtype": "plain",
"attributes": [
[
"name",
"café"
]
]
}
}
]
+1
View File
@@ -94,3 +94,4 @@ in both feature sets).
| `decoding-accessors` | `decoding_accessors`: quoted-printable text and base64 binary parts. |
| `header-views` | `header_views`: a field name with an invalid UTF-8 byte, lookups by name. |
| `decode-problems` | `decode_problems`: one part per decode problem (transfer encoding, unknown and malformed charsets). |
| `fold-after-parameter-equals` | #165, `fold_after_parameter_equals`: a folded line right after `=` in the `boundary`, `name` and `filename` parameters. |
@@ -0,0 +1,125 @@
{
"messages": [
{
"id": 0,
"root": 0,
"container": null,
"source": "raw",
"text_body": [],
"html_body": [],
"attachments": [
1
],
"has_attachments": true
}
],
"parts": [
{
"id": 0,
"message": 0,
"parent": null,
"kind": "multipart",
"children": [
1
],
"encoding": "none",
"flags": [],
"roles": [],
"role": "container",
"offset_header": 0,
"offset_body": 71,
"offset_end": 188,
"headers": [
{
"name": "From",
"value": {
"address": [
{
"mailbox": {
"name": null,
"address": "a@example.com"
}
}
]
},
"offset_field": 0,
"offset_start": 5,
"offset_end": 21
},
{
"name": "Content-Type",
"value": {
"content_type": {
"type": "multipart",
"subtype": "mixed",
"attributes": [
[
"boundary",
"X"
]
]
}
},
"offset_field": 21,
"offset_start": 34,
"offset_end": 69
}
]
},
{
"id": 1,
"message": 0,
"parent": 0,
"kind": "binary",
"encoding": "none",
"flags": [],
"roles": [
"attachment"
],
"role": "attachment",
"offset_header": 76,
"offset_body": 178,
"offset_end": 179,
"headers": [
{
"name": "Content-Type",
"value": {
"content_type": {
"type": "image",
"subtype": "png",
"attributes": [
[
"name",
"logo.png"
]
]
}
},
"offset_field": 76,
"offset_start": 89,
"offset_end": 121
},
{
"name": "Content-Disposition",
"value": {
"content_type": {
"type": "attachment",
"subtype": null,
"attributes": [
[
"filename",
"logo.png"
]
]
}
},
"offset_field": 121,
"offset_start": 141,
"offset_end": 176
}
],
"binary": "eA==",
"decode_problems": []
}
]
}
@@ -0,0 +1,12 @@
From: a@example.com
Content-Type: multipart/mixed; boundary=
"X"
--X
Content-Type: image/png; name=
"logo.png"
Content-Disposition: attachment; filename=
logo.png
x
--X--
@@ -0,0 +1,125 @@
{
"messages": [
{
"id": 0,
"root": 0,
"container": null,
"source": "raw",
"text_body": [],
"html_body": [],
"attachments": [
1
],
"has_attachments": true
}
],
"parts": [
{
"id": 0,
"message": 0,
"parent": null,
"kind": "multipart",
"children": [
1
],
"encoding": "none",
"flags": [],
"roles": [],
"role": "container",
"offset_header": 0,
"offset_body": 67,
"offset_end": 176,
"headers": [
{
"name": "From",
"value": {
"address": [
{
"mailbox": {
"name": null,
"address": "a@example.com"
}
}
]
},
"offset_field": 0,
"offset_start": 5,
"offset_end": 20
},
{
"name": "Content-Type",
"value": {
"content_type": {
"type": "multipart",
"subtype": "mixed",
"attributes": [
[
"boundary",
"X"
]
]
}
},
"offset_field": 20,
"offset_start": 33,
"offset_end": 66
}
]
},
{
"id": 1,
"message": 0,
"parent": 0,
"kind": "binary",
"encoding": "none",
"flags": [],
"roles": [
"attachment"
],
"role": "attachment",
"offset_header": 71,
"offset_body": 168,
"offset_end": 169,
"headers": [
{
"name": "Content-Type",
"value": {
"content_type": {
"type": "image",
"subtype": "png",
"attributes": [
[
"name",
"logo.png"
]
]
}
},
"offset_field": 71,
"offset_start": 84,
"offset_end": 114
},
{
"name": "Content-Disposition",
"value": {
"content_type": {
"type": "attachment",
"subtype": null,
"attributes": [
[
"filename",
"logo.png"
]
]
}
},
"offset_field": 114,
"offset_start": 134,
"offset_end": 167
}
],
"binary": "eA==",
"decode_problems": []
}
]
}
+11 -2
View File
@@ -279,7 +279,7 @@ impl Kernel {
if bytes.get(at) != Some(&b'=') {
return None;
}
at = skip_space(bytes, at + 1);
at = skip_blank(bytes, at + 1);
let value = if bytes.get(at) == Some(&b'"') {
let end = self.value_end(bytes, at + 1, &QUOTED_STOPS)?;
if bytes.get(end) != Some(&b'"') {
@@ -376,7 +376,14 @@ impl FieldCtx<'_> {
State::Type | State::AttributeName | State::SubType => {
parser.add_attribute(self);
}
State::AttributeValue => parser.add_value(self),
State::AttributeValue => {
if next_is_space && parser.token.is_none() && !parser.has_values {
pos += 1;
parser.is_token_start = true;
continue;
}
parser.add_value(self);
}
State::AttributeQuotedValue => {
if next_is_space {
pos += 1;
@@ -1002,6 +1009,8 @@ mod tests {
b" text/plain; charset =x\r\n",
b" text/plain; charset= x\r\n",
b" text/plain; charset=\r\n x\r\n",
b" image/png; name=\r\n\t\"logo.png\"\r\n",
b" text/plain; name=\r\n =?utf-8?q?a?=\r\n",
b" text/plain; boundary=Apple-Mail=_D9E8-1\r\n",
b" text/plain; name==?utf-8?q?a?=\r\n",
b" text/plain; name=\"=?utf-8?q?a?=\"\r\n",
+1 -1
View File
@@ -81,7 +81,7 @@ fn normalize(expected: Json) -> Json {
#[test]
fn content_type_fixtures() {
let tests = load_tests("content_type.json");
assert_eq!(tests.len(), 120);
assert_eq!(tests.len(), 126);
for (header, expected) in tests {
let parsed = HeaderForm::ContentType.parse(header.as_bytes());
assert_eq!(
+19
View File
@@ -242,6 +242,25 @@ fn multipart_structure() {
}
}
#[test]
fn fold_after_parameter_equals() {
let raw = regression!("fold-after-parameter-equals");
for raw in [raw.to_vec(), with_crlf(raw)] {
let message = parse(&raw);
let root = message.root_part();
assert!(matches!(root.kind(), PartKind::Multipart));
assert_eq!(root.boundary(), Some("X"));
let image = message.part(1).expect("part");
assert_eq!(image.attachment_name(), Some("logo.png"));
assert_eq!(
image
.content_type()
.and_then(|content_type| content_type.attribute("name")),
Some("logo.png")
);
}
}
#[test]
fn delimiter_rule() {
let raw = regression!("delimiter-rule");