ADR-0163: The caller's address has one reader
- Status: Accepted
- Date: 2026-09-17
- Deciders: Workstream 012, NEXT row "The two remaining header-trusting IP helpers read
RemoteAddr"
Context
ADR-0160
made r.RemoteAddr trustworthy and left two helpers that read
X-Forwarded-For from any caller, recorded as gating no budget. Reading
their call sites showed otherwise:
| Helper | Callers | What a forged header chose |
|---|---|---|
middleware.realIP | AuthLimiter, AIQuotaLimiter, Blacklist, AdminAudit | the auth-lockout bucket, the anonymous AI-quota bucket, whether a blocked address is blocked, the address in the audit log |
handlers.extractIP | click record, public biolink, push subscriber | the located country and city |
inline in handlers/cookie_consent.go | consent log line | the logged address |
So the lockout ADR-0160 was written to protect was still spoofable: the
middleware fixed RemoteAddr and the limiter did not read it. Separately, the
one limiter that did read it (RateLimiter) used the whole host:port. chi's
middleware had been writing a bare host there; after its removal a deployment
with no proxy named saw the peer's ephemeral port, and every new connection
opened a new budget.
Decision
middleware.ClientIP(r) is the only way this API names its caller. It returns
the host of r.RemoteAddr and reads no header. realIP, extractIP and the
inline read are removed; every caller above, and RateLimiter's key, use it.
The two tests that pinned header-trusting as "parity with the legacy application" are replaced by tests asserting the opposite. That is a deliberate departure from legacy behaviour, not a regression.
Consequences
A deployment behind a proxy that has not set
TRUSTED_PROXIESnow geolocates every click to the proxy and blocks or admits by the proxy's address. Before, it geolocated to whatever the caller claimed. The key is the fix in both cases anddocs/DEPLOYMENT.mdsays so beside it.The public limiter's budget is now per address rather than per connection, which is what "per IP" always claimed. A stack that drives the whole public surface from one host would spend the production 300 in seconds, so the ceiling is a key,
PUBLIC_RATE_LIMIT_PER_MINUTE; the dev compose file and the CI E2E job set 6000 and production leaves it. The authenticated limiter keys on the token and is unaffected.handlers/auth.goandhandlers/oauth.gostill passr.RemoteAddrwith its port to the CAPTCHA provider and the login audit row. Neither trusts a header; both are a ROADMAP → NEXT row.A new reader of
X-Forwarded-FororX-Real-IPoutsidemiddleware/realip.gois a defect. There is no gate for it yet;grepis the check, and it is clean on this change.Amended 2026-09-17. There is one now:
npm run check:client-addressinverify(rules A1–A3), which also refuses.RemoteAddroutside that file — the login audit row and the CAPTCHAremoteipwere still taking it with the port.
Verification
internal/middleware/clientip_test.go runs Blacklist, AuthLimiter and
RateLimiter behind RealIP as cmd/server wires them and sends forged
headers from an untrusted peer; handlers/redirect_test.go does the same for
the click record. Each was watched failing against a ClientIP that read the
header, and the limiter test against the host:port key.
Source: docs/decisions/0163-the-callers-address-has-one-reader.md