From 7c4db43aca5cd2faf6b1a3c3525ad0325b5a4bf8 Mon Sep 17 00:00:00 2001 From: Mauro D Date: Mon, 10 Oct 2022 07:56:30 +0000 Subject: [PATCH] Fixed raw offsets of multipart/* parts to include MIME epilogue. --- CHANGELOG.md | 1 + src/parsers/message.rs | 27 ++-- src/parsers/mime.rs | 25 ++++ tests/malformed/005.json | 2 +- tests/rfc/009.eml | 39 ++++++ tests/rfc/009.json | 264 ++++++++++++++++++++++++++++++++++++++ tests/thirdparty/007.json | 2 +- tests/thirdparty/008.json | 6 +- tests/thirdparty/012.json | 2 +- 9 files changed, 351 insertions(+), 17 deletions(-) create mode 100644 tests/rfc/009.eml create mode 100644 tests/rfc/009.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 4132e44..76a5c84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ mail-parser 0.6.2 - Base64/QuotedPrintable decoding optimizations. - Added "ludicrous mode" Cargo option to use some unsafe code for additional performance. - Fixed support for empty messages. +- Fixed raw offsets of multipart/* parts to include MIME epilogue. mail-parser 0.6.1 ================================ diff --git a/src/parsers/message.rs b/src/parsers/message.rs index 1d98fb6..db3ce3c 100644 --- a/src/parsers/message.rs +++ b/src/parsers/message.rs @@ -16,6 +16,7 @@ use crate::{ base64::decode_base64_mime, charsets::map::get_charset_decoder, quoted_printable::decode_quoted_printable_mime, DecodeFnc, }, + parsers::mime::seek_next_part_offset, ContentType, Encoding, GetHeader, HeaderValue, Message, MessageAttachment, MessagePart, MessagePartId, PartType, RfcHeader, }; @@ -454,25 +455,29 @@ impl<'x> Message<'x> { } if let Some(part) = message.parts.get_mut(state.part_id) { - // Update end offset - part.offset_end = seek_crlf(&stream, stream.pos); // Add headers and substructure to parent part part.body = PartType::Multipart(std::mem::take(&mut state.sub_part_ids)); - } else { - debug_assert!(false, "Invalid part ID, could not find multipart."); - } - if let Some((prev_state, _)) = state_stack.pop() { // Restore ancestor's state - state = prev_state; + if let Some((prev_state, _)) = state_stack.pop() { + state = prev_state; - if let Some(ref mime_boundary) = state.mime_boundary { - // Ancestor has a MIME boundary, seek it. - if seek_next_part(&mut stream, mime_boundary) { - continue 'inner; + if let Some(ref mime_boundary) = state.mime_boundary { + // Ancestor has a MIME boundary, seek it. + if let Some(offset) = + seek_next_part_offset(&mut stream, mime_boundary) + { + part.offset_end = offset; + continue 'inner; + } } } + + // This part has no boundary, update end offset + part.offset_end = seek_crlf(&stream, stream.pos); + } else { + debug_assert!(false, "Invalid part ID, could not find multipart."); } break 'outer; diff --git a/src/parsers/mime.rs b/src/parsers/mime.rs index b7ede0d..701ac41 100644 --- a/src/parsers/mime.rs +++ b/src/parsers/mime.rs @@ -34,6 +34,31 @@ pub fn seek_next_part(stream: &mut MessageStream, boundary: &[u8]) -> bool { false } +pub fn seek_next_part_offset(stream: &mut MessageStream, boundary: &[u8]) -> Option { + let mut pos = stream.pos; + let mut last_ch = b'\n'; + let mut iter = stream.data[stream.pos..].iter().peekable(); + let mut offset_pos = stream.pos; + + while let Some(&ch) = iter.next() { + pos += 1; + + if ch == b'\n' { + offset_pos = if last_ch == b'\r' { pos - 2 } else { pos - 1 }; + } else if ch == b'-' + && matches!(iter.peek(), Some(b'-')) + && stream.data.get(pos + 1..pos + 1 + boundary.len()) == Some(boundary) + { + stream.pos = pos + boundary.len() + 1; + return offset_pos.into(); + } + + last_ch = ch; + } + + None +} + pub fn get_mime_part<'x>( stream: &mut MessageStream<'x>, boundary: &[u8], diff --git a/tests/malformed/005.json b/tests/malformed/005.json index a8a7f75..5b976b2 100644 --- a/tests/malformed/005.json +++ b/tests/malformed/005.json @@ -69,7 +69,7 @@ }, "offset_header": 53, "offset_body": 102, - "offset_end": 151 + "offset_end": 181 }, { "headers": [ diff --git a/tests/rfc/009.eml b/tests/rfc/009.eml new file mode 100644 index 0000000..6b50150 --- /dev/null +++ b/tests/rfc/009.eml @@ -0,0 +1,39 @@ +From: Whomever +To: Someone +Date: Sat, 10 Oct 2009 00:30:04 +0200 +Subject: whatever +Content-Type: multipart/mixed; boundary=outer + +This is a multi-part message in MIME format. + +--outer +Content-Type: multipart/alternative; boundary=inner + +This is a nested multi-part message in MIME format. + +--inner +Content-Type: text/plain; charset="us-ascii" + +Hello + +--inner +Content-Type: text/html; charset="us-ascii" + +Hello + +--inner-- + +This is the end of the inner MIME multipart. + +--outer +Content-Type: message/rfc822 + +From: Someone Else +Subject: Hello, this is an elaborate request for you to finally say hello + already! + +Please say Hello + +--outer-- + +This is the end of the outer MIME multipart. diff --git a/tests/rfc/009.json b/tests/rfc/009.json new file mode 100644 index 0000000..93f5a60 --- /dev/null +++ b/tests/rfc/009.json @@ -0,0 +1,264 @@ +{ + "html_body": [ + 3 + ], + "text_body": [ + 2 + ], + "attachments": [ + 4 + ], + "parts": [ + { + "headers": [ + { + "name": { + "Rfc": "from" + }, + "value": { + "Address": { + "name": "Whomever", + "address": "whoever@example.com" + } + }, + "offset_start": 5, + "offset_end": 37 + }, + { + "name": { + "Rfc": "to" + }, + "value": { + "Address": { + "name": "Someone", + "address": "someone@example.com" + } + }, + "offset_start": 40, + "offset_end": 71 + }, + { + "name": { + "Rfc": "date" + }, + "value": { + "DateTime": { + "year": 2009, + "month": 10, + "day": 10, + "hour": 0, + "minute": 30, + "second": 4, + "tz_before_gmt": false, + "tz_hour": 2, + "tz_minute": 0 + } + }, + "offset_start": 76, + "offset_end": 109 + }, + { + "name": { + "Rfc": "subject" + }, + "value": { + "Text": "whatever" + }, + "offset_start": 117, + "offset_end": 127 + }, + { + "name": { + "Rfc": "content_type" + }, + "value": { + "ContentType": { + "c_type": "multipart", + "c_subtype": "mixed", + "attributes": [ + [ + "boundary", + "outer" + ] + ] + } + }, + "offset_start": 140, + "offset_end": 173 + } + ], + "is_encoding_problem": false, + "body": { + "Multipart": [ + 1, + 4 + ] + }, + "offset_header": 0, + "offset_body": 174, + "offset_end": 754 + }, + { + "headers": [ + { + "name": { + "Rfc": "content_type" + }, + "value": { + "ContentType": { + "c_type": "multipart", + "c_subtype": "alternative", + "attributes": [ + [ + "boundary", + "inner" + ] + ] + } + }, + "offset_start": 241, + "offset_end": 280 + } + ], + "is_encoding_problem": false, + "body": { + "Multipart": [ + 2, + 3 + ] + }, + "offset_header": 228, + "offset_body": 281, + "offset_end": 537 + }, + { + "headers": [ + { + "name": { + "Rfc": "content_type" + }, + "value": { + "ContentType": { + "c_type": "text", + "c_subtype": "plain", + "attributes": [ + [ + "charset", + "us-ascii" + ] + ] + } + }, + "offset_start": 355, + "offset_end": 387 + } + ], + "is_encoding_problem": false, + "body": { + "Text": "Hello\n" + }, + "offset_header": 342, + "offset_body": 388, + "offset_end": 394 + }, + { + "headers": [ + { + "name": { + "Rfc": "content_type" + }, + "value": { + "ContentType": { + "c_type": "text", + "c_subtype": "html", + "attributes": [ + [ + "charset", + "us-ascii" + ] + ] + } + }, + "offset_start": 416, + "offset_end": 447 + } + ], + "is_encoding_problem": false, + "body": { + "Html": "Hello\n" + }, + "offset_header": 403, + "offset_body": 448, + "offset_end": 480 + }, + { + "headers": [ + { + "name": { + "Rfc": "content_type" + }, + "value": { + "ContentType": { + "c_type": "message", + "c_subtype": "rfc822", + "attributes": null + } + }, + "offset_start": 559, + "offset_end": 575 + } + ], + "is_encoding_problem": false, + "body": { + "Message": { + "html_body": [ + 0 + ], + "text_body": [ + 0 + ], + "attachments": [], + "parts": [ + { + "headers": [ + { + "name": { + "Rfc": "from" + }, + "value": { + "Address": { + "name": "Someone Else", + "address": null + } + }, + "offset_start": 581, + "offset_end": 595 + }, + { + "name": { + "Rfc": "subject" + }, + "value": { + "Text": "Hello, this is an elaborate request for you to finally say hello already!" + }, + "offset_start": 603, + "offset_end": 679 + } + ], + "is_encoding_problem": false, + "body": { + "Text": "Please say Hello\n" + }, + "offset_header": 576, + "offset_body": 680, + "offset_end": 697 + } + ] + } + }, + "offset_header": 546, + "offset_body": 576, + "offset_end": 697 + } + ] +} \ No newline at end of file diff --git a/tests/thirdparty/007.json b/tests/thirdparty/007.json index 8c960d2..92d4065 100644 --- a/tests/thirdparty/007.json +++ b/tests/thirdparty/007.json @@ -231,7 +231,7 @@ }, "offset_header": 246, "offset_body": 343, - "offset_end": 565 + "offset_end": 564 }, { "headers": [ diff --git a/tests/thirdparty/008.json b/tests/thirdparty/008.json index 0235820..b14fa41 100644 --- a/tests/thirdparty/008.json +++ b/tests/thirdparty/008.json @@ -232,7 +232,7 @@ }, "offset_header": 295, "offset_body": 424, - "offset_end": 710 + "offset_end": 728 }, { "headers": [ @@ -290,7 +290,7 @@ }, "offset_header": 501, "offset_body": 555, - "offset_end": 701 + "offset_end": 700 }, { "headers": [ @@ -323,7 +323,7 @@ }, "offset_header": 562, "offset_body": 616, - "offset_end": 662 + "offset_end": 672 }, { "headers": [], diff --git a/tests/thirdparty/012.json b/tests/thirdparty/012.json index 82681a2..5e69882 100644 --- a/tests/thirdparty/012.json +++ b/tests/thirdparty/012.json @@ -224,7 +224,7 @@ }, "offset_header": 296, "offset_body": 425, - "offset_end": 558 + "offset_end": 576 }, { "headers": [