fix(auth): keep a queued login from passing the cap

A reservation could expire while its request queued, and the guess was then
evaluated anyway. The wait for a hashing permit is now bounded to 30 s, and an
attempt whose reservation is gone is refused at permit time. At the cap, each
failure now reaches the disk, so a restart cannot reopen a slot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-05 07:01:16 -03:00
1 parent 4a48f2bd35
commit 9ef86ac425
5 files changed
+238 -67

No files matched your search

@@ -1717,3 +1717,45 @@ fn mfa_changes_kill_stale_pending_tokens() {
s.disable_mfa("ana", &id, None, None, T0 + 120).unwrap();
assert_ne!(user_of(&s, &id).auth_epoch, before);
}
#[test]
fn an_expired_reservation_is_refused_and_the_cap_holds() {
let (_d, s) = svc();
admin(&s);
let id = analyst(&s, "ana");
let held: Vec<_> = (0..100u32)
.map(|i| s.login_begin("ana", v4(i), T0).unwrap())
.collect();
// The sweep takes them, and their places go to fresh failures.
let later = T0 + 3601;
assert_eq!(burn(&s, "ana", 100, 1000, later), 100);
let lines = s.audit_tail(100_000).unwrap().len();
for a in held {
assert!(matches!(
failure(s.login_finish(a, WRONG, later)),
AuthFailure::Throttled { .. }
));
}
assert_eq!(s.audit_tail(100_000).unwrap().len(), lines, "not evaluated");
assert_eq!(user_of(&s, &id).budget_times.len(), 100);
assert!(s.login_begin("ana", v4(5000), later).is_err());
}
#[test]
fn a_failure_at_the_cap_survives_a_restart() {
let (d, s) = svc();
admin(&s);
let id = analyst(&s, "ana");
burn(&s, "ana", 1, 0, T0);
burn(&s, "ana", 1, 1, T0 + 10);
burn(&s, "ana", 98, 2, T0 + 1000);
// Two slots open ten seconds apart, inside one write interval.
assert_eq!(burn(&s, "ana", 1, 200, T0 + DAY), 1);
assert_eq!(burn(&s, "ana", 1, 201, T0 + DAY + 10), 1);
assert_eq!(
user_of(&s, &id).budget_times.iter().max(),
Some(&(T0 + DAY + 10))
);
let s = AuthService::open(d.path(), None).unwrap();
assert!(s.login_begin("ana", v4(300), T0 + DAY + 11).is_err());
}