fix(security): complete security and delivery compliance remediation
This commit is contained in:
@@ -26,6 +26,21 @@ _LOG_GET_PATHS = {
|
||||
"/api/auth/profile",
|
||||
}
|
||||
|
||||
_SENSITIVE_PARAM_MARKERS = ("password", "passwd", "secret", "token", "authorization", "credential", "code")
|
||||
|
||||
|
||||
def sanitize_audit_params(value):
|
||||
"""Recursively redact sensitive request parameters before asynchronous logging."""
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
key: "***" if any(marker in key.lower() for marker in _SENSITIVE_PARAM_MARKERS)
|
||||
else sanitize_audit_params(item)
|
||||
for key, item in value.items()
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [sanitize_audit_params(item) for item in value]
|
||||
return value
|
||||
|
||||
|
||||
def _should_log(method: str, path: str) -> bool:
|
||||
for skip in _SKIP_PATHS:
|
||||
@@ -62,11 +77,7 @@ class AuditMiddleware(BaseHTTPMiddleware):
|
||||
if body_bytes:
|
||||
try:
|
||||
request_params = json.loads(body_bytes)
|
||||
# 脱敏:移除密码字段
|
||||
if isinstance(request_params, dict):
|
||||
for k in ("password", "passwd", "secret"):
|
||||
if k in request_params:
|
||||
request_params[k] = "***"
|
||||
request_params = sanitize_audit_params(request_params)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
|
||||
@@ -67,7 +67,7 @@ def require_permission(permission: str):
|
||||
token = authorization[7:]
|
||||
payload = verify_token(token)
|
||||
if not payload:
|
||||
logger.warning("auth rejected: token 验证失败 (permission=%s): token前20字符=%.20s...", permission, token[:20])
|
||||
logger.warning("auth rejected: token 验证失败 (permission=%s)", permission)
|
||||
raise HTTPException(status_code=401, detail="令牌无效或已过期")
|
||||
|
||||
role = payload.get('role', 'user')
|
||||
|
||||
Reference in New Issue
Block a user