diff --git a/.env.example b/.env.example index 929b322..340a845 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,12 @@ # Clients send it in the X-API-Key header; the web UI asks for it on first 401. PATCHRADAR_API_KEY= +# Optional: names the web UI is reached by besides localhost, 127.0.0.1 and [::1] +# on port 8000, comma-separated; any other Host is refused with 421 (DNS +# rebinding). `name` for any port, `name:port` for that port only: a reverse +# proxy's name, or localhost:9000 when the port is published as 9000. +PATCHRADAR_ALLOWED_HOSTS= + # Optional: raises NVD's rate limit from 5 to 50 requests per 30 seconds. # https://nvd.nist.gov/developers/request-an-api-key NVD_API_KEY= diff --git a/CHANGELOG.md b/CHANGELOG.md index ad2e649..ae84a54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,23 @@ what has already shipped, and for nothing else. See `RELEASING.md`. ### Security +- **The web UI answers only to its own names: DNS rebinding is refused.** The server never looked + at `Host`. A page on a name the attacker controls, made to resolve to 127.0.0.1, is the same origin + as `http://that-name:8000` for the browser, which therefore sends `Sec-Fetch-Site: same-origin`, an + `Origin` equal to `Host`, and lets the page read the answers: the bug hunt of 2026-11-17 added a + watchlist entry and read `GET /api/watchlist` — the software installed and its versions — with + `Host: rebind.evil.example:8000`, so "a page you visit cannot add to your watchlist" did not hold. + Every request, reads and `/static` included, is now refused with 421 Misdirected Request unless its + `Host` is `localhost`, `127.0.0.1` or `[::1]` on the port the server listens on (or with no port), + the address the connection arrived on, or a name in the new `PATCHRADAR_ALLOWED_HOSTS` + (comma-separated, `name` for any port, `name:port` for that one, no wildcard); a missing or + malformed `Host` is a 400. For a browser that sends no `Sec-Fetch-Site`, the write guard compares + `Origin` with those hosts, port included, instead of with the request's own `Host`, which the + rebinding page sets; an `Origin` with brackets around something that is not an IPv6 address, such + as `http://[1.2]`, is a 403 where it was a 500. Who reaches the UI through a reverse proxy that passes its own `Host`, a LAN + name or a Docker port other than 8000 adds that name to the variable; `docker-compose.yml` and + `.env.example` pass it through. `tests/test_host_check.py`. + - **opentelemetry-api 1.45.2** in `uv.lock`, from 1.45.1. The older release parses a `traceparent` header in quadratic time, a denial of service (CVE-2026-126360, GHSA-mf5v-w3w3-f6cg, medium, published 2026-11-13); 1.45.2, on PyPI since 2026-10-29, is the release the advisory names as fixed. diff --git a/CLAUDE.md b/CLAUDE.md index 1702dda..173be5d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -77,4 +77,6 @@ scansioni datate. Per una release già uscita: (`_get_over_https`): Cellar risponde con un 303 verso `http://`. - Chiavi lette dall'ambiente: `NVD_API_KEY` (facoltativa), `VULDB_API_KEY` (senza, VulDB non viene interrogato), `PATCHRADAR_API_KEY` (protegge solo le scritture - dell'API), `PATCHRADAR_HOME` (sposta solo la cache dei testi di legge). + dell'API), `PATCHRADAR_ALLOWED_HOSTS` (nomi a cui l'interfaccia web risponde oltre + al loopback; ogni altro `Host` riceve 421), `PATCHRADAR_HOME` (sposta solo la cache + dei testi di legge). diff --git a/README.md b/README.md index da7f82c..49b82cb 100644 --- a/README.md +++ b/README.md @@ -177,8 +177,19 @@ meant for use on the same machine: `patchradar serve` listens on 127.0.0.1 and t `docker-compose.yml` publishes the port on 127.0.0.1 only. Set the key (see `.env.example`) before exposing it any further. Either way, a write that a browser sends on behalf of another web site (`Sec-Fetch-Site: cross-site`, or an `Origin` -that is not this server) is refused with 403, so a page you visit cannot add to your -watchlist or start scans through your browser. +that is not this server) is refused with 403, and every request — reads and static +files included — whose `Host` is not a name the server answers to is refused with +421 Misdirected Request. So a page you visit cannot read your watchlist, add to it or +start scans through your browser, not even by making its own name resolve to +127.0.0.1 (DNS rebinding). + +The server answers to `localhost`, `127.0.0.1` and `[::1]` on the port it listens on, +and to the address a connection arrived on (`http://192.168.1.10:8000` with +`--host 0.0.0.0`). Any other name — a reverse proxy that passes its own `Host`, a +LAN name, a port Docker remaps such as `-p 127.0.0.1:9000:8000` — goes in +`PATCHRADAR_ALLOWED_HOSTS`, comma-separated: `name` for any port, `name:port` for +that port only, for example `PATCHRADAR_ALLOWED_HOSTS=patchradar.lan,localhost:9000`. +There is no wildcard. On Windows, `scripts/import-winget.ps1` fills the watchlist from `winget list`: @@ -289,6 +300,9 @@ the response times, how customers are told and how to roll back. only: anyone who can reach the port can read the watchlist and the CVEs found, with or without the key, over plain HTTP. Keep it on 127.0.0.1, as the default and `docker-compose.yml` do, or put it behind a proxy with TLS and authentication. +Against web pages acting through your browser, cross-site writes are refused, and so +is any request for a `Host` the server does not answer to, which closes DNS rebinding +(see [Web UI](#web-ui) for `PATCHRADAR_ALLOWED_HOSTS`). Every GitHub release carries CycloneDX SBOMs of the Docker image and of the Python environment, and dated Trivy reports of the image and of the repository at the tag. diff --git a/SECURITY.md b/SECURITY.md index 894d704..eed4924 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -68,8 +68,10 @@ listens on all of the container's interfaces. The optional `PATCHRADAR_API_KEY` protects only the endpoints that change the watchlist or start a scan: the watchlist and the vulnerabilities found are readable by anyone who can reach the port, with or without the key, over plain HTTP. Keep it on loopback, or put it behind a proxy that -adds TLS and authentication. The details are in question 2 of -[docs/nis2-compliance.md](docs/nis2-compliance.md). +adds TLS and authentication. Requests whose `Host` is not one the server answers to +— the loopback names, the address the connection arrived on, and the names in +`PATCHRADAR_ALLOWED_HOSTS` — are refused with 421, which closes DNS rebinding. The +details are in question 2 of [docs/nis2-compliance.md](docs/nis2-compliance.md). ## Evidence attached to every release diff --git a/benchmarks/test_api.py b/benchmarks/test_api.py index 65c49a5..af4c3ba 100644 --- a/benchmarks/test_api.py +++ b/benchmarks/test_api.py @@ -13,7 +13,7 @@ @pytest.fixture def client(loop, seeded_db): transport = ASGITransport(app=app) - http_client = AsyncClient(transport=transport, base_url="http://bench") + http_client = AsyncClient(transport=transport, base_url="http://localhost") yield http_client loop.run_until_complete(http_client.aclose()) diff --git a/docker-compose.yml b/docker-compose.yml index f5f0eb4..72ce389 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,9 @@ services: environment: # read from .env (see .env.example); unset or empty means "no key" PATCHRADAR_API_KEY: ${PATCHRADAR_API_KEY:-} + # names besides localhost:8000 the UI is opened by, e.g. after changing the + # published port above; any other Host is refused (see .env.example) + PATCHRADAR_ALLOWED_HOSTS: ${PATCHRADAR_ALLOWED_HOSTS:-} NVD_API_KEY: ${NVD_API_KEY:-} volumes: # must match the Dockerfile VOLUME: the image runs as the unprivileged diff --git a/docs/nis2-compliance.md b/docs/nis2-compliance.md index e21721c..aae6aa5 100644 --- a/docs/nis2-compliance.md +++ b/docs/nis2-compliance.md @@ -59,8 +59,20 @@ conta: sulla rete se davanti non c'è un proxy che termina TLS; - una scrittura che un browser invia per conto di un altro sito viene rifiutata con 403: il server accetta solo `Sec-Fetch-Site` uguale a `same-origin` o `none` e, se - quell'intestazione manca, un `Origin` uguale al server. È una difesa contro le pagine - web ostili visitate dall'utente, non un controllo degli accessi. + quell'intestazione manca, un `Origin` che sia uno degli host a cui il server + risponde, porta compresa (non più l'`Host` della richiesta stessa). È una difesa + contro le pagine web ostili visitate dall'utente, non un controllo degli accessi; +- ogni richiesta, letture e file statici compresi, il cui `Host` non è un nome a cui + il server risponde viene rifiutata con 421 (Misdirected Request), e con 400 se + `Host` manca o non è un host. Il server risponde a `localhost`, `127.0.0.1` e + `[::1]` sulla porta su cui ascolta, all'indirizzo su cui è arrivata la connessione e + ai nomi elencati nella variabile d'ambiente `PATCHRADAR_ALLOWED_HOSTS` (separati da + virgole: `nome` per qualunque porta, `nome:porta` solo per quella; nessun carattere + jolly). Difende dal DNS rebinding: una pagina ostile che fa risolvere il proprio nome + in 127.0.0.1 è "same-origin" per il browser, e fino al 2026-11-17 poteva così leggere + la watchlist e aggiungervi voci. Chi mette un proxy davanti al server e gli fa + passare il proprio `Host`, o rimappa la porta con Docker, aggiunge quel nome alla + variabile. In pratica: esposta oltre la macchina locale, l'interfaccia mostra a chiunque la raggiunga l'inventario del software e le sue vulnerabilità. Va lasciata sul loopback, diff --git a/src/patchradar/api/main.py b/src/patchradar/api/main.py index 121a878..d25cb3e 100644 --- a/src/patchradar/api/main.py +++ b/src/patchradar/api/main.py @@ -1,13 +1,15 @@ import asyncio import hmac +import ipaddress import logging import math import os import re +from collections.abc import Awaitable, Callable, MutableMapping from contextlib import asynccontextmanager from importlib.metadata import version as pkg_version from pathlib import Path as FilePath -from urllib.parse import urlsplit +from typing import Any import aiosqlite from fastapi import Depends, FastAPI, HTTPException, Path, Query, Request @@ -103,6 +105,107 @@ async def require_api_key(provided: str | None = Depends(_api_key_header)) -> No ) +ALLOWED_HOSTS_ENV = "PATCHRADAR_ALLOWED_HOSTS" + +# ASGI, spelt without importing Starlette, which is FastAPI's and not declared here. +Scope = MutableMapping[str, Any] +Receive = Callable[[], Awaitable[MutableMapping[str, Any]]] +Send = Callable[[MutableMapping[str, Any]], Awaitable[None]] +ASGIApp = Callable[[Scope, Receive, Send], Awaitable[None]] + +# The names a browser on this machine reaches the server by, whatever --host is. +_LOOPBACK_NAMES = frozenset({"localhost", "127.0.0.1", "::1"}) + +# A Host value, or the authority of an Origin: a name or an IPv4 address, or an +# IPv6 address in brackets, and an optional port. Nothing else — no userinfo, +# path or spaces — so what is matched is what a browser would have sent. +# Matched here rather than with urlsplit, which raises ValueError on brackets +# around something that is not an IPv6 address: a 500 for `Origin: http://[1.2]`. +_AUTHORITY_RE = re.compile(r"(?:(?P[A-Za-z0-9._-]+)|\[(?P[0-9A-Fa-f:.]+)\])(?::(?P[0-9]{1,5}))?") + +# An Origin header as browsers serialise one (RFC 6454, 6.1). +_ORIGIN_RE = re.compile(r"(?Phttps?)://(?P.+)") + +_DEFAULT_PORTS = {"http": 80, "https": 443} + + +def _parse_authority(value: str) -> tuple[str, int | None] | None: + """(host, port) of `name[:port]`, the host lower-cased and an address in its + canonical form; None when the value is not one.""" + match = _AUTHORITY_RE.fullmatch(value) + if match is None: + return None + port = int(match["port"]) if match["port"] else None + if port is not None and port > 65535: + return None + if match["ipv6"] is not None: + try: + return ipaddress.IPv6Address(match["ipv6"]).compressed, port + except ValueError: + return None + name = match["name"].lower() + return _as_address(name) or name, port + + +def _as_address(value: str) -> str | None: + """An IP address in its canonical form, or None when `value` is not one.""" + try: + return ipaddress.ip_address(value).compressed + except ValueError: + return None + + +def configured_hosts() -> list[tuple[str, int | None]]: + """The extra names in PATCHRADAR_ALLOWED_HOSTS, comma-separated. + + `name` is answered on any port, `name:port` on that port only. An entry that + is not a name — `*`, a URL — is ignored (the startup log says so), never + read as "any host". Read per request, like the API key. + """ + raw = os.environ.get(ALLOWED_HOSTS_ENV, "") + entries = (_parse_authority(item.strip()) for item in raw.split(",") if item.strip()) + return [entry for entry in entries if entry is not None] + + +def _answers_to(host: str, port: int | None, scope: Scope) -> bool: + """Whether this server, as reached by this connection, is `host:port`. + + The loopback names and the address the connection arrived on (uvicorn puts + the socket's own address in the scope's `server`), on the port it arrived + on or with no port; then the configured names. An address is taken from + `server` only when it is one: DNS rebinding works through a name, and no + answer of the attacker's makes a browser send an address it did not + connect to. + """ + for name, wanted in configured_hosts(): + if host == name and (wanted is None or port == wanted): + return True + own_address, own_port = scope.get("server") or (None, None) + names = set(_LOOPBACK_NAMES) + if isinstance(own_address, str) and (address := _as_address(own_address)): + names.add(address) + if host not in names: + return False + return port is None or own_port is None or port == own_port + + +def _origin_is_this_server(origin: str, request: Request) -> bool: + """Whether an Origin header names this server, port included. + + Compared with the hosts the server answers to, not with the request's own + Host: under DNS rebinding the browser makes the two equal, and behind a + proxy that rewrites Host they differ for a legitimate page. The port is the + origin's, or the scheme's default when it has none, so a page from + http://localhost (port 80) is not this server on port 8000. + """ + match = _ORIGIN_RE.fullmatch(origin) # not "null", nor anything with a path + authority = _parse_authority(match["authority"]) if match else None + if match is None or authority is None: + return False + host, port = authority + return _answers_to(host, port if port is not None else _DEFAULT_PORTS[match["scheme"]], request.scope) + + # What a browser's Sec-Fetch-Site may say about a write it is allowed to send: # the page came from this server, or the user started the request directly. _OWN_FETCH_SITES = {"same-origin", "none"} @@ -118,10 +221,14 @@ async def require_same_origin(request: Request) -> None: Browsers say where a request comes from, in Sec-Fetch-Site and, on every cross-origin POST or DELETE, in Origin. Sec-Fetch-Site is preferred because - it survives a reverse proxy that rewrites Host; Origin against Host covers - the browsers that predate it. A client that sends neither header is not a - browser — curl, a script — and cannot be used for CSRF, so it is let through - to the key check like before. + it survives a reverse proxy that rewrites Host; Origin against the hosts the + server answers to covers the browsers that predate it. A client that sends + neither header is not a browser — curl, a script — and cannot be used for + CSRF, so it is let through to the key check like before. + + "same-origin" is only as good as the origin: under DNS rebinding the + attacker's page is same-origin with the name it rebinds, and so is its + Origin. `require_known_host` refuses that name before any route runs. """ site = request.headers.get("sec-fetch-site") if site is not None: @@ -129,7 +236,7 @@ async def require_same_origin(request: Request) -> None: return else: origin = request.headers.get("origin") - if origin is None or urlsplit(origin).netloc == request.headers.get("host"): + if origin is None or _origin_is_this_server(origin, request): return raise HTTPException(status_code=403, detail="Cross-site request refused") @@ -144,11 +251,64 @@ async def lifespan(app: FastAPI): "unauthenticated. Set it before exposing PatchRadar beyond localhost.", API_KEY_ENV, ) + ignored = [item.strip() for item in os.environ.get(ALLOWED_HOSTS_ENV, "").split(",") + if item.strip() and _parse_authority(item.strip()) is None] + if ignored: + logger.warning("%s: ignoring %r, which are not host or host:port", ALLOWED_HOSTS_ENV, ignored) yield app = FastAPI(title="PatchRadar", version=pkg_version("patchradar"), lifespan=lifespan) app.mount("/static", StaticFiles(directory=FilePath(__file__).parent / "static"), name="static") + +class RequireKnownHost: + """Answer only to a Host this server is: every route, reads and /static too. + + DNS rebinding: a page on rebind.evil.example is served by the attacker, + then the name is made to resolve to 127.0.0.1. The browser still counts the + page and http://rebind.evil.example:8000 as one origin — Sec-Fetch-Site + says same-origin, Origin equals Host — and lets the page read the answers. + Only Host gives it away, and it was never looked at: the page could add to + the watchlist and read it, the software installed and its versions. + + 421 Misdirected Request (RFC 9110, 15.5.20) for a well-formed Host that is + not this server: the request reached a server that will not answer for that + target. 400 for a Host that is missing or not a host[:port] at all, as + RFC 9112, 3.2 asks. + + Plain ASGI rather than `@app.middleware("http")`: a second one of those + made the API benchmarks 14-18% slower (CodSpeed on this change). + """ + + def __init__(self, app: ASGIApp) -> None: + self.app = app + + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: + if scope["type"] != "http": + await self.app(scope, receive, send) + return + raw = Request(scope).headers.get("host", "") + authority = _parse_authority(raw) + if authority is None: + refusal = JSONResponse({"detail": "Missing or malformed Host header"}, status_code=400) + elif not _answers_to(*authority, scope): + logger.warning("refused a request for host %r: not one this server answers to (%s)", + raw[:100], ALLOWED_HOSTS_ENV) + refusal = JSONResponse( + {"detail": f"This server does not answer to that host; name it in {ALLOWED_HOSTS_ENV} if it should"}, + status_code=421, + ) + else: + await self.app(scope, receive, send) + return + await refusal(scope, receive, send) + + +# Added before `security_headers`, which therefore wraps it: the refusals carry +# the same headers as every other answer. +app.add_middleware(RequireKnownHost) + + @app.middleware("http") async def security_headers(request: Request, call_next) -> Response: """Add security headers to all responses — defense in depth against XSS and clickjacking.""" diff --git a/tests/test_affected_conditions.py b/tests/test_affected_conditions.py index 7ef9b25..4cc5665 100644 --- a/tests/test_affected_conditions.py +++ b/tests/test_affected_conditions.py @@ -211,7 +211,7 @@ def test_the_cli_scan_reads_the_edition_of_the_watched_cpe(upstream_9680): async def test_the_api_scan_reads_the_edition_of_the_watched_cpe(upstream_9680): - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as client: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as client: added = await client.post("/api/watchlist/firefox", params={"version": "115.17.0", "cpe": FIREFOX_ESR}) assert added.status_code == 200, added.text body = (await client.post("/api/scan", params={"days": 7})).json() diff --git a/tests/test_api_versioned.py b/tests/test_api_versioned.py index fe3bd60..0ab7c3d 100644 --- a/tests/test_api_versioned.py +++ b/tests/test_api_versioned.py @@ -32,7 +32,7 @@ @pytest.fixture async def client(): - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: yield ac diff --git a/tests/test_collector_failures.py b/tests/test_collector_failures.py index 067e18c..3b11f9b 100644 --- a/tests/test_collector_failures.py +++ b/tests/test_collector_failures.py @@ -213,7 +213,7 @@ def test_collector_error_is_readable(): @pytest.fixture async def client(): await add_to_watchlist("nginx") - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://t") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: yield ac diff --git a/tests/test_csp.py b/tests/test_csp.py index f2eb33a..d915df0 100644 --- a/tests/test_csp.py +++ b/tests/test_csp.py @@ -35,7 +35,7 @@ @pytest.fixture async def client(): - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: yield ac diff --git a/tests/test_health.py b/tests/test_health.py index 2fe1e2c..206934c 100644 --- a/tests/test_health.py +++ b/tests/test_health.py @@ -27,7 +27,7 @@ def isolated_db(tmp_path, monkeypatch): @pytest.fixture async def client(): await database.init_db() - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: yield ac diff --git a/tests/test_host_check.py b/tests/test_host_check.py new file mode 100644 index 0000000..c88759f --- /dev/null +++ b/tests/test_host_check.py @@ -0,0 +1,277 @@ +"""The server answers only to the names it is reached by on this machine. + +DNS rebinding: a page on rebind.evil.example is loaded from the attacker's +server, then the name is made to resolve to 127.0.0.1. The browser keeps +treating the page and every request it sends to rebind.evil.example:8000 as one +origin, so it says `Sec-Fetch-Site: same-origin`, sets `Origin` equal to `Host`, +and reads the answers. The server never looked at `Host`: the write guard passed +and the watchlist — the software installed and its versions — was served. +Found by the bug hunt of 2026-11-17 against origin/main at ad348ad. + +The ASGI scope's `server` is the address the connection arrived on: uvicorn +fills it from the socket, httpx's ASGITransport from the URL, so a base URL of +http://localhost:8000 is a server bound on port 8000. +""" +import re +from pathlib import Path + +import pytest +from httpx import ASGITransport, AsyncClient + +from patchradar.api import main as api +from patchradar.db import database + +FOREIGN = "rebind.evil.example:8000" +# The setting, by the name the documentation gives it. +ALLOWED_HOSTS_ENV = "PATCHRADAR_ALLOWED_HOSTS" + + +async def _request(method: str, path: str, headers: dict | None = None, + base_url: str = "http://localhost:8000"): + async with AsyncClient(transport=ASGITransport(app=api.app), base_url=base_url) as ac: + return await ac.request(method, path, headers=headers or {}) + + +@pytest.fixture(autouse=True) +def no_configured_hosts(monkeypatch): + monkeypatch.delenv(ALLOWED_HOSTS_ENV, raising=False) + + +# ─── A name the server does not answer to ──────────────────────────────────── + +@pytest.mark.asyncio +@pytest.mark.parametrize("path", [ + "/api/watchlist", "/api/cves", "/api/stats", "/api/cves/CVE-2026-0001", + "/", "/static/app.js", "/health", +]) +async def test_a_read_for_a_foreign_host_is_refused(path): + await database.add_to_watchlist("secretapp") + r = await _request("GET", path, {"Host": FOREIGN}) + assert r.status_code == 421 + assert "secretapp" not in r.text + + +@pytest.mark.asyncio +@pytest.mark.parametrize("method, path", [ + ("POST", "/api/watchlist/rebound"), + ("POST", "/api/scan"), + ("DELETE", "/api/watchlist/nginx"), +]) +async def test_a_write_for_a_foreign_host_is_refused_even_when_it_says_same_origin( + unreachable_network, method, path): + await database.add_to_watchlist("nginx") + r = await _request(method, path, {"Host": FOREIGN, "Sec-Fetch-Site": "same-origin"}) + assert r.status_code == 421 + assert await database.get_watchlist() == ["nginx"] + + +@pytest.mark.asyncio +async def test_an_origin_equal_to_a_foreign_host_is_refused(): + """A browser without Fetch Metadata: Origin was compared with the request's own Host.""" + r = await _request("POST", "/api/watchlist/rebound", + {"Host": FOREIGN, "Origin": f"http://{FOREIGN}"}) + assert r.status_code == 421 + assert await database.get_watchlist() == [] + + +@pytest.mark.asyncio +async def test_a_loopback_name_on_another_port_is_refused(): + """The browser puts the port it connected to in Host; another one arrived + through something in between, which has to be named.""" + r = await _request("GET", "/api/watchlist", {"Host": "localhost:9000"}) + assert r.status_code == 421 + + +@pytest.mark.asyncio +@pytest.mark.parametrize("host", [ + "", "localhost:99999", "localhost:80@evil.example", "evil.example/x", + # brackets around what is not an IPv6 address: urlsplit raises on them + "[1.2]", "[1.2.3.4]:8000", "[:::]", +]) +async def test_a_host_that_is_not_one_is_a_bad_request(host): + async with AsyncClient(transport=ASGITransport(app=api.app, raise_app_exceptions=False), + base_url="http://localhost:8000") as ac: + r = await ac.get("/api/watchlist", headers={"Host": host}) + assert r.status_code == 400 + + +@pytest.mark.asyncio +async def test_a_request_without_host_is_a_bad_request(): + """HTTP/1.1 requires Host (RFC 9112, 3.2). Sent straight to the ASGI app, + since an HTTP client always adds one.""" + sent: list[dict] = [] + + async def receive(): + return {"type": "http.request", "body": b"", "more_body": False} + + async def send(message): + sent.append(message) + + scope = {"type": "http", "asgi": {"version": "3.0"}, "http_version": "1.1", "method": "GET", + "scheme": "http", "path": "/api/watchlist", "raw_path": b"/api/watchlist", + "root_path": "", "query_string": b"", "headers": [], + "server": ("127.0.0.1", 8000), "client": ("127.0.0.1", 50000)} + await api.app(scope, receive, send) + assert sent[0]["status"] == 400 + + +# ─── The names it does answer to ───────────────────────────────────────────── + +@pytest.mark.asyncio +@pytest.mark.parametrize("host", [ + "localhost", "localhost:8000", "LOCALHOST:8000", + "127.0.0.1", "127.0.0.1:8000", "[::1]", "[::1]:8000", +]) +async def test_the_loopback_names_are_served(host): + await database.add_to_watchlist("nginx") + r = await _request("GET", "/api/watchlist", {"Host": host}) + assert r.status_code == 200 + assert r.json()["watchlist"] == ["nginx"] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("host", ["localhost:8000", "127.0.0.1:8000", "[::1]:8000"]) +async def test_the_ui_writes_through_each_loopback_name(unreachable_network, host): + """What the bundled UI sends: same-origin, Origin equal to the address bar.""" + r = await _request("POST", "/api/watchlist/nginx", + {"Host": host, "Origin": f"http://{host}", "Sec-Fetch-Site": "same-origin"}) + assert r.status_code == 200 + assert await database.get_watchlist() == ["nginx"] + + +@pytest.mark.asyncio +async def test_the_address_the_connection_arrived_on_is_served(): + """`patchradar serve --host 0.0.0.0` opened from another machine as + http://192.168.1.10:8000: an address, which no DNS answer can rebind.""" + r = await _request("GET", "/api/watchlist", {"Host": "192.168.1.10:8000"}, + base_url="http://192.168.1.10:8000") + assert r.status_code == 200 + + +@pytest.mark.asyncio +async def test_a_name_is_not_trusted_because_the_connection_was_made_to_it(): + """httpx's ASGITransport fills `server` from the URL, so here it holds the + foreign name; uvicorn always gives an address there. A name is never taken + from it.""" + r = await _request("GET", "/api/watchlist", base_url=f"http://{FOREIGN}") + assert r.status_code == 421 + + +# ─── Names the operator adds ───────────────────────────────────────────────── + +@pytest.mark.asyncio +@pytest.mark.parametrize("host", ["patchradar.lan", "patchradar.lan:8000", "PatchRadar.LAN:9999", + "proxy.example:8443"]) +async def test_a_configured_host_is_served(monkeypatch, host): + monkeypatch.setenv(ALLOWED_HOSTS_ENV, " patchradar.lan , proxy.example:8443,") + r = await _request("GET", "/api/watchlist", {"Host": host}) + assert r.status_code == 200 + + +@pytest.mark.asyncio +@pytest.mark.parametrize("host", ["proxy.example:9", "proxy.example", "other.lan:8000"]) +async def test_a_configured_port_is_the_only_one(monkeypatch, host): + monkeypatch.setenv(ALLOWED_HOSTS_ENV, "proxy.example:8443,patchradar.lan") + r = await _request("GET", "/api/watchlist", {"Host": host}) + assert r.status_code == 421 + + +@pytest.mark.asyncio +async def test_a_remapped_port_is_served_once_named(monkeypatch): + """`docker run -p 127.0.0.1:9000:8000`: the browser says localhost:9000 to a + server bound on 8000.""" + monkeypatch.setenv(ALLOWED_HOSTS_ENV, "localhost:9000") + r = await _request("GET", "/api/watchlist", {"Host": "localhost:9000"}) + assert r.status_code == 200 + + +@pytest.mark.asyncio +async def test_a_malformed_configured_entry_is_ignored(monkeypatch): + monkeypatch.setenv(ALLOWED_HOSTS_ENV, "*,http://x/,patchradar.lan") + assert (await _request("GET", "/api/watchlist", {"Host": "anything.example"})).status_code == 421 + assert (await _request("GET", "/api/watchlist", {"Host": "patchradar.lan"})).status_code == 200 + + +# ─── The Origin check names the hosts, not the request's Host ──────────────── + +@pytest.mark.asyncio +async def test_a_proxy_that_rewrites_host_keeps_the_configured_origin(monkeypatch, unreachable_network): + """A proxy forwarding https://patchradar.example.com to 127.0.0.1:8000 with + Host rewritten, and a browser without Fetch Metadata: the Origin is the + configured name, not the Host.""" + monkeypatch.setenv(ALLOWED_HOSTS_ENV, "patchradar.example.com") + r = await _request("POST", "/api/watchlist/nginx", + {"Host": "127.0.0.1:8000", "Origin": "https://patchradar.example.com"}) + assert r.status_code == 200 + + +@pytest.mark.asyncio +@pytest.mark.parametrize("origin", [ + "http://localhost:3000", # another program on this machine + "http://localhost", # port 80: another origin too + f"http://{FOREIGN}", + "null", + "http://localhost:8000@evil.example", + "http://localhost:8000/path", + "http://[1.2]:8000", # urlsplit raises on it: a 500 on ad348ad +]) +async def test_an_origin_the_server_does_not_answer_to_is_refused(origin): + async with AsyncClient(transport=ASGITransport(app=api.app, raise_app_exceptions=False), + base_url="http://localhost:8000") as ac: + r = await ac.post("/api/watchlist/nginx", headers={"Origin": origin}) + assert r.status_code == 403 + assert await database.get_watchlist() == [] + + +# ─── At startup ────────────────────────────────────────────────────────────── + +@pytest.mark.asyncio +async def test_startup_names_the_configured_entries_it_ignores(monkeypatch, caplog): + """`*` would read as "any host" elsewhere; here it is not a host, and the + operator is told rather than left to wonder why nothing matches.""" + monkeypatch.setenv(ALLOWED_HOSTS_ENV, "*, patchradar.lan, http://x/") + with caplog.at_level("WARNING", logger="patchradar"): + async with api.lifespan(api.app): + pass + ignored = [r.getMessage() for r in caplog.records if ALLOWED_HOSTS_ENV in r.getMessage()] + assert ignored == [f"{ALLOWED_HOSTS_ENV}: ignoring ['*', 'http://x/'], which are not host or host:port"] + + +@pytest.mark.asyncio +async def test_the_host_check_lets_the_lifespan_through(): + """Startup and shutdown carry no Host: the check is for HTTP requests only, + or uvicorn would never start the app. Driven through the ASGI app, as + uvicorn drives it.""" + incoming = iter([{"type": "lifespan.startup"}, {"type": "lifespan.shutdown"}]) + sent: list[str] = [] + + async def receive(): + return next(incoming) + + async def send(message): + sent.append(message["type"]) + + await api.app({"type": "lifespan", "asgi": {"version": "3.0"}, "state": {}}, receive, send) + assert sent == ["lifespan.startup.complete", "lifespan.shutdown.complete"] + + +@pytest.mark.asyncio +async def test_startup_is_quiet_about_valid_entries(monkeypatch, caplog): + monkeypatch.setenv(ALLOWED_HOSTS_ENV, "patchradar.lan,localhost:9000") + with caplog.at_level("WARNING", logger="patchradar"): + async with api.lifespan(api.app): + pass + assert not [r for r in caplog.records if ALLOWED_HOSTS_ENV in r.getMessage()] + + +# ─── The page itself ───────────────────────────────────────────────────────── + +def test_the_page_script_calls_the_api_by_path(): + """The UI keeps working under any name the server answers to only if it + never names a host itself.""" + script = (Path(api.__file__).parent / "static" / "app.js").read_text(encoding="utf-8") + calls = re.findall(r"\bapi\(\s*([`'\"])(.*?)\1", script) + assert calls + for _, path in calls: + assert path.startswith("/api/"), path + assert re.findall(r"\bfetch\(\s*([^,)]+)", script) == ["path"] diff --git a/tests/test_kev_by_cpe.py b/tests/test_kev_by_cpe.py index e176b5b..a54a2b3 100644 --- a/tests/test_kev_by_cpe.py +++ b/tests/test_kev_by_cpe.py @@ -125,7 +125,7 @@ async def test_a_scan_over_http_reads_kev_by_the_cpe_too(sources): import patchradar.api.main as api sources.route().mock(return_value=httpx.Response(503)) - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as client: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as client: assert (await client.post("/api/watchlist/chrome", params={"cpe": CHROME})).status_code == 200 body = (await client.post("/api/scan", params={"days": 7})).json() diff --git a/tests/test_nis2_evidence.py b/tests/test_nis2_evidence.py index 16e464f..2a591c9 100644 --- a/tests/test_nis2_evidence.py +++ b/tests/test_nis2_evidence.py @@ -400,7 +400,7 @@ async def test_reads_need_no_key_even_when_one_is_set(doc, monkeypatch): watchlist came back to a request without it. The document has to say so.""" monkeypatch.setenv(api_main.API_KEY_ENV, "a-key-for-this-test") await database.init_db() - async with AsyncClient(transport=ASGITransport(app=api_main.app), base_url="http://test") as client: + async with AsyncClient(transport=ASGITransport(app=api_main.app), base_url="http://localhost") as client: assert (await client.post("/api/watchlist/nginx")).status_code == 401 assert (await client.get("/api/watchlist")).status_code == 200 assert (await client.get("/api/cves")).status_code == 200 diff --git a/tests/test_placeholder.py b/tests/test_placeholder.py index b646986..bb14f28 100644 --- a/tests/test_placeholder.py +++ b/tests/test_placeholder.py @@ -18,7 +18,7 @@ def event_loop(): @pytest.fixture async def client(): - async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://localhost") as ac: yield ac @pytest.mark.asyncio diff --git a/tests/test_priority_stored.py b/tests/test_priority_stored.py index 7d8d817..967730e 100644 --- a/tests/test_priority_stored.py +++ b/tests/test_priority_stored.py @@ -182,7 +182,7 @@ async def test_a_short_listing_is_the_most_urgent_not_the_newest(): async def test_the_api_listing_says_why_each_row_ranks_where_it_does(): await _store(ROWS) - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as client: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as client: rows = (await client.get("/api/cves", params={"limit": 100})).json()["cves"] assert [r["id"] for r in rows] == _expected(ROWS) diff --git a/tests/test_scan_hardening.py b/tests/test_scan_hardening.py index c280be6..34a9704 100644 --- a/tests/test_scan_hardening.py +++ b/tests/test_scan_hardening.py @@ -56,7 +56,7 @@ async def client(tmp_path): for sw in WATCHED: await database.add_to_watchlist(sw) async with AsyncClient( - transport=ASGITransport(app=api.app), base_url="http://test", timeout=60 + transport=ASGITransport(app=api.app), base_url="http://localhost", timeout=60 ) as ac: yield ac @@ -314,7 +314,7 @@ async def test_a_non_ascii_key_header_is_a_401_not_a_500(monkeypatch): instead of a 401.""" monkeypatch.setenv(api.API_KEY_ENV, "secret") transport = ASGITransport(app=api.app, raise_app_exceptions=False) - async with AsyncClient(transport=transport, base_url="http://test") as ac: + async with AsyncClient(transport=transport, base_url="http://localhost") as ac: r = await ac.post("/api/watchlist/x", headers={"X-API-Key": "s\xe9cret".encode("latin-1")}) assert r.status_code == 401 @@ -325,7 +325,7 @@ async def test_a_non_ascii_configured_key_does_not_turn_a_wrong_header_into_a_50 stays a 401.""" monkeypatch.setenv(api.API_KEY_ENV, "chiavé") transport = ASGITransport(app=api.app, raise_app_exceptions=False) - async with AsyncClient(transport=transport, base_url="http://test") as ac: + async with AsyncClient(transport=transport, base_url="http://localhost") as ac: r = await ac.post("/api/watchlist/x", headers={"X-API-Key": "chiave"}) assert r.status_code == 401 @@ -439,9 +439,9 @@ async def test_the_scan_counts_each_cve_once_across_sources(empty_database, unre return_value=httpx.Response(200, json=_recorded("nvd_vcenter_2026_07_30.json"))) upstream.route(host="api.msrc.microsoft.com").pass_through() upstream.route(host="security-tracker.debian.org").pass_through() - upstream.route(host="test").pass_through() # the app itself, over ASGI + upstream.route(host="localhost").pass_through() # the app itself, over ASGI - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: r = await ac.post("/api/scan?days=7") assert r.status_code == 200 diff --git a/tests/test_watchlist_import.py b/tests/test_watchlist_import.py index 7530faa..4d3cd6c 100644 --- a/tests/test_watchlist_import.py +++ b/tests/test_watchlist_import.py @@ -41,7 +41,7 @@ async def fresh_db(): @pytest.fixture async def client(): - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: yield ac diff --git a/tests/test_watchlist_removal.py b/tests/test_watchlist_removal.py index fc1745b..ccd5ed9 100644 --- a/tests/test_watchlist_removal.py +++ b/tests/test_watchlist_removal.py @@ -113,7 +113,7 @@ async def test_removal_is_case_insensitive(): @pytest.fixture async def client(): - async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://test") as ac: + async with AsyncClient(transport=ASGITransport(app=api.app), base_url="http://localhost") as ac: yield ac