XXE Attack

Difficulty: Advanced

Question

What is an XXE (XML External Entity) injection, what can an attacker do with it, and how do you prevent it?

Answer

XXE is a vulnerability in applications that parse XML with external entity resolution enabled. XML lets a document define entities, including external ones that point at a URI, and a parser that resolves them will fetch that URI during parsing. An attacker who can submit XML can therefore make the server read local files, reach internal services or hang on a malicious resource.

How it works

XML has a Document Type Definition where you can declare entities. A normal entity is just a text substitution. An external entity uses a SYSTEM identifier with a URI, and a vulnerable parser resolves it by reading that URI, whether it is file:///etc/passwd or http://internal-service/. The parser then inserts whatever it fetched into the document, which the application may echo back or act upon.

What an attacker can do

  • Read local files such as configuration, credentials or /etc/passwd and have them returned in the response.
  • Server-side request forgery (SSRF): force the server to call internal URLs, including cloud metadata endpoints like 169.254.169.254, to steal instance credentials.
  • Denial of service: the 'billion laughs' attack nests entities that expand exponentially and exhaust memory.
  • Blind XXE: when nothing is reflected, exfiltrate data out of band to an attacker-controlled server, similar to blind SQL injection.
  • Port scanning of the internal network by timing or differentiating responses.

Where it hides

XXE is not only in obvious XML APIs. It appears in SOAP services, SVG image uploads, DOCX and XLSX files (which are zipped XML), RSS and SAML assertions. Any feature that accepts a file which is really XML is a candidate.

Prevention

ControlDetail
Disable DTDs entirelyThe strongest fix. If the parser never processes a DOCTYPE, external entities cannot be declared.
Disable external entities and external DTD loadingUse the parser's secure-processing flags if you must allow DTDs.
Prefer safer formatsJSON has no entity concept; use it where you control the contract.
Use hardened librariesIn Python use defusedxml; modern .NET and current Java defaults are safer but verify them.
Validate and sandbox uploadsTreat SVG, DOCX and XML uploads as untrusted and parse them with entities off.

The fix is almost always a parser configuration change, not input filtering. Most modern parsers can be told to reject DOCTYPE declarations in one line.

Common mistakes

  • Assuming only an 'XML API' is at risk and forgetting SVG, Office files and SAML.
  • Trying to blocklist <!ENTITY> strings instead of disabling the feature in the parser.
  • Leaving old parsers on insecure defaults; some older libraries resolve external entities unless told not to.
  • Fixing file read but leaving SSRF to the metadata endpoint possible.
XXE used for file disclosure and SSRF. A parser with external entities enabled fetches whatever URI the attacker declares.
  1. Attacker → App Parser: XML declaring SYSTEM entity to file:// or http://
  2. App Parser → Local/Internal Resource: Resolves the entity and fetches the URI
  3. Local/Internal Resource → App Parser: File contents or internal response
  4. App Parser → Attacker: Data reflected in the response or exfiltrated

Code examples

Harden an XML parser (Java and Python)

// Java: disable DTDs completely on the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder builder = dbf.newDocumentBuilder();

disallow-doctype-decl is the single most effective setting: with no DOCTYPE allowed, no external entity can be declared in the first place.

Safe XML parsing in Python

# The stdlib parser can be unsafe for untrusted input.
# Use defusedxml, which disables entity expansion and external access.
from defusedxml.ElementTree import fromstring

def parse_untrusted(xml_bytes: bytes):
    # Raises EntitiesForbidden / DTDForbidden on malicious input
    return fromstring(xml_bytes)

defusedxml wraps the standard parsers and blocks DTDs, external entities and billion-laughs expansion, so untrusted XML is safe to parse.

Key points

  • XXE abuses XML external entity resolution to make the server fetch a URI.
  • Impact includes local file disclosure, SSRF to cloud metadata, DoS and port scanning.
  • It hides in SVG uploads, DOCX/XLSX files, SOAP and SAML, not just XML APIs.
  • The fix is parser configuration: disable DTDs and external entities.
  • Use defusedxml in Python and disallow-doctype-decl in Java.

Common follow-up questions

  • What is the billion laughs attack and how does it cause denial of service?
  • How does blind XXE exfiltrate data when nothing is reflected?
  • How can XXE be used to reach a cloud metadata endpoint?
  • Why is disabling DTDs more reliable than filtering ENTITY strings?

Interview tips

  • Name the three impacts: file read, SSRF and DoS. It shows you see XXE as more than file disclosure.
  • Say the fix is a parser setting, not input filtering; that is the insight interviewers want.
  • Mention SVG and Office files as sneaky entry points to stand out.

Concepts covered

XML Parsing, External Entities, SSRF, File Disclosure, Parser Hardening

More Web Security (OWASP, JWT, XSS, CSRF) questions