refactor: 修复9项中等架构问题
安全修复: - WebSocket 端点添加认证(cookie/token),防止未授权窃听日志 - SPA serve_spa 添加路径遍历防护(resolve + relative_to 检查) - Token 改用 httpOnly Cookie 存储,移除前端 localStorage token(防 XSS 窃取) - 添加安全响应头中间件(X-Content-Type-Options/X-Frame-Options/Referrer-Policy) - HTTP 请求日志脱敏请求体中的 password/secret/token 等敏感字段 - 权限检查统一使用 user_has_permission(考虑自定义权限,修复 has_permission 忽略 custom_permissions 的缺陷) 性能与稳定性: - cookies.py 列表接口修复 N+1 查询(改为批量查询 Account) - login_service.py run() 结束时关闭 DB Session(防止连接泄漏) - _active_batches/_active_tests 全局字典添加 threading.Lock(防止并发竞态) 配置优化: - CORS 源支持环境变量 CORS_ORIGINS 配置 - Uvicorn reload 支持环境变量 UVICORN_RELOAD 控制(生产环境默认关闭) - Cookie 安全标志支持环境变量 COOKIE_SECURE 配置(HTTPS 部署时启用) - logs.py 权限不足返回 HTTP 403(原来返回 200 + message)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
"""请求日志路由 - 查看 HTTP 请求/响应详情日志"""
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from typing import Optional
|
||||
|
||||
from ..deps import get_current_user
|
||||
from ..permissions import has_permission
|
||||
from ..permissions import user_has_permission
|
||||
from utils.http_logger import read_http_logs, clear_http_logs
|
||||
|
||||
router = APIRouter(prefix="/api/logs", tags=["日志"])
|
||||
@@ -20,10 +20,10 @@ def list_http_logs(
|
||||
current=Depends(get_current_user),
|
||||
):
|
||||
"""查看 HTTP 请求/响应详情日志(需要审计日志查看权限)"""
|
||||
if not has_permission(current.role, "audit:view"):
|
||||
if not user_has_permission(current, "audit:view"):
|
||||
# 运营也可以查看请求日志(用于排查登录问题)
|
||||
if not has_permission(current.role, "login:batch"):
|
||||
return {"items": [], "total": 0, "message": "无权限"}
|
||||
if not user_has_permission(current, "login:batch"):
|
||||
raise HTTPException(status_code=403, detail="无权限")
|
||||
|
||||
items, total = read_http_logs(
|
||||
limit=limit,
|
||||
@@ -38,9 +38,9 @@ def list_http_logs(
|
||||
@router.delete("/http")
|
||||
def clear_http_logs_api(current=Depends(get_current_user)):
|
||||
"""清空 HTTP 请求/响应详情日志"""
|
||||
if not has_permission(current.role, "audit:view"):
|
||||
if not has_permission(current.role, "login:batch"):
|
||||
return {"success": False, "message": "无权限"}
|
||||
if not user_has_permission(current, "audit:view"):
|
||||
if not user_has_permission(current, "login:batch"):
|
||||
raise HTTPException(status_code=403, detail="无权限")
|
||||
|
||||
count = clear_http_logs()
|
||||
return {"success": True, "cleared": count}
|
||||
|
||||
Reference in New Issue
Block a user