Skip to content

fix(api): refuse requests for a Host the server does not answer to (DNS rebinding) - #91

Merged
maksimtech merged 2 commits into
mainfrom
fix/tool-security
Nov 17, 2026
Merged

maksimtech merged 2 commits into
mainfrom
fix/tool-security

Conversation

@maksimtech

@maksimtech maksimtech commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

DNS rebinding bypassed the CSRF guard: the server never checked Host

What was false. README: "a page you visit cannot add to your watchlist or start scans through your browser". 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. It sends Sec-Fetch-Site: same-origin and an Origin equal to Host, and lets the page read the answers. require_same_origin trusted the first and compared Origin with the request's own Host, and nothing looked at Host.

Evidence. Bug hunt of 2026-11-17 against ad348ad (_hunt/tests/test_hunt_cli_api.py::test_api_refuses_a_foreign_host_header): with Host: rebind.evil.example:PORT and Sec-Fetch-Site: same-origin a watchlist entry was added, and GET /api/watchlist (installed software and versions) was served.

Fix (src/patchradar/api/main.py), an HTTP middleware that runs before every route, reads and /static included:

  • 421 Misdirected Request (RFC 9110 §15.5.20: the request reached a server that will not answer for that target) unless Host is:
    • localhost, 127.0.0.1 or [::1] on the port the connection arrived on, or with no port;
    • the IP address the connection arrived on. It is taken from the ASGI scope's server, which uvicorn fills from the socket, and only when it is an IP. An address cannot be rebound, and this keeps serve --host 0.0.0.0 reachable at the LAN address;
    • a name in the new PATCHRADAR_ALLOWED_HOSTS. It is comma-separated: name allows any port, name:port only that port. There is no wildcard, and malformed entries such as * are ignored with a warning at startup.
  • 400 when Host is missing or is not host[:port] (RFC 9112 §3.2).
  • The write guard keeps Sec-Fetch-Site. For browsers that don't send it, Origin is now compared with those same hosts, port included (the scheme's default port when it has none), instead of with the request's Host. A proxy that rewrites Host keeps working for a configured Origin. http://localhost:3000 and http://localhost (port 80) stay refused against a server on 8000.

Compatibility. The default setups need no change:

  • patchradar serve on 127.0.0.1;
  • docker-compose.yml with 127.0.0.1:8000:8000;
  • the healthcheck on localhost:8000;
  • the UI, which calls the API by path only (now asserted by a test).

A reverse proxy that passes its own Host, a LAN name, or a port that Docker remaps (-p 127.0.0.1:9000:8000) needs its name added to PATCHRADAR_ALLOWED_HOSTS. docker-compose.yml and .env.example pass the variable through. The in-process test clients used made-up hosts (http://test, http://t, http://bench) and now use http://localhost.

Docs. README (Web UI and Security), SECURITY.md, docs/nis2-compliance.md §2, CLAUDE.md, and CHANGELOG [Unreleased] → Security.

Tests. New file tests/test_host_check.py, 45 cases (TDD: 23 failed before the fix for the right reason, 200 instead of 421/400 and a 403 for the proxy case; all pass after). It covers:

  • a foreign Host on 7 reads (API, /, /static/app.js, /health) and on 3 writes, even with same-origin;
  • an Origin equal to a foreign Host;
  • loopback names with and without a port;
  • a wrong port, and a malformed or missing Host;
  • configured names, configured ports and a remapped port, plus malformed config entries;
  • a proxy-rewritten Host with a configured Origin;
  • foreign, other-port and null origins;
  • app.js calling the API by relative path only.

The hunt's evidence test passes against the branch. Full suite: 2361 passed / 4 skipped before → 2410 passed / 4 skipped after. ruff check and mypy are clean.

Run for real. patchradar serve --host 127.0.0.1 --port 62222 with HOME isolated, tested with curl:

  • These work: /health, /, /static/app.js, GET /api/watchlist through localhost, 127.0.0.1, [::1] (Host) and localhost (no port), and POSTs with same-origin and with Origin: http://localhost:62222.
  • Host: rebind.evil.example:62222 gets 421 on /, /static/app.js and /api/watchlist, and on POST with Sec-Fetch-Site: same-origin or with Origin equal to Host. Host: localhost:9999 also gets 421.
  • Origin: http://rebind.evil.example:62222 or http://localhost:3000 gets 403. The 421 responses carry the CSP and other security headers.
  • With PATCHRADAR_ALLOWED_HOSTS=patchradar.lan,*: patchradar.lan[:port] gets 200, any other name gets 421, and * is logged as ignored.

Second commit (perf(api): …) responds to this PR's CI:

  • CodSpeed: −14 to −18% on three API benchmarks. The Host check was a second @app.middleware("http"), which adds a full BaseHTTPMiddleware layer. It is now a plain ASGI class. Measured locally (pytest-codspeed walltime, test_api_watchlist): main 12.1 µs, first commit 14.6 µs, now 12.1 µs.
  • codecov/patch. Tests now cover the lifespan passing through the middleware (driven via the ASGI app) and the startup warning for ignored entries.
  • A 500. urlsplit raises on brackets around something that is not an IPv6 address. Origin: http://[1.2]:8000 was a 500 on main too, and is now a 403; Host: [1.2] is a 400. Host and Origin are parsed with one regex and ipaddress.

The full suite after both commits: 2419 passed / 4 skipped.

🤖 Generated with Claude Code

…NS rebinding)

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: it sends Sec-Fetch-Site: same-origin and an Origin equal
to Host, and lets the page read the answers. The CSRF guard accepted both,
since it trusted Sec-Fetch-Site and compared Origin with the request's own
Host. The bug hunt of 2026-11-17 (against ad348ad) added a watchlist entry
and read GET /api/watchlist, the software installed and its versions, with
Host: rebind.evil.example:8000, so the README's "a page you visit cannot
add to your watchlist" did not hold.

A middleware now runs before every route, reads and /static included:

- 421 Misdirected Request (RFC 9110, 15.5.20) unless Host is localhost,
  127.0.0.1 or [::1] on the port the connection arrived on (or with no
  port), the address the connection arrived on (an IP taken from the ASGI
  scope's server, which uvicorn fills from the socket; an address cannot
  be rebound, and this keeps `serve --host 0.0.0.0` reachable at the LAN
  address), or a name in the new PATCHRADAR_ALLOWED_HOSTS (comma-separated,
  `name` for any port, `name:port` for that one; no wildcard, malformed
  entries are ignored and logged at startup);
- 400 when Host is missing or not a host[:port] (RFC 9112, 3.2).

The write guard keeps Sec-Fetch-Site; for a browser without it, Origin is
compared with those same hosts, port included (the scheme's default when
absent), instead of with the request's Host. A proxy that rewrites Host
keeps working for a configured Origin; http://localhost:3000 or
http://localhost (port 80) stay refused against a server on 8000.

The in-process test clients used made-up hosts (http://test, http://t,
http://bench) and now use http://localhost, as a real client would.
docker-compose.yml and .env.example pass PATCHRADAR_ALLOWED_HOSTS through;
the default compose setup (127.0.0.1:8000:8000, healthcheck on
localhost:8000) needs nothing. README, SECURITY.md, docs/nis2-compliance.md
and CLAUDE.md describe the check and the variable.

Tests: tests/test_host_check.py (foreign Host on reads, static files and
writes; Origin equal to a foreign Host; loopback names with and without
port; wrong port; malformed and missing Host; configured names and ports;
proxy-rewritten Host with a configured Origin; foreign and other-port
Origins; app.js calling the API by path only). Seen failing before the fix
(23 of 45), passing after. Also verified on a real `patchradar serve` bound
to 127.0.0.1 with curl.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 11 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 17 untouched benchmarks


Comparing fix/tool-security (fb51d4e) with main (ad348ad)

Open in CodSpeed

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…in without urlsplit

CodSpeed on #91 measured the API benchmarks 14-18% slower
(test_api_watchlist, test_api_cve_detail, test_api_index): the Host check
was a second `@app.middleware("http")`, and each of those is a full
BaseHTTPMiddleware layer. It is now a plain ASGI class added with
add_middleware, still inside `security_headers` so the 421 and 400 carry
the same headers. Measured locally with pytest-codspeed walltime, two runs
each: watchlist 12.1/12.2 us on main, 14.6/13.3 with the first commit,
12.1/11.8 now; index 10.8/10.8, 12.7/12.7, 11.1/11.0.

urlsplit raises ValueError on brackets around something that is not an
IPv6 address. Host and Origin are now parsed with one regex and
ipaddress, so `Host: [1.2]` is a 400 and `Origin: http://[1.2]:8000` a
403. The latter was a 500 on main too, since the old guard called
urlsplit(origin). Starlette is not imported: it is FastAPI's dependency,
not one this project declares.

Tests: bracketed non-IPv6 Host values and Origin (seen as 500 before,
400/403 after), an Origin with a path, the lifespan passing through the
middleware via the ASGI app, and the startup warning for ignored entries
in PATCHRADAR_ALLOWED_HOSTS (codecov/patch flagged those lines).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@maksimtech
maksimtech enabled auto-merge (squash) November 17, 2026 22:42
@maksimtech
maksimtech merged commit 419acdf into main Nov 17, 2026
21 checks passed
@maksimtech
maksimtech deleted the fix/tool-security branch November 17, 2026 22:43
@sonarqubecloud

Copy link
Copy Markdown

maksimtech added a commit that referenced this pull request Nov 18, 2026
The bug hunt of 2026-11-17 and its fixes (#89, #91), the law check carrying
mailradar#103, the OpenSSL exceptions of 2026-11-10 and the upkeep since
2026.48 ship as 2026.49. RELEASING.md wants the section in place before the
bump; [Unreleased] stays, empty, above it.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant