{"record":{"id":"47d68637cd28aef2","repo":"siyuan-note/siyuan","slug":"svg-contains-too-many-tokens","errorCode":null,"errorMessage":"svg contains too many tokens","messagePattern":"svg contains too many tokens","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":362,"sourceCode":"\tencoder := xml.NewEncoder(&buf)\n\trootSeen := false\n\trootClosed := false\n\tdepth := 0\n\tskipDepth := 0\n\ttokenCount := 0\n\tvar elementStack []xml.Name\n\n\tfor {\n\t\ttoken, err := decoder.RawToken()\n\t\tif err == io.EOF {\n\t\t\tbreak\n\t\t}\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"parse svg failed: %w\", err)\n\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}","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L344-L380","documentation":"SanitizeSVG caps total XML tokens at maxSVGTokens (1,000,000). When input exceeds this limit the sanitizer stops and returns this error instead of consuming unbounded CPU/memory. It is a resource-exhaustion guard for hostile or pathological SVG documents, not a correctness error.","triggerScenarios":"Passing an enormous or deliberately crafted SVG (more than 1,000,000 XML tokens) to SanitizeSVG — e.g. huge embedded custom emojis, generated SVG with massive path data split across many elements, or a zip-bomb-like nested expansion attempt.","commonSituations":"Importing a legitimately gigantic generated SVG (e.g. data-viz with millions of elements), malicious uploads intended to stall the kernel, or clipper captures of enormous pages.","solutions":["Reduce the SVG's complexity: simplify paths, merge elements, or export at lower detail","Split the content into multiple smaller SVGs","Pre-optimize the SVG with a minifier (e.g. svgo) before ingestion","If the SVG is genuinely needed at that scale, process it outside SanitizeSVG with a trusted source and serve it without sanitization only if you control the content"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const maxSVGTokens = 1_000_000\nif strings.Count(svg, \"<\")+strings.Count(svg, \">\") > 2*maxSVGTokens/10 {\n    return errors.New(\"svg likely too large for sanitization; simplify it first\")\n}","typeGuard":null,"tryCatchPattern":"clean, err := util.SanitizeSVG(input)\nif err != nil && err.Error() == \"svg contains too many tokens\" {\n    return fmt.Errorf(\"svg exceeds complexity limit (1M tokens), simplify before upload: %w\", err)\n}","preventionTips":["Run svgo or similar to simplify huge generated SVGs","Reject overly complex SVGs at upload time with a friendly message","Keep data-viz exports to a reasonable element count","Treat token-limit failures from unknown sources as potential DoS attempts"],"tags":["svg","sanitization","resource-limit","security"],"backgroundTag":"file-size-limit-exceeded","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"}