Skip to content

Commit

Permalink
fix: Ignore hidden headers in IMF section
Browse files Browse the repository at this point in the history
Hidden headers are nonstandard, so they aren't DKIM-signed by e.g. OpenDKIM if they appear in IMF
section.
  • Loading branch information
iequidoo committed Mar 6, 2025
1 parent de79bca commit 0a7bff6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/mimeparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl MimeMessage {
&mut chat_disposition_notification_to,
&mail.headers,
);
headers.retain(|k, _| !is_hidden(k));

// Parse hidden headers.
let mimetype = mail.ctype.mimetype.parse::<Mime>()?;
Expand Down Expand Up @@ -289,12 +290,7 @@ impl MimeMessage {
let key = field.get_key().to_lowercase();

// For now only avatar headers can be hidden.
if !headers.contains_key(&key)
&& (key == "chat-user-avatar"
|| key == "chat-group-avatar"
|| key == "chat-delete"
|| key == "chat-edit")
{
if !headers.contains_key(&key) && is_hidden(&key) {
headers.insert(key.to_string(), field.get_value());
}
}
Expand Down Expand Up @@ -1987,6 +1983,14 @@ fn is_known(key: &str) -> bool {
)
}

/// Returns if the header is hidden and must be ignored in the IMF section.
fn is_hidden(key: &str) -> bool {
matches!(
key,
"chat-user-avatar" | "chat-group-avatar" | "chat-delete" | "chat-edit"
)
}

/// Parsed MIME part.
#[derive(Debug, Default, Clone)]
pub struct Part {
Expand Down
44 changes: 43 additions & 1 deletion src/mimeparser/mimeparser_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use mailparse::ParsedMail;
use std::mem;

use super::*;
use crate::{
chat,
chatlist::Chatlist,
constants::{Blocked, DC_DESIRED_TEXT_LEN, DC_ELLIPSIS},
message::{MessageState, MessengerMessage},
message::{self, MessageState, MessengerMessage},
receive_imf::receive_imf,
test_utils::{TestContext, TestContextManager},
tools::time,
Expand Down Expand Up @@ -1903,3 +1904,44 @@ This is the epilogue. It is also to be ignored.";
"This is explicitly typed plain US-ASCII text.\r\nIt DOES end with a linebreak.\r\n"
);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_chat_delete_imf_header() -> Result<()> {
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
let alice_chat = alice.create_chat(bob).await;

// Alice sends a message, then sends an invalid deletion request.
let sent1 = alice.send_text(alice_chat.id, "wtf").await;
let alice_msg = sent1.load_from_db().await;
assert_eq!(alice_chat.id.get_msg_cnt(alice).await?, 1);

message::delete_msgs_ex(alice, &[alice_msg.id], true).await?;
let mut sent2 = alice.pop_sent_msg().await;
let mut s0 = String::new();
let mut s1 = String::new();
for l in sent2.payload.lines() {
if l.starts_with("Chat-Delete:") {
s1 += l;
s1 += "\n";
continue;
}
s0 += l;
s0 += "\n";
if l.starts_with("Message-ID:") && s1.is_empty() {
s1 = mem::take(&mut s0);
}
}
sent2.payload = s1 + &s0;

// Bob receives both messages, the deletion request with "Chat-Delete" in IMF headers is
// received as text message.
let bob_msg = bob.recv_msg(&sent1).await;
assert_eq!(bob_msg.text, "wtf");
assert_eq!(bob_msg.chat_id.get_msg_cnt(bob).await?, 1);
bob.recv_msg_opt(&sent2).await;
assert_eq!(bob_msg.chat_id.get_msg_cnt(bob).await?, 2);

Ok(())
}
4 changes: 2 additions & 2 deletions test-data/message/mail_with_user_and_group_avatars.eml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Chat-Group-ID: WVnDtF5azch
Chat-Group-Name: =?utf-8?q?testgr1?=
Chat-Group-Avatar: group-image.png
Chat-User-Avatar: avatar.png
Subject: =?utf-8?q?Chat=3A_testgr1=3A_hi!_?=
Date: Thu, 12 Dec 2019 17:24:03 +0000
X-Mailer: Delta Chat Core 1.0.0-beta.15/CLI
Expand All @@ -14,6 +12,8 @@ Content-Type: multipart/mixed; boundary="LV8nfXkpyyn39fsVyoB1b29PKDMeb5"

--LV8nfXkpyyn39fsVyoB1b29PKDMeb5
Content-Type: text/plain; charset=utf-8
Chat-Group-Avatar: group-image.png
Chat-User-Avatar: avatar.png
hi!
Expand Down
2 changes: 1 addition & 1 deletion test-data/message/mail_with_user_avatar.eml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Chat-User-Avatar: avatar.png
Subject: =?utf-8?q?Chat=3A_this_is_a_message_with_a_=2E=2E=2E?=
Message-ID: [email protected]
In-Reply-To: [email protected]
Expand All @@ -12,6 +11,7 @@ Content-Type: multipart/mixed; boundary="luTiGu6GBoVLCvTkzVtmZmwsmhkNMw"

--luTiGu6GBoVLCvTkzVtmZmwsmhkNMw
Content-Type: text/plain; charset=utf-8
Chat-User-Avatar: avatar.png
this is a message with a profile-image attached
Expand Down
10 changes: 8 additions & 2 deletions test-data/message/mail_with_user_avatar_deleted.eml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
Content-Type: text/plain; charset=utf-8
Chat-User-Avatar: 0
Subject: =?utf-8?q?Chat=3A_profile_image_deleted?=
Message-ID: [email protected]
Date: Sun, 08 Dec 2019 23:28:30 +0000
X-Mailer: Delta Chat Core 1.0.0-beta.12/CLI
Chat-Version: 1.0
To: <tunis3@example>
From: "=?utf-8?q??=" <[email protected]>
Content-Type: multipart/mixed; boundary="luTiGu6GBoVLCvTkzVtmZmwsmhkNMw"


--luTiGu6GBoVLCvTkzVtmZmwsmhkNMw
Content-Type: text/plain; charset=utf-8
Chat-User-Avatar: 0
profile image deleted
--
Sent with my Delta Chat Messenger: https://delta.chat

--luTiGu6GBoVLCvTkzVtmZmwsmhkNMw--

0 comments on commit 0a7bff6

Please sign in to comment.