fix: CSRF Token 日志脱敏

- 移除 CSF 中间件日志中敏感 token 值的打印(移除 %q)
- MISMATCH 日志仅保留路径和长度信息
- OK 日志移除,避免生产环境中泄露 CSRF token
This commit is contained in:
2026-06-02 16:32:20 +08:00
parent 53f18ca6ee
commit 1cb7a832f1

View File

@ -46,16 +46,14 @@ func CSRF(cfg *config.Config) gin.HandlerFunc {
} }
if subtle.ConstantTimeCompare([]byte(cookieToken), []byte(headerToken)) != 1 { if subtle.ConstantTimeCompare([]byte(cookieToken), []byte(headerToken)) != 1 {
log.Printf("[CSRF] MISMATCH | path=%s | cookie(len=%d)=%q | header(len=%d)=%q", log.Printf("[CSRF] MISMATCH | path=%s | cookie_len=%d | header_len=%d",
c.Request.URL.Path, len(cookieToken), cookieToken, len(headerToken), headerToken) c.Request.URL.Path, len(cookieToken), len(headerToken))
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{ c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"success": false, "message": "CSRF 验证失败", "success": false, "message": "CSRF 验证失败",
}) })
return return
} }
log.Printf("[CSRF] OK | path=%s | token(len=%d)=%q", c.Request.URL.Path, len(cookieToken), cookieToken)
c.Set(csrfMetaName, cookieToken) c.Set(csrfMetaName, cookieToken)
c.Next() c.Next()
} }