feat(auth): rate-limit password login and magic-link requests #35

Manually merged
jfro merged 1 commit from feat/21-rate-limit into main 2026-11-01 20:34:12 +00:00
Owner

Closes #21

Rate-limits password login (POST /users/log-in) and login-link requests (the magic-link form on the login LiveView), per client address and per email. No new deps: it reuses the fixed-window ETS limiter from #3, moved from Lamplighter.ApiTokens.RateLimiter to Lamplighter.RateLimiter since it isn't API-only anymore.

Limits (hits / window, overridable in config :lamplighter, LamplighterWeb.LoginThrottle):

per email per address
Password login 10 / 15 min 20 / 1 min
Login-link request 3 / 15 min 10 / 15 min

Email limits are the tight ones and hold no matter how many addresses an attacker has; address limits are looser because people behind one NAT/proxy share one. Login-link token posts count against the address only (the token is unguessable). Emails are compared trimmed and lowercased.

When throttled: password login redirects back with "Too many attempts. Try again in N minutes." (email kept in the form); a link request shows the same flash and sends nothing. Each throttled attempt logs a warning, without the email:
Throttled POST /users/log-in from 10.0.0.9: 4 attempts per email in 900s, limit 3, retry in 316s

Behind a proxy: every request comes from the proxy, so per-address limits would be one shared bucket. New TRUST_X_FORWARDED_FOR=true (LamplighterWeb.ClientIP, a plug in the endpoint plus from_socket/1 for LiveView via :peer_data/:x_headers connect info) takes the last x-forwarded-for entry, the one the proxy added, so a client can't pick its own address by sending the header. Off by default; documented in the README env table.

Tests: test/lamplighter_web/login_throttle_test.exs (sync: it sets global limits and resets the limiter table) covers per-email across addresses, per-address across emails, other addresses unaffected, token posts, spoofed vs proxy XFF, link emails not sent once throttled, log lines. config/test.exs sets the limits very high so the rest of the suite (all 127.0.0.1) is unaffected. mix precommit: 801 passed.

Closes #21 Rate-limits password login (`POST /users/log-in`) and login-link requests (the magic-link form on the login LiveView), per client address and per email. No new deps: it reuses the fixed-window ETS limiter from #3, moved from `Lamplighter.ApiTokens.RateLimiter` to `Lamplighter.RateLimiter` since it isn't API-only anymore. **Limits** (hits / window, overridable in `config :lamplighter, LamplighterWeb.LoginThrottle`): | | per email | per address | |---|---|---| | Password login | 10 / 15 min | 20 / 1 min | | Login-link request | 3 / 15 min | 10 / 15 min | Email limits are the tight ones and hold no matter how many addresses an attacker has; address limits are looser because people behind one NAT/proxy share one. Login-link token posts count against the address only (the token is unguessable). Emails are compared trimmed and lowercased. **When throttled:** password login redirects back with "Too many attempts. Try again in N minutes." (email kept in the form); a link request shows the same flash and sends nothing. Each throttled attempt logs a warning, without the email: `Throttled POST /users/log-in from 10.0.0.9: 4 attempts per email in 900s, limit 3, retry in 316s` **Behind a proxy:** every request comes from the proxy, so per-address limits would be one shared bucket. New `TRUST_X_FORWARDED_FOR=true` (`LamplighterWeb.ClientIP`, a plug in the endpoint plus `from_socket/1` for LiveView via `:peer_data`/`:x_headers` connect info) takes the **last** `x-forwarded-for` entry, the one the proxy added, so a client can't pick its own address by sending the header. Off by default; documented in the README env table. **Tests:** `test/lamplighter_web/login_throttle_test.exs` (sync: it sets global limits and resets the limiter table) covers per-email across addresses, per-address across emails, other addresses unaffected, token posts, spoofed vs proxy XFF, link emails not sent once throttled, log lines. `config/test.exs` sets the limits very high so the rest of the suite (all 127.0.0.1) is unaffected. `mix precommit`: 801 passed.
jfro added this to the v1.0 milestone 2026-11-01 20:11:26 +00:00
feat(auth): rate-limit password login and magic-link requests
All checks were successful
CI / test (pull_request) Successful in 27s
e1d34a25f5
Counts password logins per client address and per email, and login-link
requests the same way, in the ETS rate limiter the API already uses (now
Lamplighter.RateLimiter). Throttled attempts get a flash saying when to
try again and a Logger warning with the address, route and count.

TRUST_X_FORWARDED_FOR=true takes the client address from the proxy's
x-forwarded-for entry, so limits count clients rather than the proxy.

Closes #21
jfro manually merged commit c2c37cd832 into main 2026-11-01 20:34:12 +00:00
Sign in to join this conversation.
No description provided.