Repository navigation
Conversation
One user sending five bad passwords locked every user out of login, 2FA and recovery for the full rate-limit window. getClientIp returned the constant "unknown" unless RATE_LIMIT_TRUST_PROXY was set, so all clients shared one IP bucket. Setting the flag did not help: the SvelteKit frontend called the backend without the browser's address. The login action and the /api proxy now set X-Forwarded-For from getClientAddress(). The proxy drops client-supplied X-Forwarded-For, X-Real-IP and CF-Connecting-IP, so clients cannot choose their own key. The backend gets the peer address from Deno.serve and trusts forwarding headers only from loopback or when RATE_LIMIT_TRUST_PROXY is set. Addresses are validated and canonicalized before use as keys. If no address can be determined, the backend logs a one-time warning and falls back to a shared key. .env.example documents ADDRESS_HEADER and XFF_DEPTH for reverse-proxy setups, and that RATE_LIMIT_TRUST_PROXY requires the backend port to be unreachable by clients. Added rateLimiter_test.ts and forwardHeaders.test.ts. Not run: no Deno or Bun runtime was available. Not tested end to end.
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
One client sending five bad passwords locks every user out of login, 2FA and recovery for the full rate-limit window (15 minutes by default).
Cause
getClientIpreturns the constant"unknown"unlessRATE_LIMIT_TRUST_PROXYis set, so every client shares one IP bucket. Setting the flag does not help: the SvelteKit frontend calls the backend server-side and never passes on the browser's address.Changes
Deno.servepasses the peer address to the app.getClientIptrustsX-Forwarded-For/X-Real-IP/CF-Connecting-IPonly from loopback (the bundled frontend) or whenRATE_LIMIT_TRUST_PROXY=true. Otherwise it uses the peer address. Addresses are validated and canonicalized before use as keys. If no address can be determined, the backend logs a one-time warning and falls back to a shared key./api/[...path]proxy setX-Forwarded-ForfromgetClientAddress(). The proxy drops client-supplied address headers, so clients cannot choose their own rate-limit key..env.example: documentsADDRESS_HEADER/XFF_DEPTHfor deployments behind a reverse proxy. Also documents thatRATE_LIMIT_TRUST_PROXY=truerequires the backend port to be unreachable by clients.Deployment notes
ADDRESS_HEADER(andXFF_DEPTHforX-Forwarded-For) in the frontend's environment. Without it, all users share the proxy's bucket, as before this change.RATE_LIMIT_TRUST_PROXY=trueand keep the backend port private.Testing
backend/src/middleware/rateLimiter_test.tsandfrontend/src/lib/forwardHeaders.test.ts.Known limitation
The per-username limit can still be used to lock out the targeted account. This is existing behavior and does not lock out other usernames.