fix: 上传目录所有文件禁止执行脚本,防止同源存储型 XSS

This commit is contained in:
dujiao-next committed 2026-10-11 12:13:57 +08:00
1 parent bc076c984a
commit 3ae53014f2
1 file changed
+5 -4
+5 -4
View File
@@ -246,13 +246,14 @@ func SetupRouter(cfg *config.Config, c *container.Container) *gin.Engine {
r.Use(middleware.CallbackRouteMiddleware(c.SettingService, paymentCallbackHandler, paymentWebhookHandler, upstreamHandler))
// 静态文件服务(上传的图片)必须放在前面。
// SVG 强制下载并禁止脚本:即使上传校验被绕过,直接打开 /uploads/x.svg 也不会在站点源下执行脚本;
// <img src> 引用不受 Content-Disposition 影响,正常显示。
// 所有上传文件一律禁止脚本:即使上传校验被绕过(如 telegram 场景不限扩展名、上游图片),
// 直接打开 /uploads/x.html 也不会在站点源下执行脚本;<img src> 引用不受影响。
// SVG 额外强制下载。
r.Group("/uploads", func(c *gin.Context) {
c.Header("Content-Security-Policy", "sandbox; script-src 'none'")
c.Header("X-Content-Type-Options", "nosniff")
if strings.EqualFold(path.Ext(c.Request.URL.Path), ".svg") {
c.Header("Content-Disposition", "attachment")
c.Header("Content-Security-Policy", "sandbox; script-src 'none'")
c.Header("X-Content-Type-Options", "nosniff")
}
}).Static("/", "./uploads")