Lade Bilder aus E-Mails auf Wunsch über den Server nach und verkleinere den Kopf der Leseansicht auf dem Handy.
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

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
StommerandClaude Sonnet 5.5 committed 2026-10-22 16:47:24 +02:00
1 parent 075525ee17
commit a3f328832e
11 files changed
+365 -11

No files matched your search

+22 -1
View File
@@ -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
View File
@@ -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}"')
+133
View File
@@ -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
+43
View File
@@ -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"
+4
View File
@@ -108,3 +108,7 @@ Der Baum „Meine Ablage“ steht links auf den Listenseiten und auf den Seiten
### E-Mails als HTML
Die Mail wird formatiert (Standard) oder als Text gezeigt. Der Server bereinigt das HTML mit einer Positivliste (keine Skripte, Formulare, Rahmen, `<style>`-Blöcke; Formatierung nur aus erlaubten Eigenschaften, ohne `url()`, Position oder Ausdrücke; Bilder nur eingebettet als PNG/JPEG/GIF/WebP; Links nur `http(s)` und `mailto`, mit `noopener`). Im Browser läuft die Ansicht in einem Rahmen mit leerem `sandbox` (eigene Herkunft, keine Skripte), Content-Security-Policy `default-src 'none'; img-src data:` und ohne Referrer. Fremde Bilder werden nicht geladen.
### Bilder in E-Mails nachladen
Bilder von fremden Servern bleiben standardmäßig draußen. „Bilder nachladen“ (oder die Einstellung „Automatisch nachladen“ unter „Mein Konto“, getrennt für Computer und Touch-Geräte, gespeichert im Browser; die Gerätegruppe folgt `pointer: coarse`) ruft `GET /external-refs/{id}/mail/html-with-images` auf. Der Server lädt die Bilder selbst und liefert sie eingebettet zurück, das Gerät kontaktiert den Absender nicht. Schutz (`app/remoteimages.py`): nur `https` auf Port 443, keine Zugangsdaten in der Adresse, Auflösung der Adresse einmal und Verbindung genau dorthin (kein DNS-Rebinding), alle Ergebnisse müssen öffentliche Adressen sein (kein Loopback, privat, Link-Local, Metadaten), höchstens drei Weiterleitungen mit gleicher Prüfung, 5 Sekunden, 1,5 MB je Bild, 12 Bilder und 6 MB je Nachricht, Prüfung des Inhalts an der Dateikennung (PNG, JPEG, GIF, WebP; kein SVG). Restrisiko: Der Absender sieht den Abruf und weiß damit, dass die Nachricht geöffnet wurde.
+26
View File
@@ -4,9 +4,34 @@ import { SessionContext } from "../session.js";
import { ErrorNote } from "../pages/widgets.jsx";
import { formatWhen } from "./admin-core.js";
import { DownloadPolicy } from "./DownloadPolicy.jsx";
import { deviceClass, readImageChoice, writeImageChoice } from "../mail-images.js";
import { isEmployee } from "./admin-core.js";
import { PasswordForm } from "./widgets.jsx";
function MailImages() {
const [choice, setChoice] = useState(() => ({ desktop: readImageChoice("desktop"), touch: readImageChoice("touch") }));
const here = deviceClass();
const rows = [["desktop", "Am Computer"], ["touch", "Auf Handy und Tablet"]];
return (
<section className="card stack" aria-label="Bilder in E-Mails">
<h2>Bilder in E-Mails</h2>
<p className="muted">
Bilder liegen beim Absender. Beim Nachladen holt unser Server sie für dich (nur öffentliche https-Adressen, nur echte Bilddateien), dein Gerät nimmt keinen Kontakt auf.
Der Absender kann trotzdem erkennen, dass die Nachricht geöffnet wurde. Die Wahl gilt nur für diesen Browser; dieses Gerät zählt als „{here === "touch" ? "Handy und Tablet" : "Computer"}“.
</p>
{rows.map(([key, label]) => (
<label key={key}>
{label}
<select value={choice[key]} onChange={(event) => setChoice((current) => ({ ...current, [key]: writeImageChoice(key, event.target.value) }))}>
<option value="ask">Nur auf Klick nachladen</option>
<option value="auto">Automatisch nachladen</option>
</select>
</label>
))}
</section>
);
}
export function Account() {
const { session, logout } = useContext(SessionContext);
const [sessions, setSessions] = useState([]);
@@ -56,6 +81,7 @@ export function Account() {
</header>
<ErrorNote error={error} />
{notice ? <p className="notice" role="status">{notice}</p> : null}
<MailImages />
{isEmployee(session) ? null : (
<section className="card stack" aria-label="Herunterladen">
<h2>Herunterladen von Originalen</h2>
+12
View File
@@ -153,3 +153,15 @@ body.is-resizing .library__resizer { background: color-mix(in srgb, var(--accent
.mail-frame { display: block; width: 100%; height: 60vh; min-height: 260px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.mail-reading .segmented { display: inline-flex; gap: 6px; margin-bottom: 8px; }
/* Reader header on the phone: back link and tools on one line, a modest title below */
@media (max-width: 759px) {
.reader-pane__bar { padding: 8px 12px 4px; gap: 2px 8px; flex-wrap: wrap; }
.reader-pane__bar .reader-back { order: 1; }
.reader-pane__bar .reader-pane__tools { order: 2; margin-left: auto; }
.reader-pane__bar .reader-pane__heading { order: 3; flex: 1 1 100%; }
.reader-pane__heading h2 { font-size: 17px; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.reader-pane__heading .direction-column__icon { width: 18px; height: 18px; }
.reader-pane__heading .reader-facts { margin: 2px 0 0; font-size: 12px; }
}
.mail-images { display: grid; gap: 4px; margin: 4px 0; }
+30
View File
@@ -0,0 +1,30 @@
// Pictures in a mail come from the sender's server. The app loads them on request, through its own server.
// The choice lives in the browser: one value for pointer devices (computer) and one for touch devices (phone, tablet).
export const IMAGE_KEYS = { desktop: "tsumugu-mail-images-desktop", touch: "tsumugu-mail-images-touch" };
export function deviceClass(match = typeof window === "undefined" ? null : window.matchMedia) {
try {
return match && match.call(typeof window === "undefined" ? undefined : window, "(pointer: coarse)").matches ? "touch" : "desktop";
} catch {
return "desktop";
}
}
export function readImageChoice(device, storage = typeof window === "undefined" ? null : window.localStorage) {
try {
return storage?.getItem(IMAGE_KEYS[device]) === "auto" ? "auto" : "ask";
} catch {
return "ask";
}
}
export function writeImageChoice(device, choice, storage = typeof window === "undefined" ? null : window.localStorage) {
const value = choice === "auto" ? "auto" : "ask";
try {
storage?.setItem(IMAGE_KEYS[device], value);
} catch {
/* The choice then only lasts for this visit. */
}
return value;
}
+28
View File
@@ -0,0 +1,28 @@
import assert from "node:assert/strict";
import test from "node:test";
import { IMAGE_KEYS, deviceClass, readImageChoice, writeImageChoice } from "./mail-images.js";
function memory() {
const data = {};
return { getItem: (key) => (key in data ? data[key] : null), setItem: (key, value) => { data[key] = value; } };
}
test("pictures are asked for by default and the choice is kept per device kind", () => {
const store = memory();
assert.equal(readImageChoice("touch", store), "ask");
assert.equal(writeImageChoice("desktop", "auto", store), "auto");
assert.equal(readImageChoice("desktop", store), "auto");
assert.equal(readImageChoice("touch", store), "ask");
assert.equal(writeImageChoice("touch", "anything", store), "ask");
assert.ok(IMAGE_KEYS.desktop !== IMAGE_KEYS.touch);
});
test("a blocked store and a missing media query fall back to the safe choice", () => {
const broken = { getItem() { throw new Error("blocked"); }, setItem() { throw new Error("blocked"); } };
assert.equal(readImageChoice("desktop", broken), "ask");
assert.equal(writeImageChoice("desktop", "auto", broken), "auto");
assert.equal(readImageChoice("desktop", null), "ask");
assert.equal(deviceClass(null), "desktop");
assert.equal(deviceClass(function () { return { matches: true }; }), "touch");
assert.equal(deviceClass(function () { throw new Error("x"); }), "desktop");
});
+36 -4
View File
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useState } from "react";
// The cleaned HTML of a mail is shown in a frame without scripts and without any reach into the app:
// an empty `sandbox` gives it its own origin, and the policy inside allows no loads except inline images.
@@ -12,9 +12,33 @@ export function mailDocument(html) {
+ `</head><body>${html}</body></html>`;
}
export function MailBody({ html, text, truncated, notices = [] }) {
// `remote` describes the pictures of other servers: { key, count, auto, load }. `load` asks the app's server for them.
export function MailBody({ html, text, truncated, notices = [], remote = null }) {
const [view, setView] = useState(html ? "html" : "text");
const [withImages, setWithImages] = useState(null);
const [busy, setBusy] = useState(false);
const [problem, setProblem] = useState("");
const mode = html ? view : "text";
const shownHtml = withImages ? withImages.html : html;
async function loadImages() {
setBusy(true);
setProblem("");
try {
setWithImages(await remote.load());
} catch (err) {
setProblem(err.message || "Die Bilder konnten nicht geladen werden.");
} finally {
setBusy(false);
}
}
useEffect(() => {
setWithImages(null);
setProblem("");
if (remote?.auto && remote.count > 0) loadImages();
}, [remote?.key]);
return (
<div className="mail-reading">
{html ? (
@@ -25,8 +49,16 @@ export function MailBody({ html, text, truncated, notices = [] }) {
) : null}
{mode === "html" ? (
<>
<iframe className="mail-frame" title="Nachricht" sandbox="allow-popups allow-popups-to-escape-sandbox" referrerPolicy="no-referrer" srcDoc={mailDocument(html)} />
<p className="muted">Aus Sicherheitsgründen sind Skripte, eingebettete Formulare und Bilder von fremden Servern entfernt. Links öffnen externe Seiten: nur bekannten Absendern vertrauen.</p>
{remote && remote.count > 0 && !withImages ? (
<p className="mail-images">
<button type="button" className="quiet" disabled={busy} onClick={loadImages}>{busy ? "Bilder werden geladen …" : `Bilder nachladen (${remote.count})`}</button>
<span className="muted">Unsere Server laden die Bilder für dich, der Absender sieht deine Adresse nicht. Er kann aber erkennen, dass die Nachricht geöffnet wurde.</span>
</p>
) : null}
{withImages ? <p className="muted">{withImages.loaded} Bild(er) geladen{withImages.failed ? `, ${withImages.failed} nicht verfügbar oder nicht sicher` : ""}.</p> : null}
{problem ? <p className="error">{problem}</p> : null}
<iframe className="mail-frame" title="Nachricht" sandbox="allow-popups allow-popups-to-escape-sandbox" referrerPolicy="no-referrer" srcDoc={mailDocument(shownHtml)} />
<p className="muted">Aus Sicherheitsgründen sind Skripte und eingebettete Formulare entfernt, Bilder von fremden Servern erst nach deiner Freigabe sichtbar. Links öffnen externe Seiten: nur bekannten Absendern vertrauen.</p>
</>
) : (
<>
+13 -1
View File
@@ -2,6 +2,7 @@ import { DocumentView } from "../pages/DocumentView.jsx";
import { useEffect, useRef, useState } from "react";
import { api, documentToText, downloadMailPart, fetchExternal, fetchMailPart, markDirty, textToDocument } from "../api.js";
import { MailAttachments, MailBody } from "../mail-view.jsx";
import { deviceClass, readImageChoice } from "../mail-images.js";
import { MarkdownView } from "../markdown.jsx";
import { NoteEditor } from "../notes.jsx";
import { DirectionColumn, directionWord } from "../pages/content.jsx";
@@ -270,7 +271,18 @@ export function MailStage({ element, context, binding, shownDirection }) {
<button type="button" disabled={page >= pages} onClick={() => setPage((current) => Math.min(pages, current + 1))}>Nächste Seite</button>
</div>
) : null}
<MailBody html={letter.html} text={letter.text} truncated={letter.text_truncated} notices={letter.notices} />
<MailBody
html={letter.html}
text={letter.text}
truncated={letter.text_truncated}
notices={letter.notices}
remote={{
key: element.id,
count: letter.remote_images || 0,
auto: readImageChoice(deviceClass()) === "auto",
load: () => api(`/api/v1/external-refs/${element.id}/mail/html-with-images${context?.kind && context?.id ? `?context_kind=${context.kind}&context_id=${context.id}` : ""}`),
}}
/>
{binding?.rights?.download ? <button type="button" onClick={openOriginal}>Vollständige E-Mail herunterladen</button> : null}
</>
) : (