Fixing XXE

Everything you need to know in order to fix your code

Tools recognizing this:

Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is XXE Injection

XML External Entity (XXE) Injection is a web security vulnerability that occurs when an application processes XML input containing references to external entities without proper validation or restrictions.

An attacker can exploit XXE vulnerabilities to achieve various malicious goals:

  • Read sensitive files from the application server

  • Perform Server-Side Request Forgery (SSRF)

  • Execute denial of service attacks

  • In some cases, achieve remote code execution

  • Scan internal networks and ports

One Simple Example

Consider this example of XML processing:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
   <!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<foo>&xxe;</foo>

When processed by a vulnerable XML parser, this payload:<!ENTITY xxe SYSTEM "file:///etc/passwd">will result in the content of the /etc/passwd file being read and returned to the attacker.

This allows the attacker to read sensitive system files that should not be accessible.

Real-world Occurrences of XXE Injection

Facebook XXE Vulnerability (2014)

Facebook's "Forgot your password?" functionality was found to be vulnerable to XXE injection. An attacker could read arbitrary files from Facebook's servers, and potentially run arbirary code. The vulnerability occurred because the XML parser used to process user-supplied XML data had external entity processing enabled. Facebook fixed the issue through their bug bounty program before any known exploitation.

Uber XXE Vulnerability (2016)

A security researcher identified an XXE vulnerability in a third-party backup software utilized by Uber. This flaw allowed unauthorized access to internal files. The vulnerability could have given an attacker access to the user backup data of any company using the software, including Uber. Uber fixed the issue through their bug bounty program before any known exploitation.

Fixing XXE Injections

The most effective way to prevent XXE attacks is to disable XML external entity and DTD processing in your XML parser configuration.

Different XML parsers have different methods to disable these features, but the general principle is to turn off support for external entities and DTD processing entirely unless absolutely necessary. If DTD processing is required, external entities should still be disabled.

Code Samples

Vulnerable Code

DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = df.newDocumentBuilder();
Document doc = builder.parse(new InputSource(reader));

Fixed Code

DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
df.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
df.setFeature("http://xml.org/sax/features/external-general-entities", false);
df.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
DocumentBuilder builder = df.newDocumentBuilder();
Document doc = builder.parse(new InputSource(reader));

Fix Explanation

The vulnerable code uses default XML parser settings which allow external entities. The fix explicitly disables DOCTYPE declarations and external entities. Additional security features are enabled to prevent XXE attacks. The XML parser will now reject any attempts to use external entities.

Last updated

Was this helpful?