fix(rate-limit): stop trusting X-Forwarded-For / X-Real-IP by default - #229
Merged
Merged
Conversation
The default "ip" rate-limit key strategy honored X-Forwarded-For and X-Real-IP unconditionally, with no trusted-proxy check. Any client could bypass its rate limit by sending a fresh value on each request, or frame another client by spoofing its IP in the header. The default "ip" strategy now keys on the actual TCP peer address (scope.client) only, which the client cannot control. A new opt-in strategy, "trusted-forwarded-ip" (aliased "forwarded-ip"), restores the X-Forwarded-For / X-Real-IP behavior for deployments that are actually behind a reverse proxy overwriting those headers. Also fixes scope.client parsing: Granian's RSGI scope exposes it as a "host:port" string, but the old code indexed it with get_item(0) as if it were an ASGI-style (host, port) tuple, silently returning only the first character of the string. scope_client_host() now handles both the RSGI string form and the ASGI/test-scope tuple form. Closes #207
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The default
rate_limit_key="ip"strategy honoredX-Forwarded-For/X-Real-IPunconditionally, with no trusted-proxy check. Any client could:Fix
"ip"now keys on the actual TCP peer address (scope.client) only — the client cannot control this."trusted-forwarded-ip"(alias"forwarded-ip") restores theX-Forwarded-For/X-Real-IPbehavior, documented as safe only behind a reverse proxy that actually overwrites those headers.scope.clientparsing assumed an ASGI-style(host, port)tuple viaget_item(0), but Granian's real RSGI scope exposes it as a"host:port"string — so the old "safe" fallback was silently returning just the first character of that string. Addedscope_client_host()to handle both forms correctly.Testing
tests/test_rate_limit.py: the existing test asserted the vulnerable behavior (spoofed X-Forwarded-For not blocked) — flipped to assert it's now blocked, since it's the same underlying peer.test_trusted_forwarded_ip_opt_incovering the new explicit opt-in strategy.cargo test --lib rate_limit: 2 passed.pytest tests/): 161 passed, 1 skipped.docs/rate-limiting.md.Closes #207