Free, independent guides since 2010

Code

Secure XML parsing with dom4j

How to protect Java applications that use dom4j against XXE, entity expansion and other XML attacks, with safe SAXReader settings for every version.

<guide section="Code" type="guide" read="2 min"/>

XML is more powerful than it looks. A document can declare a DTD with entities, and an entity can point to a file on the server or to a URL. If a parser resolves those entities, an attacker who can send you XML may be able to read local files, scan your internal network or crash your application. This class of bug is called XXE (XML External Entity) and has been on the OWASP lists for years.

What an XXE attack looks like

XML
<?xml version="1.0"?>
<!DOCTYPE order [
  <!ENTITY secret SYSTEM "file:///etc/passwd">
]>
<order><note>&secret;</note></order>

A vulnerable parser replaces &secret; with the contents of the file. If your application later shows or returns the note, the attacker reads your server’s files. Similar tricks with nested entities (the “billion laughs” attack) can use up all memory.

dom4j and CVE-2020-10683

Older versions of dom4j created readers that allowed external DTDs and external entities by default. This was registered as CVE-2020-10683 and fixed in dom4j 2.0.3 and 2.1.3, which introduced SAXReader.createDefault() with external entities disabled. A separate issue, CVE-2018-1000632, allowed XML injection through element and attribute names and was fixed in 2.1.1.

The safe way to create a reader

Java
// dom4j 2.1.3 and newer
SAXReader reader = SAXReader.createDefault();
Document doc = reader.read(inputStream);

If you do not need DTDs at all – and most applications do not – go one step further and refuse any document that contains a DOCTYPE. This also blocks entity expansion attacks:

Java
SAXReader reader = SAXReader.createDefault();
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

Need to support DTDs but not external ones? Disable the individual features instead:

Java
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

A short security checklist

  • Use a current org.dom4j:dom4j release and keep it updated with your other dependencies.
  • Create readers with SAXReader.createDefault(), never with settings copied from an old tutorial.
  • Refuse DOCTYPE declarations when you do not need them.
  • Limit the size of uploaded XML before you parse it.
  • Never build XML by concatenating strings with user input – use the dom4j API so names and values are escaped properly (see writing XML with dom4j).
  • Scan your dependencies automatically with a tool such as OWASP Dependency-Check or your platform’s built-in scanner.

The OWASP XXE Prevention Cheat Sheet has settings for every major Java XML parser if your project uses more than dom4j.

Frequently asked questions

Am I safe if I use dom4j 2.1.4 or 2.2.0?

You are safe from the old default behaviour if you create your reader with SAXReader.createDefault() or configure the features yourself. Plain new SAXReader() still uses the parser settings of your environment, so be explicit.

Do I need to worry if I only parse my own files?

The risk is highest for XML that comes from users, uploads, partners or the network. Still, secure defaults cost nothing, so use them everywhere.