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:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 19:56:59 -03:00
1 parent 2b1bc63340
commit 5ad89d5be5
6 files changed
+218 -39

No files matched your search

+11 -11
View File
@@ -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 {