Render web services: X-Forwarded-For is client-forgeable; key per-IP limits on CF-Connecting-IP
A FastAPI/uvicorn service on Render (web service with a custom domain) used uvicorn's ProxyHeadersMiddleware(trusted_hosts=['*']) so request.client.host would be the real visitor for per-IP rate limits. In production, curl -H 'X-Forwarded-For: 203.0.113.77' https://<service>/... got a brand-new rate-limit bucket on every request and the uvicorn access log recorded 203.0.113.77 as the client, so any client can evade every per-IP limit by rotating the header. Render's answer on their feedback board says the first X-Forwarded-For entry is the real client, and there is no dashboard or render.yaml setting to strip or replace a client-supplied header. Trusting only known proxy hops does not help with a leftmost parser, and Render's inbound IP allow-lists for web services are Scale/Enterprise only. A second Render service (SvelteKit SSR) also calls the API server-side and needs its visitor's IP honoured, but its egress IP is in a shared Render range.
Root cause: Render's edge appends to whatever X-Forwarded-For the client sent; it never clears it (user test on https://feedback.render.com/features/p/send-the-correct-x-forwarded-for, and the prod probe in the question). Any parser that trusts the leftmost entry is spoofable.
What is unforgeable: Render's edge is Cloudflare (DNS for the service ends in *.cdn.cloudflare.net; responses carry server: cloudflare). Cloudflare adds CF-Connecting-IP (and True-Client-IP) to every origin request, and it rejects requests where the client supplies CF-Connecting-IP itself:
$ curl -s -w ' %{http_code}\n' -H 'CF-Connecting-IP: 203.0.113.77' https://<service>/healthz
error code: 1000 403(Cloudflare error 1000: "The request includes a CF-Connecting-IP header".) A forged True-Client-IP passes the edge with 200 and whether it is overwritten before the origin is not observable, so trust only CF-Connecting-IP. Render documents neither header.
Fix (ASGI): drop ProxyHeadersMiddleware and set scope['client'] yourself; never read XFF:
class ClientAddressMiddleware:
def __init__(self, app, *, ssr_forward_secret: str | None):
self.app = app
self._secret = ssr_forward_secret.encode() if ssr_forward_secret else None
async def __call__(self, scope, receive, send):
if scope["type"] in ("http", "websocket"):
h = dict(scope["headers"])
host = None
if self._secret and hmac.compare_digest(h.get(b"x-app-ssr-secret", b""), self._secret):
host = parse_ip(h.get(b"x-app-client-ip")) # SSR server forwarding its visitor
host = host or parse_ip(h.get(b"cf-connecting-ip"))
if host:
scope["client"] = (host, 0)
return await self.app(scope, receive, send)For a server-side renderer on Render calling the API, the edge header the API sees is the renderer's egress IP (shared across Render customers), so forward the visitor IP under a custom header plus a shared secret. In render.yaml: generateValue: true on the API's env var and fromService: {type: web, name: <api>, envVarKey: ...} on the renderer. In the renderer, read the visitor from its own inbound cf-connecting-ip (socket peer off-edge). Never forward it under the name cf-connecting-ip, or Cloudflare 403s the server-side call. Add the secret header name to Sentry's EventScrubber denylist, since request headers are captured.
Verified in prod: three requests with rotating forged XFF now count down one bucket (29, 28, 27); both the direct request and the SSR-proxied request are access-logged as the real client. Re-run the error-1000 probe if Render ever changes its edge.