{"record":{"id":"a8304a6159b8b69f","repo":"siyuan-note/siyuan","slug":"root-element-is-not-svg","errorCode":null,"errorMessage":"root element is not svg","messagePattern":"root element is not svg","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":377,"sourceCode":"\t\t}\n\t\ttokenCount++\n\t\tif tokenCount > maxSVGTokens {\n\t\t\treturn \"\", fmt.Errorf(\"svg contains too many tokens\")\n\t\t}\n\n\t\tswitch typed := token.(type) {\n\t\tcase xml.StartElement:\n\t\t\telementStack = append(elementStack, typed.Name)\n\t\t\tdepth++\n\t\t\tif depth > maxSVGDepth {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg nesting depth exceeds %d\", maxSVGDepth)\n\t\t\t}\n\t\t\tif rootClosed {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains multiple root elements\")\n\t\t\t}\n\t\t\tif !rootSeen {\n\t\t\t\tif !strings.EqualFold(typed.Name.Local, \"svg\") {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"root element is not svg\")\n\t\t\t\t}\n\t\t\t\trootSeen = true\n\t\t\t}\n\n\t\t\tif skipDepth > 0 {\n\t\t\t\tskipDepth++\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif _, unsafe := unsafeSVGElements[strings.ToLower(typed.Name.Local)]; unsafe {\n\t\t\t\tskipDepth = 1\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\ttyped.Name = preserveXMLName(typed.Name)\n\t\t\ttyped.Attr = sanitizeSVGAttributes(typed.Attr)\n\t\t\tif err = encoder.EncodeToken(typed); err != nil {\n\t\t\t\treturn \"\", fmt.Errorf(\"render svg failed: %w\", err)\n\t\t\t}","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L359-L395","documentation":"SanitizeSVG checks that the first (root) start element is <svg> (case-insensitive). If the document's root is any other element, it fails with this error. The sanitizer is dedicated to SVG and will not pass through other XML/HTML documents, since sanitization rules only apply to SVG content.","triggerScenarios":"Passing non-SVG content to SanitizeSVG — e.g. an HTML fragment rooted at <div>, an XML document rooted at <?xml?><g>, a renamed .svg file that actually holds PNG/HTML data, or content whose XML declaration/prolog is followed by a non-svg element.","commonSituations":"Users upload an HTML page or plain XML renamed to .svg, clipper saves an HTML snippet as .svg, or tooling misroutes file types so a non-SVG document reaches the emoji/asset sanitizer.","solutions":["Verify the file actually is an SVG: the first element must be <svg ...>","Re-export or re-save the content as a genuine SVG from the source tool","If serving images generically, route non-SVG files to a different handler instead of SanitizeSVG"],"exampleFix":"// before (root is not svg)\n<div><svg viewBox=\"0 0 10 10\"></svg></div>\n// after\n<svg viewBox=\"0 0 10 10\"></svg>","handlingStrategy":"validation","validationCode":"func hasSVGRoot(svg string) bool {\n    d := xml.NewDecoder(strings.NewReader(svg))\n    for {\n        t, err := d.RawToken()\n        if err == io.EOF || err != nil { return false }\n        if se, ok := t.(xml.StartElement); ok {\n            return strings.EqualFold(se.Name.Local, \"svg\")\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"if !hasSVGRoot(input) {\n    return errors.New(\"file is not an SVG document\")\n}\nclean, err := util.SanitizeSVG(input)","preventionTips":["Sniff content before routing: only send <svg>-rooted XML to the sanitizer","Verify file extensions match actual content (don't trust .svg alone)","Reject HTML/XML documents masquerading as SVG at upload time","Educate users that pasted fragments must be complete SVG documents"],"tags":["svg","sanitization","validation","file-type"],"backgroundTag":"unsupported-operation","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"}