refactor: CSP nonce替代unsafe-inline, HSTS始终启用, router/api拆分, 管理后台+用户设置页拆分, DB健康降级, 时区配置, UID统一解析, CI接入
Some checks failed
CI / Lint + Build (push) Has been cancelled
Some checks failed
CI / Lint + Build (push) Has been cancelled
审计修复: - CSP: unsafe-inline移除, nonce替代; unsafe-eval保留供Vditor使用 - HSTS: 始终启用(不再依赖release模式) - Controller Exp: common.GetGinExp包装, 不再直接读context - UID解析: common.ParseUIDParam统一, follow/admin controller改用 重构: - router/api.go(198行)拆为7个域文件: auth/settings/posts/comments/reactions/social/studio - 管理后台站点设置: 单页拆为4个子页(brand/security/registration/content)+子菜单 - 前台用户设置页: 1201行拆为6个独立模板+6个独立JS文件 新增: - DB健康检测中间件(middleware/db_health.go): 5s ping+降级页面 - 时区配置: config.yaml server.timezone→time.Local初始化 - .gitea/workflows/ci.yml: strict模式CI流水线
This commit is contained in:
@ -3,8 +3,7 @@ package middleware
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
|
||||
"metazone.cc/mce/internal/config"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -28,12 +27,13 @@ func SecurityHeaders() gin.HandlerFunc {
|
||||
c.Set("csp_nonce", nonce)
|
||||
|
||||
// Content-Security-Policy
|
||||
// script-src/style-src: 本站 + nonce(替代 unsafe-inline)
|
||||
// script-src: 本站 + nonce(替代 unsafe-inline),保留 unsafe-eval 供 Vditor 使用
|
||||
// style-src: 本站(所有内联样式已使用 nonce)
|
||||
// img-src: 本站 + data: URI(头像裁切)+ blob:(粘贴图片)
|
||||
c.Header("Content-Security-Policy",
|
||||
"default-src 'self'; "+
|
||||
"script-src 'self' 'unsafe-inline' 'unsafe-eval'; "+
|
||||
"style-src 'self' 'unsafe-inline'; "+
|
||||
fmt.Sprintf("script-src 'self' 'unsafe-eval' 'nonce-%s'; ", nonce)+
|
||||
"style-src 'self' 'nonce-"+nonce+"'; "+
|
||||
"img-src 'self' data: blob:; "+
|
||||
"connect-src 'self'")
|
||||
|
||||
@ -46,10 +46,8 @@ func SecurityHeaders() gin.HandlerFunc {
|
||||
// 引用策略
|
||||
c.Header("Referrer-Policy", "strict-origin-when-cross-origin")
|
||||
|
||||
// HSTS:仅在 release 模式启用,避免开发环境 localhost 证书问题
|
||||
if config.App != nil && config.App.Server.Mode == "release" {
|
||||
c.Header("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
|
||||
}
|
||||
// HSTS:始终启用(max-age=1年,含子域名,允许 preload 列表收录)
|
||||
c.Header("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user