Skip to content

Commit 1c97d11

Browse files
committed
fix: always prefer the last header
Headers are normally added at the top of the message, e.g. when forwarding new `Received` headers are added at the top. When headers are protected with DKIM-Signature and oversigning is not used, forged headers may be added on top so headers from the top are generally less trustworthy. This is tested with `test_take_last_header`, but so far last header was only preferred for known headers. This change extends preference of the last header to all headers.
1 parent d45ec7f commit 1c97d11

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

src/mimeparser.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,20 +1568,16 @@ impl MimeMessage {
15681568
for field in fields {
15691569
// lowercasing all headers is technically not correct, but makes things work better
15701570
let key = field.get_key().to_lowercase();
1571-
if !headers.contains_key(&key) || // key already exists, only overwrite known types (protected headers)
1572-
is_known(&key) || key.starts_with("chat-")
1573-
{
1574-
if key == HeaderDef::ChatDispositionNotificationTo.get_headername() {
1575-
match addrparse_header(field) {
1576-
Ok(addrlist) => {
1577-
*chat_disposition_notification_to = addrlist.extract_single_info();
1578-
}
1579-
Err(e) => warn!(context, "Could not read {} address: {}", key, e),
1571+
if key == HeaderDef::ChatDispositionNotificationTo.get_headername() {
1572+
match addrparse_header(field) {
1573+
Ok(addrlist) => {
1574+
*chat_disposition_notification_to = addrlist.extract_single_info();
15801575
}
1581-
} else {
1582-
let value = field.get_value();
1583-
headers.insert(key.to_string(), value);
1576+
Err(e) => warn!(context, "Could not read {} address: {}", key, e),
15841577
}
1578+
} else {
1579+
let value = field.get_value();
1580+
headers.insert(key.to_string(), value);
15851581
}
15861582
}
15871583
let recipients_new = get_recipients(fields);
@@ -2009,28 +2005,6 @@ pub(crate) fn parse_message_id(ids: &str) -> Result<String> {
20092005
}
20102006
}
20112007

2012-
/// Returns true if the header overwrites outer header
2013-
/// when it comes from protected headers.
2014-
fn is_known(key: &str) -> bool {
2015-
matches!(
2016-
key,
2017-
"return-path"
2018-
| "date"
2019-
| "from"
2020-
| "sender"
2021-
| "reply-to"
2022-
| "to"
2023-
| "cc"
2024-
| "bcc"
2025-
| "message-id"
2026-
| "in-reply-to"
2027-
| "references"
2028-
| "subject"
2029-
| "secure-join"
2030-
| "list-id"
2031-
)
2032-
}
2033-
20342008
/// Returns if the header is hidden and must be ignored in the IMF section.
20352009
pub(crate) fn is_hidden(key: &str) -> bool {
20362010
matches!(

0 commit comments

Comments
 (0)