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:
Lucas RayanandClaude Opus 5.5 committed 2026-11-05 06:48:59 -03:00
1 parent b7fd1b7c8a
commit b384ed91c3
6 files changed
+397 -151

No files matched your search

+7 -3
View File
@@ -675,12 +675,13 @@ pub async fn change_password(
State(state): State<AppState>,
Auth(p): Auth,
Extension(SessionToken(token)): Extension<SessionToken>,
Extension(ClientIp(ip)): Extension<ClientIp>,
JsonBody(b): JsonBody<PasswordBody>,
) -> Result<Response, ApiError> {
let auth = state.auth.clone();
let now = unix_now();
hashing(&state, move || {
auth.change_password(&p.user_id, &b.current, &b.new, Some(&token), now)
auth.change_password(&p.user_id, &b.current, &b.new, Some(&token), Some(ip), now)
})
.await?;
Ok(ok_reply())
@@ -750,6 +751,7 @@ pub struct CodeBody {
pub async fn enroll_confirm(
State(state): State<AppState>,
Extension(ClientIp(ip)): Extension<ClientIp>,
actor: EnrollActor,
JsonBody(b): JsonBody<CodeBody>,
) -> Result<Response, ApiError> {
@@ -758,7 +760,7 @@ pub async fn enroll_confirm(
let from_session = matches!(actor, EnrollActor::Session(_));
let codes = blocking(move || match actor {
EnrollActor::Pending(token) => auth.confirm_enrollment_pending(&token, &b.code, now),
EnrollActor::Session(p) => auth.confirm_enrollment(&p.user_id, &b.code, now),
EnrollActor::Session(p) => auth.confirm_enrollment(&p.user_id, &b.code, Some(ip), now),
})
.await?;
let reply = Json(serde_json::json!({ "recovery_codes": codes })).into_response();
@@ -774,13 +776,15 @@ pub async fn mfa_disable(
State(state): State<AppState>,
Auth(p): Auth,
Extension(SessionToken(token)): Extension<SessionToken>,
Extension(ClientIp(ip)): Extension<ClientIp>,
) -> Result<Response, ApiError> {
if !p.recent_auth {
return Err(AuthFailure::ReauthRequired.into());
}
let auth = state.auth.clone();
let now = unix_now();
blocking(move || auth.disable_mfa(&p.username, &p.user_id, Some(&token), now)).await?;
blocking(move || auth.disable_mfa(&p.username, &p.user_id, Some(&token), Some(ip), now))
.await?;
Ok(ok_reply())
}