What Is an XML External Entity (XXE) Vulnerability?
An XML External Entity (XXE) vulnerability is a web security flaw that allows an attacker to interfere with an application’s processing of XML data. This article explains what an XXE vulnerability is, how it functions, the security risks it poses to systems, and the essential strategies developers must implement to prevent exploitation.
Understanding XML External Entities
Extensible Markup Language (XML) is a standard format used to store and transport data. XML supports Document Type Definitions (DTDs), which define the legal building blocks and structure of an XML document.
Within a DTD, developers can define “entities,” which act as
shortcuts or variables to substitute text within the document. An
external entity is a specific type of entity that fetches its
replacement value from an outside source, such as a local file path or a
remote URL, using system identifiers like
SYSTEM "file:///path/to/file" or
SYSTEM "http://example.com".
How an XXE Vulnerability Occurs
An XXE vulnerability arises when an XML parser is configured by default to process untrusted XML input and resolve external entity references.
When an attacker submits an XML payload containing a malicious external entity, an insecure parser processes the reference and loads the referenced resource. This allows the attacker to manipulate the parser into accessing data or network resources it was never intended to expose.
Example Scenario
An attacker might submit an XML document with the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE data [
<!ENTITY sensitiveFile SYSTEM "file:///etc/passwd">
]>
<data>&sensitiveFile;</data>If the parser evaluates the entity &sensitiveFile;,
it replaces it with the contents of the /etc/passwd file
and returns the sensitive data in the application’s response.
Types of XXE Attacks and Risks
Exploiting an XXE vulnerability can lead to several severe security compromises:
- Retrieving Sensitive Files: Attackers can read sensitive files stored on the server filesystem, such as configuration files, source code, credentials, or system files.
- Server-Side Request Forgery (SSRF): By replacing
file paths with URLs (e.g.,
http://internal-service/), attackers can force the server to make requests to internal networks, bypassing firewalls and interacting with unexposed internal services. - Blind XXE Exploitation: In scenarios where the application does not return the XML data in its response, attackers can use out-of-band (OOB) techniques to exfiltrate data to an attacker-controlled external server.
- Denial of Service (DoS): Attackers can submit payloads like the “Billion Laughs” attack, where recursively nested entities consume excessive memory and CPU resources, causing the application to crash.
How to Prevent XXE Vulnerabilities
The most effective way to eliminate XXE vulnerabilities is to properly configure XML parsers to disable external entities and DTD processing entirely:
- Disable DTDs: Completely disable the processing of
DOCTYPEdeclarations if your application does not explicitly require them. - Disable External Entities: If DTDs are required, explicitly disable the resolution of external general entities and external parameter entities within the parser settings.
- Disable XInclude: Turn off XInclude processing, which allows XML documents to build from sub-documents and can be used to bypass entity-based protections.
- Use Less Complex Data Formats: Where feasible, migrate from XML to simpler data formats such as JSON to reduce the overall attack surface.