diff --git a/.gitignore b/.gitignore index bc53b59..6873570 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /test perf.* *.svg +Cargo.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index 314bf97..2b26287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +mail-parser 0.6.1 +================================ +- Support for malformed unstructured fields containing encoded words (#29). + mail-parser 0.6.0 ================================ - Maildir parsing support. diff --git a/Cargo.lock b/Cargo.lock index 6bd31c3..e8423a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,7 +135,7 @@ dependencies = [ [[package]] name = "mail-parser" -version = "0.6.0" +version = "0.6.1" dependencies = [ "bincode", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 0512fcd..a5de2d1 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.6.0" +version = "0.6.1" edition = "2018" authors = [ "Stalwart Labs "] license = "Apache-2.0 OR MIT" diff --git a/src/decoders/encoded_word.rs b/src/decoders/encoded_word.rs index 8214ca8..e521926 100644 --- a/src/decoders/encoded_word.rs +++ b/src/decoders/encoded_word.rs @@ -97,6 +97,8 @@ pub fn decode_rfc2047(stream: &MessageStream, start_pos: usize) -> (usize, Optio .into() }, ); + } else if let Some(b"?=") = stream.data.get(read_pos..read_pos + 2) { + return ((read_pos - start_pos) + 2, Some(String::new())); } } diff --git a/src/decoders/mod.rs b/src/decoders/mod.rs index 4d386ee..6ad1b15 100644 --- a/src/decoders/mod.rs +++ b/src/decoders/mod.rs @@ -20,6 +20,7 @@ pub mod quoted_printable; pub type DecodeFnc<'x> = fn(&MessageStream<'x>, usize, &[u8], bool) -> (usize, DecodeResult); +#[derive(Debug)] pub enum DecodeResult { Owned(Vec), Borrowed((usize, usize)), diff --git a/src/parsers/fields/unstructured.rs b/src/parsers/fields/unstructured.rs index 243abd5..53b5e6e 100644 --- a/src/parsers/fields/unstructured.rs +++ b/src/parsers/fields/unstructured.rs @@ -15,7 +15,6 @@ use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStr struct UnstructuredParser<'x> { token_start: usize, token_end: usize, - is_token_start: bool, tokens: Vec>, } @@ -33,7 +32,6 @@ fn add_token<'x>(parser: &mut UnstructuredParser<'x>, stream: &MessageStream<'x> } parser.token_start = 0; - parser.is_token_start = true; } } @@ -41,7 +39,6 @@ pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> let mut parser = UnstructuredParser { token_start: 0, token_end: 0, - is_token_start: true, tokens: Vec::new(), }; @@ -55,9 +52,6 @@ pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> match stream.data.get(stream.pos) { Some(b' ' | b'\t') => { - if !parser.is_token_start { - parser.is_token_start = true; - } iter.next(); stream.pos += 1; continue; @@ -71,13 +65,10 @@ pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> } } } - b' ' | b'\t' => { - if !parser.is_token_start { - parser.is_token_start = true; - } + b' ' | b'\t' | b'\r' => { continue; } - b'=' if parser.is_token_start => { + b'=' => { if let (bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) { add_token(&mut parser, stream, true); parser.tokens.push(token.into()); @@ -86,14 +77,9 @@ pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> continue; } } - b'\r' => continue, _ => (), } - if parser.is_token_start { - parser.is_token_start = false; - } - if parser.token_start == 0 { parser.token_start = stream.pos; } @@ -178,15 +164,25 @@ mod tests { false, ), ("ハロー・ワールド \n", "ハロー・ワールド", true), + ( + "[SUSPECTED SPAM]=?utf-8?B?VGhpcyBpcyB0aGUgb3JpZ2luYWwgc3ViamVjdA==?=\n", + "[SUSPECTED SPAM] This is the original subject", + true, + ), + ("Some text =?utf-8?Q??=here\n", "Some text here", true), + ( + "=?ISO-8859-1?Q?a?==?ISO-8859-1?Q?b?==?ISO-8859-1?Q?c?= =?ISO-8859-1?Q?d?=\n", + "abcd", + true, + ), ]; - for input in inputs { - let str = input.0.to_string(); + for (input, expected_result, _) in inputs { assert_eq!( - parse_unstructured(&mut MessageStream::new(str.as_bytes()),).unwrap_text(), - input.1, + parse_unstructured(&mut MessageStream::new(input.as_bytes()),).unwrap_text(), + expected_result, "Failed to parse '{:?}'", - input.0 + input ); } }