Context
The throttler keys every limit on req.ip (ADR-0030), and LOGIN_THROTTLE is 5 a minute. Two paths reach the API in production:
- The web BFF on Vercel (ADR-0022) calls the API server-side, so the socket peer is one of Vercel's shared egress addresses. Every visitor landed in the same login bucket: five failed sign-ins anywhere locked the whole site out for a minute. The web already forwarded
X-Forwarded-For, but it was ignored, correctly: Vercel is not a trusted hop, and it must not become one — the API is publicly reachable, so trusting a public hop's header would let anyone forge it and pick their own bucket. - Direct callers (curl, a future mobile app, an attacker) through Render's edge.
trust proxywas hard-coded to'loopback, uniquelocal'for Caddy (ADR-0006); on Render the hop was untested (architecture review, item 21).
What the platform documents, and what it does not:
- All Render web-service traffic passes Cloudflare, then Render's load balancer, then the instance; Render says to read the client from
X-Forwarded-For(How Render handles DDoS attacks). - Cloudflare appends the connecting address to an existing
X-Forwarded-For(Cloudflare HTTP headers), and a user test on Render's tracker confirms Render does not clear a client-supplied one (feedback.render.com). So the left-most entry — what Render's own example keys on, and whattrust proxy = truereturns — is attacker-controlled. - Render does not document how many entries its load balancer adds, nor the address range it connects from. We do not guess either; we make it configurable and verify it once per deploy.
Decision
The BFF vouches for the visitor with a shared secret.
BFF_SHARED_SECRET(optional, ≥ 32 chars, server-only on both sides) is set to the same value on Render and in the Vercel project.- On every call it makes for a visitor, the web server sends
x-bff-secret: <secret>andx-bff-client-ip: <visitor>, the visitor being the firstx-forwarded-forentry Vercel gives it, elsex-real-ip(Vercel overwrites both). It never relays an incomingx-bff-*header. BffThrottlerGuardreplaces the stockThrottlerGuardas the firstAPP_GUARD. Its tracker usesx-bff-client-iponly when the secret is configured,x-bff-secretis present once and equal (SHA-256 of both, thentimingSafeEqual), and the value parses as an IP. Otherwise it isreq.ip, exactly as before. The vouched address goes through the stock normalisation (IPv4-mapped unwrapped, IPv6 folded to /64).
trust proxy becomes TRUST_PROXY, default loopback, uniquelocal.
- Accepted: a hop count,
false, or a list of addresses / CIDRs /loopbacklinklocaluniquelocal.trueis refused at boot — it trusts the forgeable left-most entry. - The default is right behind Caddy (private peer, which overwrites the header). On Render it is right if the load balancer reaches the instance from a private address and adds nothing after Cloudflare's entry: Express then stops at the first untrusted entry from the right, which is the address Cloudflare saw. A client-written entry sits further left and is never reached.
- Verification, once per new host: set
LOG_LEVEL=debug,curl -H 'X-Forwarded-For: 192.0.2.1'/api/v1/health/live, and read theclient ip=… peer=… xff=…line.ipmust be your own public address (not192.0.2.1, not a Cloudflare or Render address). If the peer is public, oripis a proxy address, setTRUST_PROXYto your address's position inxffcounted from the right (1= the last entry) and repeat. Then putLOG_LEVELback.
Alternatives considered
- Trust Vercel as a proxy hop — its egress ranges are not published as stable, and the API is public: anyone could send
X-Forwarded-Forfrom any address and choose their bucket. - Key the login limit on the email instead of the IP — lets anyone lock a known account out, and does nothing for the other throttled routes.
- Cloudflare's
CF-Connecting-IP— undocumented on Render's side and would tie the API to Render staying on Cloudflare. - Signing each request (HMAC over method, path and timestamp) — resists replay of a captured secret, but the secret only ever travels server-to-server over TLS; the extra moving parts are not worth it for a rate-limit key. Revisit if the header ever gates anything but throttling.
Consequences
- Visitors behind the BFF are throttled one by one again; direct callers keep their own
req.ipbucket, and a spoofedx-bff-client-ipwithout the secret changes nothing. - The secret is rotated on both sides together. While the two values differ, BFF traffic falls back to one shared bucket (nothing breaks, sign-ins are just throttled together), so rotate in a quiet window and redeploy both services back to back.
- Anyone holding the secret can choose their bucket. It is a server-only value, never
NEXT_PUBLIC_, and is never logged. TRUST_PROXYmust be verified on Render after the first deploy with this change (recipe above) and whenever the platform's edge changes.