diff --git a/CHANGELOG.md b/CHANGELOG.md index 89bb7cc..f9de0e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Cargo.toml b/Cargo.toml index 87435b7..7625b01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 847103d..651a723 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -118,7 +118,7 @@ dependencies = [ [[package]] name = "mail-parser" -version = "1.0.0" +version = "1.0.1" dependencies = [ "encodify", "encoding_rs", diff --git a/resources/content_type.json b/resources/content_type.json index e518730..5f6a281 100644 --- a/resources/content_type.json +++ b/resources/content_type.json @@ -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é" + ] + ] + } } ] \ No newline at end of file diff --git a/resources/eml/regressions/README.md b/resources/eml/regressions/README.md index a2f0ea9..3b48883 100644 --- a/resources/eml/regressions/README.md +++ b/resources/eml/regressions/README.md @@ -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. | diff --git a/resources/eml/regressions/fold-after-parameter-equals.crlf.json b/resources/eml/regressions/fold-after-parameter-equals.crlf.json new file mode 100644 index 0000000..a5350dd --- /dev/null +++ b/resources/eml/regressions/fold-after-parameter-equals.crlf.json @@ -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": [] + } + ] +} \ No newline at end of file diff --git a/resources/eml/regressions/fold-after-parameter-equals.eml b/resources/eml/regressions/fold-after-parameter-equals.eml new file mode 100644 index 0000000..8ee79a0 --- /dev/null +++ b/resources/eml/regressions/fold-after-parameter-equals.eml @@ -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-- diff --git a/resources/eml/regressions/fold-after-parameter-equals.json b/resources/eml/regressions/fold-after-parameter-equals.json new file mode 100644 index 0000000..33955bc --- /dev/null +++ b/resources/eml/regressions/fold-after-parameter-equals.json @@ -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": [] + } + ] +} \ No newline at end of file diff --git a/src/fields/content_type.rs b/src/fields/content_type.rs index fe42ee4..d210ab2 100644 --- a/src/fields/content_type.rs +++ b/src/fields/content_type.rs @@ -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, "ED_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", diff --git a/src/fields/tests.rs b/src/fields/tests.rs index 52350f2..47c3921 100644 --- a/src/fields/tests.rs +++ b/src/fields/tests.rs @@ -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!( diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 7bb9212..633b1f5 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -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");