Count per-address limits on the hop a trusted proxy added; limit signup

The limits read the leftmost X-Forwarded-For entry, which the client writes,
and the LAN nginx appended to it. The address now comes from uvicorn's
proxy-headers middleware (rightmost hop not in FORWARDED_ALLOW_IPS), applied
in the app too so tunnel-replayed calls resolve the same way; nginx sets the
header to $remote_addr and the compose backend trusts the Docker networks.
/users/signup gets a per-address limit of 10 an hour.
This commit is contained in:
2026-10-06 12:15:58 +02:00
parent 8e928c3ffb
commit c5a8f170da
10 changed files with 82 additions and 19 deletions
+5 -8
View File
@@ -107,16 +107,13 @@ def _too_many(bucket: str, limit: int, window: float, record: bool = True) -> bo
def _client_ip(request: Request) -> str:
"""The caller's address, or the proxy's if it did not forward one.
"""The caller's address, as the proxy-headers middleware resolved it.
Behind Traefik `request.client.host` is the proxy for every request, which
made every per-address limit here one global bucket — so one caller could
lock out everyone. The leftmost forwarded entry is the client, and is what
the panel pairing screen already reads.
That is the rightmost forwarded hop not added by a trusted proxy
(`FORWARDED_ALLOW_IPS`). The leftmost one, which this used to read, is
whatever the client wrote, so every limit keyed on it could be dodged by
sending a new one each time.
"""
forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip()
if forwarded:
return forwarded
return request.client.host if request.client else "unknown"