fix: 审计问题全量修复 + RateLimiter 原子化重构 + CDN 本地化收紧
- 安全:移除 CSP 中 esm.sh/cdnjs.cloudflare.com,highlight.js 主题已本地化 73 个文件 - 安全:StatusBanned 分支补 dummy hash 防时序攻击 - 安全:手写 constantTimeEq 替换为 crypto/subtle.ConstantTimeCompare - Bug:锁定操作消息已删除→已锁定 - YAGNI:删除 12 个空预留模板目录 - KISS:删除 common.CheckPassword 薄封装,统一用 bcrypt 调用 - KISS:删除 tokenCtrl 别名字段,api.go 统一用 authCtrl - RateLimiter:check()+recordFail 闭包模式重构为 try() 原子操作,消除竞态 - RateLimiter:新增 ClearIP() 方法,注册成功时清除 IP 计数 - 文档:修正 audit_service.go 注释编号跳跃(3→5→4) - 文档:修正 deps_core.go 过清理→过期清理
This commit is contained in:
@ -94,7 +94,7 @@ func (ac *AdminController) UpdateUserStatus(c *gin.Context) {
|
||||
if req.Status == model.StatusActive {
|
||||
action = "解封"
|
||||
} else if req.Status == model.StatusLocked {
|
||||
action = "已删除"
|
||||
action = "已锁定"
|
||||
}
|
||||
common.OkMessage(c, action+"成功")
|
||||
}
|
||||
|
||||
@ -31,15 +31,15 @@ func (ac *AuthController) Login(c *gin.Context) {
|
||||
email := req.Email
|
||||
ip := clientIP(c)
|
||||
|
||||
// --- 限流:账户维度 ---
|
||||
acctResult, recordAccount := ac.rateLimiter.AllowAccount(email)
|
||||
// --- 限流:账户维度(原子检查+递增)---
|
||||
acctResult := ac.rateLimiter.AllowAccount(email)
|
||||
if acctResult.Blocked {
|
||||
common.Error(c, http.StatusTooManyRequests, acctResult.Message)
|
||||
return
|
||||
}
|
||||
|
||||
// --- 限流:IP 维度 ---
|
||||
ipResult, recordIP := ac.rateLimiter.AllowIP(ip)
|
||||
// --- 限流:IP 维度(原子检查+递增)---
|
||||
ipResult := ac.rateLimiter.AllowIP(ip)
|
||||
if ipResult.Blocked {
|
||||
common.Error(c, http.StatusTooManyRequests, ipResult.Message)
|
||||
return
|
||||
@ -47,14 +47,7 @@ func (ac *AuthController) Login(c *gin.Context) {
|
||||
|
||||
user, err := ac.authService.Login(req, ip)
|
||||
if err != nil {
|
||||
// 记录失败 → 两个维度各 +1
|
||||
if recordAccount != nil {
|
||||
recordAccount()
|
||||
}
|
||||
if recordIP != nil {
|
||||
recordIP()
|
||||
}
|
||||
|
||||
// 失败计数已在 AllowAccount/AllowIP 中原子递增,无需额外记录
|
||||
switch err {
|
||||
case common.ErrInvalidCred:
|
||||
common.Error(c, http.StatusUnauthorized, "邮箱或密码错误")
|
||||
|
||||
@ -34,8 +34,9 @@ func (ac *AuthController) Register(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 注册 IP 限流:1 分钟 5 次
|
||||
regResult, recordReg := ac.rateLimiter.AllowIP(clientIP(c) + ":register")
|
||||
// 注册 IP 限流:1 分钟 5 次(原子检查+递增)
|
||||
regKey := clientIP(c) + ":register"
|
||||
regResult := ac.rateLimiter.AllowIP(regKey)
|
||||
if regResult.Blocked {
|
||||
common.Error(c, http.StatusTooManyRequests, "注册请求过于频繁,请稍后重试")
|
||||
return
|
||||
@ -43,9 +44,7 @@ func (ac *AuthController) Register(c *gin.Context) {
|
||||
|
||||
user, err := ac.authService.Register(req, clientIP(c))
|
||||
if err != nil {
|
||||
if recordReg != nil {
|
||||
recordReg()
|
||||
}
|
||||
// 失败计数已在 AllowIP 中原子递增,无需额外记录
|
||||
switch err {
|
||||
case common.ErrMaintenanceMode:
|
||||
common.Error(c, http.StatusForbidden, "社区正在维护中,暂不支持注册")
|
||||
@ -61,6 +60,9 @@ func (ac *AuthController) Register(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 注册成功 → 清除该 IP 的注册限流计数
|
||||
ac.rateLimiter.ClearIP(regKey)
|
||||
|
||||
sid, err := ac.sessionManager.Create(user, req.RememberMe, clientIP(c), c.GetHeader("User-Agent"))
|
||||
if err != nil {
|
||||
common.Error(c, http.StatusInternalServerError, "注册失败,请稍后重试")
|
||||
|
||||
@ -17,11 +17,12 @@ type authUseCase interface {
|
||||
IsRegistrationEnabled() bool
|
||||
}
|
||||
|
||||
// rateLimiter AuthController 对 RateLimiter 的最小依赖(ISP:3 个方法)
|
||||
// rateLimiter AuthController 对 RateLimiter 的最小依赖(ISP:4 个方法)
|
||||
type rateLimiter interface {
|
||||
AllowAccount(email string) (middleware.RateLimitResult, func())
|
||||
AllowIP(ip string) (middleware.RateLimitResult, func())
|
||||
AllowAccount(email string) middleware.RateLimitResult
|
||||
AllowIP(ip string) middleware.RateLimitResult
|
||||
Clear(email, ip string)
|
||||
ClearIP(ipKey string)
|
||||
}
|
||||
|
||||
// postUseCase PostController 对 PostService 的最小依赖(ISP:7 个方法)
|
||||
|
||||
Reference in New Issue
Block a user