Skip to content

Commit 3207129

Browse files
committed
feat: Add "From:" to protected headers for signed-only messages
1 parent 1d98c38 commit 3207129

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/mimefactory.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! # MIME message production.
22
3+
use std::collections::HashSet;
34
use std::convert::TryInto;
45

56
use anyhow::{bail, ensure, Context as _, Result};
@@ -517,6 +518,7 @@ impl<'a> MimeFactory<'a> {
517518
// <https://datatracker.ietf.org/doc/html/rfc5322#appendix-A.1.1>.
518519
let from_header = Header::new_with_value("From".into(), vec![from]).unwrap();
519520
headers.unprotected.push(from_header.clone());
521+
headers.protected.push(from_header);
520522

521523
if let Some(sender_displayname) = &self.sender_displayname {
522524
let sender =
@@ -704,8 +706,6 @@ impl<'a> MimeFactory<'a> {
704706
)
705707
};
706708
let outer_message = if is_encrypted {
707-
headers.protected.push(from_header);
708-
709709
// Store protected headers in the inner message.
710710
let message = headers
711711
.protected
@@ -800,12 +800,18 @@ impl<'a> MimeFactory<'a> {
800800
// Store protected headers in the outer message.
801801
let message = headers
802802
.protected
803-
.into_iter()
804-
.fold(message, |message, header| message.header(header));
803+
.iter()
804+
.fold(message, |message, header| message.header(header.clone()));
805805

806806
if self.should_skip_autocrypt()
807807
|| !context.get_config_bool(Config::SignUnencrypted).await?
808808
{
809+
let protected: HashSet<Header> = HashSet::from_iter(headers.protected.into_iter());
810+
for h in headers.unprotected.split_off(0) {
811+
if !protected.contains(&h) {
812+
headers.unprotected.push(h);
813+
}
814+
}
809815
message
810816
} else {
811817
let message = message.header(get_content_type_directives_header());
@@ -2216,6 +2222,7 @@ mod tests {
22162222

22172223
let part = payload.next().unwrap();
22182224
assert_eq!(part.match_indices("multipart/signed").count(), 1);
2225+
assert_eq!(part.match_indices("From:").count(), 1);
22192226
assert_eq!(part.match_indices("Subject:").count(), 0);
22202227
assert_eq!(part.match_indices("Autocrypt:").count(), 1);
22212228
assert_eq!(part.match_indices("Chat-User-Avatar:").count(), 0);
@@ -2226,12 +2233,14 @@ mod tests {
22262233
.count(),
22272234
1
22282235
);
2236+
assert_eq!(part.match_indices("From:").count(), 1);
22292237
assert_eq!(part.match_indices("Subject:").count(), 1);
22302238
assert_eq!(part.match_indices("Autocrypt:").count(), 0);
22312239
assert_eq!(part.match_indices("Chat-User-Avatar:").count(), 0);
22322240

22332241
let part = payload.next().unwrap();
22342242
assert_eq!(part.match_indices("text/plain").count(), 1);
2243+
assert_eq!(part.match_indices("From:").count(), 0);
22352244
assert_eq!(part.match_indices("Chat-User-Avatar:").count(), 1);
22362245
assert_eq!(part.match_indices("Subject:").count(), 0);
22372246

@@ -2258,12 +2267,14 @@ mod tests {
22582267

22592268
let part = payload.next().unwrap();
22602269
assert_eq!(part.match_indices("multipart/signed").count(), 1);
2270+
assert_eq!(part.match_indices("From:").count(), 1);
22612271
assert_eq!(part.match_indices("Subject:").count(), 0);
22622272
assert_eq!(part.match_indices("Autocrypt:").count(), 1);
22632273
assert_eq!(part.match_indices("Chat-User-Avatar:").count(), 0);
22642274

22652275
let part = payload.next().unwrap();
22662276
assert_eq!(part.match_indices("text/plain").count(), 1);
2277+
assert_eq!(part.match_indices("From:").count(), 1);
22672278
assert_eq!(part.match_indices("Subject:").count(), 1);
22682279
assert_eq!(part.match_indices("Autocrypt:").count(), 0);
22692280
assert_eq!(part.match_indices("multipart/mixed").count(), 0);
@@ -2272,6 +2283,7 @@ mod tests {
22722283
let body = payload.next().unwrap();
22732284
assert_eq!(body.match_indices("this is the text!").count(), 1);
22742285
assert_eq!(body.match_indices("text/plain").count(), 0);
2286+
assert_eq!(body.match_indices("From:").count(), 0);
22752287
assert_eq!(body.match_indices("Chat-User-Avatar:").count(), 0);
22762288
assert_eq!(body.match_indices("Subject:").count(), 0);
22772289

0 commit comments

Comments
 (0)