fix content-type setting and some encryption

This commit is contained in:
dignifiedquire
2019-12-02 00:01:37 +01:00
parent d5287256e0
commit e985887739
8 changed files with 71 additions and 37 deletions

View File

@@ -78,7 +78,6 @@ impl Aheader {
Ok(header) => {
info!(context, "comparing {} - {}", header.addr, wanted_from);
if addr_cmp(&header.addr, wanted_from) {
info!(context, "found header {:?}", header);
return Some(header);
}
}

View File

@@ -372,7 +372,7 @@ fn add_parts(
// handshake messages must be processed _before_ chats are created
// (eg. contacs may be marked as verified)
if let Some(_) = mime_parser.lookup_field("Secure-Join") {
if mime_parser.lookup_field("Secure-Join").is_some() {
// avoid discarding by show_emails setting
msgrmsg = 1;
*chat_id = 0;

View File

@@ -2,6 +2,7 @@
use std::collections::HashSet;
use mailparse::MailHeaderMap;
use num_traits::FromPrimitive;
use crate::aheader::*;
@@ -121,11 +122,7 @@ pub fn try_decrypt(
mail: &mailparse::ParsedMail<'_>,
message_time: i64,
) -> Result<(Option<Vec<u8>>, HashSet<String>)> {
use mailparse::MailHeaderMap;
info!(context, "trying to decrypt: {:?}", mail.get_body());
for part in &mail.subparts {
info!(context, "trying to decrypt part: {:?}", part.get_body());
}
info!(context, "trying to decrypt");
let from = mail
.headers
@@ -137,7 +134,6 @@ pub fn try_decrypt(
let mut peerstate = None;
let autocryptheader = Aheader::from_headers(context, &from, &mail.headers);
info!(context, "got autocryptheader {:?}", &autocryptheader);
if message_time > 0 {
peerstate = Peerstate::from_addr(context, &context.sql, &from);

View File

@@ -1,4 +1,5 @@
use failure::Fail;
use lettre_email::mime;
#[derive(Debug, Fail)]
pub enum Error {

View File

@@ -1,5 +1,5 @@
use chrono::TimeZone;
use lettre_email::{Address, Header, MimeMultipartType, PartBuilder};
use lettre_email::{mime, Address, Header, MimeMultipartType, PartBuilder};
use crate::chat::{self, Chat};
use crate::config::Config;
@@ -497,15 +497,31 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
unprotected_headers.push(Header::new_with_value("From".into(), vec![from]).unwrap());
let outer_message = if is_encrypted {
// Store protected headers in the inner message.
for header in protected_headers.into_iter() {
message = message.header(header);
}
// Set the appropriate Content-Type for the inner message.
let mut existing_ct = message
.get_header("Content-Type".to_string())
.and_then(|h| h.get_value::<String>().ok())
.unwrap_or_else(|| "text/plain; charset=utf-8;".to_string());
let mut outer_message = PartBuilder::new().header((
if !existing_ct.ends_with(';') {
existing_ct += ";";
}
message = message.replace_header(Header::new(
"Content-Type".to_string(),
"multipart/encrypted; protocol=\"application/pgp-encrypted\"".to_string(),
format!("{} protected-headers=\"v1\";", existing_ct),
));
// Set the appropriate Content-Type for the outer message
let mut outer_message = PartBuilder::new().header((
"Content-Type".to_string(),
"multipart/encrypted; protocol=\"application/pgp-encrypted\";".to_string(),
));
// Store the unprotected headers on the outer message.
for header in unprotected_headers.into_iter() {
outer_message = outer_message.header(header);
}
@@ -531,7 +547,7 @@ impl<'a, 'b> MimeFactory<'a, 'b> {
.unwrap(),
)
.header(("Content-Description", "OpenPGP encrypted message"))
.header(("Content-Disposition", "inline; filename=\"encrypted.asc\""))
.header(("Content-Disposition", "inline; filename=\"encrypted.asc\";"))
.body(encrypted)
.build(),
)

View File

@@ -130,7 +130,10 @@ impl<'a> MimeParser<'a> {
if let Some(raw) = raw {
mail_raw = raw;
info!(context, "decrypted: {:?}", std::str::from_utf8(&mail_raw));
let decrypted_mail = mailparse::parse_mail(&mail_raw)?;
// Decrypted the mail
decrypted_mail
} else {
@@ -151,12 +154,13 @@ impl<'a> MimeParser<'a> {
}
};
parser.parse_mime_recursive(&mail)?;
// Handle gossip headers
let gossip_headers = mail.headers.get_all_values("Autocrypt-Gossip")?;
parser.gossipped_addr =
update_gossip_peerstates(context, message_time, &mail, gossip_headers)?;
parser.parse_mime_recursive(&mail)?;
parser.parse_headers()?;
Ok(parser)
@@ -372,7 +376,10 @@ impl<'a> MimeParser<'a> {
fn parse_mime_recursive(&mut self, mail: &mailparse::ParsedMail<'_>) -> Result<bool> {
info!(self.context, "parse mime_recursive {:?}", mail.ctype);
if mail.ctype.params.get("protected-headers").is_some() {
info!(self.context, "found protected headers");
if mail.ctype.mimetype == "text/rfc822-headers" {
info!(
self.context,
@@ -381,9 +388,11 @@ impl<'a> MimeParser<'a> {
return Ok(false);
}
if !self.parsed_protected_headers {
if self.parsed_protected_headers {
warn!(self.context, "Ignoring nested protected headers");
} else {
self.hash_header(&mail.headers);
self.parsed_protected_headers;
self.parsed_protected_headers = true;
}
}
@@ -884,7 +893,8 @@ fn update_gossip_peerstates(
info!(
context,
"Updating gossip peerstates: {:#?}", &gossip_headers
"Updating gossip peerstates: {}",
gossip_headers.len()
);
for value in &gossip_headers {
let gossip_header = value.parse::<Aheader>();
@@ -895,6 +905,7 @@ fn update_gossip_peerstates(
recipients = Some(get_recipients(mail.headers.iter().filter_map(|v| {
let key = v.get_key();
let value = v.get_value();
info!(context, "header: {:?} - {:?}", key, value);
if key.is_err() || value.is_err() {
return None;
}
@@ -1143,6 +1154,19 @@ mod tests {
assert_eq!(mimeparser.get_rfc724_mid(), None);
}
#[test]
fn test_mailparse_content_type() {
let ctype =
mailparse::parse_content_type("text/plain; charset=utf-8; protected-headers=v1;");
assert_eq!(ctype.mimetype, "text/plain");
assert_eq!(ctype.charset, "utf-8");
assert_eq!(
ctype.params.get("protected-headers"),
Some(&"v1".to_string())
);
}
#[test]
fn test_mimeparser_with_context() {
let context = dummy_context();