MHT: preserve quoted MIME parameter boundaries (#5604)

Lands #5604.

Co-Authored-By: MMexicann <44814932+MMexicann@users.noreply.github.com>
This commit is contained in:
Martin Ball
2026-11-05 23:35:12 +01:00
committed by GitHub
co-authored by MMexicann
parent 83bef65a1d
commit ec3e14f98b
2 changed files with 49 additions and 5 deletions
+3 -1
View File
File diff suppressed because one or more lines are too long
+46 -4
View File
@@ -213,10 +213,32 @@ fn header<'a>(h: &'a [(String, String)], name: &str) -> Option<&'a str> {
/// A `; name=value` parameter of a header value (quotes removed), name case-insensitive.
fn param<'a>(value: &'a str, name: &str) -> Option<&'a str> {
value.split(';').skip(1).find_map(|p| {
let (k, v) = p.split_once('=')?;
k.trim().eq_ignore_ascii_case(name).then(|| v.trim().trim_matches('"'))
})
// Split outside quoted strings and comments (RFC 2045 §5.1, RFC 822 §3.3).
let mut quoted = false;
let mut escaped = false;
let mut comment_depth = 0usize;
value
.split(|c| {
if escaped {
escaped = false;
} else if (quoted || comment_depth > 0) && c == '\\' {
escaped = true;
} else if !quoted && c == '(' {
comment_depth = comment_depth.saturating_add(1);
} else if comment_depth > 0 {
if c == ')' {
comment_depth -= 1;
}
} else if c == '"' {
quoted = !quoted;
}
c == ';' && !quoted && comment_depth == 0
})
.skip(1)
.find_map(|p| {
let (k, v) = p.split_once('=')?;
k.trim().eq_ignore_ascii_case(name).then(|| v.trim().trim_matches('"'))
})
}
/// Gather the leaf parts of a (possibly multipart) entity.
@@ -400,6 +422,26 @@ mod tests {
}
}
#[test]
fn quoted_parameters_do_not_supply_a_false_boundary() {
for name in [r#"name="notes; boundary=wrong""#, r#"name="notes\"; boundary=wrong""#, r#"name="notes (10\" screen; boundary=wrong""#] {
let page = "Content-Type: text/html\r\n\r\n<p>Kept content</p>";
let message = message(page).replace("multipart/related; boundary=", &format!("multipart/related; {name}; boundary="));
let doc = import(message.as_bytes());
assert_eq!(doc.plain_text(wordcraft_doc::StoryRef::Body).trim(), "Kept content", "{name}");
}
}
#[test]
fn quotes_in_comments_do_not_hide_the_boundary() {
for comment in [r#"(10" screen)"#, r#"(outer (10" screen); boundary=wrong)"#, r#"(10\)" screen; boundary=wrong)"#] {
let page = "Content-Type: text/html\r\n\r\n<p>Kept content</p>";
let message = message(page).replace("multipart/related; boundary=", &format!("multipart/related {comment}; boundary="));
let doc = import(message.as_bytes());
assert_eq!(doc.plain_text(wordcraft_doc::StoryRef::Body).trim(), "Kept content", "{comment}");
}
}
#[test]
fn the_part_charset_decides_the_text() {
let page = "Content-Type: text/html; charset=windows-1250\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n<p>=E8lanak</p>";