Skip to content

Commit c12c4f6

Browse files
committed
fix: Ignore hidden headers in IMF section
Hidden headers are nonstandard, so they aren't DKIM-signed by e.g. OpenDKIM if they appear in IMF section.
1 parent b5acbaa commit c12c4f6

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

src/mimefactory.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::e2ee::EncryptHelper;
2323
use crate::ephemeral::Timer as EphemeralTimer;
2424
use crate::location;
2525
use crate::message::{self, Message, MsgId, Viewtype};
26-
use crate::mimeparser::SystemMessage;
26+
use crate::mimeparser::{is_hidden, SystemMessage};
2727
use crate::param::Param;
2828
use crate::peer_channels::create_iroh_header;
2929
use crate::peerstate::Peerstate;
@@ -869,11 +869,7 @@ impl MimeFactory {
869869
if header_name == "message-id" {
870870
unprotected_headers.push(header.clone());
871871
hidden_headers.push(header.clone());
872-
} else if header_name == "chat-user-avatar"
873-
|| header_name == "chat-group-avatar"
874-
|| header_name == "chat-delete"
875-
|| header_name == "chat-edit"
876-
{
872+
} else if is_hidden(&header_name) {
877873
hidden_headers.push(header.clone());
878874
} else if header_name == "autocrypt"
879875
&& !context.get_config_bool(Config::ProtectAutocrypt).await?

src/mimeparser.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ impl MimeMessage {
253253
&mut chat_disposition_notification_to,
254254
&mail.headers,
255255
);
256+
headers.retain(|k, _| !is_hidden(k));
256257

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

291292
// For now only avatar headers can be hidden.
292-
if !headers.contains_key(&key)
293-
&& (key == "chat-user-avatar"
294-
|| key == "chat-group-avatar"
295-
|| key == "chat-delete"
296-
|| key == "chat-edit")
297-
{
293+
if !headers.contains_key(&key) && is_hidden(&key) {
298294
headers.insert(key.to_string(), field.get_value());
299295
}
300296
}
@@ -1988,6 +1984,14 @@ fn is_known(key: &str) -> bool {
19881984
)
19891985
}
19901986

1987+
/// Returns if the header is hidden and must be ignored in the IMF section.
1988+
pub(crate) fn is_hidden(key: &str) -> bool {
1989+
matches!(
1990+
key,
1991+
"chat-user-avatar" | "chat-group-avatar" | "chat-delete" | "chat-edit"
1992+
)
1993+
}
1994+
19911995
/// Parsed MIME part.
19921996
#[derive(Debug, Default, Clone)]
19931997
pub struct Part {

src/mimeparser/mimeparser_tests.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use mailparse::ParsedMail;
2+
use std::mem;
23

34
use super::*;
45
use crate::{
56
chat,
67
chatlist::Chatlist,
7-
constants::{Blocked, DC_DESIRED_TEXT_LEN, DC_ELLIPSIS},
8+
constants::{self, Blocked, DC_DESIRED_TEXT_LEN, DC_ELLIPSIS},
89
message::{MessageState, MessengerMessage},
910
receive_imf::receive_imf,
1011
test_utils::{TestContext, TestContextManager},
@@ -1916,3 +1917,45 @@ This is the epilogue. It is also to be ignored.";
19161917
"This is explicitly typed plain US-ASCII text.\r\nIt DOES end with a linebreak.\r\n"
19171918
);
19181919
}
1920+
1921+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1922+
async fn test_chat_edit_imf_header() -> Result<()> {
1923+
let mut tcm = TestContextManager::new();
1924+
let alice = &tcm.alice().await;
1925+
let bob = &tcm.bob().await;
1926+
let alice_chat = alice.create_email_chat(bob).await;
1927+
1928+
// Alice sends a message, then sends an invalid edit request.
1929+
let sent1 = alice.send_text(alice_chat.id, "foo").await;
1930+
let alice_msg = sent1.load_from_db().await;
1931+
assert_eq!(alice_chat.id.get_msg_cnt(alice).await?, 1);
1932+
1933+
chat::send_edit_request(alice, alice_msg.id, "bar".to_string()).await?;
1934+
let mut sent2 = alice.pop_sent_msg().await;
1935+
let mut s0 = String::new();
1936+
let mut s1 = String::new();
1937+
for l in sent2.payload.lines() {
1938+
if l.starts_with("Chat-Edit:") {
1939+
s1 += l;
1940+
s1 += "\n";
1941+
continue;
1942+
}
1943+
s0 += l;
1944+
s0 += "\n";
1945+
if l.starts_with("Message-ID:") && s1.is_empty() {
1946+
s1 = mem::take(&mut s0);
1947+
}
1948+
}
1949+
sent2.payload = s1 + &s0;
1950+
1951+
// Bob receives both messages, the edit request with "Chat-Edit" in IMF headers is
1952+
// received as text message.
1953+
let bob_msg = bob.recv_msg(&sent1).await;
1954+
assert_eq!(bob_msg.text, "foo");
1955+
assert_eq!(bob_msg.chat_id.get_msg_cnt(bob).await?, 1);
1956+
let bob_msg = bob.recv_msg(&sent2).await;
1957+
assert_eq!(bob_msg.text, constants::EDITED_PREFIX.to_string() + "bar");
1958+
assert_eq!(bob_msg.chat_id.get_msg_cnt(bob).await?, 2);
1959+
1960+
Ok(())
1961+
}

test-data/message/mail_with_user_and_group_avatars.eml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
Chat-Group-ID: WVnDtF5azch
22
Chat-Group-Name: =?utf-8?q?testgr1?=
3-
Chat-Group-Avatar: group-image.png
4-
Chat-User-Avatar: avatar.png
53
Subject: =?utf-8?q?Chat=3A_testgr1=3A_hi!_?=
64
Date: Thu, 12 Dec 2019 17:24:03 +0000
75
X-Mailer: Delta Chat Core 1.0.0-beta.15/CLI
@@ -14,6 +12,8 @@ Content-Type: multipart/mixed; boundary="LV8nfXkpyyn39fsVyoB1b29PKDMeb5"
1412

1513
--LV8nfXkpyyn39fsVyoB1b29PKDMeb5
1614
Content-Type: text/plain; charset=utf-8
15+
Chat-Group-Avatar: group-image.png
16+
Chat-User-Avatar: avatar.png
1717
1818
hi!
1919

test-data/message/mail_with_user_avatar.eml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Chat-User-Avatar: avatar.png
21
Subject: =?utf-8?q?Chat=3A_this_is_a_message_with_a_=2E=2E=2E?=
32
Message-ID: Mr.wOBwZNbBTVt.NZpmQDwWoNk@example.org
43
In-Reply-To: Mr.ETXqza5-WpB.zDEYOLECxAw@example.org
@@ -12,6 +11,7 @@ Content-Type: multipart/mixed; boundary="luTiGu6GBoVLCvTkzVtmZmwsmhkNMw"
1211

1312
--luTiGu6GBoVLCvTkzVtmZmwsmhkNMw
1413
Content-Type: text/plain; charset=utf-8
14+
Chat-User-Avatar: avatar.png
1515
1616
this is a message with a profile-image attached
1717
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
Content-Type: text/plain; charset=utf-8
2-
Chat-User-Avatar: 0
31
Subject: =?utf-8?q?Chat=3A_profile_image_deleted?=
42
Message-ID: Mr.tsgoJgn-cBf.0TkFWKJzeSp@example.org
53
Date: Sun, 08 Dec 2019 23:28:30 +0000
64
X-Mailer: Delta Chat Core 1.0.0-beta.12/CLI
75
Chat-Version: 1.0
86
To: <tunis3@example>
97
From: "=?utf-8?q??=" <tunis4@example.org>
8+
Content-Type: multipart/mixed; boundary="luTiGu6GBoVLCvTkzVtmZmwsmhkNMw"
9+
10+
11+
--luTiGu6GBoVLCvTkzVtmZmwsmhkNMw
12+
Content-Type: text/plain; charset=utf-8
13+
Chat-User-Avatar: 0
1014
1115
profile image deleted
1216
1317
--
1418
Sent with my Delta Chat Messenger: https://delta.chat
19+
20+
--luTiGu6GBoVLCvTkzVtmZmwsmhkNMw--

0 commit comments

Comments
 (0)