From de4aecf9f124f8458b3da39c46f5311bbadcb33b Mon Sep 17 00:00:00 2001 From: Mauro D Date: Fri, 9 Sep 2022 08:54:56 +0000 Subject: [PATCH] Support for malformed unstructured fields containing encoded words --- src/decoders/quoted_printable.rs | 14 +++++++++++--- src/parsers/fields/unstructured.rs | 9 +++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/decoders/quoted_printable.rs b/src/decoders/quoted_printable.rs index 6990d60..ebea27f 100644 --- a/src/decoders/quoted_printable.rs +++ b/src/decoders/quoted_printable.rs @@ -38,8 +38,9 @@ pub fn decode_quoted_printable<'x>( let mut lf_pos = 0; debug_assert!(boundary.is_empty() || boundary.len() >= 2); + let mut iter = stream.data[start_pos..].iter().peekable(); - for ch in stream.data[start_pos..].iter() { + while let Some(ch) = iter.next() { bytes_read += 1; // if success is false, a boundary was provided @@ -108,10 +109,17 @@ pub fn decode_quoted_printable<'x>( } b'\n' => { if is_word { - if !matches!(stream.data.get(start_pos + bytes_read), Some(ch) if [b' ', b'\t'].contains(ch)) - { + if !matches!(iter.peek(), Some(ch) if [b' ', b'\t'].contains(ch)) { success = false; break; + } else { + loop { + iter.next(); + bytes_read += 1; + if !matches!(iter.peek(), Some(ch) if [b' ', b'\t'].contains(ch)) { + break; + } + } } } else if QuotedPrintableState::Eq == state { state = QuotedPrintableState::None; diff --git a/src/parsers/fields/unstructured.rs b/src/parsers/fields/unstructured.rs index bb6e4c6..7aeeb07 100644 --- a/src/parsers/fields/unstructured.rs +++ b/src/parsers/fields/unstructured.rs @@ -175,8 +175,13 @@ mod tests { "abcd", true, ), - ("=?utf-8?Q?Hello\n _there!?=\n", "Hello there!", true), - ("=?utf-8?Q?Hello\r\n _there!?=\r\n", "Hello there!", true), + ("=?utf-8?Q?Hello\n _there!?=\n", "Hello there!", true), + ("=?utf-8?Q?Hello\r\n _there!?=\r\n", "Hello there!", true), + ( + "=?utf-8?Q?Hello\r\n \t _there!?=\r\n", + "Hello there!", + true, + ), ( "[SUSPECTED SPAM]=?utf-8?B?VGhpcyBpcyB0aGUgb\n 3JpZ2luYWwgc3ViamVjdA==?=\n", "[SUSPECTED SPAM] This is the original subject",