Skip to content

The library will panic with messages containing corrupted eml attachments #120

@cosarara

Description

@cosarara

This test when added to tests/integration_tests.rs showcases the problem:

#[test]
fn corrupted_attachments_should_not_panic() {
    let input = br#"From: Test <[email protected]>
Date: Mon, 1 Oct 2025 01:02:03 +0200
Subject: test
To: [email protected]
Content-Type: multipart/mixed; boundary="000000000000e07f5706407d1689"

--000000000000e07f5706407d1689
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

bla bla bla

--000000000000e07f5706407d1689
Content-Type: message/rfc822
Content-Disposition: attachment; filename="corrupted.eml"
Content-Transfer-Encoding: 8bit

this attachment is not a valid eml, sorry!
--000000000000e07f5706407d1689--
"#;
    let message = MessageParser::default()
        .with_minimal_headers()
        .parse(&input)
        .unwrap();

    let attachments: Vec<_> = message.attachments().collect();

    let sub_message = match &attachments[0].body {
        mail_parser::PartType::Message(m) => m,
        _ => panic!(),
    };

    // this panics with `index out of bounds: the len is 0 but the index is 0`
    // other methods on Message panic the same way
    if let Some(s) = sub_message.attachment_name() {
        println!("name: {}", s);
    }
    // this assertion fails, and that is why it panics
    // (the code everywhere assumes parts[0] will exist)
    assert_ne!(sub_message.parts.len(), 0);
}

running 1 test
test corrupted_attachments_should_not_panic ... FAILED

failures:

---- corrupted_attachments_should_not_panic stdout ----

thread 'corrupted_attachments_should_not_panic' panicked at src/core/header.rs:606:19:
index out of bounds: the len is 0 but the index is 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions