1use pulldown_cmark::{html, Options, Parser};
19
20#[must_use]
28pub fn render(src: &str) -> String {
29 let mut opts = Options::empty();
30 opts.insert(Options::ENABLE_TABLES);
31 opts.insert(Options::ENABLE_STRIKETHROUGH);
32 opts.insert(Options::ENABLE_TASKLISTS);
33 opts.insert(Options::ENABLE_FOOTNOTES);
34 let parser = Parser::new_ext(src, opts);
35 let mut html_out = String::with_capacity(src.len() + src.len() / 4);
36 html::push_html(&mut html_out, parser);
37 base_sanitizer().clean(&html_out).to_string()
38}
39
40fn base_sanitizer() -> ammonia::Builder<'static> {
46 let mut b = ammonia::Builder::default();
47 b.add_tag_attributes("code", ["class"]);
48 b.add_tag_attributes("pre", ["class"]);
49 b
50}
51
52#[must_use]
63pub fn sanitize_html(src: &str) -> String {
64 let mut builder = base_sanitizer();
65 builder.add_tag_attributes("a", ["linktype", "id"]);
66 builder.clean(src).to_string()
67}
68
69pub fn register_tera_filter(tera: &mut tera::Tera) {
73 tera.register_filter("markdown", markdown_filter);
74}
75
76fn markdown_filter(
77 value: &tera::Value,
78 _args: &std::collections::HashMap<String, tera::Value>,
79) -> tera::Result<tera::Value> {
80 let Some(src) = value.as_str() else {
81 return Ok(value.clone());
82 };
83 if src.is_empty() {
84 return Ok(tera::Value::String(String::new()));
85 }
86 Ok(tera::Value::String(render(src)))
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn renders_basic_markdown() {
95 let html = render("# Hello\n\nA *world* of [links](https://example.com).");
96 assert!(html.contains("<h1>Hello</h1>"));
97 assert!(html.contains("<em>world</em>"));
98 assert!(html.contains(r#"<a href="https://example.com""#));
99 }
100
101 #[test]
102 fn strips_inline_script() {
103 let html = render("Hello <script>alert(1)</script> world");
104 assert!(!html.contains("<script>"));
105 assert!(!html.contains("alert"));
106 }
107
108 #[test]
109 fn strips_javascript_href() {
110 let html = render("[click](javascript:alert(1))");
111 assert!(!html.contains("javascript:"));
114 }
115
116 #[test]
117 fn supports_tables() {
118 let html = render("| a | b |\n|---|---|\n| 1 | 2 |");
119 assert!(html.contains("<table>"));
120 assert!(html.contains("<th>a</th>"));
121 }
122
123 #[test]
124 fn empty_input_empty_output() {
125 assert_eq!(render(""), "");
126 }
127
128 #[test]
132 fn sanitize_html_strips_script_tags() {
133 let out = sanitize_html("<p>Hi <script>alert(1)</script></p>");
134 assert!(!out.contains("<script>"), "got `{out}`");
135 assert!(!out.contains("alert(1)"), "got `{out}`");
136 assert!(out.contains("<p>"), "got `{out}`");
137 }
138
139 #[test]
140 fn sanitize_html_keeps_safe_richtext_shape() {
141 let out = sanitize_html(r#"<h2>Title</h2><p>Body <a href="/about">link</a></p>"#);
142 assert!(out.contains("<h2>Title</h2>"));
143 assert!(out.contains(r#"<a href="/about""#));
144 }
145
146 #[test]
147 fn sanitize_html_rejects_javascript_href() {
148 let out = sanitize_html(r#"<a href="javascript:alert(1)">x</a>"#);
149 assert!(!out.contains("javascript:"), "got `{out}`");
150 }
151
152 #[test]
153 fn sanitize_html_empty_input_empty_output() {
154 assert_eq!(sanitize_html(""), "");
155 }
156
157 #[test]
160 fn fenced_code_keeps_language_class() {
161 let html = render("```rust\nfn main() {}\n```");
162 assert!(
163 html.contains(r#"class="language-rust""#),
164 "language hint must survive the sanitizer; got `{html}`"
165 );
166 let escaped = render("```html\n<script>x</script>\n```");
168 assert!(!escaped.contains("<script>"), "got `{escaped}`");
169 assert!(escaped.contains("<script>"), "got `{escaped}`");
170 }
171
172 #[test]
173 fn sanitize_html_keeps_code_language_class() {
174 let out = sanitize_html(r#"<pre><code class="language-python">x = 1</code></pre>"#);
175 assert!(
176 out.contains(r#"class="language-python""#),
177 "richtext code class must survive; got `{out}`"
178 );
179 }
180
181 #[test]
182 fn sanitize_html_still_strips_event_handlers_on_code() {
183 let out = sanitize_html(r#"<code class="language-x" onclick="alert(1)">x</code>"#);
186 assert!(out.contains(r#"class="language-x""#), "got `{out}`");
187 assert!(!out.contains("onclick"), "got `{out}`");
188 }
189
190 #[test]
191 fn sanitize_html_keeps_mailto_and_anchor_links() {
192 let mail = sanitize_html(r#"<a href="mailto:hi@example.com">mail</a>"#);
196 assert!(
197 mail.contains(r#"href="mailto:hi@example.com""#),
198 "got `{mail}`"
199 );
200 let anchor = sanitize_html(r##"<a href="#section-2">jump</a>"##);
201 assert!(anchor.contains(r##"href="#section-2""##), "got `{anchor}`");
202 }
203
204 #[test]
205 fn sanitize_html_preserves_move_safe_linktype_anchors() {
206 let out = sanitize_html(r#"<a linktype="page" id="42">Team</a>"#);
209 assert!(out.contains(r#"linktype="page""#), "got `{out}`");
210 assert!(out.contains(r#"id="42""#), "got `{out}`");
211 let media = sanitize_html(r#"<a linktype="media" id="7">Doc</a>"#);
212 assert!(
213 media.contains(r#"linktype="media""#) && media.contains(r#"id="7""#),
214 "got `{media}`"
215 );
216 }
217
218 #[test]
219 fn sanitize_html_preserves_richtext_tables_and_images() {
220 let table = sanitize_html(
227 r#"<table style="min-width:50px"><colgroup><col style="min-width:25px"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>A</p></th></tr><tr><td colspan="1" rowspan="1"><p>1</p></td></tr></tbody></table>"#,
228 );
229 assert!(
230 table.contains("<table")
231 && table.contains("<th")
232 && table.contains("<td")
233 && table.contains(r#"colspan="1""#)
234 && table.contains("A")
235 && table.contains("1"),
236 "table structure stripped: `{table}`"
237 );
238 let img = sanitize_html(r#"<p><img src="/media/serve/9/x.png" alt="pic"></p>"#);
240 assert!(
241 img.contains(r#"src="/media/serve/9/x.png""#) && img.contains(r#"alt="pic""#),
242 "img stripped: `{img}`"
243 );
244 }
245
246 #[test]
247 fn sanitize_html_still_strips_dangerous_attrs_on_linktype_anchor() {
248 let out = sanitize_html(
250 r#"<a linktype="page" id="1" onclick="alert(1)" href="javascript:alert(2)">x</a>"#,
251 );
252 assert!(out.contains(r#"linktype="page""#), "got `{out}`");
253 assert!(!out.contains("onclick"), "got `{out}`");
254 assert!(!out.contains("javascript:"), "got `{out}`");
255 }
256}