Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ impl<'a> Iterator for PartsIterator<'a> {
/// assert_eq!(dateparse(parsed.headers.get_first_value("Date").unwrap().as_str()).unwrap(), 1475417182);
/// ```
pub fn parse_mail(raw_data: &[u8]) -> Result<ParsedMail, MailParseError> {
parse_mail_recursive(raw_data, false)
parse_mail_recursive(raw_data, false, u8::MAX)
}

/// Strips LF or CRLF if there is one at the end of the string raw_data[ix_start..ix].
Expand All @@ -954,6 +954,7 @@ fn strip_trailing_crlf(raw_data: &[u8], ix_start: usize, mut ix: usize) -> usize
fn parse_mail_recursive(
raw_data: &[u8],
in_multipart_digest: bool,
depth: u8,
) -> Result<ParsedMail, MailParseError> {
let (headers, ix_body) = parse_headers(raw_data)?;
let ctype = headers
Expand Down Expand Up @@ -994,6 +995,9 @@ fn parse_mail_recursive(
result.subparts.push(parse_mail_recursive(
&raw_data[ix_part_start..ix_part_end],
in_multipart_digest,
depth
.checked_sub(1)
.ok_or(MailParseError::Generic("Recursion limit reached"))?,
)?);
ix_boundary_end = ix_part_boundary_start
.map(|x| x + boundary.len())
Expand Down Expand Up @@ -1908,6 +1912,14 @@ mod tests {
};
}

#[test]
fn test_recursion_limit() {
let mail_filepath = "./tests/files/nested.eml";
let mail = std::fs::read(mail_filepath)
.expect(&format!("Unable to open the file [{}]", mail_filepath));
parse_mail(&mail).unwrap();
}

#[test]
fn test_body_content_encoding_with_multipart() {
let mail_filepath = "./tests/files/test_email_01.txt";
Expand Down
Loading