//! Mandatory: the hostile HTML corpus and the sanitiser's properties. use epistula_core::sanitize::{BLOCKED_IMAGE_SRC, SanitizeOptions, sanitize, text_to_html}; use proptest::prelude::*; fn clean(html: &str) -> String { sanitize(html, &SanitizeOptions::default()).html } /// Anything executable, anything that loads something, anything that navigates by itself. const FORBIDDEN: &[&str] = &[ "alert(1)", "", "ipt>alert(1)", "", "", "", "hi", "", "", "", "", "", "", "", "", "
", "", "", "", "", "
x
", "x", "", "", "
x
", "ok", "alert(1)", "", "", "x", "", "0", "x", ]; /// The output escapes `:` and `=` in text and attribute values (`:`, `=`); undo that so the checks /// see what a browser sees. fn decoded(s: &str) -> String { s.replace(":", ":").replace("=", "=") } /// Every tag in the output, with its attribute names and decoded values (a small scanner for the serialised /// form ammonia writes: double-quoted values, no raw `<` in text). fn tags(out: &str) -> Vec<(String, Vec<(String, String)>)> { let mut result = Vec::new(); let mut rest = out; while let Some(start) = rest.find('<') { rest = &rest[start + 1..]; let mut name = String::new(); let mut chars = rest.char_indices().peekable(); while let Some(&(_, c)) = chars.peek() { if c.is_whitespace() || c == '>' || c == '/' && !name.is_empty() { break; } name.push(c); chars.next(); } let mut attrs = Vec::new(); let mut end = rest.len(); loop { while let Some(&(_, c)) = chars.peek() { if c.is_whitespace() || c == '/' { chars.next(); } else { break; } } let Some(&(i, c)) = chars.peek() else { break }; if c == '>' { end = i + 1; break; } let mut attr = String::new(); while let Some(&(_, c)) = chars.peek() { if c == '=' || c == '>' || c.is_whitespace() { break; } attr.push(c); chars.next(); } let mut value = String::new(); if let Some(&(_, '=')) = chars.peek() { chars.next(); if let Some(&(_, '"')) = chars.peek() { chars.next(); for (_, c) in chars.by_ref() { if c == '"' { break; } value.push(c); } } } let value = decoded(&value).replace("&", "&").replace(""", "\""); attrs.push((attr.to_ascii_lowercase(), value)); } result.push((name.to_ascii_lowercase(), attrs)); rest = &rest[end.min(rest.len())..]; } result } /// What a browser would run or load: script tags, event handler attributes, script URLs. fn dangerous(out: &str) -> Option { for (name, attrs) in tags(out) { if name == "script" || name == "iframe" || name == "object" || name == "embed" || name == "svg" { return Some(format!("tag {name}")); } for (attr, value) in attrs { if attr.starts_with("on") { return Some(format!("attribute {attr}")); } let compact: String = value.chars().filter(|c| !c.is_whitespace() && !c.is_control()).collect(); let compact = compact.to_ascii_lowercase(); let url_attribute = matches!( attr.as_str(), "href" | "src" | "action" | "formaction" | "background" | "poster" | "data" | "srcset" | "lowsrc" | "dynsrc" | "cite" | "xlink:href" | "longdesc" | "usemap" | "codebase" ); if url_attribute && (compact.starts_with("javascript:") || compact.starts_with("vbscript:") || compact.starts_with("data:text")) { return Some(format!("url in {attr}")); } if attr == "style" && ["expression", "url(", "javascript", "@import", "behavior", "binding"] .iter() .any(|b| compact.contains(b)) { return Some("css".into()); } if url_attribute && attr != "href" && (compact.starts_with("http:") || compact.starts_with("https:")) && !compact.contains("cdn.example") { return Some(format!("remote load in {attr}")); } } } None } #[test] fn hostile_corpus_leaves_nothing_dangerous() { for input in CORPUS { let raw = clean(input); assert_eq!(dangerous(&raw), None, "input {input:?} output {raw:?}"); let out = decoded(&raw).to_ascii_lowercase(); for bad in FORBIDDEN { assert!(!out.contains(bad), "input {input:?}\noutput {out:?}\ncontains {bad:?}"); } } } #[test] fn remote_images_become_placeholders_and_are_counted() { let html = "

Hi

\"logo\""; let out = sanitize(html, &SanitizeOptions::default()); assert_eq!(out.blocked_images, 2); assert!(!out.html.contains("evil.example"), "{}", out.html); let placeholder = BLOCKED_IMAGE_SRC.replace(':', ":"); assert_eq!(out.html.matches(&placeholder).count(), 2, "{}", out.html); assert!(out.html.contains("alt=\"logo\"")); } #[test] fn load_images_keeps_remote_images_only() { let html = "j"; let out = sanitize(html, &SanitizeOptions { load_remote_images: true, ..Default::default() }); assert_eq!(out.blocked_images, 0); assert!(out.html.contains("https://cdn.example/a.png"), "{}", out.html); let lower = out.html.to_ascii_lowercase(); assert!(!lower.contains("javascript") && !lower.contains("onerror"), "{}", out.html); } #[test] fn ordinary_mail_formatting_survives() { let html = "
\

Bold it u s e

\
  • one
  1. two
quoted
\
cell
\ link mail
"; let out = clean(html); for kept in ["Bold", "it", "u", "