siyuan-note/siyuan · warning

svg directives are not allowed

Error message

svg directives are not allowed

What it means

SanitizeSVG rejects any xml.Directive token that is not a benign DOCTYPE declaration. Directives (e.g. DOCTYPE with entity declarations or other DTD directives) can enable XXE/billion-laughs attacks and reference external resources in browsers, so the sanitizer refuses the whole document rather than silently stripping them.

Solutions

  1. Remove the DOCTYPE/directive lines from the SVG source entirely
  2. Re-export the SVG without DTD/entity declarations
  3. Replace custom entity references with literal characters or numeric character references

Example fix

// before
<!DOCTYPE svg PUBLIC "..." [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<svg>...</svg>
// after
<svg xmlns="http://www.w3.org/2000/svg">...</svg>
Defensive patterns

Strategy: validation

Validate before calling

if bytes.Contains(data, []byte("<!DOCTYPE")) || bytes.Contains(data, []byte("<!ENTITY")) {
    return errors.New("SVG must not contain DOCTYPE or entity declarations")
}

Try / catch

sanitized, err := util.SanitizeSVG(raw)
if err != nil && strings.Contains(err.Error(), "directives are not allowed") {
    raw = stripDoctype(raw) // remove DOCTYPE line, inline any needed entities
    sanitized, err = util.SanitizeSVG(raw)
}

Prevention

When it happens

Trigger: Calling SanitizeSVG on an SVG that starts with a DOCTYPE containing entity definitions, internal subsets, or any non-DOCTYPE directive like `<?xml-stylesheet?>` content emitted as a Directive.

Common situations: SVG exported from tools that include DTDs (older Adobe Illustrator exports), SVG templates using custom entities, clipped SVGs from websites that embed `<!DOCTYPE svg [ ... ]>`.

Related errors


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

Appendix: source

Thrown at kernel/util/misc.go:441

				continue
			}
			if (!rootSeen || rootClosed) && strings.TrimSpace(string(typed)) != "" {
				return "", fmt.Errorf("svg contains text outside the root element")
			}
			if rootSeen && !rootClosed {
				if err = encoder.EncodeToken(typed); err != nil {
					return "", fmt.Errorf("render svg failed: %w", err)
				}
			}
		case xml.Comment:
			if skipDepth == 0 && rootSeen && !rootClosed {
				if err = encoder.EncodeToken(typed); err != nil {
					return "", fmt.Errorf("render svg failed: %w", err)
				}
			}
		case xml.Directive:
			if !isBenignSVGDoctype(string(typed)) {
				return "", fmt.Errorf("svg directives are not allowed")
			}
			// 良性 DOCTYPE 声明不写入输出,与 XML 声明(ProcInst)的处理方式一致,不影响浏览器渲染
		case xml.ProcInst:
			// XML 声明和处理指令不影响 SVG 图像内容,输出时统一省略。
		}
	}

	if !rootSeen || !rootClosed || depth != 0 || skipDepth != 0 || len(elementStack) != 0 {
		return "", fmt.Errorf("svg root element is incomplete")
	}
	if err := encoder.Close(); err != nil {
		return "", fmt.Errorf("render svg failed: %w", err)
	}
	return buf.String(), nil
}

func preserveXMLName(name xml.Name) xml.Name {
	if name.Space != "" {

View on GitHub (pinned to 9f775e8a12)