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>
This commit is contained in:
1 parent
075525ee17
commit
a3f328832e
11 files changed
+365
-11
No files matched your search
@@ -36,3 +36,46 @@ def test_links_and_images_are_limited():
|
||||
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"
|
||||
Reference in new issue
Block a user