{"record":{"id":"a2abd04c6598a210","repo":"siyuan-note/siyuan","slug":"svg-directives-are-not-allowed","errorCode":null,"errorMessage":"svg directives are not allowed","messagePattern":"svg directives are not allowed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kernel/util/misc.go","lineNumber":441,"sourceCode":"\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (!rootSeen || rootClosed) && strings.TrimSpace(string(typed)) != \"\" {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains text outside the root element\")\n\t\t\t}\n\t\t\tif 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.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 != \"\" {","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L423-L459","documentation":"SanitizeSVG rejects any xml.Directive token that is not a benign DOCTYPE declaration. Directives (e.g. DOCTYPE with entity declarations or other DTD directives) can enable XXE/billion-laughs attacks and reference external resources in browsers, so the sanitizer refuses the whole document rather than silently stripping them.","triggerScenarios":"Calling SanitizeSVG on an SVG that starts with a DOCTYPE containing entity definitions, internal subsets, or any non-DOCTYPE directive like `<?xml-stylesheet?>` content emitted as a Directive.","commonSituations":"SVG exported from tools that include DTDs (older Adobe Illustrator exports), SVG templates using custom entities, clipped SVGs from websites that embed `<!DOCTYPE svg [ ... ]>`.","solutions":["Remove the DOCTYPE/directive lines from the SVG source entirely","Re-export the SVG without DTD/entity declarations","Replace custom entity references with literal characters or numeric character references"],"exampleFix":"// before\n<!DOCTYPE svg PUBLIC \"...\" [ <!ENTITY xxe SYSTEM \"file:///etc/passwd\"> ]>\n<svg>...</svg>\n// after\n<svg xmlns=\"http://www.w3.org/2000/svg\">...</svg>","handlingStrategy":"validation","validationCode":"if bytes.Contains(data, []byte(\"<!DOCTYPE\")) || bytes.Contains(data, []byte(\"<!ENTITY\")) {\n    return errors.New(\"SVG must not contain DOCTYPE or entity declarations\")\n}","typeGuard":null,"tryCatchPattern":"sanitized, err := util.SanitizeSVG(raw)\nif err != nil && strings.Contains(err.Error(), \"directives are not allowed\") {\n    raw = stripDoctype(raw) // remove DOCTYPE line, inline any needed entities\n    sanitized, err = util.SanitizeSVG(raw)\n}","preventionTips":["Reject or strip DOCTYPE/ENTITY declarations at ingest","Replace custom entities with literal characters or numeric references","Configure exporters (Illustrator etc.) to omit DTD output"],"tags":["svg","security","xxe","sanitization"],"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"}