{"record":{"id":"3d6483e9c460bd66","repo":"siyuan-note/siyuan","slug":"svg-root-element-is-incomplete","errorCode":null,"errorMessage":"svg root element is incomplete","messagePattern":"svg root element is incomplete","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":450,"sourceCode":"\t\t\t}\n\t\tcase xml.Comment:\n\t\t\tif skipDepth == 0 && rootSeen && !rootClosed {\n\t\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t\t}\n\t\t\t}\n\t\tcase xml.Directive:\n\t\t\tif !isBenignSVGDoctype(string(typed)) {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg directives are not allowed\")\n\t\t\t}\n\t\t\t// 良性 DOCTYPE 声明不写入输出，与 XML 声明（ProcInst）的处理方式一致，不影响浏览器渲染\n\t\tcase xml.ProcInst:\n\t\t\t// XML 声明和处理指令不影响 SVG 图像内容，输出时统一省略。\n\t\t}\n\t}\n\n\tif !rootSeen || !rootClosed || depth != 0 || skipDepth != 0 || len(elementStack) != 0 {\n\t\treturn \"\", fmt.Errorf(\"svg root element is incomplete\")\n\t}\n\tif err := encoder.Close(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t}\n\treturn buf.String(), nil\n}\n\nfunc preserveXMLName(name xml.Name) xml.Name {\n\tif name.Space != \"\" {\n\t\tname.Local = name.Space + \":\" + name.Local\n\t\tname.Space = \"\"\n\t}\n\treturn name\n}\n\n// isBenignSVGDoctype 判断 DOCTYPE 是否仅为 svg 根元素的无内部子集声明。\n// Go 的 xml.Directive 是 <! 与 > 之间的内容；内部子集 [...] 内可声明实体，存在 XXE 风险，一律拒绝。\nfunc isBenignSVGDoctype(d string) bool {","sourceCodeStart":432,"sourceCodeEnd":468,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L432-L468","documentation":"After token processing, SanitizeSVG verifies structural invariants: the root element must have been seen, closed, depth must be zero, and no elements may remain skipped or on the stack. If any invariant fails, the document is truncated or only partially an SVG, so sanitization fails with 'svg root element is incomplete'. This guards against serving half-parsed SVG to browsers.","triggerScenarios":"SanitizeSVG receives input where the root <svg> never closes (truncated file), never opens, closes early leaving elements unbalanced, or where skipped subtrees never terminate.","commonSituations":"Network-truncated SVG uploads, users saving only part of an SVG file, string operations that cut off the closing `</svg>`, SVG fragments pasted without the root element.","solutions":["Check that the input ends with a closing `</svg>` matching the opening tag","Repair truncation by re-uploading or re-exporting the complete SVG file","Ensure the payload is a single complete SVG document, not a fragment, before calling SanitizeSVG"],"exampleFix":"// before\n<svg xmlns=\"...\"><rect .../>\n// after\n<svg xmlns=\"...\"><rect .../></svg>","handlingStrategy":"validation","validationCode":"func looksCompleteSVG(data []byte) bool {\n    return bytes.HasPrefix(bytes.TrimSpace(data), []byte(\"<svg\")) &&\n        bytes.Contains(data, []byte(\"</svg>\"))\n}","typeGuard":null,"tryCatchPattern":"sanitized, err := util.SanitizeSVG(raw)\nif err != nil && strings.Contains(err.Error(), \"incomplete\") {\n    return errors.New(\"uploaded SVG is truncated; please re-upload\")\n}","preventionTips":["Verify upload completeness (size/etag) before processing","Ensure file transfer is atomic; avoid partial writes to asset storage","Reject SVG fragments lacking a closing root tag"],"tags":["xml","svg","sanitization","truncation"],"backgroundTag":"xml-parse-error","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}