Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:lock: Close stored XSS asset extension bypass with allow-listed media types https://github.com/siyuan-note/siyuan/security/advisories/GHSA-7h8j-qw37-w46g

Danielca0a0689d4f5
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index a641225e92..f4da39b900 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -853,23 +853,29 @@ func setAssetsAttachmentDisposition(c *gin.Context, pathForBaseName string) {
c.Header("Content-Disposition", formatContentDispositionAttachment(filepath.Base(pathForBaseName)))
}
-// assetScriptCapableExts 为可执行脚本的资产扩展名,禁止浏览器内联渲染,必须强制以附件形式返回
-// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-mjf3-jwmf-r6wf
-var assetScriptCapableExts = map[string]bool{
- ".cjs": true, ".htm": true, ".html": true, ".js": true, ".mjs": true,
- ".shtml": true, ".svg": true, ".xhtml": true, ".xml": true,
-}
-
const htmlAssetIFrameCSP = "sandbox allow-scripts; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'"
-// isAssetInlineUnsafe 判断资产是否禁止浏览器内联渲染:
-// 可执行脚本的扩展名,或标准库无法识别 Content-Type 的扩展名(http.ServeFile 会内容嗅探,可能识别为 text/html 执行脚本)
+// isAssetInlineUnsafe 判断资产是否禁止浏览器内联渲染,采用媒体类型白名单策略:
+// 仅图片、音视频、PDF 和纯文本允许内联渲染;白名单之外的任何类型(包括所有 text/html、
+// text/xml 及 +xml 类型,以及无法识别 Content-Type 的扩展名)一律强制以附件形式下载。
+// 无法识别的类型可能被 http.ServeFile 内容嗅探识别为 text/html,因此同样视为不安全
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-7h8j-qw37-w46g
func isAssetInlineUnsafe(absPath string) bool {
ext := strings.ToLower(filepath.Ext(absPath))
- if assetScriptCapableExts[ext] {
+ mediaType := mime.TypeByExtension(ext)
+ if mediaType == "" {
return true
}
- return mime.TypeByExtension(ext) == ""
+ mediaType, _, _ = mime.ParseMediaType(mediaType)
+ switch {
+ case strings.HasPrefix(mediaType, "image/") && "image/svg+xml" != mediaType:
+ return false
+ case strings.HasPrefix(mediaType, "audio/"), strings.HasPrefix(mediaType, "video/"):
+ return false
+ case "application/pdf" == mediaType, "text/plain" == mediaType:
+ return false
+ }
+ return true
}
// secureAssetContentHeaders 统一为资产响应设置安全头:
diff --git a/kernel/server/serve_assets_test.go b/kernel/server/serve_assets_test.go
index abecf728e0..214148c0ec 100644
--- a/kernel/server/serve_assets_test.go
+++ b/kernel/server/serve_assets_test.go
@@ -153,11 +153,17 @@ func TestIsValidResolvedAssetPath(t *testing.T) {
func TestSecureAssetContentHeadersForcesAttachmentOnScriptCapableAssets(t *testing.T) {
// 可执行脚本的资产必须强制附件下载,禁止浏览器同源内联渲染
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-mjf3-jwmf-r6wf
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-7h8j-qw37-w46g
cases := map[string]string{
"test.html": "<script>fetch('/api/system/getConf')</script>",
"test.xhtml": "<script>fetch('/api/system/getConf')</script>",
"test.js": "fetch('/api/system/getConf')",
"test.svg": "<svg xmlns='http://www.w3.org/2000/svg'><script>fetch('/api/system/getConf')</script></svg>",
+ "test.xht": "<script>fetch('/api/system/getConf')</script>",
+ "test.ehtml": "<script>fetch('/api/system/getConf')</script>",
+ "test.xsl": "<script>fetch('/api/system/getConf')</script>",
+ "test.xbl": "<script>fetch('/api/system/getConf')</script>",
+ "test.rdf": "<script>fetch('/api/system/getConf')</script>",
}
for name, content := range cases {
recorder := httptest.NewRecorder()
@@ -177,6 +183,25 @@ func TestSecureAssetContentHeadersForcesAttachmentOnScriptCapableAssets(t *testi
}
}
+func TestSecureAssetContentHeadersForcesAttachmentOnNonAllowListedTypes(t *testing.T) {
+ // 白名单之外的类型即使 Content-Type 可识别也必须强制附件下载,未知类型默认禁止内联
+ cases := []string{"test.json", "test.css", "test.md", "test.zip", "test.wasm"}
+ for _, name := range cases {
+ recorder := httptest.NewRecorder()
+ context, _ := gin.CreateTestContext(recorder)
+ context.Request = httptest.NewRequest(http.MethodGet, "/assets/"+name, nil)
+ assetPath := filepath.Join(t.TempDir(), name)
+ if err := os.WriteFile(assetPath, []byte("test"), 0644); err != nil {
+ t.Fatalf("write test asset failed: %v", err)
+ }
+ secureAssetContentHeaders(context, assetPath, assetPath)
+ if !strings.HasPrefix(recorder.Header().Get("Content-Disposition"), "attachment") {
+ t.Fatalf("non allow-listed asset [%s] must be forced to download, got Content-Disposition %q",
+ name, recorder.Header().Get("Content-Disposition"))
+ }
+ }
+}
+
func TestSecureAssetContentHeadersSandboxesHTMLIFrameAssets(t *testing.T) {
assetPath := filepath.Join(t.TempDir(), "component.html")
if err := os.WriteFile(assetPath, []byte("<script>fetch('https://example.com')</script>"), 0644); err != nil {