Skip to content

Commit

Permalink
refactor: use mail-builder instead of lettre_email
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Feb 18, 2025
1 parent 82ea4e2 commit 67f768f
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 853 deletions.
332 changes: 68 additions & 264 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ brotli = { version = "7", default-features=false, features = ["std"] }
bytes = "1"
chrono = { workspace = true, features = ["alloc", "clock", "std"] }
data-encoding = "2.7.0"
email = { git = "https://github.com/deltachat/rust-email", branch = "master" }
encoded-words = "0.2"
escaper = "0.1"
fast-socks5 = "0.10"
Expand All @@ -67,8 +66,8 @@ image = { version = "0.25.5", default-features=false, features = ["gif", "jpeg",
iroh-gossip = { version = "0.32", default-features = false, features = ["net"] }
iroh = { version = "0.32", default-features = false }
kamadak-exif = "0.6.1"
lettre_email = { git = "https://github.com/deltachat/lettre", branch = "master" }
libc = { workspace = true }
mail-builder = { git = "https://github.com/stalwartlabs/mail-builder", branch = "main", default-features = false }
mailparse = "0.16"
mime = "0.3.17"
num_cpus = "1.16"
Expand Down
13 changes: 1 addition & 12 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
[advisories]
ignore = [
"RUSTSEC-2020-0071",

# Timing attack on RSA.
# Delta Chat does not use RSA for new keys
# and this requires precise measurement of the decryption time by the attacker.
# There is no fix at the time of writing this (2023-11-28).
# <https://rustsec.org/advisories/RUSTSEC-2023-0071>
"RUSTSEC-2023-0071",

# Unmaintained encoding
"RUSTSEC-2021-0153",

# Unmaintained instant
"RUSTSEC-2024-0384",

Expand All @@ -32,16 +27,12 @@ skip = [
{ name = "core-foundation", version = "0.9.4" },
{ name = "event-listener", version = "2.5.3" },
{ name = "generator", version = "0.7.5" },
{ name = "getrandom", version = "<0.2" },
{ name = "http", version = "0.2.12" },
{ name = "loom", version = "0.5.6" },
{ name = "netlink-packet-route", version = "0.17.1" },
{ name = "nix", version = "0.26.4" },
{ name = "nix", version = "0.27.1" },
{ name = "quick-error", version = "<2.0" },
{ name = "rand_chacha", version = "<0.3" },
{ name = "rand_core", version = "<0.6" },
{ name = "rand", version = "<0.8" },
{ name = "redox_syscall", version = "0.3.5" },
{ name = "regex-automata", version = "0.1.10" },
{ name = "regex-syntax", version = "0.6.29" },
Expand All @@ -51,11 +42,9 @@ skip = [
{ name = "syn", version = "1.0.109" },
{ name = "thiserror-impl", version = "1.0.69" },
{ name = "thiserror", version = "1.0.69" },
{ name = "time", version = "<0.3" },
{ name = "tokio-tungstenite", version = "0.21.0" },
{ name = "tungstenite", version = "0.21.0" },
{ name = "unicode-width", version = "0.1.11" },
{ name = "wasi", version = "<0.11" },
{ name = "windows" },
{ name = "windows_aarch64_gnullvm" },
{ name = "windows_aarch64_msvc" },
Expand Down Expand Up @@ -102,5 +91,5 @@ license-files = [
[sources.allow-org]
# Organisations which we allow git sources from.
github = [
"deltachat",
"stalwartlabs",
]
15 changes: 10 additions & 5 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::cmp;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::io::Cursor;
use std::marker::Sync;
use std::path::{Path, PathBuf};
use std::str::FromStr;
Expand All @@ -11,6 +12,7 @@ use std::time::Duration;
use anyhow::{anyhow, bail, ensure, Context as _, Result};
use deltachat_contact_tools::{sanitize_bidi_characters, sanitize_single_line, ContactAddress};
use deltachat_derive::{FromSql, ToSql};
use mail_builder::mime::MimePart;
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use tokio::task;
Expand All @@ -32,7 +34,6 @@ use crate::debug_logging::maybe_set_logging_xdc;
use crate::download::DownloadState;
use crate::ephemeral::{start_chat_ephemeral_timers, Timer as EphemeralTimer};
use crate::events::EventType;
use crate::html::new_html_mimepart;
use crate::location;
use crate::log::LogExt;
use crate::message::{self, Message, MessageState, MsgId, Viewtype};
Expand Down Expand Up @@ -2157,14 +2158,18 @@ impl Chat {
} else {
None
};
let new_mime_headers = new_mime_headers.map(|s| new_html_mimepart(s).build().as_string());
let new_mime_headers: Option<String> = new_mime_headers.map(|s| {
let html_part = MimePart::new("text/html", s);
let mut buffer = Vec::new();
let cursor = Cursor::new(&mut buffer);
html_part.write_part(cursor).ok();
String::from_utf8_lossy(&buffer).to_string()
});
let new_mime_headers = new_mime_headers.or_else(|| match was_truncated {
// We need to add some headers so that they are stripped before formatting HTML by
// `MsgId::get_html()`, not a part of the actual text. Let's add "Content-Type", it's
// anyway a useful metadata about the stored text.
true => Some(
"Content-Type: text/plain; charset=utf-8\r\n\r\n".to_string() + &msg.text + "\r\n",
),
true => Some("Content-Type: text/plain; charset=utf-8\r\n\r\n".to_string() + &msg.text),
false => None,
});
let new_mime_headers = match new_mime_headers {
Expand Down
11 changes: 10 additions & 1 deletion src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,16 @@ pub async fn make_vcard(context: &Context, contacts: &[ContactId]) -> Result<Str
timestamp: Ok(now),
});
}
Ok(contact_tools::make_vcard(&vcard_contacts))

// XXX: newline at the end of vCard is trimmed
// for compatibility with core <=1.155.3
// Newer core should be able to deal with
// trailing CRLF as the fix
// <https://github.com/deltachat/deltachat-core-rust/pull/6522>
// is merged.
Ok(contact_tools::make_vcard(&vcard_contacts)
.trim_end()
.to_string())
}

/// Imports contacts from the given vCard.
Expand Down
23 changes: 13 additions & 10 deletions src/e2ee.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! End-to-end encryption support.
use std::io::Cursor;

use anyhow::{format_err, Context as _, Result};
use mail_builder::mime::MimePart;
use num_traits::FromPrimitive;

use crate::aheader::{Aheader, EncryptPreference};
Expand Down Expand Up @@ -95,7 +98,7 @@ impl EncryptHelper {
self,
context: &Context,
verified: bool,
mail_to_encrypt: lettre_email::PartBuilder,
mail_to_encrypt: MimePart<'static>,
peerstates: Vec<(Option<Peerstate>, String)>,
compress: bool,
) -> Result<String> {
Expand Down Expand Up @@ -136,7 +139,9 @@ impl EncryptHelper {

let sign_key = load_self_secret_key(context).await?;

let raw_message = mail_to_encrypt.build().as_string().into_bytes();
let mut raw_message = Vec::new();
let cursor = Cursor::new(&mut raw_message);
mail_to_encrypt.clone().write_part(cursor).ok();

let ctext = pgp::pk_encrypt(&raw_message, keyring, Some(sign_key), compress).await?;

Expand All @@ -145,15 +150,13 @@ impl EncryptHelper {

/// Signs the passed-in `mail` using the private key from `context`.
/// Returns the payload and the signature.
pub async fn sign(
self,
context: &Context,
mail: lettre_email::PartBuilder,
) -> Result<(lettre_email::MimeMessage, String)> {
pub async fn sign(self, context: &Context, mail: &MimePart<'static>) -> Result<String> {
let sign_key = load_self_secret_key(context).await?;
let mime_message = mail.build();
let signature = pgp::pk_calc_signature(mime_message.as_string().as_bytes(), &sign_key)?;
Ok((mime_message, signature))
let mut buffer = Vec::new();
let cursor = Cursor::new(&mut buffer);
mail.clone().write_part(cursor).ok();
let signature = pgp::pk_calc_signature(&buffer, &sign_key)?;
Ok(signature)
}
}

Expand Down
13 changes: 1 addition & 12 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use std::mem;

use anyhow::{Context as _, Result};
use base64::Engine as _;
use lettre_email::mime::Mime;
use lettre_email::PartBuilder;
use mailparse::ParsedContentType;
use mime::Mime;

use crate::context::Context;
use crate::headerdef::{HeaderDef, HeaderDefMap};
Expand Down Expand Up @@ -277,16 +276,6 @@ impl MsgId {
}
}

/// Wraps HTML text into a new text/html mimepart structure.
///
/// Used on forwarding messages to avoid leaking the original mime structure
/// and also to avoid sending too much, maybe large data.
pub fn new_html_mimepart(html: String) -> PartBuilder {
PartBuilder::new()
.content_type(&"text/html; charset=utf-8".parse::<mime::Mime>().unwrap())
.body(html)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 67f768f

Please sign in to comment.