What is XML external entity (XXE) injection?

XML external entity (XXE) injection is a vulnerability where an application parses XML from a user with a parser that resolves external entities. The document can then declare an entity pointing at a local file or a URL, and the parser reads it into the data the application processes and often returns.

Part of the roadmap: Stage 5, Deeper web and APIs

Why it matters

MITRE files it as CWE-611, improper restriction of XML external entity reference. It had its own category in the OWASP Top 10 2017, and the 2025 edition counts it under A02, security misconfiguration, which is the honest description: the bug is in how the parser was configured, not in the XML. A parser that fetches URLs can also be turned into SSRF against internal services.

It hides in places that do not look like XML: SOAP and SAML endpoints, RSS and product-feed imports, and uploaded files, since DOCX documents and SVG images are XML inside. Some frameworks also pick a parser from the Content-Type header, so an endpoint built for JSON may parse XML when a client sends it.

Example: an import endpoint one parser option away from safe

CyberGym, the legal practice target (OWASP Juice Shop), has two XXE challenges, but Juice Shop disables them when it runs in a Docker container or on Heroku, because the attacks crashed the process, so a hosted copy may not offer them. Our lab was a local one. A small Python import endpoint built with lxml echoed the product name from the uploaded XML; with entity resolution turned on, a document that declared an entity for a lab file got that file's contents back in the reply, and the app's own log showed only two ordinary 200 lines.

In your own app (https://your-app.example), the fix is in the parser settings:

from lxml import etree

# Vulnerable: resolves SYSTEM entities, so files and URLs are read
parser = etree.XMLParser(resolve_entities=True)

# Safe: no external entities, no network, no DTD loading
parser = etree.XMLParser(resolve_entities=False, no_network=True, load_dtd=False)

Defaults differ by library and version: lxml 5.0 stopped expanding external entities by default, and Python's standard library parsers do not read local files. Check the parser your code actually uses, list every endpoint that accepts XML or uploaded documents, and refuse any document with a DOCTYPE if you do not need one.

Common mistakes

  • Assuming the parser's default is safe. It depends on the library and its version. Set the options explicitly and test them.
  • Checking only endpoints that are meant to take XML. SVG and DOCX uploads, and JSON endpoints that also accept XML, reach the parser too.
  • Filtering for the word ENTITY. Encodings and parameter entities slip past text filters. Turn off DTDs in the parser.
  • Adding an option that substitutes entities. PHP's LIBXML_NOENT and xmllint's --noent switch resolution on. Leave them out for untrusted input.

Related terms

Go deeper

Sources

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. Practise on CyberGym.