Something went wrong. Try again.
Mail user agent backed by notmuch.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109//! The script the sanitizer injects into the document it produces.//!//! `sanitize_html` neutralises: an `a` loses its `href` (kept as `data-href`)//! and an `img` loses its `src` (kept as `data-original-src`, `src` becomes a//! dead `data:,` placeholder). Neutered alone, the placeholders promise an//! interaction they cannot keep — this is the other half of that promise: the//! document's own scripts are removed, and this one is put in their place.
/// The script: one delegated click listener. A placeholder link opens a/// confirm dialog and only then navigates; a placeholder image does the same/// before its original `src` is restored. Nothing is fetched before the user/// has said yes, so `specs/ui.md` "no HTTP traffic to remote hosts" holds for/// an unconfirmed document. It only ever reaches URLs that are already in the/// sanitized markup.const SCRIPT: &str = r#"<script>document.addEventListener("click", function (e) { var t = e.target; if (!t || !t.closest) return; var a = t.closest("a[data-href]"); if (a) { e.preventDefault(); var href = a.getAttribute("data-href"); if (window.confirm("Open this link?\n" + href)) { var w = window.open(href, "_blank"); if (w) { try { w.opener = null; } catch (err) {} } else { window.location.href = href; } } return; } var img = t.closest("img[data-placeholder]"); if (!img) return; var src = img.getAttribute("data-original-src"); if (!src) return; if (window.confirm("Load this image?\n" + src)) { img.setAttribute("src", src); img.removeAttribute("data-placeholder"); img.removeAttribute("data-original-src"); img.removeAttribute("style"); img.removeAttribute("title"); }});</script>"#;
/// Place `SCRIPT` where the document's own scripts used to be: before/// `</body>`, else before `</html>` (a fragment), else at the end. The/// rewriter runs before this, so the sanitizer's own `<script>` removal can/// never eat it.pub fn inject(doc: &str) -> String { // Case folding of ASCII only, so every byte offset still lines up. let lowered = doc.to_ascii_lowercase(); let at = ["</body>", "</html>"] .iter() .filter_map(|tag| lowered.find(tag)) .min(); let mut out = String::with_capacity(doc.len() + SCRIPT.len()); if let Some(at) = at { out.push_str(&doc[..at]); } else { out.push_str(doc); } out.push_str(SCRIPT); if let Some(at) = at { out.push_str(&doc[at..]); } out}
#[cfg(test)]mod tests { use super::*;
#[test] fn the_document_keeps_its_own_scripts_out() { let doc = inject("<html><body><p>hi</p></body></html>"); assert_eq!(doc.matches("<script").count(), 1, "{doc}"); assert!(doc.contains("data-href"), "the link half: {doc}"); assert!(doc.contains("data-placeholder"), "the image half: {doc}"); }
#[test] fn the_script_lands_before_the_body_closes() { let doc = inject("<body><p>hi</p></body>"); assert!(doc.contains("</p><script>"), "{doc}"); assert!(doc.ends_with("</body>"), "the document still closes: {doc}"); }
#[test] fn a_fragment_without_a_body_still_gets_the_script() { let doc = inject("<p>hi</p>"); assert!(doc.starts_with("<p>hi</p><script>"), "{doc}"); }
#[test] fn a_uppercase_body_tag_is_found_too() { let doc = inject("<BODY><p>hi</p></BODY>"); assert!(doc.contains("<p>hi</p><script>"), "{doc}"); }
#[test] fn the_script_only_reaches_urls_already_in_the_document() { // The one URL source it has is the attribute of the element that was // clicked; there is no fetch, no eval, no template of its own. assert!(!SCRIPT.contains("fetch("), "{SCRIPT}"); assert!(!SCRIPT.contains("eval("), "{SCRIPT}"); assert!(!SCRIPT.contains("XMLHttpRequest"), "{SCRIPT}"); }}