{"record":{"id":"e8a954569abcd8f0","repo":"siyuan-note/siyuan","slug":"svg-nesting-depth-exceeds-d","errorCode":null,"errorMessage":"svg nesting depth exceeds %d","messagePattern":"svg nesting depth exceeds (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/misc.go","lineNumber":370,"sourceCode":"\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}\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","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/misc.go#L352-L388","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Flatten the SVG structure — merge nested <g> groups or use a tool like svgo to collapse redundant nesting","Reduce nesting in the generator that produced the SVG (e.g. avoid per-point nested groups)","If the input may be adversarial, treat it as an attack and reject the upload"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func svgDepthOK(svg string, max int) bool {\n    depth := 0\n    d := xml.NewDecoder(strings.NewReader(svg))\n    for {\n        t, err := d.RawToken()\n        if err == io.EOF { return true }\n        if err != nil { return true }\n        switch t.(type) {\n        case xml.StartElement:\n            depth++\n            if depth > max { return false }\n        case xml.EndElement:\n            depth--\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"clean, err := util.SanitizeSVG(input)\nif err != nil && strings.HasPrefix(err.Error(), \"svg nesting depth exceeds\") {\n    return fmt.Errorf(\"svg nesting too deep (>256); flatten groups before upload: %w\", err)\n}","preventionTips":["Collapse nested <g> groups with svgo before ingestion","Avoid generators that create one element per data point recursively","Reject deeply nested uploads from untrusted sources","Check design-tool export settings for group flattening options"],"tags":["svg","sanitization","resource-limit","security"],"backgroundTag":"value-out-of-range","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"}