Billion Laughs Attack via SVG File Upload
Scalable Vector Graphics (SVG) files are widely used for vector-based imagery on the web, but because they are built using XML, they inherit fundamental XML vulnerabilities. A classic Denial of Service (DoS) vulnerability, known as the XML Entity Expansion or “Billion Laughs” attack, occurs when a server-side parser processes an SVG containing nested, recursively defined XML entities. This article explains the technical mechanics of executing a Billion Laughs attack using SVG files, how backend image processing pipelines are affected, and the standard methods for mitigating this threat.
The Underlying Mechanism: SVG as XML
Unlike binary image formats such as PNG or JPEG, SVG is a text-based format governed by XML specifications. Because SVGs are standard XML documents, standard XML parsers often handle them during server-side operations like image processing, sanitization, thumbnail generation, or format conversion (e.g., converting SVG to PNG).
By default, many XML parsers support Document Type Definitions (DTDs), which permit the definition of custom entities. When a parser encounters an entity reference, it replaces it with the corresponding literal string defined in the DTD.
How the Attack is Structured in an SVG
The Billion Laughs attack leverages exponential entity expansion. An
attacker constructs a custom <!DOCTYPE> definition
within the SVG file, defining a base entity and several subsequent
layers of entities where each layer references the previous one multiple
times.
Below is an example of an SVG carrying a recursive entity payload:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg [
<!ELEMENT svg ANY >
<!ENTITY lol "lol">
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<text x="10" y="20">&lol9;</text>
</svg>When this file is parsed, lol9 expands into ten
instances of lol8, which each expand into ten instances of
lol7, and so on. A file containing less than a single
kilobyte of data suddenly expands into 109 (one billion)
copies of the string "lol" in memory, requiring roughly 3
gigabytes of RAM to parse. Adding just a couple more entity layers
causes the memory requirement to scale into terabytes.
Server-Side Execution Vectors
The attack executes when an application accepts untrusted SVG uploads and parses them using an insecurely configured XML engine. Common vulnerable integration points include:
- Avatar and Document Uploads: Users upload an SVG avatar, resume, or asset, which the server parses to inspect dimensions, metadata, or file validity.
- Server-Side Rasterization: Libraries such as
ImageMagick, Apache Batik, Sharp (via
librsvg), or headless browsers convert incoming SVGs into PNG or JPEG formats for storage or display. - PDF Generation Services: Services that render user-supplied vector graphics into PDF reports or invoices using vulnerable backend XML interpreters.
Server-Side Impact
When the server-side XML engine resolves the recursive entities, the following chain of failures typically occurs:
- Memory Exhaustion: Rapid allocation of memory causes Out-Of-Memory (OOM) exceptions, crashing the parsing process or the host server.
- CPU Starvation: The CPU becomes fully saturated attempting to process billions of string allocations, freezing the execution thread.
- Service Outage: If worker processes or web servers crash repeatedly, the entire application becomes unresponsive to legitimate users, resulting in a complete Denial of Service.
Mitigation Strategies
Defending against entity expansion via SVG requires disabling dangerous XML parsing features at the parser configuration level:
- Disable Inline DTDs: Configure the XML parser to
disallow
<!DOCTYPE>declarations entirely. If DTD processing is turned off, the parser will reject or ignore custom entity definitions. - Disable External and General Entity Resolution: If DTDs cannot be fully disabled, explicitly configure the parser to ignore general entity expansion and external entity loading.
- Enforce Entity Expansion Limits: For environments
where custom entities are required, set strict expansion thresholds
(e.g., using
XMLConstants.FEATURE_SECURE_PROCESSINGin Java or libxml limits in other languages) to abort parsing if expansion depth or count exceeds a safe threshold. - Sanitize SVGs Pre-Processing: Use dedicated,
security-hardened SVG sanitizers that strip
<script>,<!DOCTYPE>, and non-essential XML tags before passing files to backend image processors.