feat: mimeparser: Omit Legacy Display Elements (#7130)

Omit Legacy Display Elements from "text/plain" and "text/html" (implement 4.5.3.{2,3} of
https://www.rfc-editor.org/rfc/rfc9788 "Header Protection for Cryptographically Protected Email").
This commit is contained in:
iequidoo
2025-10-28 04:48:15 -03:00
committed by iequidoo
parent 966ea28f83
commit e2ae6ae013
7 changed files with 140 additions and 18 deletions

View File

@@ -1323,6 +1323,10 @@ impl MimeMessage {
let is_html = mime_type == mime::TEXT_HTML;
if is_html {
self.is_mime_modified = true;
// NB: This unconditionally removes Legacy Display Elements (see
// <https://www.rfc-editor.org/rfc/rfc9788.html#section-4.5.3.3>). We
// don't check for the "hp-legacy-display" Content-Type parameter
// for simplicity.
if let Some(text) = dehtml(&decoded_data) {
text
} else {
@@ -1350,16 +1354,30 @@ impl MimeMessage {
let (simplified_txt, simplified_quote) = if mime_type.type_() == mime::TEXT
&& mime_type.subtype() == mime::PLAIN
&& is_format_flowed
{
let delsp = if let Some(delsp) = mail.ctype.params.get("delsp") {
delsp.as_str().eq_ignore_ascii_case("yes")
} else {
false
// Don't check that we're inside an encrypted or signed part for
// simplicity.
let simplified_txt = match mail
.ctype
.params
.get("hp-legacy-display")
.is_some_and(|v| v == "1")
{
false => simplified_txt,
true => rm_legacy_display_elements(&simplified_txt),
};
let unflowed_text = unformat_flowed(&simplified_txt, delsp);
let unflowed_quote = top_quote.map(|q| unformat_flowed(&q, delsp));
(unflowed_text, unflowed_quote)
if is_format_flowed {
let delsp = if let Some(delsp) = mail.ctype.params.get("delsp") {
delsp.as_str().eq_ignore_ascii_case("yes")
} else {
false
};
let unflowed_text = unformat_flowed(&simplified_txt, delsp);
let unflowed_quote = top_quote.map(|q| unformat_flowed(&q, delsp));
(unflowed_text, unflowed_quote)
} else {
(simplified_txt, top_quote)
}
} else {
(simplified_txt, top_quote)
};
@@ -1981,6 +1999,20 @@ impl MimeMessage {
}
}
fn rm_legacy_display_elements(text: &str) -> String {
let mut res = None;
for l in text.lines() {
res = res.map(|r: String| match r.is_empty() {
true => l.to_string(),
false => r + "\r\n" + l,
});
if l.is_empty() {
res = Some(String::new());
}
}
res.unwrap_or_default()
}
fn remove_header(
headers: &mut HashMap<String, String>,
key: &str,