fix(middleware): correct order - Auth runs BEFORE RBAC

In Axum, layers are applied bottom-to-top (last added runs first).
So Auth middleware must be added AFTER RBAC in the chain to run BEFORE it.

Previous order (wrong): RBAC -> Auth -> Handler
New order (correct): Auth -> RBAC -> Handler
This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-01-10 14:07:23 -03:00
parent 0bda3ed466
commit 113f44b957

View file

@ -500,6 +500,14 @@ async fn run_axum_server(
.layer(rate_limit_extension)
// Request ID tracking for all requests
.layer(middleware::from_fn(request_id_middleware))
// Authentication middleware using provider registry
// NOTE: In Axum, layers are applied bottom-to-top, so this runs BEFORE RBAC
.layer(middleware::from_fn(move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
let state = auth_middleware_state.clone();
async move {
botserver::security::auth_middleware_with_providers(req, next, state).await
}
}))
// RBAC middleware - checks permissions AFTER authentication
.layer(middleware::from_fn(move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
let rbac = Arc::clone(&rbac_manager_for_middleware);
@ -507,13 +515,6 @@ async fn run_axum_server(
botserver::security::rbac_middleware_fn(req, next, rbac).await
}
}))
// Authentication middleware using provider registry
.layer(middleware::from_fn(move |req: axum::http::Request<axum::body::Body>, next: axum::middleware::Next| {
let state = auth_middleware_state.clone();
async move {
botserver::security::auth_middleware_with_providers(req, next, state).await
}
}))
// Panic handler catches panics and returns safe 500 responses
.layer(middleware::from_fn(move |req, next| {
let config = panic_config.clone();