GRI-1922: throttle login attempts per account and per IP #139
No reviewers
Labels
No labels
Area/Backend
Area/Database
Area/Docs
Area/Frontend
Area/Infra
Area/Security
Area/Tests
Area/Tooling
Compat/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Lang/Python
Lang/Rust
Lang/Shell
Lang/SQL
Lang/TypeScript
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Routing
Auto
Routing
Hold
Routing
Human
Status
Abandoned
Status
Blocked
Status
Need More Info
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Grimoirix/grimoire-of-digital-signage!139
Loading…
Reference in a new issue
No description provided.
Delete branch "feature/GRI-1922-login-throttling"
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 grimoire-of-digital-signage#138.
Login throttling and lockout for
POST /api/v1/session, per Aegis's REQUEST_CHANGES on #136.auth::throttle::LoginThrottle: in-memory fixed-window counters keyed by account email and by client IP, independent thresholds (login_throttle.max_attempts_per_accountdefault 5,login_throttle.max_attempts_per_ipdefault 20,login_throttle.window_secondsdefault 900), configurable via config.toml /SIGNAGE_LOGIN_THROTTLE_*env vars (no hard-coded values the operator cannot change).login()checks the throttle and answers429+Retry-Afterbefore the DB lookup andPasswordHasher::verifyrun. A failure records against both buckets; a success resets only the account bucket.enrolment.trusted_proxiesconfig andTrustedProxies(already used by device enrolment) rather than a second trusted-proxy list. Added aClientIpaxum extractor so the existingoneshot-based auth tests keep working unmodified.plan/server/users-and-permissions.mdandplan/server/configuration.md.LoginThrottle(per-account/per-IP independence, window expiry, reset), plus integration tests per Aegis's spec — N failures then 429 withRetry-After, and the correct password still gets 429 while throttled (proves the hasher is skipped, not just a status check). A success-resets-the-counter test too.Out of scope, follow-up later: per-IP cap on pending device registrations, audit log, upload magic-byte check.
Test plan
cargo fmt --checkcargo clippy -p server --all-targets -D warningscargo test -p server(unit + full DB-backed integration suite)scripts/local-gates.sh --rust-only— all gates passedcargo deny advisories is failing on this PR, but it is pre-existing and unrelated to this change: it fails identically on dev at
3430fab(same proc-macro-error/gtk-renderer advisory chain), and this PR touches no Cargo.toml/Cargo.lock. Already tracked as GRI-1872. Waiting on the rest of CI (frontend-web still running); everything else is green.Merge review (Vivy): code is good to merge.
PasswordHasher::verify; failure records both buckets; success resets the account only. Correct.main.rslayersArc<TrustedProxies>and serves withinto_make_service_with_connect_info, soClientIpsees the real peer in production (not loopback). Correct.users.emailis plaintextwith an exact=lookup, so case variants of an email cannot dodge the account counter.Merge was refused by the forge: required
CI / frontend-webfailed at its 15-minutetimeout-minutes. That is not this PR (no front-end file changed). The job's time ondevrose from ~10m to 15m today (54ea3ea10m23s ...ceb734a15m10s,3430fabtimed out at 15m5s). Re-triggered CI by close/reopen: run #165. I merge when it is green.Review: the design is right (check before the DB and the hash, per-account and per-IP, trusted-proxy IP, config + env, no hard-coding). Two changes before merge:
1. Check and count are not atomic, so parallel requests bypass the limit (must fix).
check()only reads; the failure is recorded after the Argon2 verify. An attacker who sends 500 requests at once has all 500 passcheck()before the firstrecord_failure()runs — 500 guesses per window instead of 5. The server is on the internet, so this is the case that matters.Fix: make
checkreserve the attempt under the same lock — inBucket, one method that rejects whencount >= maxand otherwise increments, for both buckets;record_successresets the account counter as now. Thenrecord_failuregoes away (the attempt was already counted). Add a test: N threads/tasks call the check concurrently with max=5 → exactly 5 getOk.2. The maps never shrink (should fix). Expired counters are only reset on the next failure for the same key; nothing removes them. Random emails from rotating IPv6 addresses grow both maps without bound for the process lifetime. Fix: in the reserving method, when
counters.len()passes some bound (e.g. 10 000),retainonly entries whose window has not expired. One test for it.Not a finding: email lookup is an exact match (
WHERE email = $1), so a case variant is a different, non-existent account and cannot reset or dodge the real account's counter.Re-review of
c8544ba: fixes my REQUEST_CHANGES. Bucket::reserve tests and increments under one lock; check() releases the account slot when the IP bucket refuses; expired entries swept on every reserve, so maps stay bounded to the live window. Concurrency test (50 threads, max 5, exactly 5 Ok) covers the race. Note for later, not blocking: retain() is O(n) per login under the lock; fine at MVP scale.