fix(auth): close unauthenticated lockout and tighten the daemon's edges

Key login failures by username and client address with decay, plus a loose
per-username limit that delays every address by a bounded time. Refused
attempts write nothing. Unknown usernames share the code path. MFA and session
password checks keep a persistent user-bound count that needs a password or a
session to reach.

Also: local mode refuses a non-loopback bind outside a container; security
headers on every response; the Argon2 permit lives as long as the blocking
task; a required password change rechecks role, MFA and epoch; auth reset
works for any user; mfa_enabled in /api/auth/state; disabling MFA that is off
is refused with no side effects; StateCorrupt keeps only line and column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 21:03:27 -03:00
1 parent 3e4c4201e5
commit 348e61ca95
10 files changed
+725 -199

No files matched your search

+31 -1
View File
@@ -10,7 +10,7 @@ use axum::body::Body;
use axum::extract::Request;
use axum::extract::{DefaultBodyLimit, Query, State};
use axum::http::{header, HeaderValue, Method, StatusCode, Uri};
use axum::middleware::{from_fn_with_state, Next};
use axum::middleware::{from_fn, from_fn_with_state, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{delete, get, patch, post, MethodRouter};
use axum::{Json, Router};
@@ -238,6 +238,7 @@ pub fn build_app(state: AppState) -> Router {
.layer(DefaultBodyLimit::max(auth_http::MAX_BODY_BYTES))
.layer(from_fn_with_state(state.clone(), require_auth))
.layer(from_fn_with_state(state.clone(), hsts))
.layer(from_fn(security_headers))
.with_state(state)
.layer(CompressionLayer::new())
.layer(TraceLayer::new_for_http())
@@ -256,6 +257,34 @@ async fn hsts(State(state): State<AppState>, req: Request, next: Next) -> Respon
response
}
/// `style-src 'unsafe-inline'` is the narrowest that works: the report carries
/// its stylesheet inline and Paged.js adds `<style>` elements while it paginates.
/// Scripts stay same-origin only. `data:` images are the report's embedded logo
/// and the MFA QR code.
pub(crate) const CSP: &str = "default-src 'none'; script-src 'self'; \
style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; \
connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'";
/// On every response, refusals included.
async fn security_headers(req: Request, next: Next) -> Response {
let mut response = next.run(req).await;
let headers = response.headers_mut();
headers.insert(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static(CSP),
);
headers.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
headers.insert(
header::X_CONTENT_TYPE_OPTIONS,
HeaderValue::from_static("nosniff"),
);
headers.insert(
header::REFERRER_POLICY,
HeaderValue::from_static("no-referrer"),
);
response
}
#[derive(Serialize)]
struct Health {
status: &'static str,
@@ -418,6 +447,7 @@ pub async fn serve(bind: &str, data_dir: PathBuf) -> Result<()> {
// Decide the exposure before touching the data directory or printing a
// setup code: a refusal must leave nothing behind.
let transport = Transport::from_env(&|name| std::env::var(name).ok())?;
transport.check_bind(bind, &|name| std::env::var(name).ok())?;
let _ = tracing_subscriber::fmt().try_init();
std::fs::create_dir_all(&data_dir)?;
let auth = Arc::new(AuthService::open(