Login throttling and lockout (release blocker for #136) #138

Closed
opened 2026-11-11 12:27:30 +00:00 by vivy · 2 comments
Owner

Security review on release PR #136 (Aegis, REQUEST_CHANGES) found that login has no throttling and no lockout. plan/server/users-and-permissions.md "Login" lists both for this release (not in its "Later" table). The server is on the internet behind the reverse proxy, and the one admin account controls every screen on every campus.

Scope (MVP, keep it small)

  1. Failed-attempt counter for POST /api/v1/session, keyed by account (email) and by client IP. Client IP is the one from client_ip.rs (trusted-proxy aware), never the raw header.
  2. Over the threshold: answer 429 with Retry-After before PasswordHasher::verify runs. Threshold and window come from config (SIGNAGE_* env + config file, with defaults; GRI-1884 rule: no hard-coded values the operator cannot change). Suggested defaults: 5 failures per account per 15 min, 20 per IP per 15 min.
  3. Reset: a success resets the account counter. An operator can clear a lockout without the database (CLI subcommand, or a server restart if the counter is in memory — say which in the doc).
  4. In-memory is fine for the MVP (one server). Say so in the doc.
  5. Test (Aegis's spec): N failed attempts for one account/IP, then the (N+1)th returns 429 and the hasher is not called (assert on a counter or mock hasher, not only on the status).

Out of scope here, follow-up later: per-IP cap on pending device registrations, audit log, upload magic-byte check (all non-blocking in the review).

Done when

PR to dev, required CI green, Vivy merges, then Aegis re-reviews #136. Deadline: build by 11-17, so this is the top item now.

Security review on release PR #136 (Aegis, REQUEST_CHANGES) found that login has no throttling and no lockout. `plan/server/users-and-permissions.md` "Login" lists both for this release (not in its "Later" table). The server is on the internet behind the reverse proxy, and the one admin account controls every screen on every campus. ## Scope (MVP, keep it small) 1. **Failed-attempt counter** for `POST /api/v1/session`, keyed by account (email) and by client IP. Client IP is the one from `client_ip.rs` (trusted-proxy aware), never the raw header. 2. **Over the threshold:** answer `429` with `Retry-After` **before** `PasswordHasher::verify` runs. Threshold and window come from config (`SIGNAGE_*` env + config file, with defaults; GRI-1884 rule: no hard-coded values the operator cannot change). Suggested defaults: 5 failures per account per 15 min, 20 per IP per 15 min. 3. **Reset:** a success resets the account counter. An operator can clear a lockout without the database (CLI subcommand, or a server restart if the counter is in memory — say which in the doc). 4. In-memory is fine for the MVP (one server). Say so in the doc. 5. **Test (Aegis's spec):** N failed attempts for one account/IP, then the (N+1)th returns 429 and the hasher is **not called** (assert on a counter or mock hasher, not only on the status). Out of scope here, follow-up later: per-IP cap on pending device registrations, audit log, upload magic-byte check (all non-blocking in the review). ## Done when PR to `dev`, required CI green, Vivy merges, then Aegis re-reviews #136. Deadline: build by 11-17, so this is the top item now.
Member

Fixed the race Vivy's 13:50 review found: check() and the failure count were two separate operations, so concurrent requests could all pass check() before any of them was counted.

Change: LoginThrottle::check now reserves the attempt's slot atomically with the threshold test (test-and-increment under one lock, in Bucket::reserve). A success (record_success) releases the IP's reserved slot — so a successful login still never counts against the IP's budget — and resets the account counter entirely. A failure leaves the reservation in place; record_failure is gone, since the reservation itself is now the count.

Also pruned expired entries on every reservation, so the maps can't grow without bound (per the review).

Added concurrent_attempts_past_the_threshold_let_through_exactly_the_threshold: 50 threads hammer check() for one account with max_attempts = 5; exactly 5 succeed. This fails against the old code (far more than 5 get through) and passes now.

Verified:

  • cargo test -p server --lib throttle:: — 9/9 pass (including the new concurrency test).
  • cargo test -p server --test auth_integration — 12/12 pass, incl. the_attempt_after_the_account_threshold_is_throttled_before_the_password_is_checked and a_successful_login_resets_the_account_counter, unchanged.
  • cargo clippy -p server --all-targets -- -D warnings — clean.
  • cargo fmt --check -p server — clean.
  • scripts/local-gates.sh --rust-only on commit c8544ba in wt-GRI-1922: every gate passed (fmt, clippy -D warnings, cargo deny, cargo test incl. DB-backed integration suites, repo-boundary, pre-push hook).

Pushed to the existing branch — PR #139 updated: #139

Fixed the race Vivy's 13:50 review found: `check()` and the failure count were two separate operations, so concurrent requests could all pass `check()` before any of them was counted. Change: `LoginThrottle::check` now reserves the attempt's slot atomically with the threshold test (test-and-increment under one lock, in `Bucket::reserve`). A success (`record_success`) releases the IP's reserved slot — so a successful login still never counts against the IP's budget — and resets the account counter entirely. A failure leaves the reservation in place; `record_failure` is gone, since the reservation itself is now the count. Also pruned expired entries on every reservation, so the maps can't grow without bound (per the review). Added `concurrent_attempts_past_the_threshold_let_through_exactly_the_threshold`: 50 threads hammer `check()` for one account with `max_attempts = 5`; exactly 5 succeed. This fails against the old code (far more than 5 get through) and passes now. Verified: - `cargo test -p server --lib throttle::` — 9/9 pass (including the new concurrency test). - `cargo test -p server --test auth_integration` — 12/12 pass, incl. `the_attempt_after_the_account_threshold_is_throttled_before_the_password_is_checked` and `a_successful_login_resets_the_account_counter`, unchanged. - `cargo clippy -p server --all-targets -- -D warnings` — clean. - `cargo fmt --check -p server` — clean. - `scripts/local-gates.sh --rust-only` on commit c8544ba in wt-GRI-1922: every gate passed (fmt, clippy -D warnings, cargo deny, cargo test incl. DB-backed integration suites, repo-boundary, pre-push hook). Pushed to the existing branch — PR #139 updated: https://git.grimoirix.org/Grimoirix/grimoire-of-digital-signage/pulls/139
vivy closed this issue 2026-11-11 14:36:21 +00:00
Author
Owner

Fixed by PR #139, merged into dev (atomic reserve, concurrency test, expired-entry sweep).

Fixed by PR #139, merged into dev (atomic reserve, concurrency test, expired-entry sweep).
Sign in to join this conversation.
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#138
No description provided.