基于 backend-code-audit 审计报告,修复剩余 P0/P1/P2 共 34 项问题: P0 生产 Bug: - 修复 time.Since(time.Now()) 计时逻辑错误 (P0-03) - generateRandomID 改用 crypto/rand 替代固定索引 (P0-04) - IncrementQuotaUsed 重写为 Ent 原子操作消除 TOCTOU 竞态 (P0-05) 安全加固: - gateway/openai handler 错误响应替换为泛化消息,防止内部信息泄露 (P1-14) - usage_log_repo dateFormat 参数改用白名单映射,防止 SQL 注入 (P1-16) - 默认配置安全加固:sslmode=prefer、response_headers=true、mode=release (P1-18/19, P2-15) 性能优化: - gateway handler 循环内 defer 替换为显式 releaseWait 闭包 (P1-02) - group_repo/promo_code_repo Count 前 Clone 查询避免状态污染 (P1-03) - usage_log_repo 四个查询添加 LIMIT 10000 防止 OOM (P1-07) - GetBatchUsageStats 添加时间范围参数,默认最近 30 天 (P1-10) - ip.go CIDR 预编译为包级变量 (P1-11) - BatchUpdateCredentials 重构为先验证后更新 (P1-13) 缓存一致性: - billing_cache 添加 jitteredTTL 防止缓存雪崩 (P2-10) - DeductUserBalance/UpdateSubscriptionUsage 错误传播修复 (P2-12) - UserService.UpdateBalance 成功后异步失效 billingCache (P2-13) 代码质量: - search 截断改为按 rune 处理,支持多字节字符 (P2-01) - TLS Handshake 改为 HandshakeContext 支持 context 取消 (P2-07) - CORS 预检添加 Access-Control-Max-Age: 86400 (P2-16) 测试覆盖: - 新增 user_service_test.go(UpdateBalance 缓存失效 6 个用例) - 新增 batch_update_credentials_test.go(fail-fast + 类型验证 7 个用例) - 新增 response_transformer_test.go、ip_test.go、usage_log_repo_unit_test.go、search_truncate_test.go - 集成测试:IncrementQuotaUsed 并发测试、billing_cache 错误传播测试 - config_test.go 补充 server.mode/sslmode 默认值断言 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
var corsWarningOnce sync.Once
|
|
|
|
// CORS 跨域中间件
|
|
func CORS(cfg config.CORSConfig) gin.HandlerFunc {
|
|
allowedOrigins := normalizeOrigins(cfg.AllowedOrigins)
|
|
allowAll := false
|
|
for _, origin := range allowedOrigins {
|
|
if origin == "*" {
|
|
allowAll = true
|
|
break
|
|
}
|
|
}
|
|
wildcardWithSpecific := allowAll && len(allowedOrigins) > 1
|
|
if wildcardWithSpecific {
|
|
allowedOrigins = []string{"*"}
|
|
}
|
|
allowCredentials := cfg.AllowCredentials
|
|
|
|
corsWarningOnce.Do(func() {
|
|
if len(allowedOrigins) == 0 {
|
|
log.Println("Warning: CORS allowed_origins not configured; cross-origin requests will be rejected.")
|
|
}
|
|
if wildcardWithSpecific {
|
|
log.Println("Warning: CORS allowed_origins includes '*'; wildcard will take precedence over explicit origins.")
|
|
}
|
|
if allowAll && allowCredentials {
|
|
log.Println("Warning: CORS allowed_origins set to '*', disabling allow_credentials.")
|
|
}
|
|
})
|
|
if allowAll && allowCredentials {
|
|
allowCredentials = false
|
|
}
|
|
|
|
allowedSet := make(map[string]struct{}, len(allowedOrigins))
|
|
for _, origin := range allowedOrigins {
|
|
if origin == "" || origin == "*" {
|
|
continue
|
|
}
|
|
allowedSet[origin] = struct{}{}
|
|
}
|
|
|
|
return func(c *gin.Context) {
|
|
origin := strings.TrimSpace(c.GetHeader("Origin"))
|
|
originAllowed := allowAll
|
|
if origin != "" && !allowAll {
|
|
_, originAllowed = allowedSet[origin]
|
|
}
|
|
|
|
if originAllowed {
|
|
if allowAll {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
} else if origin != "" {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
|
|
c.Writer.Header().Add("Vary", "Origin")
|
|
}
|
|
if allowCredentials {
|
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
}
|
|
}
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With, X-API-Key")
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE, PATCH")
|
|
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
|
|
|
|
// 处理预检请求
|
|
if c.Request.Method == http.MethodOptions {
|
|
if originAllowed {
|
|
c.AbortWithStatus(http.StatusNoContent)
|
|
} else {
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
}
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func normalizeOrigins(values []string) []string {
|
|
if len(values) == 0 {
|
|
return nil
|
|
}
|
|
normalized := make([]string, 0, len(values))
|
|
for _, value := range values {
|
|
trimmed := strings.TrimSpace(value)
|
|
if trimmed == "" {
|
|
continue
|
|
}
|
|
normalized = append(normalized, trimmed)
|
|
}
|
|
return normalized
|
|
}
|