Secure Backend SVG Processing: XML Settings to Disable

Scalable Vector Graphics (SVG) are XML-based image files, which exposes backend processing pipelines to severe security risks like XML External Entity (XXE) injection, Server-Side Request Forgery (SSRF), and XML entity expansion denial-of-service attacks (Billion Laughs). Securing backend SVG handling requires hardening the underlying XML parser. This guide details the exact XML parser configurations, features, and flags you must disable to ensure safe SVG processing.

1. Completely Disallow DOCTYPE Declarations

The most effective way to secure an XML parser processing SVGs is to disable Document Type Declarations (<!DOCTYPE>) entirely. Standard SVG files do not require custom DTDs to render or convert correctly.

2. Disable External Entity Resolution

If your application cannot completely disallow DOCTYPEs, you must disable the loading and resolution of external entities. This prevents attackers from exfiltrating local server files or forcing the server to make unauthorized network requests (SSRF).

3. Disable XInclude Processing

XInclude allows XML documents to reference and merge external files via tags like <xi:include>. Attackers can use this to bypass entity restrictions and include arbitrary backend files.

4. Restrict or Disable Entity Expansion (XML Bombs)

To mitigate Billion Laughs attacks—where nested entities consume all memory and CPU—parsers must not recursively expand custom internal entities.

Summary Checklist for Backend SVG Parsing

To securely accept and process SVG uploads: 1. Enable FEATURE_SECURE_PROCESSING. 2. Disable disallow-doctype-decl. 3. Disable external-general-entities and external-parameter-entities. 4. Disable load-external-dtd. 5. Disable XInclude capabilities. 6. Block outbound network access from the parser using flags like XML_PARSE_NONET.