Files
tsumugu/backend/app/remoteimages.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

134 lines
4.5 KiB
Python

"""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