fix(auth): make the login budget a true rolling cap
The leaky bucket allowed about 199 failures in a day; persisted timestamps now hold it to 100 in any 24 hours, and only evaluated failures reach the disk. A credential change from a live session keeps its own address as known. Promotion clears the list, session MFA changes void stale pending tokens, reset frees pairs whose budget was evicted, and only real accounts audit exhaustion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b7fd1b7c8a
commit
b384ed91c3
6 files changed
+397
-151
No files matched your search
@@ -310,13 +310,24 @@ fn remember_address(u: &mut User, hash: &str, now: u64) {
|
||||
|
||||
fn stored_budget(u: &User) -> BudgetState {
|
||||
BudgetState {
|
||||
failures: u.budget_failures,
|
||||
last: u.budget_at,
|
||||
times: u.budget_times.clone(),
|
||||
audited_at: u.budget_audited_at,
|
||||
generation: u.budget_generation,
|
||||
}
|
||||
}
|
||||
|
||||
/// A credential changed: the old addresses no longer vouch for anyone. A change
|
||||
/// made from a live session proved the password and a recent reauthentication,
|
||||
/// so its own address stays known; a change made with a pending ticket passes
|
||||
/// `None`, and its address is recorded when the login completes.
|
||||
fn replace_known_addresses(u: &mut User, acting: Option<IpAddr>, now: u64) {
|
||||
u.known_addresses.clear();
|
||||
if let Some(ip) = acting {
|
||||
let hash = address_hash(&u.id, ip);
|
||||
remember_address(u, &hash, now);
|
||||
}
|
||||
}
|
||||
|
||||
/// Clears the throttle, voids outstanding pending tokens and opens a session.
|
||||
/// `address` is where the login came from, the only thing that makes one known.
|
||||
fn complete_login(
|
||||
@@ -606,7 +617,7 @@ impl AuthService {
|
||||
Default::default()
|
||||
};
|
||||
let id = attempt.account.as_ref().map(|k| k.id.as_str());
|
||||
if let Some(state) = note.write {
|
||||
if let Some(state) = ¬e.write {
|
||||
// A name with no account writes too, with nothing to store: a real
|
||||
// name must not answer slower once a minute.
|
||||
self.store.update(|st| {
|
||||
@@ -614,8 +625,7 @@ impl AuthService {
|
||||
.and_then(|id| user_mut(st, id))
|
||||
.filter(|u| u.budget_generation == state.generation)
|
||||
{
|
||||
u.budget_failures = state.failures;
|
||||
u.budget_at = state.last;
|
||||
u.budget_times = state.times.clone();
|
||||
u.budget_audited_at = state.audited_at;
|
||||
}
|
||||
Ok(())
|
||||
@@ -631,10 +641,12 @@ impl AuthService {
|
||||
false,
|
||||
Some("invalid credentials"),
|
||||
)?;
|
||||
if note.audit {
|
||||
// Only a real account has anything to report: a name with no account
|
||||
// may be a password, and its failures are already in the log.
|
||||
if let Some(name) = name.as_deref().filter(|_| note.audit) {
|
||||
self.log(
|
||||
"login.budget_exhausted",
|
||||
name.as_deref(),
|
||||
Some(name),
|
||||
None,
|
||||
false,
|
||||
Some(&format!(
|
||||
@@ -695,7 +707,14 @@ impl AuthService {
|
||||
LoginStep::Session(complete_login(st, &id, Some(address), now)?)
|
||||
};
|
||||
Ok(Some(step))
|
||||
})?;
|
||||
});
|
||||
let step = match step {
|
||||
Ok(step) => step,
|
||||
Err(e) => {
|
||||
self.login_abandon(attempt);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
let Some(step) = step else {
|
||||
return self.login_failed(attempt, now);
|
||||
};
|
||||
@@ -852,8 +871,14 @@ impl AuthService {
|
||||
/// here. The accepted step is spent, so finishing a login needs a later
|
||||
/// code. Ends every session of the user. A pending token goes through
|
||||
/// `confirm_enrollment_pending`.
|
||||
pub fn confirm_enrollment(&self, user_id: &str, code: &str, now: u64) -> Result<Vec<String>> {
|
||||
self.confirm_enrollment_guarded(user_id, None, code, now)
|
||||
pub fn confirm_enrollment(
|
||||
&self,
|
||||
user_id: &str,
|
||||
code: &str,
|
||||
acting: Option<IpAddr>,
|
||||
now: u64,
|
||||
) -> Result<Vec<String>> {
|
||||
self.confirm_enrollment_guarded(user_id, None, code, acting, now)
|
||||
}
|
||||
|
||||
/// As `start_enrollment_pending`: two racing requests get one activation,
|
||||
@@ -865,7 +890,7 @@ impl AuthService {
|
||||
now: u64,
|
||||
) -> Result<Vec<String>> {
|
||||
let claims = self.open_pending(pending, PENDING_PURPOSE, now)?;
|
||||
self.confirm_enrollment_guarded(&claims.user_id, Some(claims.epoch), code, now)
|
||||
self.confirm_enrollment_guarded(&claims.user_id, Some(claims.epoch), code, None, now)
|
||||
}
|
||||
|
||||
fn confirm_enrollment_guarded(
|
||||
@@ -873,6 +898,7 @@ impl AuthService {
|
||||
user_id: &str,
|
||||
pending_epoch: Option<u64>,
|
||||
code: &str,
|
||||
acting: Option<IpAddr>,
|
||||
now: u64,
|
||||
) -> Result<Vec<String>> {
|
||||
let (texts, hashes) = new_recovery_codes();
|
||||
@@ -897,7 +923,13 @@ impl AuthService {
|
||||
u.totp_pending_sealed = None;
|
||||
u.totp_last_step = Some(step);
|
||||
u.recovery_hashes = hashes;
|
||||
u.known_addresses.clear();
|
||||
replace_known_addresses(u, acting, now);
|
||||
// Voids pending tokens issued before the new factor. A pending
|
||||
// enrollment is the exception: its own token goes on to finish that
|
||||
// first login, under the new factor.
|
||||
if pending_epoch.is_none() {
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
}
|
||||
end_sessions(st, user_id, None);
|
||||
drop_admin_tokens(st, user_id);
|
||||
Ok((name, true))
|
||||
@@ -985,6 +1017,11 @@ impl AuthService {
|
||||
let name = u.username.clone();
|
||||
if changed {
|
||||
let leaving_admin = u.role == Role::Admin && role != Role::Admin;
|
||||
if role == Role::Admin {
|
||||
// An address trusted for an analyst has not earned the
|
||||
// exception for an administrator.
|
||||
u.known_addresses.clear();
|
||||
}
|
||||
u.role = role;
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
end_sessions(st, user_id, None);
|
||||
@@ -1038,6 +1075,7 @@ impl AuthService {
|
||||
current: &str,
|
||||
new: &str,
|
||||
keep_session_token: Option<&str>,
|
||||
acting: Option<IpAddr>,
|
||||
now: u64,
|
||||
) -> Result<()> {
|
||||
validate_password(new)?;
|
||||
@@ -1064,7 +1102,7 @@ impl AuthService {
|
||||
refund_attempt(u);
|
||||
u.password_hash = new_hash;
|
||||
u.must_change_password = false;
|
||||
u.known_addresses.clear();
|
||||
replace_known_addresses(u, acting, now);
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
end_sessions(st, user_id, keep_session_token);
|
||||
drop_admin_tokens(st, user_id);
|
||||
@@ -1095,7 +1133,7 @@ impl AuthService {
|
||||
if !live {
|
||||
return Err(invalid());
|
||||
}
|
||||
self.change_password(&claims.user_id, current, new, None, now)?;
|
||||
self.change_password(&claims.user_id, current, new, None, None, now)?;
|
||||
self.store.update(|st| {
|
||||
finish_required_change(
|
||||
st,
|
||||
@@ -1114,7 +1152,8 @@ impl AuthService {
|
||||
actor: &str,
|
||||
user_id: &str,
|
||||
keep_session_token: Option<&str>,
|
||||
_now: u64,
|
||||
acting: Option<IpAddr>,
|
||||
now: u64,
|
||||
) -> Result<()> {
|
||||
let result = self.store.update(|st| {
|
||||
let u = user_or_missing(st, user_id)?;
|
||||
@@ -1129,7 +1168,8 @@ impl AuthService {
|
||||
u.totp_pending_sealed = None;
|
||||
u.totp_last_step = None;
|
||||
u.recovery_hashes.clear();
|
||||
u.known_addresses.clear();
|
||||
replace_known_addresses(u, acting, now);
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
end_sessions(st, user_id, keep_session_token);
|
||||
drop_admin_tokens(st, user_id);
|
||||
Ok(name)
|
||||
@@ -1167,8 +1207,7 @@ impl AuthService {
|
||||
u.failed_attempts = 0;
|
||||
u.locked_until = 0;
|
||||
u.known_addresses.clear();
|
||||
u.budget_failures = 0;
|
||||
u.budget_at = 0;
|
||||
u.budget_times.clear();
|
||||
u.budget_audited_at = 0;
|
||||
u.budget_generation = u.budget_generation.wrapping_add(1);
|
||||
u.auth_epoch = u.auth_epoch.wrapping_add(1);
|
||||
|
||||
Reference in new issue
Block a user