siyuan-note/siyuan · error

svg nesting depth exceeds

Error message

svg nesting depth exceeds %d

What it means

SanitizeSVG enforces a maximum element nesting depth of maxSVGDepth (256). When a start element pushes depth beyond that, sanitization fails with this error. Deep nesting is a classic XML-parser stack-exhaustion / denial-of-service vector, so the sanitizer hard-fails rather than recursing further.

Solutions

  1. Flatten the SVG structure — merge nested <g> groups or use a tool like svgo to collapse redundant nesting
  2. Reduce nesting in the generator that produced the SVG (e.g. avoid per-point nested groups)
  3. If the input may be adversarial, treat it as an attack and reject the upload
Defensive patterns

Strategy: validation

Validate before calling

func svgDepthOK(svg string, max int) bool {
    depth := 0
    d := xml.NewDecoder(strings.NewReader(svg))
    for {
        t, err := d.RawToken()
        if err == io.EOF { return true }
        if err != nil { return true }
        switch t.(type) {
        case xml.StartElement:
            depth++
            if depth > max { return false }
        case xml.EndElement:
            depth--
        }
    }
}

Try / catch

clean, err := util.SanitizeSVG(input)
if err != nil && strings.HasPrefix(err.Error(), "svg nesting depth exceeds") {
    return fmt.Errorf("svg nesting too deep (>256); flatten groups before upload: %w", err)
}

Prevention

When it happens

Trigger: SanitizeSVG receives SVG whose element hierarchy is deeper than 256 levels — hand-crafted adversarial input with thousands of nested <g> elements, or tool-generated SVG that nests groups programmatically (e.g. deep transformations or exploded design-tool exports).

Common situations: Malicious uploads crafted to crash parsers, and occasionally legitimately generated SVG from recursive algorithms or heavily nested design tools (Figma/Sketch exports with deep group hierarchies).

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/e8a954569abcd8f0. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/misc.go:370

	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
			}

			if skipDepth > 0 {
				skipDepth++
				continue
			}
			if _, unsafe := unsafeSVGElements[strings.ToLower(typed.Name.Local)]; unsafe {
				skipDepth = 1
				continue

View on GitHub (pinned to 9f775e8a12)