feat(auth): cap login guessing with a per-account budget

Failures from unknown addresses are capped at 100 per rolling 24 hours per
username, refused before Argon2 and before the hashing queue. Addresses with
a completed login in the last 30 days skip the budget, stored hashed on the
user. IPv6 counts per /48 for the pair throttle and the budget. Unknown names
take the same path, with their budget in the stand-in table. Crossing the cap
audits login.budget_exhausted once per account per 24 hours. Eviction drops
faded entries, then the least counted, not the oldest.

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:27:38 -03:00
1 parent d48cf9d3f0
commit b9137e0948
7 files changed
+1128 -206

No files matched your search

+36 -3
View File
@@ -7,7 +7,8 @@
use crate::web::{access_of, is_public, is_public_asset, Access, AppState};
use acaua_core::auth::{
csrf_matches, csrf_token, NewSession, PendingMfa, Principal, Role, Scope, TotpEnrollment, Via,
csrf_matches, csrf_token, AuthService, LoginAttempt, NewSession, PendingMfa, Principal, Role,
Scope, TotpEnrollment, Via,
};
use acaua_core::{AcauaError, AuthFailure};
use axum::extract::rejection::JsonRejection;
@@ -20,6 +21,7 @@ use axum::{Extension, Json};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
pub const MAX_BODY_BYTES: usize = 64 * 1024;
@@ -544,15 +546,46 @@ pub async fn login(
Extension(ClientIp(ip)): Extension<ClientIp>,
JsonBody(b): JsonBody<LoginBody>,
) -> Result<Response, ApiError> {
let auth = state.auth.clone();
let now = unix_now();
// Whatever can be refused without Argon2 is, before the queue for a permit,
// or a flood of hopeless attempts would stand in front of the owner.
let auth = state.auth.clone();
let username = b.username;
let mut queued = blocking(move || {
let attempt = auth.login_begin(&username, ip, now)?;
Ok(QueuedLogin {
auth,
attempt: Some(attempt),
})
})
.await?;
let password = b.password;
let step = hashing(&state, move || {
auth.login(&b.username, &b.password, ip, now)
let attempt = queued
.attempt
.take()
.ok_or(AuthFailure::InvalidCredentials)?;
queued.auth.login_finish(attempt, &password, now)
})
.await?;
Ok(step_reply(&state, &step))
}
/// A login between its checks and its evaluation. A client that disconnects
/// while queued made no guess, so what it reserved goes back.
struct QueuedLogin {
auth: Arc<AuthService>,
attempt: Option<LoginAttempt>,
}
impl Drop for QueuedLogin {
fn drop(&mut self) {
if let Some(attempt) = self.attempt.take() {
self.auth.login_abandon(attempt);
}
}
}
#[derive(Deserialize)]
pub struct MfaBody {
pending: String,