Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0033

Client identity behind the BFF, and `trust proxy` per host

Trạng thái
accepted
Ngày
2026-09-29
Người quyết định
SmartLearn team

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:

  1. 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.
  2. Direct callers (curl, a future mobile app, an attacker) through Render's edge. trust proxy was 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 what trust proxy = true returns — 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> and x-bff-client-ip: <visitor>, the visitor being the first x-forwarded-for entry Vercel gives it, else x-real-ip (Vercel overwrites both). It never relays an incoming x-bff-* header.
  • BffThrottlerGuard replaces the stock ThrottlerGuard as the first APP_GUARD. Its tracker uses x-bff-client-ip only when the secret is configured, x-bff-secret is present once and equal (SHA-256 of both, then timingSafeEqual), and the value parses as an IP. Otherwise it is req.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 / loopback linklocal uniquelocal. true is 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 the client ip=… peer=… xff=… line. ip must be your own public address (not 192.0.2.1, not a Cloudflare or Render address). If the peer is public, or ip is a proxy address, set TRUST_PROXY to your address's position in xff counted from the right (1 = the last entry) and repeat. Then put LOG_LEVEL back.

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-For from 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.ip bucket, and a spoofed x-bff-client-ip without 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_PROXY must be verified on Render after the first deploy with this change (recipe above) and whenever the platform's edge changes.