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
+23
-18
@@ -90,6 +90,9 @@ pub enum Access {
|
||||
SessionOrPending,
|
||||
/// Administrator role, and Admin scope when through a token.
|
||||
Admin,
|
||||
/// Administrator in a session that reauthenticated recently, never a
|
||||
/// token: otherwise a hijacked session could create an admin outliving it.
|
||||
AdminRecentSession,
|
||||
}
|
||||
|
||||
pub struct RouteDef {
|
||||
@@ -117,7 +120,7 @@ const fn route(
|
||||
/// entries also in `PUBLIC_ROUTES` is never consulted.
|
||||
pub const ALL_ROUTES: &[RouteDef] = {
|
||||
use auth_http as a;
|
||||
use Access::{Admin, Authenticated, Session, SessionOrPending};
|
||||
use Access::{Admin, AdminRecentSession, Authenticated, Session, SessionOrPending};
|
||||
use Verb::{Delete, Get, Patch, Post};
|
||||
&[
|
||||
route(Get, "/api/health", Authenticated, || get(health)),
|
||||
@@ -167,11 +170,13 @@ pub const ALL_ROUTES: &[RouteDef] = {
|
||||
delete(a::tokens_revoke)
|
||||
}),
|
||||
route(Get, "/api/admin/users", Admin, || get(a::users_list)),
|
||||
route(Post, "/api/admin/users", Admin, || post(a::users_create)),
|
||||
route(Patch, "/api/admin/users", Admin, || {
|
||||
route(Post, "/api/admin/users", AdminRecentSession, || {
|
||||
post(a::users_create)
|
||||
}),
|
||||
route(Patch, "/api/admin/users", AdminRecentSession, || {
|
||||
patch(a::users_set_role)
|
||||
}),
|
||||
route(Delete, "/api/admin/users", Admin, || {
|
||||
route(Delete, "/api/admin/users", AdminRecentSession, || {
|
||||
delete(a::users_delete)
|
||||
}),
|
||||
route(Get, "/api/admin/audit", Admin, || get(a::audit)),
|
||||
@@ -255,6 +260,18 @@ struct AnalyzeRequest {
|
||||
path: String,
|
||||
}
|
||||
|
||||
/// The detail (paths, command output) goes to the log, not to the caller.
|
||||
fn failure(status: StatusCode, message: &str, detail: &acaua_core::AcauaError) -> Response {
|
||||
tracing::error!(error = %detail, "{message}");
|
||||
(
|
||||
status,
|
||||
Json(ErrorBody {
|
||||
error: message.to_string(),
|
||||
}),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn health() -> Json<Health> {
|
||||
Json(Health { status: "ok" })
|
||||
}
|
||||
@@ -295,13 +312,7 @@ async fn analyze(
|
||||
*state.report() = Some(report.clone());
|
||||
Json(report).into_response()
|
||||
}
|
||||
Ok(Err(error)) => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(ErrorBody {
|
||||
error: error.to_string(),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
Ok(Err(error)) => failure(StatusCode::BAD_REQUEST, "analysis failed", &error),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
}
|
||||
}
|
||||
@@ -332,13 +343,7 @@ async fn update_check() -> Response {
|
||||
let outcome = tokio::task::spawn_blocking(|| update::check(env!("CARGO_PKG_VERSION"))).await;
|
||||
match outcome {
|
||||
Ok(Ok(status)) => Json::<UpdateStatus>(status).into_response(),
|
||||
Ok(Err(error)) => (
|
||||
StatusCode::BAD_GATEWAY,
|
||||
Json(ErrorBody {
|
||||
error: error.to_string(),
|
||||
}),
|
||||
)
|
||||
.into_response(),
|
||||
Ok(Err(error)) => failure(StatusCode::BAD_GATEWAY, "update check failed", &error),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user