fix: 第3轮调试Bug修复 (P1: 弱随机+crypto/rand, Knowledge键对齐; P2: 简报created_at, CORS+安全头)

This commit is contained in:
2026-05-20 14:20:47 +08:00
parent 25d6eff7c3
commit 3adf0137cc
6 changed files with 349 additions and 16 deletions
+15 -2
View File
@@ -6,15 +6,28 @@ import (
"github.com/gin-gonic/gin"
)
// CORS 跨域中间件
// CORS 跨域中间件 (含安全头)
func CORS() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
origin := c.Request.Header.Get("Origin")
if origin == "" {
origin = "*"
}
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization, X-Request-ID")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Max-Age", "86400")
// 安全头
c.Header("X-Content-Type-Options", "nosniff")
c.Header("X-Frame-Options", "DENY")
c.Header("X-XSS-Protection", "1; mode=block")
c.Header("Referrer-Policy", "strict-origin-when-cross-origin")
c.Header("Permissions-Policy", "camera=(), microphone=(), geolocation=()")
c.Header("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https:; font-src 'self'; object-src 'none'")
c.Header("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
// 预检请求
if c.Request.Method == http.MethodOptions {
c.AbortWithStatus(http.StatusNoContent)