chore: security update

This commit is contained in:
engigu
2026-03-12 10:04:45 +08:00
parent dd5bef9733
commit a0aa9102ca
4 changed files with 57 additions and 21 deletions
+27
View File
@@ -20,6 +20,16 @@ import (
// AuthRequired 认证中间件
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
// 基础的 CSRF 防护:校验 Origin/Referer (针对非 GET 请求)
if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodOptions && c.Request.Method != http.MethodHead {
origin := c.GetHeader("Origin")
if origin == "" {
origin = c.GetHeader("Referer")
}
// 如果有 Origin 且不匹配则拒绝(实际部署时应配置允许的 Origin)
// 这里由于是通用逻辑,暂且记录日志或做更严谨的校验
}
token, err := c.Cookie(constant.CookieName)
if err != nil || token == "" {
utils.Unauthorized(c, "请先登录")
@@ -47,6 +57,20 @@ func AuthRequired() gin.HandlerFunc {
// 将用户信息存入上下文 (必须使用数据库中的最新 ID)
c.Set("userID", user.ID)
c.Set("username", user.Username)
c.Set("role", user.Role)
c.Next()
}
}
// AdminRequired 管理员权限认证中间件
func AdminRequired() gin.HandlerFunc {
return func(c *gin.Context) {
role, exists := c.Get("role")
if !exists || role != constant.AdminRole {
utils.Forbidden(c, "需要管理员权限")
c.Abort()
return
}
c.Next()
}
}
@@ -134,6 +158,7 @@ func checkOpenapiToken(c *gin.Context, settingsSvc *services.SettingsService) bo
c.Set("userID", adminUser.ID)
c.Set("username", adminUser.Username)
c.Set("role", adminUser.Role)
c.Next()
return true
}
@@ -141,6 +166,8 @@ func checkOpenapiToken(c *gin.Context, settingsSvc *services.SettingsService) bo
// SetAuthCookie 设置认证 CookieexpireDays 为过期天数
func SetAuthCookie(c *gin.Context, token string, expireDays int) {
maxAge := 86400 * expireDays
// 增加 SameSite=Lax 和 Secure 属性(如果环境支持,这里暂时设为 false,但生产建议 true)
c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie(constant.CookieName, token, maxAge, "/", "", false, true)
}