39 lines
1.9 KiB
Python
39 lines
1.9 KiB
Python
"""HTML mail: formatting stays, everything active or loading from outside is removed."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app.mailpack import sanitize_html
|
|
|
|
|
|
def test_formatting_stays_and_active_content_goes():
|
|
cleaned = sanitize_html(
|
|
'<div style="color:#333;font-size:14px" onclick="steal()"><font color="red" size="3">Hallo</font>'
|
|
'<table bgcolor="#eee" width="100%"><tr><td align="center">x</td></tr></table><hr>'
|
|
'<script>alert(1)</script><iframe src="http://x"></iframe><form action="http://x"><input></form></div>'
|
|
)
|
|
assert 'style="color: #333; font-size: 14px"' in cleaned
|
|
assert '<font color="red" size="3">' in cleaned
|
|
assert 'bgcolor="#eee"' in cleaned and "<hr>" in cleaned
|
|
for forbidden in ("onclick", "script", "iframe", "<form", "<input", "alert"):
|
|
assert forbidden not in cleaned
|
|
|
|
|
|
def test_styles_cannot_load_or_position():
|
|
cleaned = sanitize_html(
|
|
'<p style="background:url(http://track/p.png);position:fixed;width:expression(alert(1));'
|
|
'color:red;behavior:url(x.htc);margin:4px">Text</p>'
|
|
)
|
|
assert "url" not in cleaned and "position" not in cleaned and "expression" not in cleaned and "behavior" not in cleaned
|
|
assert "color: red" in cleaned and "margin: 4px" in cleaned
|
|
|
|
|
|
def test_links_and_images_are_limited():
|
|
cleaned = sanitize_html(
|
|
'<a href="javascript:alert(1)">a</a><a href="https://ok.example/x">b</a><a href="data:text/html,x">c</a>'
|
|
'<img src="http://track/p.gif"><img src="data:image/png;base64,AAAA" alt="logo"><img src="data:image/svg+xml;base64,AAAA">'
|
|
)
|
|
assert "javascript" not in cleaned and "data:text" not in cleaned and "track" not in cleaned and "svg" not in cleaned
|
|
assert 'href="https://ok.example/x"' in cleaned
|
|
assert 'rel="noreferrer noopener"' in cleaned and 'target="_blank"' in cleaned
|
|
assert 'src="data:image/png;base64,AAAA"' in cleaned
|