Security hardening from the assessment; testing fixes

Security (ultimo-security-report.md):
- Security headers on every response, the app shell included: they lived at
  server level and nginx dropped them in any location with its own header.
  CSP, framing, nosniff, referrer, permissions (docker/nginx-security.conf).
- Plain HTTP forwarded by the reverse proxy is redirected to HTTPS; HSTS on
  HTTPS responses. server_tokens off.
- Cross-site state-changing /api requests refused (login CSRF), WebSocket too.
- Validation errors in the usual error shape, without the request model.
- New-password policy: max 128, common/own-address/repeated refused,
  breached-password check (HIBP k-anonymity, fails open).
- Message HTML: links keep only http(s)/mailto (sanitizer v6); the HTML
  endpoint carries its own no-script CSP.
- Outbound fetches pinned to the checked IP (DNS rebinding); external
  IMAP/SMTP servers re-checked before each connection; CGNAT and mapped
  addresses counted as non-public.

Fixes from testing:
- IDLE loop catches up before idling: mail that came while the worker was
  away no longer waits up to 10 minutes.
- Restore from Spam/Trash/Archive moves only the removed messages, not your
  reply in Sent.
- Narrow desktop list stacks sender/subject/snippet (subject was squeezed
  to nothing at the default width), with hover actions.
- Phone: wide emails scale to fit the screen.
- Singular forms ("1 message"); no zip button for a single attachment;
  app passwords no longer claim "never used" for mail apps.
- ruff format across the backend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 22:08:06 +02:00
co-authored by Claude Opus 5.5
parent 30b806e7da
commit addb2232a1
54 changed files with 1265 additions and 187 deletions
+1 -3
View File
@@ -125,9 +125,7 @@ def parse_autoconfig_xml(xml_text: str) -> CandidateConfig | None:
smtp = _server("outgoingServer", "smtp")
if imap is None or smtp is None:
return None
return CandidateConfig(
imap, smtp, "autoconfig", auth_strategy=_strategy_from_auth(imap.auth)
)
return CandidateConfig(imap, smtp, "autoconfig", auth_strategy=_strategy_from_auth(imap.auth))
# --- DNS SRV (RFC 6186) ------------------------------------------------------
+49 -28
View File
@@ -2,9 +2,10 @@
All autodiscovery HTTP is HTTPS-only, 5 s timeout, response capped at 256 KB, no
off-domain redirects, and the target host must resolve exclusively to public IPs
(reject private/loopback/link-local/reserved ranges). A residual TOCTOU gap
(DNS rebinding between our resolve and httpx's own resolve) is accepted for M1;
a pinned-IP transport can close it later.
(reject private/loopback/link-local/reserved ranges). The request then goes to
the very address that was checked — the name is not looked up a second time —
so a DNS answer that changes in between (DNS rebinding) cannot point it inward.
TLS is still verified against the host name.
"""
from __future__ import annotations
@@ -26,15 +27,12 @@ class SsrfError(Exception):
def _is_public_ip(ip: str) -> bool:
addr = ipaddress.ip_address(ip)
return not (
addr.is_private
or addr.is_loopback
or addr.is_link_local
or addr.is_multicast
or addr.is_reserved
or addr.is_unspecified
)
addr = ipaddress.ip_address(ip.split("%", 1)[0]) # drop an IPv6 zone index
if isinstance(addr, ipaddress.IPv6Address) and addr.ipv4_mapped is not None:
addr = addr.ipv4_mapped # ::ffff:127.0.0.1 is 127.0.0.1
# is_global: not private, loopback, link-local, reserved, unspecified, nor
# shared/carrier-grade NAT (100.64.0.0/10, which is_private misses).
return addr.is_global and not addr.is_multicast
async def _resolve_all(host: str) -> list[str]:
@@ -43,8 +41,10 @@ async def _resolve_all(host: str) -> list[str]:
return [info[4][0] for info in infos]
async def assert_host_is_public(host: str) -> None:
"""Resolve ``host`` and reject if any address is non-public (SPEC §10.6)."""
async def assert_host_is_public(host: str) -> list[str]:
"""Resolve ``host`` and reject if any address is non-public (SPEC §10.6).
Returns the addresses checked, for the caller to connect to."""
try:
addresses = await _resolve_all(host)
except socket.gaierror as exc:
@@ -54,6 +54,31 @@ async def assert_host_is_public(host: str) -> None:
for ip in addresses:
if not _is_public_ip(ip):
raise SsrfError(f"{host} resolves to non-public address {ip}")
return addresses
async def _pinned(url: str) -> tuple[str, dict[str, str], dict[str, str]]:
"""``url`` aimed at the address just checked: (url, headers, extensions).
The URL names the IP, the Host header keeps the name (virtual hosting), and
TLS gets the name for SNI and for checking the certificate."""
parts = urlsplit(url)
if parts.scheme != "https":
raise SsrfError("only https is permitted")
hostname = parts.hostname
if not hostname:
raise SsrfError("missing host")
addresses = await assert_host_is_public(hostname)
# IPv4 first: containers often have no IPv6 route, though DNS returns AAAA.
ip = next((a for a in addresses if ":" not in a), addresses[0])
literal = f"[{ip}]" if ":" in ip else ip
netloc = f"{literal}:{parts.port}" if parts.port else literal
host_header = f"{hostname}:{parts.port}" if parts.port else hostname
return (
parts._replace(netloc=netloc).geturl(),
{"Host": host_header},
{"sni_hostname": hostname},
)
async def guarded_get(url: str) -> str | None:
@@ -63,12 +88,7 @@ async def guarded_get(url: str) -> str | None:
(any non-200 response). Raises :class:`SsrfError` on a policy violation
(bad scheme, private IP, oversize); lets httpx network errors propagate.
"""
parts = urlsplit(url)
if parts.scheme != "https":
raise SsrfError("only https is permitted")
if not parts.hostname:
raise SsrfError("missing host")
await assert_host_is_public(parts.hostname)
target, pin_headers, extensions = await _pinned(url)
async with (
httpx.AsyncClient(
@@ -76,7 +96,7 @@ async def guarded_get(url: str) -> str | None:
timeout=HTTP_TIMEOUT,
headers={"User-Agent": "webmail-autodiscovery/1.0"},
) as client,
client.stream("GET", url) as resp,
client.stream("GET", target, headers=pin_headers, extensions=extensions) as resp,
):
if resp.status_code != 200:
return None
@@ -114,16 +134,17 @@ async def guarded_request(
fetch ICS feeds. HTTPS-only, public host required, no off-domain redirects, body
capped at ``max_bytes``. ``auth`` is an (user, password) basic-auth pair. Returns
status + headers + body (all statuses; the caller decides what's an error)."""
parts = urlsplit(url)
if parts.scheme != "https":
raise SsrfError("only https is permitted")
if not parts.hostname:
raise SsrfError("missing host")
await assert_host_is_public(parts.hostname)
target, pin_headers, extensions = await _pinned(url)
async with (
httpx.AsyncClient(follow_redirects=False, timeout=timeout, auth=auth) as client,
client.stream(method, url, headers=headers, content=content) as resp,
client.stream(
method,
target,
headers={**(headers or {}), **pin_headers},
content=content,
extensions=extensions,
) as resp,
):
chunks: list[bytes] = []
total = 0
+1
View File
@@ -89,6 +89,7 @@ async def test_account(
smtp_tls=smtp.tls,
username=username,
password=password,
public_only=True,
)
)
try: