feat(auth): rate-limit password login and magic-link requests #35
No reviewers
Labels
No labels
bug
docs
duplicate
enhancement
help wanted
invalid
nice-to-have
pre-1.0
question
security
wontfix
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jfro/lamplighter!35
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/21-rate-limit"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 fromLamplighter.ApiTokens.RateLimitertoLamplighter.RateLimitersince it isn't API-only anymore.Limits (hits / window, overridable in
config :lamplighter, LamplighterWeb.LoginThrottle):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 316sBehind 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 plusfrom_socket/1for LiveView via:peer_data/:x_headersconnect info) takes the lastx-forwarded-forentry, 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.exssets the limits very high so the rest of the suite (all 127.0.0.1) is unaffected.mix precommit: 801 passed.