diff --git a/CHANGELOG.md b/CHANGELOG.md index b89770f..3f55e3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +mail-parser 0.9.1 +================================ +- Fixed panic when Content-Disposition is empty (#63) +- Removed `content_type()` and `address()` functions that could `panic!`. Use `as_content_type()` and `as_address()` instead. +- Updated Rust edition to 2021. + mail-parser 0.9.0 ================================ This version introduces multiple breaking changes. Please read the following notes carefully. diff --git a/Cargo.toml b/Cargo.toml index 543c218..47ee02e 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 = "0.9.0" +version = "0.9.1" edition = "2021" authors = [ "Stalwart Labs "] license = "Apache-2.0 OR MIT" diff --git a/README.md b/README.md index be93b9b..53d430e 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,6 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 // Integrates with Serde println!("{}", serde_json::to_string_pretty(&message).unwrap()); - println!("{}", serde_yaml::to_string(&message).unwrap()); ``` More examples available under the [examples](examples) directory. Please note that this library does not support building e-mail messages as this functionality is provided separately by the [`mail-builder`](https://crates.io/crates/mail-builder) crate. diff --git a/resources/eml/malformed/017.crlf.json b/resources/eml/malformed/017.crlf.json new file mode 100644 index 0000000..4972aa8 --- /dev/null +++ b/resources/eml/malformed/017.crlf.json @@ -0,0 +1,147 @@ +{ + "html_body": [ + 1 + ], + "text_body": [ + 1 + ], + "attachments": [ + 1 + ], + "parts": [ + { + "headers": [ + { + "name": "subject", + "value": { + "Text": "Hello world" + }, + "offset_field": 0, + "offset_start": 8, + "offset_end": 22 + }, + { + "name": "from", + "value": { + "Address": { + "List": [ + { + "name": "Test User", + "address": "test@example.org" + } + ] + } + }, + "offset_field": 22, + "offset_start": 27, + "offset_end": 58 + }, + { + "name": "to", + "value": { + "Address": { + "List": [ + { + "name": "Another User", + "address": "test2@example.org" + } + ] + } + }, + "offset_field": 58, + "offset_start": 61, + "offset_end": 96 + }, + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "multipart", + "c_subtype": "mixed", + "attributes": [ + [ + "boundary", + "boundary" + ] + ] + } + }, + "offset_field": 96, + "offset_start": 109, + "offset_end": 149 + } + ], + "is_encoding_problem": false, + "body": { + "Multipart": [ + 1 + ] + }, + "offset_header": 0, + "offset_body": 151, + "offset_end": 348 + }, + { + "headers": [ + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "image", + "c_subtype": "png", + "attributes": null + } + }, + "offset_field": 163, + "offset_start": 176, + "offset_end": 188 + }, + { + "name": "content_transfer_encoding", + "value": { + "Text": "base64" + }, + "offset_field": 188, + "offset_start": 214, + "offset_end": 223 + }, + { + "name": "content_disposition", + "value": "Empty", + "offset_field": 223, + "offset_start": 243, + "offset_end": 280 + }, + { + "name": "content_id", + "value": { + "Text": "image001.png@[...]" + }, + "offset_field": 280, + "offset_start": 291, + "offset_end": 314 + } + ], + "is_encoding_problem": false, + "body": { + "InlineBinary": [ + 104, + 101, + 108, + 108, + 111, + 32, + 119, + 111, + 114, + 108, + 100, + 10 + ] + }, + "offset_header": 163, + "offset_body": 316, + "offset_end": 332 + } + ] +} \ No newline at end of file diff --git a/resources/eml/malformed/017.eml b/resources/eml/malformed/017.eml new file mode 100644 index 0000000..106b5bc --- /dev/null +++ b/resources/eml/malformed/017.eml @@ -0,0 +1,15 @@ +Subject: Hello world +From: Test User +To: Another User +Content-Type: multipart/mixed + boundary="boundary" + +--boundary +Content-Type: image/png +Content-Transfer-Encoding: base64 +Content-Disposition: ; + filename="image001.png" +Content-ID: + +aGVsbG8gd29ybGQK +--boundary-- diff --git a/resources/eml/malformed/017.json b/resources/eml/malformed/017.json new file mode 100644 index 0000000..f90de12 --- /dev/null +++ b/resources/eml/malformed/017.json @@ -0,0 +1,147 @@ +{ + "html_body": [ + 1 + ], + "text_body": [ + 1 + ], + "attachments": [ + 1 + ], + "parts": [ + { + "headers": [ + { + "name": "subject", + "value": { + "Text": "Hello world" + }, + "offset_field": 0, + "offset_start": 8, + "offset_end": 21 + }, + { + "name": "from", + "value": { + "Address": { + "List": [ + { + "name": "Test User", + "address": "test@example.org" + } + ] + } + }, + "offset_field": 21, + "offset_start": 26, + "offset_end": 56 + }, + { + "name": "to", + "value": { + "Address": { + "List": [ + { + "name": "Another User", + "address": "test2@example.org" + } + ] + } + }, + "offset_field": 56, + "offset_start": 59, + "offset_end": 93 + }, + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "multipart", + "c_subtype": "mixed", + "attributes": [ + [ + "boundary", + "boundary" + ] + ] + } + }, + "offset_field": 93, + "offset_start": 106, + "offset_end": 144 + } + ], + "is_encoding_problem": false, + "body": { + "Multipart": [ + 1 + ] + }, + "offset_header": 0, + "offset_body": 145, + "offset_end": 333 + }, + { + "headers": [ + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "image", + "c_subtype": "png", + "attributes": null + } + }, + "offset_field": 156, + "offset_start": 169, + "offset_end": 180 + }, + { + "name": "content_transfer_encoding", + "value": { + "Text": "base64" + }, + "offset_field": 180, + "offset_start": 206, + "offset_end": 214 + }, + { + "name": "content_disposition", + "value": "Empty", + "offset_field": 214, + "offset_start": 234, + "offset_end": 269 + }, + { + "name": "content_id", + "value": { + "Text": "image001.png@[...]" + }, + "offset_field": 269, + "offset_start": 280, + "offset_end": 302 + } + ], + "is_encoding_problem": false, + "body": { + "InlineBinary": [ + 104, + 101, + 108, + 108, + 111, + 32, + 119, + 111, + 114, + 108, + 100, + 10 + ] + }, + "offset_header": 156, + "offset_body": 303, + "offset_end": 319 + } + ] +} \ No newline at end of file diff --git a/src/core/header.rs b/src/core/header.rs index a356bab..67a14cd 100644 --- a/src/core/header.rs +++ b/src/core/header.rs @@ -164,13 +164,6 @@ impl<'x> HeaderValue<'x> { } } - pub fn address(&self) -> &Address<'x> { - match *self { - HeaderValue::Address(ref a) => a, - _ => panic!("HeaderValue::address called on non-Address: {:?}", self), - } - } - pub fn as_address(&self) -> Option<&Address<'x>> { match *self { HeaderValue::Address(ref a) => Some(a), @@ -185,16 +178,6 @@ impl<'x> HeaderValue<'x> { } } - pub fn content_type(&self) -> &ContentType<'x> { - match *self { - HeaderValue::ContentType(ref ct) => ct, - _ => panic!( - "HeaderValue::content_type called on non-ContentType: {:?}", - self - ), - } - } - pub fn as_content_type(&self) -> Option<&ContentType> { match *self { HeaderValue::ContentType(ref c) => Some(c), diff --git a/src/parsers/message.rs b/src/parsers/message.rs index e6b550e..1cc8d47 100644 --- a/src/parsers/message.rs +++ b/src/parsers/message.rs @@ -272,7 +272,10 @@ impl MessageParser { let is_inline = is_inline && part_headers .header_value(&HeaderName::ContentDisposition) - .map_or_else(|| true, |d| !d.content_type().is_attachment()) + .map_or_else( + || true, + |d| !d.as_content_type().map_or(false, |ct| ct.is_attachment()), + ) && (state.parts == 1 || (state.mime_type != MimeType::MultipartRelated && (mime_type == MimeType::Inline