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
@@ -13,7 +13,8 @@ from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.routes import _created, _ctx_model
|
||||
from app.api.schemas import ConnectorIn, ExternalIn, SourceAssignmentIn
|
||||
from app.core.errors import DOWNLOAD_REFUSED, forbidden, not_found, unprocessable, unsupported
|
||||
from app.mailpack import parse_mail
|
||||
from app.mailpack import _inline_images, parse_mail, sanitize_html
|
||||
from app.remoteimages import RemoteImages
|
||||
from app.media.storage import content_disposition
|
||||
from app.ops import create_external
|
||||
from app.retrieval import RetrievalFailure
|
||||
@@ -133,6 +134,7 @@ def view_mail(
|
||||
"message_cc": parsed.copy,
|
||||
"text": parsed.text,
|
||||
"html": parsed.html,
|
||||
"remote_images": len(parsed.remote_images),
|
||||
"text_truncated": parsed.text_truncated,
|
||||
"complete": not parsed.missing,
|
||||
"notices": parsed.notices,
|
||||
@@ -190,6 +192,25 @@ def _complete_mail(conn, principal: Principal, element, context, ref):
|
||||
return parsed, raw
|
||||
|
||||
|
||||
@router.get("/api/v1/external-refs/{element_id}/mail/html-with-images")
|
||||
def view_mail_with_images(
|
||||
element_id: UUID,
|
||||
context_kind: str | None = None,
|
||||
context_id: str | None = None,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
"""The formatted mail with its pictures from other servers, loaded by the server on request."""
|
||||
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
|
||||
parsed, _raw = _complete_mail(conn, principal, element, context, ref)
|
||||
loader = RemoteImages()
|
||||
rendered = sanitize_html(parsed.html_source, _inline_images(parsed.attachments), remote=loader) if parsed.html_source else ""
|
||||
return JSONResponse(
|
||||
content={"html": rendered, "loaded": loader.loaded, "failed": loader.failed},
|
||||
headers={"Cache-Control": "private, no-store", "Referrer-Policy": "no-referrer"},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/external-refs/{element_id}/mail/original")
|
||||
def download_mail(
|
||||
element_id: UUID,
|
||||
|
||||
+18
-5
@@ -70,6 +70,8 @@ class ParsedMail:
|
||||
attachments: list[AttachmentBytes] = field(default_factory=list)
|
||||
missing: list[str] = field(default_factory=list)
|
||||
html: str = ""
|
||||
html_source: str = ""
|
||||
remote_images: list[str] = field(default_factory=list)
|
||||
notices: list[str] = field(default_factory=list)
|
||||
text_truncated: bool = False
|
||||
document_on: str | None = None
|
||||
@@ -168,7 +170,9 @@ def parse_mail(raw: bytes) -> ParsedMail:
|
||||
simplified = "\n".join(chunk for chunk in plain if chunk).strip()
|
||||
rendered = ""
|
||||
if html_parts:
|
||||
rendered = sanitize_html("\n".join(html_parts), _inline_images(attachments))
|
||||
source = "\n".join(html_parts)
|
||||
remote_found: list[str] = []
|
||||
rendered = sanitize_html(source, _inline_images(attachments), found=remote_found)
|
||||
if not simplified:
|
||||
simplified = simplified_text("\n".join(html_parts))
|
||||
if missing:
|
||||
@@ -183,6 +187,8 @@ def parse_mail(raw: bytes) -> ParsedMail:
|
||||
attachments=attachments,
|
||||
missing=missing,
|
||||
html=rendered,
|
||||
html_source=source if html_parts else "",
|
||||
remote_images=remote_found if html_parts else [],
|
||||
notices=notices,
|
||||
document_on=_message_date(message.get("Date")),
|
||||
)
|
||||
@@ -235,8 +241,11 @@ def simplified_text(value: str) -> str:
|
||||
return text.strip()
|
||||
|
||||
|
||||
def sanitize_html(value: str, images: dict[str, str] | None = None) -> str:
|
||||
"""Keep a formatted reading. Scripts, active content and remote loads stay out."""
|
||||
def sanitize_html(value: str, images: dict[str, str] | None = None, *, remote=None, found: list[str] | None = None) -> str:
|
||||
"""Keep a formatted reading. Scripts and active content stay out.
|
||||
|
||||
Pictures from other servers stay out too, unless `remote` turns an address into an inline picture.
|
||||
`found` collects the addresses that were met."""
|
||||
images = images or {}
|
||||
|
||||
class Cleaner(HTMLParser):
|
||||
@@ -252,7 +261,7 @@ def sanitize_html(value: str, images: dict[str, str] | None = None) -> str:
|
||||
return
|
||||
if self.skip or name not in _ALLOWED_TAGS:
|
||||
return
|
||||
rendered = _clean_attrs(name, attrs, images)
|
||||
rendered = _clean_attrs(name, attrs, images, remote, found)
|
||||
if rendered is None:
|
||||
return
|
||||
self.parts.append(f"<{name}{rendered}>")
|
||||
@@ -299,7 +308,7 @@ def _safe_style(value: str) -> str:
|
||||
return "; ".join(kept)
|
||||
|
||||
|
||||
def _clean_attrs(tag: str, attrs, images: dict[str, str]) -> str | None:
|
||||
def _clean_attrs(tag: str, attrs, images: dict[str, str], remote=None, found: list[str] | None = None) -> str | None:
|
||||
kept: list[str] = []
|
||||
for key, value in attrs:
|
||||
name = (key or "").lower()
|
||||
@@ -332,6 +341,10 @@ def _clean_attrs(tag: str, attrs, images: dict[str, str]) -> str | None:
|
||||
if source.lower().startswith("cid:"):
|
||||
token = source[4:].strip().strip("<>")
|
||||
source = images.get(token.casefold(), "")
|
||||
elif re.match(r"https://", source, re.IGNORECASE):
|
||||
if found is not None and source not in found:
|
||||
found.append(source)
|
||||
source = remote(source) if remote is not None else ""
|
||||
if not _DATA_IMAGE.match(source):
|
||||
continue
|
||||
kept.append(f' src="{source}"')
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Fetching the pictures a mail refers to, on the server and only on request.
|
||||
|
||||
The browser never contacts the sender: the server loads each picture, checks that it really is
|
||||
a PNG, JPEG, GIF or WebP, and hands it over inline. This hides the reader's address and device.
|
||||
It cannot hide that the mail was opened, because the sender sees the request.
|
||||
|
||||
Only public https addresses are fetched. The address is resolved once, every result must be a
|
||||
public one, and the connection goes to exactly that address, so a changing DNS answer cannot
|
||||
lead to an internal host. Redirects are followed a few times with the same checks.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import http.client
|
||||
import ipaddress
|
||||
import socket
|
||||
import ssl
|
||||
from urllib.parse import urljoin, urlsplit
|
||||
|
||||
MAX_IMAGE_BYTES = 1_500_000
|
||||
MAX_REDIRECTS = 3
|
||||
TIMEOUT_SECONDS = 5
|
||||
MAX_IMAGES = 12
|
||||
MAX_TOTAL_BYTES = 6_000_000
|
||||
|
||||
_SIGNATURES = (
|
||||
(b"\x89PNG\r\n\x1a\n", "png"),
|
||||
(b"\xff\xd8\xff", "jpeg"),
|
||||
(b"GIF87a", "gif"),
|
||||
(b"GIF89a", "gif"),
|
||||
)
|
||||
|
||||
|
||||
class _Pinned(http.client.HTTPSConnection):
|
||||
"""An https connection that goes to a fixed, already checked address."""
|
||||
|
||||
def __init__(self, host: str, address: str, port: int) -> None:
|
||||
super().__init__(host, port, timeout=TIMEOUT_SECONDS, context=ssl.create_default_context())
|
||||
self._address = address
|
||||
|
||||
def connect(self) -> None:
|
||||
sock = socket.create_connection((self._address, self.port), self.timeout)
|
||||
self.sock = self._context.wrap_socket(sock, server_hostname=self.host)
|
||||
|
||||
|
||||
def is_public(address: str) -> bool:
|
||||
try:
|
||||
ip = ipaddress.ip_address(address)
|
||||
except ValueError:
|
||||
return False
|
||||
return ip.is_global and not ip.is_multicast
|
||||
|
||||
|
||||
def _public_address(host: str, port: int) -> str | None:
|
||||
try:
|
||||
found = {item[4][0] for item in socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)}
|
||||
except OSError:
|
||||
return None
|
||||
if not found or not all(is_public(address) for address in found):
|
||||
return None
|
||||
return sorted(found)[0]
|
||||
|
||||
|
||||
def _kind(data: bytes) -> str | None:
|
||||
for signature, kind in _SIGNATURES:
|
||||
if data.startswith(signature):
|
||||
return kind
|
||||
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
||||
return "webp"
|
||||
return None
|
||||
|
||||
|
||||
def fetch_image(url: str) -> bytes | None:
|
||||
"""The bytes of a picture, or None when anything about it is not safe."""
|
||||
for _ in range(MAX_REDIRECTS + 1):
|
||||
parts = urlsplit(url)
|
||||
if parts.scheme != "https" or not parts.hostname or parts.username or parts.password:
|
||||
return None
|
||||
port = parts.port or 443
|
||||
if port != 443:
|
||||
return None
|
||||
address = _public_address(parts.hostname, port)
|
||||
if address is None:
|
||||
return None
|
||||
target = (parts.path or "/") + (f"?{parts.query}" if parts.query else "")
|
||||
connection = _Pinned(parts.hostname, address, port)
|
||||
try:
|
||||
connection.request("GET", target, headers={"User-Agent": "Mozilla/5.0", "Accept": "image/png,image/jpeg,image/gif,image/webp", "Connection": "close"})
|
||||
response = connection.getresponse()
|
||||
if response.status in {301, 302, 303, 307, 308}:
|
||||
location = response.getheader("Location")
|
||||
if not location:
|
||||
return None
|
||||
url = urljoin(url, location)
|
||||
continue
|
||||
if response.status != 200:
|
||||
return None
|
||||
data = response.read(MAX_IMAGE_BYTES + 1)
|
||||
except (OSError, http.client.HTTPException, ssl.SSLError):
|
||||
return None
|
||||
finally:
|
||||
connection.close()
|
||||
if len(data) > MAX_IMAGE_BYTES or _kind(data) is None:
|
||||
return None
|
||||
return data
|
||||
return None
|
||||
|
||||
|
||||
class RemoteImages:
|
||||
"""Loads the pictures of one mail within a budget and remembers each address once."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.cache: dict[str, str] = {}
|
||||
self.total = 0
|
||||
self.loaded = 0
|
||||
self.failed = 0
|
||||
|
||||
def __call__(self, url: str) -> str:
|
||||
if url in self.cache:
|
||||
return self.cache[url]
|
||||
result = ""
|
||||
if len(self.cache) < MAX_IMAGES and self.total < MAX_TOTAL_BYTES:
|
||||
data = fetch_image(url)
|
||||
if data is not None:
|
||||
self.total += len(data)
|
||||
result = f"data:image/{_kind(data)};base64,{base64.b64encode(data).decode('ascii')}"
|
||||
self.cache[url] = result
|
||||
if result:
|
||||
self.loaded += 1
|
||||
else:
|
||||
self.failed += 1
|
||||
return result
|
||||
@@ -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