oxedyne/fe2o3/fe2o3_mail/tests/message_view.rs
19.1 KiB, 68 runs
created by r1870400018:75211, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `MessageView`, the reader a viewer and the page share, and `DraftMessage` with attachments |
| 2 | //! read back through it (C1a, D-20261026-01). |
| 3 | |
| 4 | mod mime_corpus; |
| 5 | |
| 6 | use crate::mime_corpus::corpus; |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_mail::message::{ |
| 10 | headers, |
| 11 | DraftAttachment, |
| 12 | DraftMessage, |
| 13 | Mailbox, |
| 14 | Marked, |
| 15 | MessageView, |
| 16 | ParsedMessage, |
| 17 | Remote, |
| 18 | strip_html, |
| 19 | }; |
| 20 | use oxedyne_fe2o3_mail::search::{ |
| 21 | Day, |
| 22 | Limits, |
| 23 | Mail, |
| 24 | Matcher, |
| 25 | Mode, |
| 26 | Query, |
| 27 | Scope, |
| 28 | Term, |
| 29 | }; |
| 30 | use oxedyne_fe2o3_text::html_inert::inert_html; |
| 31 | |
| 32 | |
| 33 | const LOGO: &[u8] = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR-not-a-real-png"; |
| 34 | |
| 35 | fn related_with_logo() -> Vec<u8> { |
| 36 | let b64 = oxedyne_fe2o3_text::base64::encode(LOGO); |
| 37 | format!("From: Shop <shop@x.example>\r\nSubject: Logo\r\nContent-Type: multipart/related; boundary=\"R\"\r\n\r\n\ |
| 38 | --R\r\nContent-Type: multipart/alternative; boundary=\"A\"\r\n\r\n\ |
| 39 | --A\r\nContent-Type: text/plain\r\n\r\nplain version\r\n\ |
| 40 | --A\r\nContent-Type: text/html\r\n\r\n<p>Hi <img src=\"cid:Logo.1@x\"></p>\r\n--A--\r\n\ |
| 41 | --R\r\nContent-Type: image/png; name=\"logo.png\"\r\nContent-Transfer-Encoding: base64\r\n\ |
| 42 | Content-ID: <logo.1@x>\r\nContent-Disposition: inline\r\n\r\n{}\r\n--R--\r\n", b64).into_bytes() |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn an_embedded_logo_is_a_part_the_html_names_by_cid() { |
| 47 | let v = MessageView::parse(&related_with_logo()); |
| 48 | assert_eq!(v.text, "plain version"); |
| 49 | assert!(v.html.contains("cid:Logo.1@x"), "{:?}", v.html); |
| 50 | assert_eq!(v.parts.len(), 1, "{:?}", v.parts); |
| 51 | let p = &v.parts[0]; |
| 52 | assert_eq!((p.name.as_str(), p.media_type.as_str(), p.content_id.as_str(), p.inline), |
| 53 | ("logo.png", "image/png", "logo.1@x", true)); |
| 54 | assert_eq!(p.bytes, LOGO); |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn a_part_no_html_names_is_not_inline() { |
| 59 | let raw = b"Content-Type: multipart/mixed; boundary=\"M\"\r\n\r\n--M\r\nContent-Type: text/plain\r\n\r\nbody\r\n\ |
| 60 | --M\r\nContent-Type: image/png\r\nContent-ID: <a@b>\r\n\r\nxx\r\n--M--\r\n"; |
| 61 | let v = MessageView::parse(raw); |
| 62 | assert_eq!(v.html, ""); |
| 63 | assert_eq!(v.parts.len(), 1); |
| 64 | assert!(!v.parts[0].inline); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn a_plain_footer_beside_an_html_body_is_escaped_into_the_html() { |
| 69 | let raw = b"Content-Type: multipart/mixed; boundary=\"M\"\r\n\r\n--M\r\nContent-Type: text/html\r\n\r\n<b>body</b>\r\n\ |
| 70 | --M\r\nContent-Type: text/plain\r\n\r\nlist <footer>\r\n--M--\r\n"; |
| 71 | let v = MessageView::parse(raw); |
| 72 | assert!(v.html.starts_with("<b>body</b>"), "{:?}", v.html); |
| 73 | assert!(v.html.contains("list <footer>"), "{:?}", v.html); |
| 74 | assert_eq!(v.text, "body\n\nlist <footer>"); |
| 75 | } |
| 76 | |
| 77 | /// The view's text is the agent's text, and its parts are the agent's attachments, for every |
| 78 | /// message in the corpus. |
| 79 | #[test] |
| 80 | fn the_view_agrees_with_the_agents_reader_on_the_corpus() { |
| 81 | for (name, raw) in corpus() { |
| 82 | let v = MessageView::parse(&raw); |
| 83 | let m = ParsedMessage::parse(&raw); |
| 84 | assert_eq!(v.text, m.body, "{}", name); |
| 85 | let listed: Vec<String> = v.parts.iter().map(|p| p.name.clone()).collect(); |
| 86 | for a in &m.attachments { |
| 87 | assert!(listed.contains(a), "{}: {:?} not in {:?}", name, a, listed); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn a_draft_with_attachments_round_trips_through_the_reader() { |
| 94 | let d = DraftMessage { |
| 95 | from: String::from("me@x.example"), |
| 96 | to: vec![Mailbox { name: String::new(), addr: String::from("you@y.example") }], |
| 97 | subject: String::from("Files"), |
| 98 | body: String::from("See attached.\n"), |
| 99 | date: String::from("Mon, 1 Sep 2026 12:00:00 +0000"), |
| 100 | message_id: String::from("<f@x.example>"), |
| 101 | attachments: vec![ |
| 102 | DraftAttachment { name: String::from("report \"q3\".pdf"), media_type: String::from("application/pdf"), |
| 103 | bytes: (0..=255u8).cycle().take(1000).collect() }, |
| 104 | DraftAttachment { name: String::from("Grüße.txt"), media_type: String::new(), |
| 105 | bytes: b"hallo".to_vec() }, |
| 106 | ], |
| 107 | ..Default::default() |
| 108 | }; |
| 109 | let raw = d.build().expect("builds"); |
| 110 | assert_eq!(raw, d.build().expect("builds again"), "the same draft builds the same bytes"); |
| 111 | assert!(String::from_utf8_lossy(&raw).lines().all(|l| l.len() <= 78), "a line over 78"); |
| 112 | let v = MessageView::parse(&raw); |
| 113 | assert_eq!(v.subject, "Files"); |
| 114 | assert_eq!(v.text, "See attached."); |
| 115 | assert_eq!(v.parts.len(), 2, "{:?}", v.parts); |
| 116 | assert_eq!(v.parts[0].name, "report \"q3\".pdf"); |
| 117 | assert_eq!(v.parts[0].media_type, "application/pdf"); |
| 118 | assert_eq!(v.parts[0].bytes, d.attachments[0].bytes); |
| 119 | assert_eq!(v.parts[1].name, "Grüße.txt"); |
| 120 | assert_eq!(v.parts[1].media_type, "application/octet-stream"); |
| 121 | assert_eq!(v.parts[1].bytes, b"hallo"); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn a_tracking_pixel_is_gone_until_pictures_are_allowed() { |
| 126 | let raw = b"From: Shop <shop@x.example>\r\nSubject: Sale\r\nContent-Type: text/html\r\n\r\n\ |
| 127 | <p>Sale</p><img src=\"https://track.example/p.gif\" srcset=\"https://track.example/p2.gif 2x\">\ |
| 128 | <picture><source srcset=\"https://track.example/p.webp\"></picture>\r\n"; |
| 129 | let off = MessageView::parse(raw); |
| 130 | assert_eq!(off, MessageView::read(raw, Remote::Blocked)); |
| 131 | assert!(!off.html.contains("track.example"), "{:?}", off.html); |
| 132 | assert!(off.html.starts_with("<p>Sale</p><img>"), "{:?}", off.html); |
| 133 | let on = MessageView::read(raw, Remote::Allowed); |
| 134 | assert!(on.html.contains(r#"<img src="https://track.example/p.gif" srcset="https://track.example/p2.gif 2x">"#), "{:?}", on.html); |
| 135 | assert_eq!(on.text, off.text); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn a_percent_encoded_cid_reference_names_its_part() { |
| 140 | // RFC 2392 percent-encodes a Content-ID in a URL, so `<a@b>` is referred to as `cid:a%40b`; |
| 141 | // a prefix of a longer reference names nothing. |
| 142 | let raw = b"Content-Type: multipart/related; boundary=\"R\"\r\n\r\n\ |
| 143 | --R\r\nContent-Type: text/html\r\n\r\n<img src=\"cid:A%40B\"><img src=\"cid:x@y.long\">\r\n\ |
| 144 | --R\r\nContent-Type: image/png\r\nContent-ID: <a@b>\r\n\r\nxx\r\n\ |
| 145 | --R\r\nContent-Type: image/png\r\nContent-ID: <x@y>\r\n\r\nyy\r\n--R--\r\n"; |
| 146 | let v = MessageView::parse(raw); |
| 147 | assert_eq!(v.parts.len(), 2, "{:?}", v.parts); |
| 148 | assert_eq!((v.parts[0].content_id.as_str(), v.parts[0].inline), ("a@b", true)); |
| 149 | assert_eq!((v.parts[1].content_id.as_str(), v.parts[1].inline), ("x@y", false)); |
| 150 | } |
| 151 | |
| 152 | /// E6: a character whose lower case is another length (Ω, K, İ) before a style or script block |
| 153 | /// neither panics nor shifts the cut. |
| 154 | #[test] |
| 155 | fn strip_html_survives_a_case_fold_of_another_length() { |
| 156 | for c in ['\u{2126}', '\u{212A}', '\u{0130}'] { |
| 157 | for block in ["<style>p{}</style>", "<SCRIPT>x()</SCRIPT>"] { |
| 158 | let html = format!("{}{}after", c, block); |
| 159 | assert_eq!(strip_html(&html), format!("{}after", c), "{:?}", html); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /// E7: entities are decoded once, so an escaped entity is text, never markup made by a second |
| 165 | /// decoding. |
| 166 | #[test] |
| 167 | fn strip_html_decodes_entities_once() { |
| 168 | assert_eq!(strip_html("<p>&lt;script&gt;</p>"), "<script>\n"); |
| 169 | assert_eq!(strip_html("a <b> &amp; "c""), "a <b> & \"c\""); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // ── Marks (MAIL-qs-mark, cases M1-M8 of MAIL-qs/CASES.md) ─────────────── |
| 174 | |
| 175 | const TODAY: Day = Day { year: 2026, month: 11, day: 16 }; |
| 176 | const LIM: Limits = Limits { search_marks_max: 3, ..Limits::DEFAULT }; |
| 177 | const MARK: &str = "<mark class=\"o3-hit\" data-n=\""; |
| 178 | |
| 179 | fn mail(raw: &[u8]) -> Mail<'_> { |
| 180 | Mail { raw, seen: true, flagged: false, size: raw.len() as u64, day: Some(TODAY) } |
| 181 | } |
| 182 | |
| 183 | fn marked_by(raw: &[u8], q: &Query, lim: &Limits) -> Outcome<(MessageView, Marked)> { |
| 184 | let v = MessageView::parse(raw); |
| 185 | let m = res!(Matcher::new(q, lim, TODAY)); |
| 186 | let k = res!(v.marked(&m, &mail(raw), lim)); |
| 187 | Ok((v, k)) |
| 188 | } |
| 189 | |
| 190 | fn marked(raw: &[u8], q: &str) -> Outcome<(MessageView, Marked)> { |
| 191 | marked_by(raw, &res!(Query::parse(q, &LIM)), &LIM) |
| 192 | } |
| 193 | |
| 194 | fn plain(body: &str) -> Vec<u8> { |
| 195 | format!("From: Ada <ada@x.example>\r\nSubject: s\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n", body) |
| 196 | .into_bytes() |
| 197 | } |
| 198 | |
| 199 | fn html(body: &str) -> Vec<u8> { |
| 200 | format!("From: Ada <ada@x.example>\r\nSubject: s\r\nContent-Type: text/html; charset=utf-8\r\n\r\n{}\r\n", body) |
| 201 | .into_bytes() |
| 202 | } |
| 203 | |
| 204 | fn alt(text: &str, html: &str) -> Vec<u8> { |
| 205 | format!("From: Ada <ada@x.example>\r\nSubject: s\r\nContent-Type: multipart/alternative; boundary=\"A\"\r\n\r\n\ |
| 206 | --A\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n\ |
| 207 | --A\r\nContent-Type: text/html; charset=utf-8\r\n\r\n{}\r\n--A--\r\n", text, html).into_bytes() |
| 208 | } |
| 209 | |
| 210 | /// The marked texts of `v.text`, in order. |
| 211 | fn shown<'a>(v: &'a MessageView, k: &Marked) -> Vec<&'a str> { |
| 212 | k.text.iter().map(|s| &v.text[s.start..s.end]).collect() |
| 213 | } |
| 214 | |
| 215 | /// The HTML with every mark this unit makes taken out again. |
| 216 | fn unmarked(h: &str) -> String { |
| 217 | let mut out = String::new(); |
| 218 | let mut rest = h; |
| 219 | while let Some(a) = rest.find(MARK) { |
| 220 | out.push_str(&rest[..a]); |
| 221 | let after = &rest[a..]; |
| 222 | let b = after.find('>').map_or(after.len(), |b| b + 1); |
| 223 | rest = &after[b..]; |
| 224 | if let Some(c) = rest.find("</mark>") { |
| 225 | out.push_str(&rest[..c]); |
| 226 | rest = &rest[c + 7..]; |
| 227 | } |
| 228 | } |
| 229 | out.push_str(rest); |
| 230 | out |
| 231 | } |
| 232 | |
| 233 | /// What every marked view keeps: the marks taken out give the view's HTML, and the HTML read |
| 234 | /// again by `inert_html` is unchanged. |
| 235 | fn keeps(v: &MessageView, k: &Marked) { |
| 236 | assert_eq!(unmarked(&k.html), v.html); |
| 237 | assert_eq!(inert_html(&k.html, Remote::Blocked), k.html); |
| 238 | } |
| 239 | |
| 240 | /// M1: bidi controls and a decomposed accent before and inside the hit; the span is on the text |
| 241 | /// as shown, not on the matcher's cleaned text. |
| 242 | #[test] |
| 243 | fn m1_spans_are_on_the_text_as_shown() -> Outcome<()> { |
| 244 | let raw = plain("Re\u{301}sume\u{301}: \u{202E}\u{2066}cafe\u{301} and ca\u{2066}fe\u{301}."); |
| 245 | let (v, k) = res!(marked(&raw, "café")); |
| 246 | assert_eq!(shown(&v, &k), vec!["cafe\u{301}", "ca\u{2066}fe\u{301}"], "{:?}", k.text); |
| 247 | assert_eq!((k.count, k.html.as_str()), (2, "")); |
| 248 | Ok(()) |
| 249 | } |
| 250 | |
| 251 | /// M2: only visible text is marked, never a tag, attribute, title, stylesheet or comment. |
| 252 | #[test] |
| 253 | fn m2_only_visible_text_is_marked() -> Outcome<()> { |
| 254 | let raw = html("<html><head><title>umart</title><style>p{font-family:umart}</style></head><body>\ |
| 255 | <!-- umart --><p><a href=\"https://x.example/umart\">link</a> <img alt=\"umart\" src=\"cid:a\"> umart here</p>\ |
| 256 | <textarea>umart</textarea><svg><text>umart</text></svg></body></html>"); |
| 257 | let (v, k) = res!(marked(&raw, "umart")); |
| 258 | assert_eq!(k.html.matches("<mark").count(), 1, "{}", k.html); |
| 259 | assert!(k.html.contains("<mark class=\"o3-hit\" data-n=\"1\">umart</mark> here"), "{}", k.html); |
| 260 | assert_eq!(k.count, 1); |
| 261 | keeps(&v, &k); |
| 262 | Ok(()) |
| 263 | } |
| 264 | |
| 265 | /// M3: a hit through a character reference is marked within its text node; one across two nodes |
| 266 | /// is not, and the markup stays whole. |
| 267 | #[test] |
| 268 | fn m3_references_inside_a_node_tags_between_nodes() -> Outcome<()> { |
| 269 | let raw = alt("café and umart", "<p>café and <b>um</b>art</p>"); |
| 270 | let (v, k) = res!(marked(&raw, "café")); |
| 271 | assert!(k.html.contains("<mark class=\"o3-hit\" data-n=\"1\">café</mark> and"), "{}", k.html); |
| 272 | keeps(&v, &k); |
| 273 | let (v, k) = res!(marked(&raw, "umart")); |
| 274 | assert_eq!((k.html.matches("<mark").count(), k.count), (0, 0), "{}", k.html); |
| 275 | assert_eq!(shown(&v, &k), vec!["umart"]); |
| 276 | keeps(&v, &k); |
| 277 | Ok(()) |
| 278 | } |
| 279 | |
| 280 | /// M4: a hit that spells markup is wrapped as the escaped text it is; no element is made. |
| 281 | #[test] |
| 282 | fn m4_a_hit_that_spells_markup_stays_text() -> Outcome<()> { |
| 283 | let hit = "</mark><script>alert(1)</script>"; |
| 284 | let raw = html("<p></mark><script>alert(1)</script></p>"); |
| 285 | let q = Query { terms: Term::Text { scope: None, mode: Mode::Word, text: hit.to_string() }, ..Query::default() }; |
| 286 | let (v, k) = res!(marked_by(&raw, &q, &LIM)); |
| 287 | assert!(!k.html.contains("<script"), "{}", k.html); |
| 288 | assert_eq!((k.html.matches("<mark").count(), k.html.matches("</mark>").count()), (1, 1), "{}", k.html); |
| 289 | assert!(k.html.contains("data-n=\"1\"></mark><script>alert(1)</script></mark>"), "{}", k.html); |
| 290 | assert_eq!(shown(&v, &k), vec![hit]); |
| 291 | keeps(&v, &k); |
| 292 | Ok(()) |
| 293 | } |
| 294 | |
| 295 | /// M5: 20,000 hits in 5 MB of HTML: the marks stop at the bound, the rest are counted. |
| 296 | #[test] |
| 297 | fn m5_marks_are_bounded_the_rest_counted() -> Outcome<()> { |
| 298 | let block = format!("<p>{} umart {}</p>\n", "lorem ipsum ".repeat(11), "dolor sit ".repeat(11)); |
| 299 | let raw = html(&block.repeat(20_000)); |
| 300 | assert!(raw.len() > 5_000_000, "{}", raw.len()); |
| 301 | let lim = Limits { search_marks_max: 100, ..Limits::DEFAULT }; |
| 302 | let t = std::time::Instant::now(); |
| 303 | let (v, k) = res!(marked_by(&raw, &res!(Query::parse("umart", &lim)), &lim)); |
| 304 | println!("m5: {} bytes marked in {:?}", raw.len(), t.elapsed()); |
| 305 | assert_eq!((k.count, k.html.matches("<mark").count(), k.text.len()), (20_000, 100, 100)); |
| 306 | assert!(k.html.contains("data-n=\"100\">") && !k.html.contains("data-n=\"101\">")); |
| 307 | keeps(&v, &k); |
| 308 | Ok(()) |
| 309 | } |
| 310 | |
| 311 | /// M6: a hit only in a header marks nothing in the body and says where it was; a message |
| 312 | /// without HTML has none. |
| 313 | #[test] |
| 314 | fn m6_a_header_hit_is_elsewhere() -> Outcome<()> { |
| 315 | let (v, k) = res!(marked(&plain("nothing here"), "from:ada")); |
| 316 | assert_eq!((k.text.len(), k.count, k.html.as_str()), (0, 0, "")); |
| 317 | assert_eq!(k.elsewhere, vec![Scope::From]); |
| 318 | assert_eq!(v.html, ""); |
| 319 | Ok(()) |
| 320 | } |
| 321 | |
| 322 | /// M7: only the alternative on the road that decided the hit is marked; overlapping hits of two |
| 323 | /// leaves make one mark. |
| 324 | #[test] |
| 325 | fn m7_only_the_deciding_road_is_marked() -> Outcome<()> { |
| 326 | let raw = plain("alpha one beta two umart"); |
| 327 | let (v, k) = res!(marked(&raw, "(alpha zeta) OR beta")); |
| 328 | assert_eq!(shown(&v, &k), vec!["beta"]); |
| 329 | let (v, k) = res!(marked(&raw, "umart OR mart OR uma")); |
| 330 | assert_eq!(shown(&v, &k), vec!["umart"]); |
| 331 | assert_eq!(k.count, 1); |
| 332 | Ok(()) |
| 333 | } |
| 334 | |
| 335 | /// M8: a sender's own mark with the reader's class is not taken for a hit. |
| 336 | #[test] |
| 337 | fn m8_a_senders_mark_is_not_a_hit() -> Outcome<()> { |
| 338 | let raw = html("<p><mark class=\"o3-hit\" data-n=\"1\">fake</mark> umart</p>"); |
| 339 | let (_, k) = res!(marked(&raw, "umart")); |
| 340 | assert_eq!(k.html.matches("o3-hit").count(), 1, "{}", k.html); |
| 341 | assert!(k.html.contains("<mark class=\"o3-hit\" data-n=\"1\">umart</mark>"), "{}", k.html); |
| 342 | Ok(()) |
| 343 | } |
| 344 | |
| 345 | /// G7: what stays. A miss changes nothing, a plain ASCII mail is marked where it reads, and |
| 346 | /// across the corpus the marks come out again to the view's HTML. |
| 347 | #[test] |
| 348 | fn marks_leave_everything_else_as_it_was() -> Outcome<()> { |
| 349 | let raw = html("<p>a <b>b</b> & c</p>"); |
| 350 | let (v, k) = res!(marked(&raw, "zeta")); |
| 351 | assert_eq!((k.html.as_str(), k.count, k.text.len()), (v.html.as_str(), 0, 0)); |
| 352 | let (v, k) = res!(marked(&plain("one two one"), "one")); |
| 353 | assert_eq!(shown(&v, &k), vec!["one", "one"]); |
| 354 | assert_eq!(k.text.iter().map(|s| s.start).collect::<Vec<_>>(), vec![0, 8]); |
| 355 | for (name, raw) in corpus() { |
| 356 | for q in ["the", "body OR html", "test"] { |
| 357 | let (v, k) = res!(marked(&raw, q)); |
| 358 | assert_eq!(unmarked(&k.html), v.html, "{} {}", name, q); |
| 359 | assert!(k.html.matches("<mark").count() <= LIM.search_marks_max, "{} {}", name, q); |
| 360 | } |
| 361 | } |
| 362 | Ok(()) |
| 363 | } |
| 364 | |
| 365 | fn mb(name: &str, addr: &str) -> Mailbox { |
| 366 | Mailbox { name: name.to_string(), addr: addr.to_string() } |
| 367 | } |
| 368 | |
| 369 | /// A draft whose header value or address would write a line of its own, as a decoded Reply-To |
| 370 | /// holding CR LF did, becoming a `Bcc` line and a second `RCPT TO` (BC9-send F7). The first list |
| 371 | /// is refused; the second is a name or subject, always quoted or encoded, so built inert. |
| 372 | fn hostile_drafts() -> (DraftMessage, Vec<(&'static str, DraftMessage)>, Vec<(&'static str, DraftMessage)>) { |
| 373 | let s = String::from; |
| 374 | let ok = DraftMessage { |
| 375 | from: s("me@x.example"), |
| 376 | to: vec![mb("Ada", "ada@x.example")], |
| 377 | cc: vec![mb("", "b@y.example")], |
| 378 | subject: s("Re: Lunch"), |
| 379 | body: s("Yes.\n"), |
| 380 | in_reply_to: s("<m3@x.example>"), |
| 381 | references: s("<r1@x.example> <m3@x.example>"), |
| 382 | date: s("Mon, 1 Sep 2026 12:00:00 +0000"), |
| 383 | message_id: s("<r@x.example>"), |
| 384 | ..Default::default() |
| 385 | }; |
| 386 | let bad = vec![ |
| 387 | ("CR LF in an address", DraftMessage { to: vec![mb("", "a@x.example\r\nBcc: evil@y.example")], ..ok.clone() }), |
| 388 | ("a second address after one", DraftMessage { to: vec![mb("A", "a@x.example>, evil@y.example")], ..ok.clone() }), |
| 389 | ("a bracket inside an address", DraftMessage { cc: vec![mb("x", "a>,b@evil.example")], ..ok.clone() }), |
| 390 | ("an address with no @", DraftMessage { to: vec![mb("", "nobody")], ..ok.clone() }), |
| 391 | ("a space in an address", DraftMessage { to: vec![mb("", "a b@x.example")], ..ok.clone() }), |
| 392 | ("a name in an address", DraftMessage { to: vec![mb("", "Ada <ada@x.example>")], ..ok.clone() }), |
| 393 | ("a malformed sender", DraftMessage { from: s("me@x.example>"), ..ok.clone() }), |
| 394 | ("LF in In-Reply-To", DraftMessage { in_reply_to: s("<m3@x>\nBcc: evil@y.example"), ..ok.clone() }), |
| 395 | ("CR in References", DraftMessage { references: s("<r1@x>\rBcc: evil@y.example"), ..ok.clone() }), |
| 396 | ("CR LF in the Message-ID", DraftMessage { message_id: s("<r@x>\r\nBcc: evil@y.example"), ..ok.clone() }), |
| 397 | ]; |
| 398 | let inert = vec![ |
| 399 | ("CR LF in a name", DraftMessage { to: vec![mb("Ada\r\nBcc: e@y.example", "ada@x.example")], ..ok.clone() }), |
| 400 | ("CR LF in the sender's name", DraftMessage { from_name: s("Me\r\nBcc: evil@y.example"), ..ok.clone() }), |
| 401 | ("LF in the subject", DraftMessage { subject: s("Hi\nBcc: evil@y.example"), ..ok.clone() }), |
| 402 | ]; |
| 403 | (ok, bad, inert) |
| 404 | } |
| 405 | |
| 406 | #[test] |
| 407 | fn a_header_or_address_that_would_make_a_line_of_its_own_is_refused() { |
| 408 | let (ok, bad, inert) = hostile_drafts(); |
| 409 | let raw = ok.build().expect("an ordinary reply builds"); |
| 410 | assert!(String::from_utf8_lossy(&raw).contains("To: Ada <ada@x.example>\r\nCc: b@y.example\r\n")); |
| 411 | for (what, d) in bad { |
| 412 | match d.build() { |
| 413 | Ok(raw) => panic!("{}: built {:?}", what, String::from_utf8_lossy(&raw)), |
| 414 | Err(e) => assert!(e.tags().contains(&ErrTag::Invalid) && e.tags().contains(&ErrTag::Input), |
| 415 | "{}: {:?}", what, e.tags()), |
| 416 | } |
| 417 | } |
| 418 | for (what, d) in inert { |
| 419 | let raw = d.build().expect("a name or subject is encoded"); |
| 420 | let names: Vec<String> = headers(&raw).into_iter().map(|(n, _)| n).collect(); |
| 421 | let want: Vec<String> = headers(&ok.build().expect("builds")).into_iter().map(|(n, _)| n).collect(); |
| 422 | assert_eq!(names, want, "{}: {:?}", what, String::from_utf8_lossy(&raw)); |
| 423 | assert_eq!(d.recipients().expect("the same envelope"), vec!["ada@x.example", "b@y.example"], "{}", what); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn the_envelope_names_exactly_the_addresses_the_headers_do() { |
| 429 | let (ok, bad, _) = hostile_drafts(); |
| 430 | assert_eq!(ok.recipients().expect("plain addresses"), vec!["ada@x.example", "b@y.example"]); |
| 431 | for (what, d) in bad.into_iter().filter(|(_, d)| d.to != ok.to || d.cc != ok.cc) { |
| 432 | assert!(d.recipients().is_err(), "{}: an envelope of {:?}", what, d.recipients()); |
| 433 | } |
| 434 | } |