diff --git a/CHANGELOG.md b/CHANGELOG.md index edfb5c1..92c5563 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ mail-parser 0.11.5 ================================ - Fix: Address names containing LF are not parsed correctly (#149) - Fix: Decode the `iso-8859-1` label as `windows-1252` per the WHATWG Encoding Standard (#131) +- Fix: `panic` with messages containing corrupted eml attachments (#120). - Recognize additional charset labels supported by `encoding_rs` (#123) - Added `Dkim2Signature` and `MessageInstance` to support DKIM2 headers. diff --git a/resources/eml/malformed/022.crlf.json b/resources/eml/malformed/022.crlf.json new file mode 100644 index 0000000..baa58c3 --- /dev/null +++ b/resources/eml/malformed/022.crlf.json @@ -0,0 +1,136 @@ +{ + "html_body": [ + 1 + ], + "text_body": [ + 1 + ], + "attachments": [ + 2 + ], + "parts": [ + { + "headers": [ + { + "name": "from", + "value": { + "Address": { + "List": [ + { + "name": null, + "address": "a@example.com" + } + ] + } + }, + "offset_field": 0, + "offset_start": 5, + "offset_end": 21 + }, + { + "name": "subject", + "value": { + "Text": "broken rfc822 attachment" + }, + "offset_field": 21, + "offset_start": 29, + "offset_end": 56 + }, + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "multipart", + "c_subtype": "mixed", + "attributes": [ + { + "name": "boundary", + "value": "BOUND" + } + ] + } + }, + "offset_field": 56, + "offset_start": 69, + "offset_end": 105 + } + ], + "is_encoding_problem": false, + "body": { + "Multipart": [ + 1, + 2 + ] + }, + "offset_header": 0, + "offset_body": 107, + "offset_end": 303 + }, + { + "headers": [ + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "text", + "c_subtype": "plain", + "attributes": null + } + }, + "offset_field": 116, + "offset_start": 129, + "offset_end": 142 + } + ], + "is_encoding_problem": false, + "body": { + "Text": "hello" + }, + "offset_header": 116, + "offset_body": 144, + "offset_end": 149 + }, + { + "headers": [ + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "message", + "c_subtype": "rfc822", + "attributes": null + } + }, + "offset_field": 160, + "offset_start": 173, + "offset_end": 190 + }, + { + "name": "content_disposition", + "value": { + "ContentType": { + "c_type": "attachment", + "c_subtype": null, + "attributes": [ + { + "name": "filename", + "value": "broken.eml" + } + ] + } + }, + "offset_field": 190, + "offset_start": 210, + "offset_end": 246 + } + ], + "is_encoding_problem": true, + "body": { + "Text": "this attachment is not a valid eml, sorry!" + }, + "offset_header": 160, + "offset_body": 248, + "offset_end": 303 + } + ] +} \ No newline at end of file diff --git a/resources/eml/malformed/022.eml b/resources/eml/malformed/022.eml new file mode 100644 index 0000000..7932e7e --- /dev/null +++ b/resources/eml/malformed/022.eml @@ -0,0 +1,14 @@ +From: a@example.com +Subject: broken rfc822 attachment +Content-Type: multipart/mixed; boundary="BOUND" + +--BOUND +Content-Type: text/plain + +hello +--BOUND +Content-Type: message/rfc822 +Content-Disposition: attachment; filename="broken.eml" + +this attachment is not a valid eml, sorry! +--BOUND-- diff --git a/resources/eml/malformed/022.json b/resources/eml/malformed/022.json new file mode 100644 index 0000000..3192c5a --- /dev/null +++ b/resources/eml/malformed/022.json @@ -0,0 +1,136 @@ +{ + "html_body": [ + 1 + ], + "text_body": [ + 1 + ], + "attachments": [ + 2 + ], + "parts": [ + { + "headers": [ + { + "name": "from", + "value": { + "Address": { + "List": [ + { + "name": null, + "address": "a@example.com" + } + ] + } + }, + "offset_field": 0, + "offset_start": 5, + "offset_end": 20 + }, + { + "name": "subject", + "value": { + "Text": "broken rfc822 attachment" + }, + "offset_field": 20, + "offset_start": 28, + "offset_end": 54 + }, + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "multipart", + "c_subtype": "mixed", + "attributes": [ + { + "name": "boundary", + "value": "BOUND" + } + ] + } + }, + "offset_field": 54, + "offset_start": 67, + "offset_end": 102 + } + ], + "is_encoding_problem": false, + "body": { + "Multipart": [ + 1, + 2 + ] + }, + "offset_header": 0, + "offset_body": 103, + "offset_end": 289 + }, + { + "headers": [ + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "text", + "c_subtype": "plain", + "attributes": null + } + }, + "offset_field": 111, + "offset_start": 124, + "offset_end": 136 + } + ], + "is_encoding_problem": false, + "body": { + "Text": "hello" + }, + "offset_header": 111, + "offset_body": 137, + "offset_end": 142 + }, + { + "headers": [ + { + "name": "content_type", + "value": { + "ContentType": { + "c_type": "message", + "c_subtype": "rfc822", + "attributes": null + } + }, + "offset_field": 151, + "offset_start": 164, + "offset_end": 180 + }, + { + "name": "content_disposition", + "value": { + "ContentType": { + "c_type": "attachment", + "c_subtype": null, + "attributes": [ + { + "name": "filename", + "value": "broken.eml" + } + ] + } + }, + "offset_field": 180, + "offset_start": 200, + "offset_end": 235 + } + ], + "is_encoding_problem": true, + "body": { + "Text": "this attachment is not a valid eml, sorry!" + }, + "offset_header": 151, + "offset_body": 236, + "offset_end": 289 + } + ] +} \ No newline at end of file diff --git a/src/parsers/message.rs b/src/parsers/message.rs index 0705e34..c4b3c46 100644 --- a/src/parsers/message.rs +++ b/src/parsers/message.rs @@ -407,8 +407,17 @@ impl MessageParser { //raw_message[state.offset_header..offset_end].as_ref().into(); if let Some(part) = prev_message.parts.get_mut(state.part_id as usize) { - part.body = PartType::Message(message); part.offset_end = offset_end as u32; + if message.is_empty() { + part.is_encoding_problem = true; + part.body = PartType::Text(String::from_utf8_lossy( + raw_message + .get(part.offset_body as usize..offset_end) + .unwrap_or_default(), + )); + } else { + part.body = PartType::Message(message); + } } else { debug_assert!(false, "Invalid part ID, could not find message."); } @@ -493,8 +502,34 @@ impl MessageParser { message.raw_message = raw_message.into(); //raw_message[state.offset_header..stream.offset()].as_ref().into(); if let Some(part) = prev_message.parts.get_mut(state.part_id as usize) { - part.body = PartType::Message(message); part.offset_end = stream.offset() as u32; + if message.is_empty() { + part.is_encoding_problem = true; + let raw_text = raw_message.get(part.offset_body as usize..stream.offset()); + let text = match raw_text + .and_then(|bytes| std::str::from_utf8(bytes).ok()) + .and_then(|text| { + state + .mime_boundary + .as_deref() + .and_then(|b| std::str::from_utf8(b).ok()) + .and_then(|boundary| { + text.rsplit_once(boundary) + .and_then(|(text, _)| text.strip_suffix("--")) + }) + .map(|text| { + let text = text.strip_suffix('\n').unwrap_or(text); + text.strip_suffix('\r').unwrap_or(text) + }) + }) { + Some(text) => Cow::Borrowed(text), + _ => String::from_utf8_lossy(raw_text.unwrap_or_default()), + }; + + part.body = PartType::Text(text); + } else { + part.body = PartType::Message(message); + } } else { debug_assert!(false, "Invalid part ID, could not find message."); }