Skip to content

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

:bug: Accept UTF-8 BOM in SVG assets https://github.com/siyuan-note/siyuan/issues/19762

Danieldc2c2dbac26b
diff --git a/kernel/util/misc.go b/kernel/util/misc.go
index 147e048889..55493de6de 100644
--- a/kernel/util/misc.go
+++ b/kernel/util/misc.go
@@ -337,6 +337,7 @@ var unsafeSVGElements = map[string]struct{}{
// SanitizeSVG 使用 XML 语义过滤 SVG,避免 HTML 与 XML 解析规则差异导致活动内容绕过过滤。
func SanitizeSVG(svgInput string) (string, error) {
+ svgInput = strings.TrimPrefix(svgInput, "\ufeff")
decoder := xml.NewDecoder(strings.NewReader(svgInput))
decoder.Strict = true
diff --git a/kernel/util/sanitize_svg_test.go b/kernel/util/sanitize_svg_test.go
index d1d42f0452..d6796f7cbf 100644
--- a/kernel/util/sanitize_svg_test.go
+++ b/kernel/util/sanitize_svg_test.go
@@ -78,6 +78,24 @@ func TestSanitizeSVGPreservesStaticSVG(t *testing.T) {
}
}
+func TestSanitizeSVGAllowsUTF8BOM(t *testing.T) {
+ input := "\ufeff" + `<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg"><rect width="10" height="10"/></svg>`
+ output, err := SanitizeSVG(input)
+ if err != nil {
+ t.Fatalf("sanitize failed: %v", err)
+ }
+ if strings.Contains(output, "\ufeff") || !strings.Contains(output, "<rect") {
+ t.Fatalf("static SVG was not preserved: %q", output)
+ }
+ output, err = SanitizeSVG("\ufeff" + `<svg><script>alert(1)</script></svg>`)
+ if err != nil {
+ t.Fatalf("sanitize failed: %v", err)
+ }
+ if strings.Contains(output, "script") {
+ t.Fatalf("active content remains in %q", output)
+ }
+}
+
func TestSanitizeSVGAllowsBenignDoctype(t *testing.T) {
tests := []struct {
name string