use std::collections::HashSet;
const DEFAULT_MAX_HTML_BYTES: usize = 512 * 1024;
const DEFAULT_MAX_MD_CHARS: usize = 200_000;
const ALLOWED_TAGS: &[&str] = &[
"p", "br", "hr", "blockquote", "pre",
"h1", "h2", "h3", "h4", "h5", "h6",
"b", "strong", "i", "em", "code", "s", "del", "u", "small", "sub", "sup",
"ul", "ol", "li",
"a",
];
#[derive(Debug, Clone)]
pub struct Config {
pub max_html_bytes: usize,
pub max_md_chars: usize,
pub extra_tags: HashSet<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
max_html_bytes: DEFAULT_MAX_HTML_BYTES,
max_md_chars: DEFAULT_MAX_MD_CHARS,
extra_tags: HashSet::new(),
}
}
}
pub fn sanitize_html(html: &str) -> String {
sanitize_html_with(html, &Config::default())
}
pub fn sanitize_html_with(html: &str, config: &Config) -> String {
let html = &html[..html.len().min(config.max_html_bytes)];
let clean = clean_html(html, config);
let mut md = html2md::parse_html(&clean);
md.truncate(config.max_md_chars);
md
}
pub fn render_email(text_plain: Option<&str>, text_html: Option<&str>) -> String {
render_email_with(text_plain, text_html, &Config::default())
}
pub fn render_email_with(
text_plain: Option<&str>,
text_html: Option<&str>,
config: &Config,
) -> String {
if let Some(plain) = text_plain {
if !is_junk_plain(plain) {
return plain.to_string();
}
}
if let Some(html) = text_html {
return sanitize_html_with(html, config);
}
if let Some(plain) = text_plain {
return plain.to_string();
}
"[No displayable content]".to_string()
}
pub fn render_email_plain(text_plain: Option<&str>, text_html: Option<&str>) -> String {
if let Some(plain) = text_plain {
return plain.to_string();
}
if let Some(html) = text_html {
return html2text::from_read(html.as_bytes(), 80).unwrap_or_default();
}
"[No displayable content]".to_string()
}
pub fn is_junk_plain(s: &str) -> bool {
let t = s.trim();
t.is_empty() || t.len() < 40 || t.lines().count() <= 2
}
fn clean_html(html: &str, config: &Config) -> String {
let mut tags: HashSet<&str> = ALLOWED_TAGS.iter().copied().collect();
let extras: Vec<&str> = config.extra_tags.iter().map(|s| s.as_str()).collect();
tags.extend(extras);
ammonia::Builder::new().tags(tags).clean(html).to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strips_script_and_style() {
let html = r#"
<style>.foo { color: red; }</style>
<script>alert('xss')</script>
<p>Safe content</p>
"#;
let result = sanitize_html(html);
assert!(result.contains("Safe content"));
assert!(!result.contains("color: red"));
assert!(!result.contains("alert"));
}
#[test]
fn strips_tracking_pixels() {
let html = r#"<p>Real content</p><img src="https://track.example.com/open.gif" width="1" height="1">"#;
let result = sanitize_html(html);
assert!(result.contains("Real content"));
assert!(!result.contains("track.example.com"));
}
#[test]
fn strips_layout_tables() {
let html = r#"
<table><tr><td>
<p>Actual message</p>
</td></tr></table>
"#;
let result = sanitize_html(html);
assert!(result.contains("Actual message"));
assert!(!result.contains("|"));
}
#[test]
fn preserves_links() {
let html = r#"<p>Click <a href="https://example.com">here</a></p>"#;
let result = sanitize_html(html);
assert!(result.contains("https://example.com"));
assert!(result.contains("here"));
}
#[test]
fn preserves_formatting() {
let html = "<p>This is <strong>bold</strong> and <em>italic</em></p>";
let result = sanitize_html(html);
assert!(result.contains("**bold**") || result.contains("__bold__"));
assert!(result.contains("*italic*") || result.contains("_italic_"));
}
#[test]
fn preserves_lists() {
let html = "<ul><li>one</li><li>two</li><li>three</li></ul>";
let result = sanitize_html(html);
assert!(result.contains("one"));
assert!(result.contains("two"));
assert!(result.contains("three"));
}
#[test]
fn preserves_blockquotes() {
let html = "<blockquote><p>Quoted text</p></blockquote>";
let result = sanitize_html(html);
assert!(result.contains("Quoted text"));
}
#[test]
fn strips_forms() {
let html = r#"<form action="/phish"><input type="text"><button>Submit</button></form><p>Content</p>"#;
let result = sanitize_html(html);
assert!(result.contains("Content"));
assert!(!result.contains("<form"));
assert!(!result.contains("<input"));
}
#[test]
fn strips_iframes() {
let html = r#"<iframe src="https://evil.com"></iframe><p>Content</p>"#;
let result = sanitize_html(html);
assert!(result.contains("Content"));
assert!(!result.contains("evil.com"));
}
#[test]
fn truncates_huge_input() {
let huge = "<p>".to_string() + &"x".repeat(DEFAULT_MAX_HTML_BYTES + 1000) + "</p>";
let result = sanitize_html(&huge);
assert!(result.len() <= DEFAULT_MAX_MD_CHARS);
}
#[test]
fn empty_html_produces_empty_output() {
let result = sanitize_html("");
assert!(result.trim().is_empty());
}
#[test]
fn custom_config_extra_tags() {
let mut config = Config::default();
config.extra_tags.insert("img".to_string());
let html = r#"<p>Text</p><img alt="photo">"#;
let result = sanitize_html_with(html, &config);
assert!(result.contains("Text"));
}
#[test]
fn custom_config_smaller_limits() {
let config = Config {
max_html_bytes: 100,
max_md_chars: 50,
extra_tags: HashSet::new(),
};
let html = "<p>".to_string() + &"word ".repeat(200) + "</p>";
let result = sanitize_html_with(&html, &config);
assert!(result.len() <= 50);
}
#[test]
fn prefers_real_plain_text() {
let plain = "Hey,\n\nThis is a real email body with enough content to pass the junk filter.\n\nCheers";
let html = "<p>HTML version</p>";
let result = render_email(Some(plain), Some(html));
assert_eq!(result, plain);
}
#[test]
fn skips_junk_plain_for_html() {
let junk = "View online";
let html = "<p>This is the <strong>real</strong> email content right here.</p>";
let result = render_email(Some(junk), Some(html));
assert_ne!(result, junk);
assert!(result.contains("real"));
}
#[test]
fn shows_junk_plain_when_no_html() {
let junk = "View online";
let result = render_email(Some(junk), None);
assert_eq!(result, junk);
}
#[test]
fn no_content_fallback() {
assert_eq!(render_email(None, None), "[No displayable content]");
}
#[test]
fn html_only_sanitizes() {
let html = r#"<style>evil</style><p>Good <strong>content</strong></p><img src="tracker.gif">"#;
let result = render_email(None, Some(html));
assert!(result.contains("Good"));
assert!(result.contains("**content**") || result.contains("__content__"));
assert!(!result.contains("evil"));
assert!(!result.contains("tracker"));
}
#[test]
fn plain_text_output_prefers_plain() {
let result = render_email_plain(Some("Hello, world"), Some("<p>Hello, world</p>"));
assert_eq!(result, "Hello, world");
}
#[test]
fn plain_text_falls_back_to_html() {
let result = render_email_plain(None, Some("<p>Hello</p>"));
assert!(!result.is_empty());
assert!(result.contains("Hello"));
assert!(!result.contains("<p>"));
}
#[test]
fn plain_text_no_content() {
assert_eq!(render_email_plain(None, None), "[No displayable content]");
}
#[test]
fn empty_is_junk() {
assert!(is_junk_plain(""));
}
#[test]
fn whitespace_only_is_junk() {
assert!(is_junk_plain(" \n\t \n "));
}
#[test]
fn short_stub_is_junk() {
assert!(is_junk_plain("Click here to view"));
}
#[test]
fn real_content_not_junk() {
let real = "Hey,\n\nJust wanted to follow up on our conversation from yesterday.\n\nLet me know what you think.\n\nThanks";
assert!(!is_junk_plain(real));
}
}