XXE example: how XML external entities leak files, and how to fix it

13 min read

TL;DR: XXE (XML external entity injection) happens when your app parses XML from a user with a parser that resolves entities, so the document can declare <!ENTITY xxe SYSTEM "file:///..."> and the parser reads that file into the data your code returns. A typical XXE example is an import endpoint that echoes a field from the uploaded XML. You fix it by refusing DTDs (the <!DOCTYPE> block) or turning off entity resolution and network access in the parser.

This article is for developers and site owners whose app reads XML from anyone else: imports, SOAP or SAML endpoints, office documents and SVG uploads. It shows the mechanism, a vulnerable import endpoint and its fix in a small Python app we ran on our own machine, how eight parser setups treated the same files, and a harmless check you can run against your own app. Test only systems you own or have written permission to test.

What is an XXE vulnerability?

XXE is a bug in how an application configures its XML parser, not in the XML it receives. MITRE catalogues it as CWE-611: Improper Restriction of XML External Entity Reference: the product processes an XML document that can contain entities with URIs that resolve to documents outside its intended sphere of control, and embeds them in its output.

XML lets a document carry a document type definition (DTD) that declares entities, named pieces of text that are substituted wherever &name; appears. An internal entity holds the text itself. An external entity, declared with SYSTEM, holds a URI, and a parser that resolves it fetches that URI and puts the result in the document. The feature was designed for assembling documents from parts. On a server that parses a stranger's upload, it lets the stranger choose what the server reads.

XXE had its own place in the OWASP Top 10 2017, A4. In the OWASP Top 10:2025 CWE-611 is mapped to A02, Security Misconfiguration, which is the right way to think about it: the fix is almost always a parser setting. Our security misconfiguration examples cover the rest of that category.

How does an XML external entity attack work?

The attacker sends an XML document whose DTD declares an external entity, and references that entity in a field the application later shows. The parser resolves the entity while it parses, so by the time your code reads the field, it already holds the file.

Diagram: an uploaded XML document declares an entity pointing at a local file; the parser reads it into the response

The application asked for the name element. The parser had already filled it with the file.

What the entity points at decides what the attacker gets. PortSwigger's XXE guide lists the main outcomes:

  • File disclosure. A file:// URI makes the parser read a file the application's user can read: configuration, keys, source code. It "often allows an attacker to view files on the application server filesystem."
  • Server-side request forgery. An http:// URI makes the parser send a request from inside your network. It is the same problem as in our SSRF example, reached through a parser instead of a URL field.
  • Blind XXE. When the response does not echo the value, an attacker can sometimes use "out-of-band techniques" to send the data to a server they control. The Cisco BroadWorks bug below is of this kind.
  • Denial of service. Entities that reference other entities can expand into a very large document, the pattern usually called "billion laughs". Python's documentation notes that Expat versions lower than 2.7.2 may be vulnerable to it.

Where does XXE hide in my own app?

Anywhere your server parses XML that someone else wrote, including formats that do not look like XML. PortSwigger names the less obvious places:

  • Endpoints that take XML on purpose: SOAP services, XML APIs, SAML single sign-on, RSS or sitemap imports, product feeds.
  • File uploads: "office document formats like DOCX and image formats like SVG" are XML inside. An image library that accepts SVG may parse it even when the form only expected PNG.
  • Endpoints that accept JSON or form data but also tolerate XML. Some frameworks pick the parser from the Content-Type header, so the same endpoint parses application/xml if a client sends it.
  • Server-side XML you build from user input and pass to a back-end service, where the user controls part of the document.

To find these, start from the list of your endpoints and the content types each accepts. AppMap builds that list for your own app, with every API endpoint and its auth requirement, and exports it as OpenAPI; then look at which of them read XML or uploaded files.

What does an XXE example look like in code?

It looks like an ordinary import endpoint whose parser was told to resolve entities. We wrote a one-file Python app (standard-library HTTP server plus lxml) that accepts a product as XML at POST /import and answers with the product name. It ran on 127.0.0.1 only, with lxml 6.1.3 and its bundled libxml2 2.14.6.

The vulnerable parser is one argument away from the safe one. Someone wanted entities like &company; to expand, and turned resolution on:

from lxml import etree

def parse_vulnerable(body: bytes):
    # Resolves every entity, including SYSTEM entities: files and URLs.
    parser = etree.XMLParser(resolve_entities=True)
    return etree.fromstring(body, parser)

A normal product goes through as expected:

curl -s -X POST --data-binary @product.xml -H 'Content-Type: application/xml' http://127.0.0.1:8124/import
Imported product: Blue mug

Then we sent the same product with a DTD that declares an external entity pointing at a file we had created for the lab, /tmp/xxe-lab/secret.txt, and put the entity where the name goes:

<?xml version="1.0"?>
<!DOCTYPE product [
  <!ENTITY xxe SYSTEM "file:///tmp/xxe-lab/secret.txt">
]>
<product>
  <name>&xxe;</name>
  <sku>MUG-001</sku>
</product>
curl -s -X POST --data-binary @xxe-file.xml -H 'Content-Type: application/xml' http://127.0.0.1:8124/import
Imported product: LAB-ONLY db_password=not-a-real-secret

The app returned HTTP 200 and the file's contents, and its own log showed nothing unusual: two lines of "POST /import HTTP/1.1" 200. Nothing in the code reads a file. The parser did it while building the tree, before root.findtext("name") ran.

Do modern XML parsers still allow XXE by default?

Many no longer do, but the defaults depend on the library and its version, so check yours rather than assume. We sent the same file entity, and a second one pointing at an HTTP service we ran on 127.0.0.1:8999, to eight setups:

Table of eight XML parser setups and whether each leaked a local file or made an HTTP request in our lab

Our lab, 16 November 2026. The versions are part of the result.

What stood out:

  • lxml's defaults refused it. With no options, lxml 6.1.3 answered Entity 'xxe' not defined and our app returned 400. That matches the lxml 5.0.0 changelog (29 December 2023): "lxml no longer expands external entities (XXE) by default", and "the new default is resolve_entities='internal'." Code written for lxml 4 that set nothing was exposed; the same code on lxml 5 or later is not.
  • resolve_entities=True reopened the file read, as shown above. lxml's no_network option, which defaults to True, still stopped the HTTP entity: Attempt to load network entity.
  • Setting no_network=False did not produce a request in our build. The internal service logged nothing and the name came back empty. The libxml2 NEWS file explains why: 2.13.0 (June 2024) disabled HTTP support by default, and 2.15.0 (September 2025) removed the built-in HTTP client. That is a property of this build, not of XXE.
  • An older libxml2 did make the request. macOS ships xmllint with libxml2 2.9.13. With --noent, which substitutes entities, it printed the lab file and, for the HTTP entity, the internal service's reply; the service logged "GET /internal-only HTTP/1.0" 200. With --noent --nonet it refused: I/O error : Attempt to load network entity.
  • Python's standard library refused it. xml.etree.ElementTree on expat 2.8.2 raised undefined entity &xxe;; Python's documentation says that by default "Expat itself does not access local files or create network connections." defusedxml 0.7.1 raised EntitiesForbidden and named the system identifier it refused.

The --noent result is worth remembering beyond Python. PHP passes the same libxml2 option as LIBXML_NOENT, which the PHP manual describes only as "Substitute entities". Adding it to simplexml_load_string() or DOMDocument::loadXML() is the PHP version of resolve_entities=True.

How do I check my own app for XXE?

Send a document with a harmless internal entity first, then, only if the DTD is processed, an external entity that points at a file you placed on your own server. Do this on a staging copy of your own app; never on a site you do not own or have permission to test.

The first probe declares an entity whose value is a fixed string:

<?xml version="1.0"?>
<!DOCTYPE product [
  <!ENTITY probe "xxe-probe-123">
]>
<product>
  <name>&probe;</name>
  <sku>MUG-001</sku>
</product>

Against our three main modes it gave:

vulnerable  Imported product: xxe-probe-123   [HTTP 200]
default     Imported product: xxe-probe-123   [HTTP 200]
fixed       Rejected: the XML is not allowed  [HTTP 400]

This surprised us at first, and it is the useful lesson of the probe: the vulnerable and the safe-by-default parser answer the same. lxml's default resolve_entities='internal' expands internal entities and refuses external ones, so the probe only tells you that your parser processes DTDs. A rejection means you can stop. An expansion means you need the second step.

For the second step, put a canary file you created yourself on the staging server, with a value that means nothing (LAB-ONLY above), and point a SYSTEM entity at it with a file:// URI. If its contents come back, the parser resolves external entities and you have a finding. If you get an error or an empty field, check the logs: blind XXE does not echo anything, so the absence of output is not proof on its own. In that case the parser configuration in the code is the stronger evidence.

How do I fix XXE?

Configure the parser so it cannot resolve external entities, and reject DTDs outright when your format does not need them. The OWASP XXE Prevention Cheat Sheet puts the general rule in one line: "Disable document type definitions (DTDs) whenever possible." It then lists the settings for each language and parser.

This is the fixed parser in our lab app:

from lxml import etree

def parse_fixed(body: bytes):
    # No DTD at all, no entity expansion, no network.
    parser = etree.XMLParser(
        resolve_entities=False, load_dtd=False, no_network=True, huge_tree=False
    )
    root = etree.fromstring(body, parser)
    if root.getroottree().docinfo.doctype:
        raise ValueError("DOCTYPE is not allowed")
    return root

It parsed the normal product as before and rejected both the file entity and the probe with HTTP 400, logging the reason:

127.0.0.1 - - [15/Nov/2026 23:42:30] rejected XML: DOCTYPE is not allowed

The explicit DOCTYPE check matters. Without it, lxml with these options still accepted both documents and left &xxe; in the tree as an unexpanded reference, so the app would have answered 200 with an empty name; with it, the app refuses anything that carries a DTD, which a product feed never needs, and the log line tells you someone tried.

In other stacks the same idea takes a different name:

  • Python: for untrusted XML with the standard library, parse through defusedxml; with lxml 5 or later, keep resolve_entities at its default or set it to False, and never set no_network=False.
  • PHP: do not pass LIBXML_NOENT or LIBXML_DTDLOAD. Since PHP 8.0, libxml_disable_entity_loader() is deprecated "as external entity loading is disabled by default", because libxml2 2.9.0 and later do not substitute entities unless asked.
  • C and anything on libxml2: avoid XML_PARSE_NOENT and XML_PARSE_DTDLOAD. libxml2 2.13.0 added XML_PARSE_NO_XXE, which the NEWS file describes as disabling the loading of external entities or DTDs.
  • Java and .NET: Java's parsers need explicit settings, and the OWASP cheat sheet gives the feature flags for each factory (DocumentBuilderFactory, SAXParserFactory, StAX and others). For .NET it notes that XmlDocument, XmlTextReader and XPathNavigator are protected from version 4.5.2 of the .NET Framework.

Two things the parser settings do not cover. A library you call may parse XML with its own parser: an image library reading SVG, an office-document library, a SAML toolkit. Check its version and its own advice. And keep the dependency current: the lxml and libxml2 changes above made the default safer only for apps that upgraded.

Is XXE still exploited?

Yes. On 10 November 2026 CISA added Cisco BroadWorks CVE-2026-20320 to its Known Exploited Vulnerabilities catalogue, as covered in our roundup for the week of 15 November. Cisco's advisory describes an out-of-band blind XXE in the XML parser of the Open Client Interface (OCI), which "could allow an unauthenticated, remote attacker to read sensitive configuration information." Cisco also says it has no direct evidence of active exploitation; the roundup gives both positions. If you run BroadWorks, the fixed release is in the advisory.

It is also why a practice target may not show you XXE. The OWASP Juice Shop companion guide says its two XXE challenges, "XXE Data Access" and "XXE DoS", "are not available when running the Juice Shop in either a Docker container or on a Heroku dyno", because the attacks made the process crash. That is why this article used a local lab app. For the rest of the web vulnerability classes, CyberGym is a legal target to practise on.

Key takeaways

  • Find every place your server parses XML from someone else, including SVG and office uploads and endpoints that accept XML because of a Content-Type header.
  • Reject any document with a <!DOCTYPE> when your format does not need one, and log the rejection.
  • Never set the options that turn resolution on: resolve_entities=True and no_network=False in lxml, LIBXML_NOENT in PHP, XML_PARSE_NOENT in libxml2.
  • Upgrade XML libraries; lxml 5.0 and libxml2 2.13 changed defaults in your favour only for apps that took the new version.
  • When you test your own app, an internal-entity probe shows only that DTDs are processed; confirm with a canary file you placed yourself.

FAQ

What is an example of XXE?

An import endpoint receives XML whose DTD declares <!ENTITY xxe SYSTEM "file:///path/to/file"> and puts &xxe; in a field the app shows. If the parser resolves external entities, the response contains the file. In our lab, a Python endpoint using lxml with resolve_entities=True returned the contents of a lab file in its "Imported product" message.

What does XXE stand for?

XXE stands for XML external entity, and the bug is usually called XML external entity injection. MITRE lists it as CWE-611, Improper Restriction of XML External Entity Reference. In the OWASP Top 10:2025 it is part of A02, Security Misconfiguration.

How is XXE different from SSRF?

SSRF is the result, XXE is one way to get there. In SSRF your server makes a request to a destination someone else chose; an external entity with an http:// URI does that through the XML parser. XXE can also read local files and exhaust memory, which SSRF through a URL field does not.

Is JSON safe from XXE?

JSON has no entities, so a JSON parser cannot be tricked into XXE. The risk is an endpoint that accepts JSON but also parses XML when the request says Content-Type: application/xml. Check that your framework rejects content types you did not mean to accept.

Does disabling DTDs break anything?

Rarely, for data that comes from users. Product feeds, API payloads and SAML messages do not need a DTD. Formats that legitimately use one are usually documents you author yourself, which you can parse with a separate, trusted configuration.

Can a WAF stop XXE?

A web application firewall can block requests that contain <!DOCTYPE or <!ENTITY, and that helps, but it is not a fix. It may not inspect uploaded files such as DOCX, which are compressed archives. The parser setting is the fix; the WAF rule is an extra layer.

Sources

Want to see which of your own endpoints read XML or uploaded files? Map your app at hackergpt.app, or practise web vulnerabilities on CyberGym.

All articles on Web vulnerabilities