docs_core/sanitize.rs
1//! Sanitization for untrusted content (Wave 2).
2//!
3//! Design, recorded in the plan ledger (W2-2): untrusted markdown cannot
4//! declare islands. [`render_untrusted`] strips `island` fences
5//! *pre-render* - hydration is a trusted-author surface, a third-party
6//! document must not choose which bundles a page loads, and the island
7//! props live in a `<script type="application/json">` child that a
8//! sanitizer cannot value-constrain (an attribute-presence check would
9//! admit executable `<script data-island-props>`). Everything else that
10//! survives rendering must also survive the ammonia allowlist below.
11//!
12//! [`sanitize`] is the allowlist itself, exported for callers holding
13//! HTML from other estate sources. It extends ammonia's defaults with
14//! the MathML set `latex2mathml` emits, so build-time math (Wave 2,
15//! `render`) survives a sanitize pass unchanged.
16
17use crate::error::DocsError;
18use ammonia::Builder;
19use std::collections::{HashMap, HashSet};
20
21/// Sanitize HTML with the docs allowlist: ammonia's defaults (scripts,
22/// event handlers, styles, most attributes die) plus the MathML element
23/// set the crate's own math emission produces.
24#[must_use]
25pub fn sanitize(html: &str) -> String {
26 builder().clean(html).to_string()
27}
28
29/// Render *untrusted* Markdown: island fences are dropped before
30/// rendering, the HTML is then sanitized. Raw HTML from the source
31/// survives only where the allowlist says so - this is the boundary the
32/// trusted `render` deliberately does not have.
33///
34/// # Errors
35/// [`DocsError`] for malformed non-island content, per `render`.
36pub fn render_untrusted(markdown: &str) -> Result<String, DocsError> {
37 let html = crate::render::render_inner(markdown, "", false)?;
38 Ok(sanitize(&html))
39}
40
41/// The docs allowlist.
42fn builder() -> Builder<'static> {
43 let mut b = Builder::default();
44 // MathML: the element set latex2mathml 0.2 emits (plus the standard
45 // ones a future renderer version may reach). Token elements carry
46 // styling attributes; the root carries the display hint and namespace.
47 let token_attrs: HashSet<&str> = ["class", "mathvariant", "mathsize", "mathcolor"]
48 .into_iter()
49 .collect();
50 let math_attrs: HashSet<&str> = ["class", "display", "xmlns"].into_iter().collect();
51 let math_tags: HashSet<&str> = [
52 "mi",
53 "mn",
54 "mo",
55 "ms",
56 "mtext",
57 "mrow",
58 "mfrac",
59 "msqrt",
60 "mroot",
61 "msub",
62 "msup",
63 "msubsup",
64 "munder",
65 "mover",
66 "munderover",
67 "mtable",
68 "mtr",
69 "mtd",
70 "mspace",
71 "mstyle",
72 "mmultiscripts",
73 "mprescripts",
74 "mphantom",
75 "menclose",
76 "merror",
77 ]
78 .into_iter()
79 .collect();
80 b.add_tags(math_tags);
81 let mut attrs: HashMap<&str, HashSet<&str>> = HashMap::new();
82 for tag in [
83 "mi",
84 "mn",
85 "mo",
86 "ms",
87 "mtext",
88 "mrow",
89 "mfrac",
90 "msqrt",
91 "mroot",
92 "msub",
93 "msup",
94 "msubsup",
95 "munder",
96 "mover",
97 "munderover",
98 "mtable",
99 "mtr",
100 "mtd",
101 "mspace",
102 "mstyle",
103 "mmultiscripts",
104 "mprescripts",
105 "mphantom",
106 "menclose",
107 "merror",
108 ] {
109 attrs.insert(tag, token_attrs.clone());
110 }
111 b.add_tags(["math"]);
112 attrs.insert("math", math_attrs);
113 b.tag_attributes(attrs);
114 b
115}