Repository navigation
fix(api): refuse requests for a Host the server does not answer to (DNS rebinding) - #91
Merged
Merged
Conversation
…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>
Merging this PR will not alter performance1 warning
Comparing |
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



DNS rebinding bypassed the CSRF guard: the server never checked
HostWhat 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:8000for the browser. It sendsSec-Fetch-Site: same-originand anOriginequal toHost, and lets the page read the answers.require_same_origintrusted the first and comparedOriginwith the request's ownHost, and nothing looked atHost.Evidence. Bug hunt of 2026-11-17 against
ad348ad(_hunt/tests/test_hunt_cli_api.py::test_api_refuses_a_foreign_host_header): withHost: rebind.evil.example:PORTandSec-Fetch-Site: same-origina watchlist entry was added, andGET /api/watchlist(installed software and versions) was served.Fix (
src/patchradar/api/main.py), an HTTP middleware that runs before every route, reads and/staticincluded:Hostis:localhost,127.0.0.1or[::1]on the port the connection arrived on, or with no port;server, which uvicorn fills from the socket, and only when it is an IP. An address cannot be rebound, and this keepsserve --host 0.0.0.0reachable at the LAN address;PATCHRADAR_ALLOWED_HOSTS. It is comma-separated:nameallows any port,name:portonly that port. There is no wildcard, and malformed entries such as*are ignored with a warning at startup.Hostis missing or is nothost[:port](RFC 9112 §3.2).Sec-Fetch-Site. For browsers that don't send it,Originis now compared with those same hosts, port included (the scheme's default port when it has none), instead of with the request'sHost. A proxy that rewritesHostkeeps working for a configuredOrigin.http://localhost:3000andhttp://localhost(port 80) stay refused against a server on 8000.Compatibility. The default setups need no change:
patchradar serveon 127.0.0.1;docker-compose.ymlwith127.0.0.1:8000:8000;localhost:8000;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 toPATCHRADAR_ALLOWED_HOSTS.docker-compose.ymland.env.examplepass the variable through. The in-process test clients used made-up hosts (http://test,http://t,http://bench) and now usehttp://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:Hoston 7 reads (API,/,/static/app.js,/health) and on 3 writes, even withsame-origin;Originequal to a foreignHost;Host;Hostwith a configuredOrigin;nullorigins;app.jscalling 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 checkandmypyare clean.Run for real.
patchradar serve --host 127.0.0.1 --port 62222with HOME isolated, tested with curl:/health,/,/static/app.js,GET /api/watchlistthroughlocalhost,127.0.0.1,[::1](Host) andlocalhost(no port), and POSTs withsame-originand withOrigin: http://localhost:62222.Host: rebind.evil.example:62222gets 421 on/,/static/app.jsand/api/watchlist, and on POST withSec-Fetch-Site: same-originor withOriginequal toHost.Host: localhost:9999also gets 421.Origin: http://rebind.evil.example:62222orhttp://localhost:3000gets 403. The 421 responses carry the CSP and other security headers.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:@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.urlsplitraises on brackets around something that is not an IPv6 address.Origin: http://[1.2]:8000was a 500 on main too, and is now a 403;Host: [1.2]is a 400. Host and Origin are parsed with one regex andipaddress.The full suite after both commits: 2419 passed / 4 skipped.
🤖 Generated with Claude Code