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:

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: