Skip to main content

fetchium_core/http/
sanitize.rs

1//! HTML sanitization for safe terminal and file output (PRD §41).
2//!
3//! All extracted HTML must pass through `sanitize_html()` before display.
4//! Uses the `ammonia` crate (the same library used by crates.io itself).
5
6use ammonia::Builder;
7use std::collections::HashSet;
8
9/// Sanitize HTML for safe display.
10///
11/// Strips `<script>`, `<iframe>`, `<object>`, event handlers (`onclick`, etc.),
12/// and data URIs. Preserves semantic content tags.
13///
14/// # Examples
15/// ```rust
16/// use fetchium_core::http::sanitize::sanitize_html;
17/// let safe = sanitize_html(r#"<p>Hello</p><script>alert(1)</script>"#);
18/// assert!(safe.contains("Hello"));
19/// assert!(!safe.contains("script"));
20/// ```
21pub fn sanitize_html(html: &str) -> String {
22    Builder::default()
23        .tags(allowed_tags())
24        .clean(html)
25        .to_string()
26}
27
28/// Strip ALL HTML tags, returning plain text only.
29///
30/// # Examples
31/// ```rust
32/// use fetchium_core::http::sanitize::sanitize_to_text;
33/// let text = sanitize_to_text(r#"<p>Hello <strong>world</strong></p>"#);
34/// assert!(!text.contains('<'));
35/// assert!(text.contains("Hello"));
36/// ```
37pub fn sanitize_to_text(html: &str) -> String {
38    ammonia::clean_text(html)
39}
40
41fn allowed_tags() -> HashSet<&'static str> {
42    [
43        "p",
44        "h1",
45        "h2",
46        "h3",
47        "h4",
48        "h5",
49        "h6",
50        "a",
51        "code",
52        "pre",
53        "table",
54        "thead",
55        "tbody",
56        "tr",
57        "th",
58        "td",
59        "ul",
60        "ol",
61        "li",
62        "blockquote",
63        "em",
64        "strong",
65        "br",
66        "span",
67        "div",
68        "section",
69        "article",
70        "header",
71        "footer",
72        "aside",
73        "nav",
74    ]
75    .iter()
76    .copied()
77    .collect()
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn strips_script_tags() {
86        let input = r#"<p>Hello</p><script>alert('xss')</script><p>World</p>"#;
87        let output = sanitize_html(input);
88        assert!(!output.contains("script"), "script tag should be stripped");
89        assert!(
90            !output.contains("alert"),
91            "script content should be stripped"
92        );
93        assert!(output.contains("Hello"));
94        assert!(output.contains("World"));
95    }
96
97    #[test]
98    fn strips_event_handlers() {
99        let input = r#"<p onclick="evil()">Click me</p>"#;
100        let output = sanitize_html(input);
101        assert!(
102            !output.contains("onclick"),
103            "event handler should be stripped"
104        );
105        assert!(output.contains("Click me"));
106    }
107
108    #[test]
109    fn strips_iframes() {
110        let input = r#"<p>Text</p><iframe src="https://evil.com"></iframe>"#;
111        let output = sanitize_html(input);
112        assert!(!output.contains("iframe"), "iframe should be stripped");
113        assert!(output.contains("Text"));
114    }
115
116    #[test]
117    fn preserves_code_blocks() {
118        let input = r#"<pre><code>fn main() { println!("hello"); }</code></pre>"#;
119        let output = sanitize_html(input);
120        assert!(output.contains("fn main()"));
121        assert!(output.contains("<code>"));
122        assert!(output.contains("<pre>"));
123    }
124
125    #[test]
126    fn preserves_tables() {
127        let input = r#"<table><thead><tr><th>A</th></tr></thead><tbody><tr><td>1</td></tr></tbody></table>"#;
128        let output = sanitize_html(input);
129        assert!(output.contains("<table>"));
130        assert!(output.contains("<th>"));
131        assert!(output.contains("<td>"));
132    }
133
134    #[test]
135    fn sanitize_to_text_strips_all_tags() {
136        let input = r#"<p>Hello <strong>world</strong></p>"#;
137        let output = sanitize_to_text(input);
138        assert!(!output.contains('<'), "no HTML tags in plain text");
139        assert!(output.contains("Hello"));
140        assert!(output.contains("world"));
141    }
142
143    #[test]
144    fn sanitize_to_text_handles_empty() {
145        assert_eq!(sanitize_to_text(""), "");
146    }
147
148    #[test]
149    fn sanitize_html_handles_empty() {
150        assert_eq!(sanitize_html(""), "");
151    }
152}