XML External Entity (XXE) Processing
Introduction
XML External Entity (XXE) processing is a type of attack that exploits vulnerabilities in XML parsers. These attacks occur when an XML input containing a reference to an external entity is processed by a weakly configured XML parser. This can lead to serious consequences such as sensitive data exposure, denial of service (DoS), and server-side request forgery (SSRF).
In this article, we’ll explore the basics of XXE processing, present scenario-based examples with source code, show the expected outputs, and discuss effective mitigation strategies.
Scenario: XXE Vulnerability in a File Upload Feature
Scenario Description: A web application allows users to upload XML files. The application parses the uploaded XML files to extract data. However, the XML parser is not configured to disable external entity processing, leading to a potential XXE vulnerability.
Vulnerable Source Code:
| import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class XXEVulnerabilityExample { |
Example of Malicious XML:
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE root [
<!ENTITY xxe SYSTEM “file:///etc/passwd”>
]>
<root>&xxe;</root>
Expected Output:
When the above XML is processed by the vulnerable code, it will attempt to read the contents of the /etc/passwd file and print it as part of the XML output. This can expose sensitive information from the server.
Mitigation Strategies
To prevent XXE attacks, you need to disable external entity processing in your XML parsers. Below are the mitigations for various languages and libraries.

Java:
| import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class XXEMitigationExample { |
Python (using lxml):
from lxml import etree
def parse_xml(file_path):
parser = etree.XMLParser(resolve_entities=False, no_network=True)
tree = etree.parse(file_path, parser)
root = tree.getroot()
print(“Root element:”, root.tag)
parse_xml(“input.xml”)
.NET (C#):
using System;
using System.Xml;
public class XXEMitigationExample
{
public static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null; // Disable external entity processing
xmlDoc.Load(“input.xml”);
Console.WriteLine(“Root element: ” + xmlDoc.DocumentElement.Name);
}
}
PHP:
| $xmlString = file_get_contents(‘input.xml’); $disableEntities = libxml_disable_entity_loader(true); $xml = simplexml_load_string($xmlString, ‘SimpleXMLElement’, LIBXML_NOENT); libxml_disable_entity_loader($disableEntities); echo “Root element: ” . $xml->getName(); |
Conclusion
XML External Entity (XXE) attacks can have severe consequences if not mitigated properly. It’s essential to understand the risks and implement the necessary measures to secure XML parsers. By following the best practices and disabling external entity processing, you can protect your applications from XXE vulnerabilities.
Always keep your dependencies up to date and review security guidelines for the libraries and frameworks you use to ensure robust protection against such attacks.
