Support for malformed Thunderbird messages (#27).

This commit is contained in:
Mauro D
2022-07-19 07:26:15 +00:00
parent 4e49dff9e5
commit bb0e3366da
5 changed files with 3793 additions and 12 deletions
+10 -5
View File
@@ -35,6 +35,7 @@ pub fn decode_quoted_printable<'x>(
let mut state = QuotedPrintableState::None;
let mut match_count = 0;
let mut hex1 = 0;
let mut lf_pos = 0;
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
@@ -53,6 +54,9 @@ pub fn decode_quoted_printable<'x>(
} else {
is_boundary_end =
crate::parsers::mime::is_boundary_end(stream, start_pos + bytes_read);
if is_boundary_end && bytes_read - boundary.len() == lf_pos {
buf.pop();
}
is_boundary_end
};
@@ -66,8 +70,8 @@ pub fn decode_quoted_printable<'x>(
}
if match_count > 0 {
for ch in boundary[..match_count].iter() {
if *ch != b'\n' || QuotedPrintableState::Eq != state {
if QuotedPrintableState::Eq != state {
for ch in boundary[..match_count].iter() {
buf.push(*ch);
}
}
@@ -110,6 +114,7 @@ pub fn decode_quoted_printable<'x>(
state = QuotedPrintableState::None;
} else {
buf.push(b'\n');
lf_pos = bytes_read;
}
}
b'_' if is_word => {
@@ -191,19 +196,19 @@ mod tests {
(
"=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry\n--boundary",
"— Antoine de Saint-Exupéry",
"\n--boundary",
"--boundary",
false,
),
(
"=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry\n--\n--boundary",
"— Antoine de Saint-Exupéry\n--",
"\n--boundary",
"--boundary",
false,
),
(
"=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry=\n--\n--boundary",
"— Antoine de Saint-Exupéry--",
"\n--boundary",
"--boundary",
false,
),
(
+17 -5
View File
@@ -226,7 +226,8 @@ impl<'x> Message<'x> {
if let Some(mime_boundary) =
content_type.map_or_else(|| None, |f| f.get_attribute("boundary"))
{
let mime_boundary = format!("\n--{}", mime_boundary).into_bytes();
//let mime_boundary = format!("\n--{}", mime_boundary).into_bytes();
let mime_boundary = format!("--{}", mime_boundary).into_bytes();
state.offset_body = seek_crlf(&stream, stream.pos);
if seek_next_part(&mut stream, mime_boundary.as_ref()) {
@@ -385,7 +386,7 @@ impl<'x> Message<'x> {
state.offset_end = state
.mime_boundary
.as_ref()
.map(|b| stream.pos.saturating_sub(b.len() - 1))
.map(|b| stream.pos.saturating_sub(b.len()))
.unwrap_or(stream.pos);
let body_part = if mime_type != MimeType::Message {
@@ -491,7 +492,7 @@ impl<'x> Message<'x> {
let offset_end = state
.mime_boundary
.as_ref()
.map(|b| stream.pos.saturating_sub(b.len() - 1))
.map(|b| stream.pos.saturating_sub(b.len()))
.unwrap_or(stream.pos);
message.raw_message =
raw_message[state.offset_header..offset_end].as_ref().into();
@@ -665,7 +666,7 @@ mod tests {
#[test]
fn parse_full_messages() {
for test_suite in ["malformed", "rfc", "legacy", "thirdparty", "malformed"] {
for test_suite in ["rfc", "legacy", "thirdparty", "malformed"] {
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push(test_suite);
@@ -754,7 +755,7 @@ mod tests {
fn generate_test_samples() {
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----".as_bytes();
for test_suite in ["legacy", "malformed", "rfc", "thirdparty"] {
for test_suite in [/*"legacy", "malformed", "rfc",*/ "thirdparty"] {
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("tests");
test_dir.push(test_suite);
@@ -771,6 +772,17 @@ mod tests {
continue;
}
/*if !file_name
.as_ref()
.unwrap()
.path()
.to_str()
.unwrap()
.contains("011")
{
continue;
}*/
println!("{:}", file_name.as_ref().unwrap().path().display());
let mut input = fs::read(file_name.as_ref().unwrap().path()).unwrap();
let mut pos = 0;
+14 -2
View File
@@ -54,6 +54,8 @@ pub fn get_bytes_to_boundary<'x>(
if !boundary.is_empty() {
let mut match_count = 0;
let mut cr_pos = 0;
let mut lf_pos = 0;
for ch in &stream.data[read_pos..] {
read_pos += 1;
@@ -64,8 +66,12 @@ pub fn get_bytes_to_boundary<'x>(
if is_boundary_end(stream, read_pos) {
let mut match_pos = read_pos - match_count;
if let Some(b'\r') = stream.data.get(match_pos.saturating_sub(1)) {
match_pos -= 1;
if match_pos > 0 && match_pos == lf_pos {
if lf_pos == cr_pos + 1 {
match_pos -= 2;
} else {
match_pos -= 1;
}
}
return (
@@ -89,6 +95,12 @@ pub fn get_bytes_to_boundary<'x>(
match_count = 0;
}
}
if ch == &b'\r' {
cr_pos = read_pos;
} else if ch == &b'\n' {
lf_pos = read_pos;
}
}
(0, DecodeResult::Empty)
BIN
View File
Binary file not shown.
+3752
View File
File diff suppressed because it is too large Load Diff