GRI-1922: throttle login attempts per account and per IP #139

Merged
vivy merged 2 commits from feature/GRI-1922-login-throttling into dev 2026-11-11 14:36:12 +00:00
Member

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_account default 5, login_throttle.max_attempts_per_ip default 20, login_throttle.window_seconds default 900), configurable via config.toml / SIGNAGE_LOGIN_THROTTLE_* env vars (no hard-coded values the operator cannot change).
  • login() checks the throttle and answers 429 + Retry-After before the DB lookup and PasswordHasher::verify run. A failure records against both buckets; a success resets only the account bucket.
  • Client IP comes from the existing enrolment.trusted_proxies config and TrustedProxies (already used by device enrolment) rather than a second trusted-proxy list. Added a ClientIp axum extractor so the existing oneshot-based auth tests keep working unmodified.
  • Reset/recovery: in-memory only for this MVP (one server) — a restart clears every counter. Documented in plan/server/users-and-permissions.md and plan/server/configuration.md.
  • Tests: unit tests on LoginThrottle (per-account/per-IP independence, window expiry, reset), plus integration tests per Aegis's spec — N failures then 429 with Retry-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 --check
  • cargo clippy -p server --all-targets -D warnings
  • cargo test -p server (unit + full DB-backed integration suite)
  • scripts/local-gates.sh --rust-only — all gates passed
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_account` default 5, `login_throttle.max_attempts_per_ip` default 20, `login_throttle.window_seconds` default 900), configurable via config.toml / `SIGNAGE_LOGIN_THROTTLE_*` env vars (no hard-coded values the operator cannot change). - `login()` checks the throttle and answers `429` + `Retry-After` *before* the DB lookup and `PasswordHasher::verify` run. A failure records against both buckets; a success resets only the account bucket. - Client IP comes from the existing `enrolment.trusted_proxies` config and `TrustedProxies` (already used by device enrolment) rather than a second trusted-proxy list. Added a `ClientIp` axum extractor so the existing `oneshot`-based auth tests keep working unmodified. - Reset/recovery: in-memory only for this MVP (one server) — a restart clears every counter. Documented in `plan/server/users-and-permissions.md` and `plan/server/configuration.md`. - Tests: unit tests on `LoginThrottle` (per-account/per-IP independence, window expiry, reset), plus integration tests per Aegis's spec — N failures then 429 with `Retry-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 - [x] `cargo fmt --check` - [x] `cargo clippy -p server --all-targets -D warnings` - [x] `cargo test -p server` (unit + full DB-backed integration suite) - [x] `scripts/local-gates.sh --rust-only` — all gates passed
GRI-1922: throttle login attempts per account and per IP
Some checks failed
CI / the pre-push hook (pull_request) Successful in 7s
CI / cargo fmt (pull_request) Successful in 6s
CI / cargo deny (pull_request) Successful in 51s
CI / cargo deny advisories (pull_request) Failing after 1m16s
CI / repository boundary (pull_request) Successful in 33s
CI / cargo clippy (pull_request) Successful in 10m32s
CI / cargo test (pull_request) Successful in 12m9s
CI / CI (pull_request) Has been cancelled
CI / Notify gate failed (pull_request) Has been cancelled
CI / frontend-web (pull_request) Failing after 15m6s
1fb1a3c112
429 + Retry-After before PasswordHasher::verify once a threshold is
crossed; thresholds and window configurable via config.toml /
SIGNAGE_LOGIN_THROTTLE_* (GRI-1884). In-memory fixed-window counters,
reset on successful login. Reuses enrolment's TrustedProxies for
client IP instead of a second trusted-proxy list.
Author
Member

cargo 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.

cargo 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.
Owner

Merge review (Vivy): code is good to merge.

  • Throttle check runs before the DB lookup and PasswordHasher::verify; failure records both buckets; success resets the account only. Correct.
  • main.rs layers Arc<TrustedProxies> and serves with into_make_service_with_connect_info, so ClientIp sees the real peer in production (not loopback). Correct.
  • users.email is plain text with an exact = lookup, so case variants of an email cannot dodge the account counter.
  • Non-blocking, for later: expired counters are never removed from the maps. Growth per IP is capped by the IP threshold, so this is fine for the MVP.

Merge was refused by the forge: required CI / frontend-web failed at its 15-minute timeout-minutes. That is not this PR (no front-end file changed). The job's time on dev rose from ~10m to 15m today (54ea3ea 10m23s ... ceb734a 15m10s, 3430fab timed out at 15m5s). Re-triggered CI by close/reopen: run #165. I merge when it is green.

Merge review (Vivy): code is good to merge. - Throttle check runs before the DB lookup and `PasswordHasher::verify`; failure records both buckets; success resets the account only. Correct. - `main.rs` layers `Arc<TrustedProxies>` and serves with `into_make_service_with_connect_info`, so `ClientIp` sees the real peer in production (not loopback). Correct. - `users.email` is plain `text` with an exact `=` lookup, so case variants of an email cannot dodge the account counter. - Non-blocking, for later: expired counters are never removed from the maps. Growth per IP is capped by the IP threshold, so this is fine for the MVP. Merge was refused by the forge: required `CI / frontend-web` failed at its 15-minute `timeout-minutes`. That is not this PR (no front-end file changed). The job's time on `dev` rose from ~10m to 15m today (54ea3ea 10m23s ... ceb734a 15m10s, 3430fab timed out at 15m5s). Re-triggered CI by close/reopen: run #165. I merge when it is green.
vivy requested changes 2026-11-11 13:50:08 +00:00
Dismissed
vivy left a comment

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 pass check() before the first record_failure() runs — 500 guesses per window instead of 5. The server is on the internet, so this is the case that matters.
Fix: make check reserve the attempt under the same lock — in Bucket, one method that rejects when count >= max and otherwise increments, for both buckets; record_success resets the account counter as now. Then record_failure goes away (the attempt was already counted). Add a test: N threads/tasks call the check concurrently with max=5 → exactly 5 get Ok.

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), retain only 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.

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 pass `check()` before the first `record_failure()` runs — 500 guesses per window instead of 5. The server is on the internet, so this is the case that matters. Fix: make `check` reserve the attempt under the same lock — in `Bucket`, one method that rejects when `count >= max` and otherwise increments, for both buckets; `record_success` resets the account counter as now. Then `record_failure` goes away (the attempt was already counted). Add a test: N threads/tasks call the check concurrently with max=5 → exactly 5 get `Ok`. **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), `retain` only 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.
GRI-1922: make login-throttle check atomic, not check-then-record
Some checks failed
CI / the pre-push hook (pull_request) Successful in 26s
CI / cargo fmt (pull_request) Successful in 38s
CI / cargo clippy (pull_request) Successful in 1m59s
CI / cargo deny (pull_request) Successful in 1m24s
CI / repository boundary (pull_request) Successful in 27s
CI / cargo deny advisories (pull_request) Failing after 1m27s
CI / frontend-web (pull_request) Successful in 1m55s
CI / cargo test (pull_request) Successful in 4m22s
CI / Notify gate failed (pull_request) Has been skipped
CI / CI (pull_request) Successful in 3s
c8544ba55e
check() now reserves the attempt's slot under the same lock as the
threshold test, so concurrent requests can no longer all pass the
check before any of them is counted. A success releases the IP's
reserved slot (so it still never counts against the IP budget) and
resets the account counter; a failure leaves the reservation in
place. record_failure is gone — the reservation itself is the count.

Also prunes expired entries on every reservation so the maps do not
grow without bound.
vivy approved these changes 2026-11-11 14:36:04 +00:00
vivy left a comment

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.

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.
vivy merged commit 7831bde481 into dev 2026-11-11 14:36:12 +00:00
vivy deleted branch feature/GRI-1922-login-throttling 2026-11-11 14:36:17 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Grimoirix/grimoire-of-digital-signage!139
No description provided.