XML bomb: a small document that a parser expands into a huge one
An XML bomb is an XML document built so that the parser, while resolving its entity definitions, produces far more data than the document itself holds. The input is small, the memory it demands is not, and the service that parsed it slows down or stops. The best-known form is called billion laughs. It is a denial-of-service attack through the same DTD feature that makes XXE possible.
Quick facts
- Also called
- Billion laughs attack, XML entity expansion
- Weakness
- CWE-776, improper restriction of recursive entity references in DTDs
- What it exhausts
- Parser memory and processor time
- What it needs
- A parser that processes a DTD and an endpoint that accepts XML
- Related
- XXE uses external entities to read data; the bomb uses internal ones to exhaust resources
- Main fix
- Disable DTD processing; where it is needed, limit entity expansion and the size of the input
Definitions follow CWE-776 and the entity rules in W3C XML 1.0, section 4. Prevention follows the OWASP XML External Entity Prevention Cheat Sheet.
How XML entities work
An entity in XML is a named piece of replacement text. A document type definition, the DTD, can declare an entity once, and the document then refers to it by name wherever the text should appear. The parser swaps each reference for the declared text. XML 1.0 describes this in section 4: the declaration form in 4.2, internal entities, whose text sits in the declaration itself, in 4.2.1.
The feature exists to save repetition. A company name or a legal notice is written once and referenced many times. Nothing about it is malicious, and a parser that supports DTDs is doing what the specification asks.
What the specification forbids, and what it allows
XML 1.0 contains a well-formedness constraint called No Recursion: a parsed entity must not contain a recursive reference to itself, directly or indirectly. A parser rejects documents that break it.
The constraint is about loops. It says nothing about how many entities may refer to one another in a chain, or how large the final text may become. A chain of entities, each defined from the one before, is well-formed. This gap is what the attack uses.
Why expansion becomes a denial of service
When an entity is defined as several references to another entity, resolving it multiplies the work. Stack a few levels and the growth is exponential in the depth, while the document grows only by a line per level. A short file can describe text many orders of magnitude larger than itself once expanded.
The cost falls on the receiver. Most parsers build the expanded text in memory, and a DOM parser holds it as a tree on top of that. The process slows, starts swapping or is stopped by the operating system, and every other request on the same worker waits. No vulnerability in the application code is needed: the parser is working as configured.
The attack is cheap for the sender, and a single request can be enough. That separates it from the floods described under application layer attacks, where many requests add up. A request-rate limit, covered under rate limiting, counts requests. It does not bound how much work one request causes.
Where it shows up
Any component that parses XML from an untrusted source can be reached. The usual places are SOAP and other XML APIs, SVG and office documents handled by an upload feature, RSS and sitemap importers, and configuration or data files that users can supply. Anything that accepts a document and reads it on the server deserves the same question: does this parser process a DTD?
The file upload case is easy to miss. The file is accepted as an image or a spreadsheet, and the parsing happens later in a library nobody thought of as an XML parser. Query-language injection into XML data is a separate problem, treated under XPath injection.
The weakness is listed by MITRE as CWE-776, and it sits within the OWASP category security misconfiguration, because the fix is mostly a parser setting rather than code. CWE-776 gives a real-world example: CVE-2011-1755, a billion laughs flaw in an XMPP server daemon.
Where a WAF helps and where it does not
A WAF reads the request before it reaches the origin. That is useful here in one narrow way. If an endpoint is supposed to receive XML without a DTD, a body that contains an entity declaration is out of the ordinary, and a rule can match it. Because that same pattern is normal for a few legitimate integrations, the match should not be trusted until it has been watched.
That is what monitor mode is for. Start there and read the activity log: it shows which requests would have been refused and which endpoints they were aimed at, while every request is still served. If the matches turn out to be real integrations, handle them as false positives. When the log holds only unwanted traffic, switch the domain to block. The three modes are off, monitor and block, as described under WAF monitor mode.
The limits are plain. A filter sees the request body as it arrives. It does not see what a parser will build from it, and it does not know which parser your application uses or how that parser is configured. Bodies can arrive in encodings and wrappers the filter does not unpack, and an XML payload inside an uploaded archive is out of its sight. A matching rule lowers the risk; it does not close the hole. The parser configuration does that.
How to prevent an XML bomb
- Disable DTDs. The OWASP cheat sheet says to disable document type definitions whenever possible. Most applications never need them, and doing so also closes XXE. CWE-776 gives the same advice: prohibit DTDs, or use a parser that limits the expansion of recursive entities.
- If DTDs are required, limit expansion. OWASP says to limit entity expansion in that case. For Java it points to enabling the secure processing feature where the parser supports it and setting the provider's own processing limits. The names of those limits differ by library, so read the documentation of the parser you actually use.
- Cap the size of the request body. Set the maximum in the web server or framework, so an oversized document is refused before it is parsed. A parsed document can still be larger than the body, so this limits the input rather than the expansion.
- Bound time and memory per parse. Run parsing with a timeout and a memory ceiling, or in a worker that can be restarted without taking the service with it.
- Prefer a format without entities. Where the client can send JSON instead of XML, the whole class of attack goes away for that endpoint.
- Test the configuration, not the claim. OWASP warns that preventing XXE does not prevent every XML denial-of-service attack. Check expansion limits separately, in a test environment with documents that your own team has built.
Questions
What is an XML bomb?
An XML bomb is an XML document whose entity definitions expand, when the parser resolves them, into far more data than the document itself contains. The parser runs out of memory or processor time, and the service stops answering. It is a denial-of-service attack, not a data leak.
What is the billion laughs attack?
Billion laughs is the best-known XML bomb. Each entity is defined in terms of the previous one, so every level of nesting multiplies the expanded size. CWE-776 lists it as an alternate name for improper restriction of recursive entity references in DTDs.
How do you prevent an XML bomb?
Disable DTD processing in the XML parser wherever the application does not need it. If DTDs are required, enable the parser's entity expansion limits. Also cap the size of the request body and the time and memory a single parse may use.
Is an XML bomb the same as XXE?
No. XXE abuses external entities to read files or reach internal systems. An XML bomb abuses internal entities to exhaust resources. Both come from the same parser feature, DTDs, and disabling DTDs addresses both.
Can a WAF stop an XML bomb?
Partly. A filter can refuse a request body that contains an entity declaration on an endpoint that never receives XML with a DTD, and can record it first in monitor mode. It cannot make a parser safe. The expansion limits belong in the parser configuration.