fix(auth): throttle bootstrap and require reauth for user changes
Bootstrap takes the client IP, is limited per IP, and writes no audit line once an admin exists. Creating, changing the role of or deleting a user now needs a session with recent reauthentication; API tokens are refused. /api/analyze and /api/update return fixed messages and log the detail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b1bc63340
commit
5ad89d5be5
6 files changed
+218
-39
No files matched your search
@@ -326,20 +326,28 @@ impl AuthService {
|
||||
|
||||
/// Creates the first admin. Bad input does not burn the code. The result
|
||||
/// is an enrollment-stage pending token: there is no session until MFA.
|
||||
///
|
||||
/// Anyone can call this, so it must not let them grow the audit log: once
|
||||
/// an admin exists the refusal writes nothing, and before that wrong codes
|
||||
/// are limited per IP.
|
||||
pub fn complete_bootstrap(
|
||||
&self,
|
||||
code: &str,
|
||||
username: &str,
|
||||
password: &str,
|
||||
ip: IpAddr,
|
||||
now: u64,
|
||||
) -> Result<PendingMfa> {
|
||||
if self.has_admin()? {
|
||||
return Err(AuthFailure::BootstrapInvalid.into());
|
||||
}
|
||||
let name = validate_username(username)?;
|
||||
validate_password(password)?;
|
||||
self.ips.acquire(ip, now).map_err(throttled)?;
|
||||
let code_hash = hash_token(code);
|
||||
// Checked first so a wrong code costs no Argon2 work.
|
||||
if !self.store.read(|st| bootstrap_valid(st, &code_hash, now))? {
|
||||
self.log("bootstrap.complete", None, None, false, None)?;
|
||||
return Err(AuthFailure::BootstrapInvalid.into());
|
||||
return self.refuse_bootstrap(ip);
|
||||
}
|
||||
let password_hash = hash_password(password)?;
|
||||
// Checked and consumed in one closure: concurrent requests get one win.
|
||||
@@ -360,13 +368,21 @@ impl AuthService {
|
||||
Ok(Some(self.seal_pending(&id, 0, PENDING_PURPOSE, now)?))
|
||||
})?;
|
||||
let Some(pending) = done else {
|
||||
self.log("bootstrap.complete", None, None, false, None)?;
|
||||
return Err(AuthFailure::BootstrapInvalid.into());
|
||||
return self.refuse_bootstrap(ip);
|
||||
};
|
||||
self.log("bootstrap.complete", Some(&name), None, true, None)?;
|
||||
self.ips.release(ip);
|
||||
self.log("bootstrap.complete", Some(&name), Some(ip), true, None)?;
|
||||
Ok(pending)
|
||||
}
|
||||
|
||||
/// Losing a race to the first admin is not worth an audit line either.
|
||||
fn refuse_bootstrap(&self, ip: IpAddr) -> Result<PendingMfa> {
|
||||
if !self.has_admin()? {
|
||||
self.log("bootstrap.complete", None, Some(ip), false, None)?;
|
||||
}
|
||||
Err(AuthFailure::BootstrapInvalid.into())
|
||||
}
|
||||
|
||||
fn seal_pending(
|
||||
&self,
|
||||
user_id: &str,
|
||||
|
||||
@@ -69,7 +69,7 @@ fn enroll(s: &AuthService, id: &str, now: u64) -> Vec<String> {
|
||||
/// First admin, enrolled. Returns its id.
|
||||
fn admin(s: &AuthService) -> String {
|
||||
let code = s.ensure_bootstrap(T0).unwrap().unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, T0).unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, ip(), T0).unwrap();
|
||||
let id = uid(s, "root");
|
||||
enroll(s, &id, T0);
|
||||
id
|
||||
@@ -106,16 +106,16 @@ fn bootstrap_code_single_use_and_expires() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
failure(s.complete_bootstrap("wrong", "root", PW, T0)),
|
||||
failure(s.complete_bootstrap("wrong", "root", PW, ip(), T0)),
|
||||
AuthFailure::BootstrapInvalid
|
||||
);
|
||||
assert_eq!(
|
||||
failure(s.complete_bootstrap(&code, "root", PW, T0 + 3601)),
|
||||
failure(s.complete_bootstrap(&code, "root", PW, ip(), T0 + 3601)),
|
||||
AuthFailure::BootstrapInvalid
|
||||
);
|
||||
// A weak password does not burn the code.
|
||||
assert!(matches!(
|
||||
s.complete_bootstrap(&code, "root", "short", T0)
|
||||
s.complete_bootstrap(&code, "root", "short", ip(), T0)
|
||||
.unwrap_err(),
|
||||
AcauaError::InvalidInput(_)
|
||||
));
|
||||
@@ -123,13 +123,13 @@ fn bootstrap_code_single_use_and_expires() {
|
||||
let renewed = s.ensure_bootstrap(T0 + 10).unwrap().unwrap();
|
||||
assert_ne!(renewed, code);
|
||||
assert_eq!(
|
||||
failure(s.complete_bootstrap(&code, "root", PW, T0 + 10)),
|
||||
failure(s.complete_bootstrap(&code, "root", PW, ip(), T0 + 10)),
|
||||
AuthFailure::BootstrapInvalid
|
||||
);
|
||||
s.complete_bootstrap(&renewed, "root", PW, T0 + 3610)
|
||||
s.complete_bootstrap(&renewed, "root", PW, ip(), T0 + 3610)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
failure(s.complete_bootstrap(&renewed, "other", PW, T0 + 3610)),
|
||||
failure(s.complete_bootstrap(&renewed, "other", PW, ip(), T0 + 3610)),
|
||||
AuthFailure::BootstrapInvalid
|
||||
);
|
||||
assert_eq!(s.ensure_bootstrap(T0 + 3620).unwrap(), None);
|
||||
@@ -145,7 +145,7 @@ fn bootstrap_code_is_consumed_once_under_concurrency() {
|
||||
.map(|i| {
|
||||
let (s, code) = (&s, &code);
|
||||
sc.spawn(move || {
|
||||
s.complete_bootstrap(code, &format!("adm{i}"), PW, T0)
|
||||
s.complete_bootstrap(code, &format!("adm{i}"), PW, ip(), T0)
|
||||
.is_ok()
|
||||
})
|
||||
})
|
||||
@@ -163,7 +163,7 @@ fn bootstrap_code_is_consumed_once_under_concurrency() {
|
||||
fn admin_cannot_finish_login_without_mfa() {
|
||||
let (_d, s) = svc();
|
||||
let code = s.ensure_bootstrap(T0).unwrap().unwrap();
|
||||
let boot = s.complete_bootstrap(&code, "root", PW, T0).unwrap();
|
||||
let boot = s.complete_bootstrap(&code, "root", PW, ip(), T0).unwrap();
|
||||
let id = uid(&s, "root");
|
||||
let LoginStep::MfaEnrollRequired(p) = s.login("root", PW, ip(), T0).unwrap() else {
|
||||
panic!("admin must enroll");
|
||||
@@ -712,7 +712,7 @@ fn session_of_any(s: &AuthService, id: &str) {
|
||||
fn audit_records_operations_without_secrets() {
|
||||
let (_d, s) = svc();
|
||||
let code = s.ensure_bootstrap(T0).unwrap().unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, T0).unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, ip(), T0).unwrap();
|
||||
let id = uid(&s, "root");
|
||||
let codes = enroll(&s, &id, T0);
|
||||
// A password typed into the username field must not reach the log.
|
||||
@@ -950,7 +950,7 @@ fn change_password_clears_flag_and_refuses_the_same_password() {
|
||||
|
||||
fn first_admin(s: &AuthService) -> PendingMfa {
|
||||
let code = s.ensure_bootstrap(T0).unwrap().unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, T0).unwrap()
|
||||
s.complete_bootstrap(&code, "root", PW, ip(), T0).unwrap()
|
||||
}
|
||||
|
||||
fn root_id(s: &AuthService) -> String {
|
||||
|
||||
@@ -607,7 +607,7 @@ mod tests {
|
||||
/// Bootstraps `root` (admin, MFA enrolled).
|
||||
fn root(s: &AuthService) -> String {
|
||||
let code = s.ensure_bootstrap(T0).unwrap().unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, T0).unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, ip(), T0).unwrap();
|
||||
let id = uid(s, "root");
|
||||
enroll(s, &id, T0);
|
||||
id
|
||||
|
||||
Reference in new issue
Block a user