1 How XXE Works
XML External Entity (XXE) injection exploits XML parsers that process external entity references defined in a DOCTYPE declaration. When a parser fetches the entity, it can read local files, make internal network requests, or trigger denial-of-service.
Malicious XML payload reading /etc/passwd:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<root>
<name>&xxe;</name>
</root>
When the server parses this, the &xxe; entity is replaced with the contents of /etc/passwd, which then appears in the response or error message.
Vulnerable Python code using lxml:
from lxml import etree
def parse_xml(xml_data):
root = etree.fromstring(xml_data) # Dangerous — external entities enabled by default
return root.find('name').text
XXE can also be used for SSRF — replacing the file:// URL with http:// to reach internal services — and for a Billion Laughs DoS attack via deeply nested entity expansion.