A document type definition (DTD) describes the permitted structure of an XML document: its element names, ordering, content types, and attributes. Applications can use it to validate incoming XML, but DTD is legacy technology, largely replaced by more flexible XML schemas. section 11.5
Treat DTD support as a security boundary when accepting XML:
- Disable inline DTD processing. An inline DTD is embedded in the submitted document, so an attacker controls its declarations. section 11.5 section 11.8
- Watch for entity expansion. DTD entities act as string-substitution macros and can be nested until a small request expands into gigabytes of data. section 11.6
- Harden the parser according to its platform. Options and libraries differ across Python, Ruby, Node.js, Java, and .NET. section 11.8
The billion-laughs attack
A malicious inline DTD can define lol, lol2, and successive entities that repeat one another. When the parser expands &lol9;, the document grows exponentially, potentially exhausting server memory and causing denial of service from a single HTTP request. section 11.6
<?xml version="1.0"?> <!DOCTYPE lolz [ <!ENTITY lol "lol"> <!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;"> <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;"> <!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;"> <!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;"> <!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;"> <!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;"> <!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;"> <!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;"> ]> <lolz>&lol9;</lolz>
Inspect the nested entity declarations that demonstrate how recursive expansion creates an XML bomb. section 11.6
