feat(auth): deny-by-default HTTP layer
A global middleware wraps the router and its fallback: only PUBLIC_ROUTES answer without a credential, and unknown paths look like protected ones. Adds cookie sessions with per-session CSRF, scoped Bearer tokens, the /api/auth and /api/admin handlers, Transport for client IP and Secure, and pending-token enrollment re-checked inside one store update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a90ff81650
commit
2b1bc63340
11 files changed
+2455
-79
No files matched your search
@@ -945,3 +945,94 @@ fn change_password_clears_flag_and_refuses_the_same_password() {
|
||||
.unwrap();
|
||||
assert!(!flag);
|
||||
}
|
||||
|
||||
// ---- Enrollment through a pending token: re-checked inside the update ----
|
||||
|
||||
fn first_admin(s: &AuthService) -> PendingMfa {
|
||||
let code = s.ensure_bootstrap(T0).unwrap().unwrap();
|
||||
s.complete_bootstrap(&code, "root", PW, T0).unwrap()
|
||||
}
|
||||
|
||||
fn root_id(s: &AuthService) -> String {
|
||||
s.store
|
||||
.read(|st| {
|
||||
st.users
|
||||
.iter()
|
||||
.find(|u| u.username == "root")
|
||||
.unwrap()
|
||||
.id
|
||||
.clone()
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn code_from_uri(uri: &str, unix: u64) -> String {
|
||||
totp_rs::Totp::from_url(uri)
|
||||
.unwrap()
|
||||
.generate(unix)
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_enrollment_activates_mfa_and_then_refuses_the_token() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let s = AuthService::open(dir.path(), None).unwrap();
|
||||
let pending = first_admin(&s);
|
||||
let e = s.start_enrollment_pending(&pending.token, T0).unwrap();
|
||||
let codes = s
|
||||
.confirm_enrollment_pending(&pending.token, &code_from_uri(&e.uri, T0), T0)
|
||||
.unwrap();
|
||||
assert!(!codes.is_empty());
|
||||
// A password alone can never replace a second factor that is now active.
|
||||
assert!(s.start_enrollment_pending(&pending.token, T0).is_err());
|
||||
assert!(s
|
||||
.confirm_enrollment_pending(&pending.token, &code_from_uri(&e.uri, T0 + 30), T0 + 30)
|
||||
.is_err());
|
||||
// The same token still finishes the login, with a later code.
|
||||
let step = s
|
||||
.confirm_mfa(&pending.token, &code_from_uri(&e.uri, T0 + 30), T0 + 30)
|
||||
.unwrap();
|
||||
assert!(matches!(step, LoginStep::Session(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_enrollment_refuses_a_stale_epoch_and_other_purposes() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let s = AuthService::open(dir.path(), None).unwrap();
|
||||
let pending = first_admin(&s);
|
||||
let e = s.start_enrollment_pending(&pending.token, T0).unwrap();
|
||||
let id = root_id(&s);
|
||||
s.store
|
||||
.update(|st| {
|
||||
st.users.iter_mut().find(|u| u.id == id).unwrap().auth_epoch += 1;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert!(s.start_enrollment_pending(&pending.token, T0).is_err());
|
||||
assert!(s
|
||||
.confirm_enrollment_pending(&pending.token, &code_from_uri(&e.uri, T0), T0)
|
||||
.is_err());
|
||||
assert!(s.start_enrollment_pending("forged", T0).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_users_exposes_no_secret_material() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let s = AuthService::open(dir.path(), None).unwrap();
|
||||
first_admin(&s);
|
||||
let json = serde_json::to_string(&s.list_users().unwrap()).unwrap();
|
||||
assert!(json.contains("root"));
|
||||
for leak in ["password_hash", "recovery", "totp_", "$argon2"] {
|
||||
assert!(!json.contains(leak), "{leak} in {json}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csrf_value_is_bound_to_the_session_and_is_not_its_token_hash() {
|
||||
let a = crate::auth::tokens::csrf_token("session-a");
|
||||
assert_ne!(a, crate::auth::tokens::csrf_token("session-b"));
|
||||
assert_ne!(a, hash_token("session-a"));
|
||||
assert!(crate::auth::tokens::csrf_matches("session-a", &a));
|
||||
assert!(!crate::auth::tokens::csrf_matches("session-b", &a));
|
||||
assert!(!crate::auth::tokens::csrf_matches("session-a", ""));
|
||||
}
|
||||
Reference in new issue
Block a user