Files
tsumugu/backend/tests/test_mail_html.py
T
StommerandClaude Sonnet 5.5 a3f328832e
Deploy Development / deploy (push) Successful in 41s
Test Suite / pytest-backend (push) Successful in 5m32s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 21s
Lade Bilder aus E-Mails auf Wunsch über den Server nach und verkleinere den Kopf der Leseansicht auf dem Handy.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 16:47:24 +02:00

82 lines
3.8 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
def test_remote_pictures_stay_out_unless_the_server_loads_them():
source = '<img src="https://cdn.example/a.png" alt="a"><img src="http://plain.example/b.png"><p>Text</p>'
found: list[str] = []
assert "<img" not in sanitize_html(source, found=found)
assert found == ["https://cdn.example/a.png"]
loaded = sanitize_html(source, remote=lambda url: "data:image/png;base64,AAAA" if "cdn" in url else "")
assert loaded.count("<img") == 1 and 'src="data:image/png;base64,AAAA"' in loaded
def test_the_server_never_fetches_internal_or_odd_addresses():
from app.remoteimages import RemoteImages, fetch_image, is_public
for address in ("127.0.0.1", "10.0.0.5", "192.168.1.1", "169.254.169.254", "::1", "fd00::1", "224.0.0.1", "not-an-ip"):
assert is_public(address) is False
assert is_public("93.184.216.34") is True
for url in (
"http://example.org/a.png",
"https://127.0.0.1/a.png",
"https://localhost/a.png",
"https://[::1]/a.png",
"https://example.org:8443/a.png",
"https://user:pw@example.org/a.png",
"file:///etc/passwd",
"https:///a.png",
):
assert fetch_image(url) is None
assert RemoteImages()("https://127.0.0.1/a.png") == ""
def test_remote_pictures_are_checked_by_content_and_limited(monkeypatch):
import app.remoteimages as module
png = b"\x89PNG\r\n\x1a\n" + b"0" * 20
monkeypatch.setattr(module, "fetch_image", lambda url: png if url.endswith(".png") else None)
loader = module.RemoteImages()
assert loader("https://a.example/1.png").startswith("data:image/png;base64,")
assert loader("https://a.example/1.png").startswith("data:image/png")
assert loader("https://a.example/1.svg") == ""
assert (loader.loaded, loader.failed) == (1, 1)
assert module._kind(b"<svg xmlns='http://www.w3.org/2000/svg'></svg>") is None
assert module._kind(b"RIFF\x00\x00\x00\x00WEBPVP8 ") == "webp"