siyuan-note/siyuan · error
svg contains too many tokens
Error message
svg contains too many tokens
What it means
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.
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
Defensive patterns
Strategy: validation
Validate before calling
const maxSVGTokens = 1_000_000
if strings.Count(svg, "<")+strings.Count(svg, ">") > 2*maxSVGTokens/10 {
return errors.New("svg likely too large for sanitization; simplify it first")
} Try / catch
clean, err := util.SanitizeSVG(input)
if err != nil && err.Error() == "svg contains too many tokens" {
return fmt.Errorf("svg exceeds complexity limit (1M tokens), simplify before upload: %w", err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "File too large" / "file size exceeds limit" errors: why libraries cap file sizes and how to fix them — this error's family across 46 libraries.
Related errors
- svg nesting depth exceeds
- svg closing element does not match
- svg directives are not allowed
- parse svg failed
- render svg failed
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/47d68637cd28aef2.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/misc.go:362
encoder := xml.NewEncoder(&buf)
rootSeen := false
rootClosed := false
depth := 0
skipDepth := 0
tokenCount := 0
var elementStack []xml.Name
for {
token, err := decoder.RawToken()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("parse svg failed: %w", err)
}
tokenCount++
if tokenCount > maxSVGTokens {
return "", fmt.Errorf("svg contains too many tokens")
}
switch typed := token.(type) {
case xml.StartElement:
elementStack = append(elementStack, typed.Name)
depth++
if depth > maxSVGDepth {
return "", fmt.Errorf("svg nesting depth exceeds %d", maxSVGDepth)
}
if rootClosed {
return "", fmt.Errorf("svg contains multiple root elements")
}
if !rootSeen {
if !strings.EqualFold(typed.Name.Local, "svg") {
return "", fmt.Errorf("root element is not svg")
}
rootSeen = true
}View on GitHub (pinned to 9f775e8a12)