ADR-0160: A client address is taken only from a proxy the deployment names
- Status: Accepted
- Date: 2026-09-09
- Deciders: Workstream 012, NEXT row "CI green on
main"
Context
The first staticcheck run after the pin (2026.1) reported SA1019 on
chimw.RealIP in cmd/server/main.go: chi deprecated it because it rewrites
r.RemoteAddr to the leftmost X-Forwarded-For value, or to X-Real-IP,
from any caller (GHSA-3fxj-6jh8-hvhx). Two things in this tree key on
RemoteAddr: the authenticated-API rate limiter and the auth lockout
(middleware/ratelimit.go). With that middleware in place, a client that set
the header chose which budget it spent from, so the brute-force lockout was
one header away from not existing.
Decision
middleware.RealIP(trusted) replaces it. A forwarded address is read only
when the TCP peer is inside a network the deployment names in
TRUSTED_PROXIES (comma-separated CIDRs or addresses), and the address taken
is the rightmost X-Forwarded-For entry that is not itself a trusted proxy —
the hop the outermost trusted proxy appended, which the caller could not
forge. X-Real-IP is honoured only from a trusted peer and only when no
X-Forwarded-For is present. Unset, the middleware is a no-op and
RemoteAddr is the peer.
The unset default errs toward counting every proxied request as one client, which throttles a proxied deployment that forgot the key too hard rather than not at all; the deployment document names the key beside the rate limits so the omission is visible.
Consequences
A deployment behind Caddy, nginx or a load balancer sets
TRUSTED_PROXIESto that hop's address or range;deploy/compose.prod.ymlsets the compose network. Without it, per-IP limits apply per proxy.Two helpers still read the headers directly and are as spoofable as chi's middleware was:
middleware/maintenance.go'srealIP(the maintenance allowlist) andhandlers/redirect.go'sextractIP(the click record's geolocation). Neither gates a budget; both are a ROADMAP → NEXT row to move ontoRemoteAddrnow that it is trustworthy.Amended 2026-09-17. "Neither gates a budget" was wrong:
realIPwas also what the auth lockout, the AI quota and the admin audit log called, so the lockout stayed one header away from not existing until ADR-0163 removed both helpers.deploy/compose.prod.ymldid not set the key either, whatever the first bullet says; it does from the same change, to the web container's fixed address rather than the network.
Source: docs/decisions/0160-a-client-address-is-taken-only-from-a-proxy-the-deployment-names.md