fix(auth): close unauthenticated lockout and tighten the daemon's edges
Key login failures by username and client address with decay, plus a loose per-username limit that delays every address by a bounded time. Refused attempts write nothing. Unknown usernames share the code path. MFA and session password checks keep a persistent user-bound count that needs a password or a session to reach. Also: local mode refuses a non-loopback bind outside a container; security headers on every response; the Argon2 permit lives as long as the blocking task; a required password change rechecks role, MFA and epoch; auth reset works for any user; mfa_enabled in /api/auth/state; disabling MFA that is off is refused with no side effects; StateCorrupt keeps only line and column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e4c4201e5
commit
348e61ca95
10 files changed
+725
-199
No files matched your search
@@ -379,13 +379,15 @@ fn same_error_for_unknown_user_and_bad_password() {
|
||||
assert_eq!(unknown, AuthFailure::InvalidCredentials);
|
||||
assert_eq!(unknown, bad);
|
||||
assert_eq!(unknown.to_string(), bad.to_string());
|
||||
// The throttle must not tell them apart either.
|
||||
for _ in 0..4 {
|
||||
let _ = s.login("ghost", PW, "192.0.2.2".parse().unwrap(), T0);
|
||||
let _ = s.login("ana", WRONG, "192.0.2.2".parse().unwrap(), T0);
|
||||
// The throttle must not tell them apart either, per address or past the
|
||||
// account-wide limit.
|
||||
let spent: IpAddr = "192.0.2.2".parse().unwrap();
|
||||
for _ in 0..5 {
|
||||
let _ = s.login("ghost", PW, spent, T0);
|
||||
let _ = s.login("ana", WRONG, spent, T0);
|
||||
}
|
||||
let unknown = failure(s.login("ghost", PW, "192.0.2.3".parse().unwrap(), T0));
|
||||
let bad = failure(s.login("ana", WRONG, "192.0.2.3".parse().unwrap(), T0));
|
||||
let unknown = failure(s.login("ghost", PW, spent, T0));
|
||||
let bad = failure(s.login("ana", WRONG, spent, T0));
|
||||
assert_eq!(
|
||||
unknown,
|
||||
AuthFailure::Throttled {
|
||||
@@ -393,25 +395,37 @@ fn same_error_for_unknown_user_and_bad_password() {
|
||||
}
|
||||
);
|
||||
assert_eq!(unknown, bad);
|
||||
for name in ["ghost", "ana"] {
|
||||
for i in 0..100u32 {
|
||||
let addr = IpAddr::V4(std::net::Ipv4Addr::from(0x0a00_0000 + i));
|
||||
let _ = s.logins.acquire(&hash_token(name), addr, T0);
|
||||
}
|
||||
}
|
||||
let fresh: IpAddr = "192.0.2.3".parse().unwrap();
|
||||
let unknown = failure(s.login("ghost", PW, fresh, T0));
|
||||
let bad = failure(s.login("ana", WRONG, fresh, T0));
|
||||
assert_eq!(
|
||||
unknown,
|
||||
AuthFailure::Throttled {
|
||||
retry_after_secs: 60
|
||||
}
|
||||
);
|
||||
assert_eq!(unknown, bad);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_throttle_delay_grows_capped() {
|
||||
fn login_delay_grows_per_address() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
let id = analyst(&s, "ana");
|
||||
let mut now = T0;
|
||||
for _ in 0..4 {
|
||||
for _ in 0..5 {
|
||||
assert_eq!(
|
||||
failure(s.login("ana", WRONG, ip(), now)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
failure(s.login("ana", WRONG, ip(), now)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
// Locked: even the right password is not evaluated.
|
||||
// Locked: even the right password is not evaluated from this address.
|
||||
assert_eq!(
|
||||
failure(s.login("ana", PW, ip(), now)),
|
||||
AuthFailure::Throttled {
|
||||
@@ -429,40 +443,204 @@ fn account_throttle_delay_grows_capped() {
|
||||
retry_after_secs: 60
|
||||
}
|
||||
);
|
||||
// Survives a restart and is capped.
|
||||
s.store
|
||||
.update(|st| {
|
||||
st.users
|
||||
.iter_mut()
|
||||
.find(|u| u.id == id)
|
||||
.unwrap()
|
||||
.failed_attempts = 50;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let dir = s.auth_dir.parent().unwrap().to_path_buf();
|
||||
let s2 = AuthService::open(&dir, None).unwrap();
|
||||
now += 61;
|
||||
assert_eq!(
|
||||
failure(s2.login("ana", WRONG, ip(), now)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
assert_eq!(
|
||||
failure(s2.login("ana", PW, ip(), now)),
|
||||
AuthFailure::Throttled {
|
||||
retry_after_secs: 900
|
||||
}
|
||||
);
|
||||
now += 901;
|
||||
session_of(s2.login("ana", PW, ip(), now).unwrap());
|
||||
let (f, l) = s2
|
||||
session_of(s.login("ana", PW, ip(), now).unwrap());
|
||||
// None of it touched the user-bound counter.
|
||||
let (f, l) = s
|
||||
.store
|
||||
.read(|st| {
|
||||
let u = st.users.iter().find(|u| u.id == id).unwrap();
|
||||
(u.failed_attempts, u.locked_until)
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!((f, l), (0, 0), "success clears the counter");
|
||||
assert_eq!((f, l), (0, 0));
|
||||
// A right password clears the address.
|
||||
for _ in 0..4 {
|
||||
let _ = s.login("ana", WRONG, ip(), now);
|
||||
}
|
||||
assert_eq!(
|
||||
failure(s.login("ana", WRONG, ip(), now)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn right_password_from_another_ip_still_gets_in_while_an_attacker_keeps_failing() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
analyst(&s, "ana");
|
||||
let attacker: IpAddr = "203.0.113.9".parse().unwrap();
|
||||
let owner: IpAddr = "198.51.100.1".parse().unwrap();
|
||||
let mut now = T0;
|
||||
for _ in 0..6 {
|
||||
let _ = s.login("ana", WRONG, attacker, now);
|
||||
}
|
||||
// The cadence of the proof of concept: a failure each time the lock lapses,
|
||||
// for a day, with the owner trying a second later.
|
||||
for _ in 0..96 {
|
||||
now += 901;
|
||||
assert_eq!(
|
||||
failure(s.login("ana", WRONG, attacker, now)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
session_of(s.login("ana", PW, owner, now + 1).unwrap());
|
||||
}
|
||||
assert!(matches!(
|
||||
failure(s.login("ana", PW, attacker, now + 1)),
|
||||
AuthFailure::Throttled { .. }
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn login_counts_decay_over_time() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
analyst(&s, "ana");
|
||||
for _ in 0..5 {
|
||||
let _ = s.login("ana", WRONG, ip(), T0);
|
||||
}
|
||||
// Long after, four failures are free again: the count was forgotten.
|
||||
let later = T0 + 5 * 900;
|
||||
for _ in 0..4 {
|
||||
assert_eq!(
|
||||
failure(s.login("ana", WRONG, ip(), later)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
}
|
||||
session_of(s.login("ana", PW, ip(), later).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn user_bound_failures_decay_over_time() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
let id = analyst(&s, "ana");
|
||||
enroll(&s, &id, T0);
|
||||
let p = pending_of(s.login("ana", PW, ip(), T0 + 60).unwrap());
|
||||
for _ in 0..4 {
|
||||
let _ = s.confirm_mfa(&p.token, "000000", T0 + 60);
|
||||
}
|
||||
let p = pending_of(s.login("ana", PW, ip(), T0 + 1920).unwrap());
|
||||
let _ = s.confirm_mfa(&p.token, "000000", T0 + 1920);
|
||||
let failures = s
|
||||
.store
|
||||
.read(|st| {
|
||||
st.users
|
||||
.iter()
|
||||
.find(|u| u.id == id)
|
||||
.unwrap()
|
||||
.failed_attempts
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(failures, 3, "two windows forgot two of four, then one more");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_passwords_and_refusals_write_no_state() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
analyst(&s, "ana");
|
||||
let state = s.auth_dir.join(crate::auth::STATE_FILE);
|
||||
let before = std::fs::read(&state).unwrap();
|
||||
let modified = std::fs::metadata(&state).unwrap().modified().unwrap();
|
||||
for _ in 0..8 {
|
||||
let _ = s.login("ana", WRONG, ip(), T0);
|
||||
let _ = s.login("ghost", WRONG, ip(), T0);
|
||||
}
|
||||
assert_eq!(std::fs::read(&state).unwrap(), before);
|
||||
assert_eq!(
|
||||
std::fs::metadata(&state).unwrap().modified().unwrap(),
|
||||
modified
|
||||
);
|
||||
// Refusals do not reach the audit log either.
|
||||
let lines = s.audit_tail(1000).unwrap().len();
|
||||
let _ = s.login("ana", WRONG, ip(), T0);
|
||||
assert_eq!(s.audit_tail(1000).unwrap().len(), lines);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stranger_cannot_trigger_the_user_bound_throttle() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
let id = analyst(&s, "ana");
|
||||
enroll(&s, &id, T0);
|
||||
let attacker: IpAddr = "203.0.113.9".parse().unwrap();
|
||||
for _ in 0..8 {
|
||||
let _ = s.login("ana", WRONG, attacker, T0 + 60);
|
||||
}
|
||||
let p = pending_of(s.login("ana", PW, ip(), T0 + 60).unwrap());
|
||||
let good = code_at_active(&s, &id, T0 + 60);
|
||||
session_of(s.confirm_mfa(&p.token, &good, T0 + 60).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn required_change_final_step_refuses_a_changed_account() {
|
||||
let (_d, s) = svc();
|
||||
let root = admin(&s);
|
||||
let ana = analyst(&s, "ana");
|
||||
let finish = |id: &str, epoch: u64| {
|
||||
s.store
|
||||
.update(|st| finish_required_change(st, id, epoch, T0))
|
||||
};
|
||||
// An administrator without MFA, as after a promotion in between.
|
||||
let epoch = s
|
||||
.store
|
||||
.update(|st| {
|
||||
let u = user_mut(st, &root).unwrap();
|
||||
u.totp_sealed = None;
|
||||
Ok(u.auth_epoch)
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
failure(finish(&root, epoch.wrapping_sub(1))),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
// The wrong epoch.
|
||||
let epoch = s
|
||||
.store
|
||||
.read(|st| st.users.iter().find(|u| u.id == ana).unwrap().auth_epoch)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
failure(finish(&ana, epoch)),
|
||||
AuthFailure::InvalidCredentials
|
||||
);
|
||||
// The expected epoch opens the session.
|
||||
finish(&ana, epoch.wrapping_sub(1)).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_analyst_who_lost_mfa_can_be_reset() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
let id = analyst(&s, "ana");
|
||||
enroll(&s, &id, T0);
|
||||
let temp = s.reset_account("ana", T0 + 100).unwrap();
|
||||
assert!(!s.mfa_enabled(&id).unwrap());
|
||||
let LoginStep::PasswordChangeRequired(ticket) = s.login("ana", &temp, ip(), T0 + 200).unwrap()
|
||||
else {
|
||||
panic!("expected the password step");
|
||||
};
|
||||
s.change_required_password(&ticket.token, &temp, PW2, T0 + 200)
|
||||
.unwrap();
|
||||
session_of(s.login("ana", PW2, ip(), T0 + 300).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabling_mfa_that_is_off_changes_nothing() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
let id = analyst(&s, "ana");
|
||||
let session = session_of(s.login("ana", PW, ip(), T0).unwrap());
|
||||
let state = s.auth_dir.join(crate::auth::STATE_FILE);
|
||||
let before = std::fs::read(&state).unwrap();
|
||||
let lines = s.audit_tail(1000).unwrap().len();
|
||||
assert_eq!(
|
||||
failure(s.disable_mfa("ana", &id, Some(&session.token), T0)),
|
||||
AuthFailure::MfaNotEnabled
|
||||
);
|
||||
assert_eq!(std::fs::read(&state).unwrap(), before);
|
||||
assert_eq!(s.audit_tail(1000).unwrap().len(), lines);
|
||||
assert_eq!(sessions(&s, &id), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -505,7 +683,7 @@ fn bad_codes_count_towards_the_account_throttle() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_guesses_cannot_outrun_the_account_throttle() {
|
||||
fn concurrent_guesses_cannot_outrun_the_address_throttle() {
|
||||
let (_d, s) = svc();
|
||||
admin(&s);
|
||||
analyst(&s, "ana");
|
||||
@@ -514,8 +692,8 @@ fn concurrent_guesses_cannot_outrun_the_account_throttle() {
|
||||
.map(|i| {
|
||||
let s = &s;
|
||||
sc.spawn(move || {
|
||||
let addr = IpAddr::from([192, 0, 2, 100 + i]);
|
||||
s.login("ana", WRONG, addr, T0)
|
||||
let _ = i;
|
||||
s.login("ana", WRONG, ip(), T0)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
@@ -677,7 +855,7 @@ fn reset_admin_is_audited_and_forces_enrollment() {
|
||||
let (_d, s) = svc();
|
||||
let root = admin(&s);
|
||||
session_of_any(&s, &root);
|
||||
let temp = s.reset_admin("root", T0 + 200).unwrap();
|
||||
let temp = s.reset_account("root", T0 + 200).unwrap();
|
||||
assert_eq!(sessions(&s, &root), 0);
|
||||
assert!(temp.len() >= 24);
|
||||
assert_eq!(
|
||||
@@ -689,15 +867,13 @@ fn reset_admin_is_audited_and_forces_enrollment() {
|
||||
LoginStep::MfaEnrollRequired(_)
|
||||
));
|
||||
assert_eq!(
|
||||
failure(s.reset_admin("ghost", T0)),
|
||||
failure(s.reset_account("ghost", T0)),
|
||||
AuthFailure::UserNotFound
|
||||
);
|
||||
analyst(&s, "ana");
|
||||
assert_eq!(failure(s.reset_admin("ana", T0)), AuthFailure::UserNotFound);
|
||||
let log = s.audit_tail(100).unwrap();
|
||||
assert!(log
|
||||
.iter()
|
||||
.any(|e| e.event.action == "admin.reset" && e.event.ok));
|
||||
.any(|e| e.event.action == "auth.reset" && e.event.ok));
|
||||
let raw = std::fs::read_to_string(s.auth_dir.join("audit.jsonl")).unwrap();
|
||||
assert!(!raw.contains(&temp));
|
||||
}
|
||||
@@ -795,10 +971,10 @@ fn open_fails_on_missing_key_without_touching_state() {
|
||||
assert_eq!(std::fs::read(auth.join("state.json")).unwrap(), before);
|
||||
}
|
||||
|
||||
/// Admin after `reset_admin`, enrolled again. Returns (id, temp password).
|
||||
/// Admin after `reset_account`, enrolled again. Returns (id, temp password).
|
||||
fn reset_and_enroll(s: &AuthService) -> (String, String) {
|
||||
let id = admin(s);
|
||||
let temp = s.reset_admin("root", T0 + 200).unwrap();
|
||||
let temp = s.reset_account("root", T0 + 200).unwrap();
|
||||
enroll(s, &id, T0 + 200);
|
||||
(id, temp)
|
||||
}
|
||||
@@ -890,7 +1066,7 @@ fn flag_also_blocks_password_only_login() {
|
||||
s.create_user("root", "second", PW, Role::Admin, T0)
|
||||
.unwrap();
|
||||
let second = uid(&s, "second");
|
||||
let temp = s.reset_admin("second", T0).unwrap();
|
||||
let temp = s.reset_account("second", T0).unwrap();
|
||||
s.set_role("root", &second, Role::Analyst, T0).unwrap();
|
||||
let t = change_ticket(s.login("second", &temp, ip(), T0).unwrap());
|
||||
assert_eq!(sessions(&s, &second), 0);
|
||||
|
||||
Reference in new issue
Block a user