{"record":{"id":"ed43442b88f6c873","repo":"siyuan-note/siyuan","slug":"svg-closing-element-q-does-not-match-q","errorCode":null,"errorMessage":"svg closing element %q does not match %q","messagePattern":"svg closing element %q does not match %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":402,"sourceCode":"\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}\n\t\tcase xml.EndElement:\n\t\t\tif depth <= 0 || len(elementStack) == 0 {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg contains an unexpected closing element\")\n\t\t\t}\n\t\t\tstartName := elementStack[len(elementStack)-1]\n\t\t\tif startName != typed.Name {\n\t\t\t\treturn \"\", fmt.Errorf(\"svg closing element %q does not match %q\", typed.Name.Local, startName.Local)\n\t\t\t}\n\t\t\telementStack = elementStack[:len(elementStack)-1]\n\t\t\tif skipDepth > 0 {\n\t\t\t\tskipDepth--\n\t\t\t\tdepth--\n\t\t\t\tif depth == 0 {\n\t\t\t\t\trootClosed = true\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\ttyped.Name = preserveXMLName(typed.Name)\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}\n\t\t\tdepth--\n\t\t\tif depth == 0 {\n\t\t\t\trootClosed = true\n\t\t\t}","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L384-L420","documentation":"SanitizeSVG parses an SVG document token-by-token with encoding/xml and re-encodes only whitelisted content. When an end element arrives whose local name differs from the innermost start element on the stack, the document is malformed (mismatched nesting), so sanitization is aborted and this error is returned. The library refuses to guess at a repair because output SVG is served back to browsers.","triggerScenarios":"Calling SanitizeSVG (directly, via serveSVG, or via normalizeCustomEmojiData) with SVG whose tags are unbalanced, e.g. `<svg><path></circle></svg>` or a self-closing tag written as `<g></g></svg>`.","commonSituations":"Hand-edited SVG assets, SVG pasted from editors that emit non-well-formed markup, truncated uploads, or custom emoji data produced by string concatenation instead of an XML writer.","solutions":["Validate the SVG with a strict XML parser (encoding/xml Decoder with input processed to EOF) before calling SanitizeSVG","Locate the mismatched tag: compare each closing tag against the open-element stack; fix or regenerate the SVG source","If the SVG comes from a third party, re-export it from a vector editor (Inkscape/Illustrator) which writes well-formed XML"],"exampleFix":"// before\ndecoder.Token() sees </circle> while stack top is <path> -> error\n// after\n<svg xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0\"/></svg>","handlingStrategy":"validation","validationCode":"func isWellFormedXML(data []byte) error {\n    dec := xml.NewDecoder(bytes.NewReader(data))\n    for {\n        _, err := dec.Token()\n        if err == io.EOF {\n            return nil\n        }\n        if err != nil {\n            return err\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"sanitized, err := util.SanitizeSVG(raw)\nif err != nil {\n    log.Warnf(\"SVG rejected: %v\", err)\n    return http.StatusBadRequest\n}","preventionTips":["Generate SVG with an XML writer, never by string concatenation","Run a strict XML parse on all third-party SVGs before sanitizing","Re-export SVGs from vector editors rather than hand-editing tags"],"tags":["xml","svg","sanitization","security"],"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"}