Support for malformed unstructured fields containing encoded words (#29)

This commit is contained in:
Mauro D
2022-09-08 15:56:30 +00:00
parent f16aca7ee0
commit c5ce06c586
7 changed files with 27 additions and 23 deletions
+1
View File
@@ -2,3 +2,4 @@
/test
perf.*
*.svg
Cargo.lock
+4
View File
@@ -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.
Generated
+1 -1
View File
@@ -135,7 +135,7 @@ dependencies = [
[[package]]
name = "mail-parser"
version = "0.6.0"
version = "0.6.1"
dependencies = [
"bincode",
"chrono",
+1 -1
View File
@@ -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 <hello@stalw.art>"]
license = "Apache-2.0 OR MIT"
+2
View File
@@ -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()));
}
}
+1
View File
@@ -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<u8>),
Borrowed((usize, usize)),
+17 -21
View File
@@ -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<Cow<'x, str>>,
}
@@ -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
);
}
}