SuperAPI AI 工具集

security-fastapi

编程与开发

查看项目

更新于 10-15 22:17

技能介绍

用途:审计 FastAPI 应用的安全配置,重点检查认证依赖、CORS 与中间件。

主要能力:

  1. 路由认证检查:FastAPI 通过路由或路由器的依赖实现认证授权;若代码中没有 Depends()/Security(),需逐条路由排查是否被意外公开。示例:@app.get("/private") 配合 user=Depends(get_current_user),或 user=Security(get_current_user, scopes=["items"])。
  2. API Key 方案:使用 API Key 时优先采用请求头方案 APIKeyHeader,并在服务端校验,例如 api_key = APIKeyHeader(name="x-api-key"),路由参数 key: str = Depends(api_key)。
  3. CORS:allow_origins=["*"] 会排除携带凭据(Cookie/Authorization)的请求;面向已认证浏览器客户端时必须显式列出允许来源,并配合 allow_credentials=True。
  4. 主机头与 HTTPS:建议使用 Starlette 中间件防御主机头攻击并在生产强制 HTTPS,即 TrustedHostMiddleware(allowed_hosts=["example.com", "*.example.com"])与 HTTPSRedirectMiddleware。

使用方式:提供快速审计命令,用 ripgrep 检索 fastapi 依赖声明、@app.(get|post|put|patch|delete) 路由、Depends(/Security( 认证依赖、CORSMiddleware/allow_origins/allow_credentials 配置、TrustedHostMiddleware/HTTPSRedirectMiddleware 中间件;并附 scripts/scan.sh 作为首轮 FastAPI 安全扫描脚本。

加固清单:敏感路由均需 Depends() 或 Security() 认证依赖;API Key 使用请求头而非查询参数;allow_credentials=True 时 allow_origins 必须显式;生产配置 TrustedHostMiddleware;生产启用 HTTPSRedirectMiddleware 或由代理强制 HTTPS。

限制:资料未提供具体版本要求、安装命令、收费与平台信息,暂未提供。