Skip to content

Fix instance-wide login lockout caused by shared rate-limit key - #198

Open
ieno wants to merge 1 commit into
kittendevv:mainfrom
ieno:fix/login-rate-limit-shared-key
Open

ieno wants to merge 1 commit into
kittendevv:mainfrom
ieno:fix/login-rate-limit-shared-key

Conversation

@ieno

@ieno ieno commented Nov 26, 2026

Copy link
Copy Markdown

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

getClientIp returns the constant "unknown" unless RATE_LIMIT_TRUST_PROXY is 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

  • Backend: Deno.serve passes the peer address to the app. getClientIp trusts X-Forwarded-For / X-Real-IP / CF-Connecting-IP only from loopback (the bundled frontend) or when RATE_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.
  • Frontend: the login action and the /api/[...path] proxy set X-Forwarded-For from getClientAddress(). The proxy drops client-supplied address headers, so clients cannot choose their own rate-limit key.
  • .env.example: documents ADDRESS_HEADER / XFF_DEPTH for deployments behind a reverse proxy. Also documents that RATE_LIMIT_TRUST_PROXY=true requires the backend port to be unreachable by clients.

Deployment notes

  • Single container (default): no configuration needed.
  • Behind nginx, Traefik, Caddy or Cloudflare: make sure the proxy sets the client-address header, then set adapter-node's ADDRESS_HEADER (and XFF_DEPTH for X-Forwarded-For) in the frontend's environment. Without it, all users share the proxy's bucket, as before this change.
  • Frontend and backend in separate containers: set RATE_LIMIT_TRUST_PROXY=true and keep the backend port private.

Testing

  • Added backend/src/middleware/rateLimiter_test.ts and frontend/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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant