Summary
SVGO, a popular Node.js SVG optimization library and CLI, processes XML input without restricting custom entity expansion or recursion.
Crafted SVG files using malicious XML entities can trigger excessive memory use, stalling the optimization process and potentially crashing the Node.js runtime with an out-of-memory error.
Affected context
This vulnerability affects applications and build pipelines that use SVGO versions:
- 2.1.0 to < 2.8.1
- 3.0.0 to < 3.3.3
- < 4.0.1
The issue is most relevant where:
- SVG or XML content is accepted from external or semi-trusted sources
- SVGO runs automatically (e.g., during uploads, build steps, or image processing)
- There are no upstream checks to reject or sanitize SVGs with custom XML entities
In these contexts, a small but malicious SVG can tie up CPU and memory, degrading service availability.
Exploit preconditions
- Attack surface: Any feature or pipeline that passes SVG/XML input into SVGO, including:
- Web upload endpoints that optimize images
- CI/CD or asset build pipelines processing third-party SVGs
- Developer tools that auto-run SVGO on opened or imported files
- Required access: Ability to supply or influence an SVG file that will be processed by SVGO (remote or local, depending on the application).
- Trigger point: The moment SVGO parses the XML and resolves custom entities during optimization.
- Why exploitation is possible: The XML parser used by SVGO allows unrestricted entity expansion and recursion. A small, specially crafted SVG can expand into a very large in-memory structure, consuming excessive heap and causing process stalls or crashes.
Why this happens
The root cause aligns with CWE-776: Improper Restriction of Recursive Entity References in DTDs (XML Entity Expansion).
SVGO accepts XML input that includes custom entities defined via a document type definition (DTD), but it does not enforce limits on:
- The depth of recursive entity references
- The number of expansions
- The overall size of the expanded content in memory
When a malicious SVG defines entities that reference each other repeatedly or expand exponentially, the XML parser tries to resolve them fully. This unbounded expansion leads to disproportionate memory and CPU consumption relative to the tiny input size. Abstractly, the insecure pattern is:
- Trusting XML entities without limits
- Allowing unbounded recursion or expansion during parsing in a performance-critical path
Potential impact
- Node.js process may crash due to "JavaScript heap out of memory" errors.
- Affected services can become unavailable until processes are restarted.
- Resource exhaustion could cascade, causing slowdowns or failures in other components sharing the same host.
- Automated build or deployment pipelines may fail, delaying releases or causing timeouts.
- In multi-tenant environments, heavy resource use may impact neighboring workloads, depending on isolation controls.
General mitigation guidance
Effective mitigation focuses on limiting XML entity features and controlling resource use when handling SVG input:
- Prefer XML parsers or configurations that:
- Disable DTDs and external/custom entity expansion where not strictly needed.
- Enforce limits on recursion depth, entity count, and total expansion size.
- Treat SVG input as untrusted by default:
- Introduce pre-filters that reject files containing DTD declarations or suspicious entity constructs.
- Use allowlists for expected SVG structures and attributes instead of parsing arbitrary XML.
- Isolate and constrain the SVGO execution environment:
- Run SVG optimization in a separate, low-privilege process or container with strict memory and CPU limits.
- Apply timeouts to optimization tasks to prevent long-running hangs.
- Incorporate defense in depth:
- Combine parser hardening, input validation, and runtime resource limits to reduce the impact of malformed or malicious SVGs.
How teams detect issues like this
Teams can detect and manage this class of vulnerability using a combination of code-focused and operational strategies:
- Static analysis / configuration review:
- Identify XML parsing libraries and check whether DTD and entity features are disabled or constrained.
- Review code paths that pass untrusted SVG/XML data into SVGO without prior validation.
- Code review signals:
- Look for acceptance of arbitrary user-provided SVGs with automatic optimization.
- Flag use of XML parsing configurations that allow external entities or unconstrained DTD processing.
- Runtime and operational monitoring:
- Monitor Node.js processes for spikes in memory and CPU usage during SVG processing.
- Track repeated process crashes or "heap out of memory" errors related to asset optimization.
- Automated tooling:
- Use software composition analysis (SCA) tools to surface known XML entity expansion and DoS issues in third-party dependencies.
- Enforce CI policies that fail builds when vulnerable versions of SVGO or similar libraries are present.

