A FastAPI app run with --forwarded-allow-ips="*" rate-limits on request.client.host; how can a client evade the limit, and what should the setting be?
answer
- who wrote the header first
- leftmost versus rightmost entry
- trusted hops are skipped
- a list or a network, not a star
basics
~20 sWith "*", Uvicorn takes the leftmost X-Forwarded-For entry, which a client can forge and an appending proxy passes along. Set --forwarded-allow-ips to the proxy's address or network so Uvicorn picks the nearest untrusted hop instead.
solid answer
~40 s`--forwarded-allow-ips="*"` makes Uvicorn's `ProxyHeadersMiddleware` trust every peer, and in that mode it sets `request.client` to the **leftmost** `X-Forwarded-For` entry. A proxy that appends to an incoming header keeps whatever the client sent at the front, so a client sending `X-Forwarded-For: 198.51.100.66` gets a new identity on each request; if the app's port is reachable directly, anyone can set the header outright. List the proxy instead: `--forwarded-allow-ips="10.0.0.2"` or a network like `10.0.0.0/8`. Uvicorn then reads the header from the right and takes the first address that is not a trusted proxy, the one your proxy actually saw. `"*"` is safe only when nothing but the proxy reaches the port and every proxy replaces the header rather than appending.
code
python · 22 linesfrom fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
app = FastAPI()
@app.get("/whoami")
def whoami(request: Request):
return {"client": request.client.host}
def seen_client(trusted_hosts: str) -> str:
wrapped = ProxyHeadersMiddleware(app, trusted_hosts=trusted_hosts)
# The proxy at 10.0.0.2 appended the real peer to a value the client forged.
client = TestClient(wrapped, base_url="http://api.internal", client=("10.0.0.2", 4000))
r = client.get("/whoami", headers={"X-Forwarded-For": "198.51.100.66, 203.0.113.9"})
return r.json()["client"]
print(seen_client("*")) # 198.51.100.66, the forged entry
print(seen_client("10.0.0.2")) # 203.0.113.9, the real peergo deeper
Remember that X-Forwarded-For is just a header a client can send, and that the server must be told which proxies to believe.
Explain how Uvicorn chooses the client address: the leftmost entry under "*", the rightmost untrusted entry under an explicit list.
Show the evasion with a forged header through an appending proxy, set a precise trust list or network, and verify it with a test request.
Weigh rate limits in each app against limits at the edge, and own the trusted proxy ranges as platform configuration that changes when the network does.
## What request.client.host reports `request.client.host` in a FastAPI path operation is the first element of the ASGI scope's `client`. Uvicorn sets it to the peer of the TCP connection, which behind a reverse proxy is the proxy. When the peer is trusted, Uvicorn's `ProxyHeadersMiddleware` replaces it with an address taken from `X-Forwarded-For`. Which address it takes depends on `--forwarded-allow-ips` (the `trusted_hosts` of the middleware): | `--forwarded-allow-ips` | Who is trusted | Entry chosen from `X-Forwarded-For` | |---|---|---| | unset | `$FORWARDED_ALLOW_IPS`, else `127.0.0.1,::1` | rightmost entry not in the list | | `"10.0.0.2"` or `"10.0.0.0/8"` | that address or network | rightmost entry not in the list | | `"*"` | every peer | the **leftmost** entry | The list form also accepts several comma-separated values, IPv6 addresses and literals such as a UNIX socket path. If every entry in the header is trusted, the leftmost is used. ## How the limit is evaded `X-Forwarded-For` is a list each proxy appends to. A client can send its own value first: ```http GET /whoami HTTP/1.1 Host: api.example.com X-Forwarded-For: 198.51.100.66 ``` A proxy that appends forwards `X-Forwarded-For: 198.51.100.66, 203.0.113.9`, where `203.0.113.9` is the real client it saw. With `"*"`, Uvicorn takes the leftmost entry, so the app sees `198.51.100.66`, a value the client chose. Changing it on every request gives the client a fresh rate-limit bucket each time; setting a victim's address can get the victim blocked. If the application server's port is reachable without the proxy, an attacker needs no proxy at all: any peer is trusted, so the header is believed as sent. `X-Forwarded-Proto` is believed the same way, so a direct caller can claim `https`. ## What the setting should be Trust exactly the hops you run: 1. Find the address or network the proxy connects from. 2. Start the server with `fastapi run --forwarded-allow-ips="10.0.0.2"`, or a network such as `"10.0.0.0/8"` when proxy replicas move, or several values separated by commas. 3. With a list, the middleware walks `X-Forwarded-For` from the right, skips every trusted proxy, and uses the first address that is not trusted: `203.0.113.9` in the example above. The forged entry sits to its left and is never reached. 4. A request whose peer is not trusted keeps its peer address, so a direct caller cannot change its identity. With several proxies in a chain, list all of them; each one's address appears in the header and has to be skipped. A network range that is too broad, such as one covering the clients themselves, brings the spoofing back. ## A chain of two proxies Suppose a load balancer at `10.0.1.5` forwards to an inner proxy at `10.0.0.2`, which forwards to Uvicorn. The header arriving at Uvicorn reads `X-Forwarded-For: 198.51.100.66, 203.0.113.9, 10.0.1.5`: the forged value, the real client the load balancer saw, and the load balancer as the inner proxy saw it. - With `--forwarded-allow-ips="10.0.0.2"` only, Uvicorn stops at the load balancer: `10.0.1.5` is the rightmost untrusted entry, so every request appears to come from the load balancer and the rate limit throttles all users together. - With `--forwarded-allow-ips="10.0.0.2,10.0.1.5"` or `"10.0.0.0/16"`, Uvicorn skips both proxies and reports `203.0.113.9`. - With `"*"`, it reports `198.51.100.66`, the forged value. Missing a hop is the safe failure, one shared identity; trusting everyone is the unsafe one, an identity the client chooses. ## When "*" is acceptable Uvicorn's documentation states the condition: trust everything only when you can trust every value in the forwarded headers, for example because your proxies remove the existing headers before setting their own. In practice that needs both: - the application server is reachable only from the proxy, not from clients or other workloads; - the proxy **replaces** `X-Forwarded-For` instead of appending to it. FastAPI's deployment docs suggest `"*"` for a server that only the proxy can reach. That is enough for the scheme, provided the proxy sets `X-Forwarded-Proto` itself; it is not enough for client addresses if the proxy appends. ## Defending the rate limit - Rate-limit on the address Uvicorn derives through an explicit trust list, rather than on a header your app parses itself. - Keep the application server off the public network so no one can skip the proxy. - Test it: send a forged `X-Forwarded-For` through the proxy and confirm `request.client.host` does not change. How the forwarded headers are defined, and how a proxy should be configured to write them, belongs to the HTTP header and proxy lessons; here the point is which address Uvicorn hands to FastAPI.
- With --forwarded-allow-ips="10.0.0.0/8" and X-Forwarded-For: 198.51.100.66, 203.0.113.9, 10.9.9.9 from a peer at 10.1.2.3, which client address does the app see?`203.0.113.9`. The peer is trusted, so Uvicorn reads the header from the right: `10.9.9.9` is inside the trusted network and is skipped, `203.0.113.9` is not trusted and is returned. The forged `198.51.100.66` is never reached.
- Besides the client address, what else does trusting every peer let a direct caller change?The scheme. Uvicorn sets `scope["scheme"]` from `X-Forwarded-Proto` for any trusted peer, so a caller reaching the server directly can send `https` and make `request.url` and anything keyed on the scheme treat a plain-HTTP request as secure.
- Thinks "*" is safe because a proxy always sits in front of the app
- Expects Uvicorn to pick the rightmost entry when every peer is trusted
- Parses X-Forwarded-For in the app instead of configuring the trust list
- Believes an appending proxy removes the client's forged entries
- Trusts a network range wide enough to include the clients themselves