fix(saas): admin_guard_middleware — 非 admin 用户统一返回 403

BUG-M4 修复: 之前非 admin 用户发送 malformed body 到 admin 端点时,
Axum 先反序列化 body 返回 422,绕过了权限检查。

- 新增 admin_guard_middleware (auth/mod.rs) 在中间件层拦截
- account::admin_routes() 拆分 (dashboard 独立)
- billing::admin_routes() + account::admin_routes() 加 guard layer
- 非 admin 用户无论 body 是否合法,统一返回 403
This commit is contained in:
iven
2026-04-17 11:45:55 +08:00
parent b2758d34e9
commit 90340725a4
3 changed files with 35 additions and 2 deletions

View File

@@ -203,6 +203,27 @@ pub async fn auth_middleware(
}
}
/// Admin 路由守卫中间件: 确保 AuthContext 具有 admin/super_admin 角色
/// 必须在 auth_middleware 之后使用(依赖 Extension<AuthContext>
pub async fn admin_guard_middleware(
mut req: Request,
next: Next,
) -> Response {
use crate::auth::handlers::check_permission;
let ctx = req.extensions().get::<AuthContext>().cloned();
match ctx {
Some(ctx) => {
if let Err(e) = check_permission(&ctx, "account:admin") {
e.into_response()
} else {
next.run(req).await
}
}
None => SaasError::Unauthorized.into_response(),
}
}
/// 路由 (无需认证的端点)
pub fn routes() -> axum::Router<AppState> {
use axum::routing::post;