📄
Security

XXE Vulnerabilities and XML Security: Stopping External Entities

03.12.2026
← All articles

XXE, or XML External Entity injection, abuses the entity feature of XML. A document may contain a DOCTYPE declaration that defines entities, originally a shorthand for repeated text, but an entity can also point to an external source such as a server file or a network address. If a site parses user-supplied XML with a badly configured parser, an attacker can read server files, send requests into the internal network or exhaust the server's memory.

Where XML still shows up

Modern sites prefer JSON, yet XML is everywhere: data exchange with accounting software, SOAP integrations with banks and government services, product import files, RSS feeds, sitemaps and SVG images. DOCX and XLSX files are ZIP archives full of XML, so any feature that accepts Word or Excel uploads also runs an XML parser.

What the attack looks like

The attacker submits an ordinary order file with an external entity declared at the top and used in place of the customer name:

<?xml version="1.0"?>
<!DOCTYPE order [
  <!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<order>
  <customer>&xxe;</customer>
  <item>Cement M400</item>
</order>

If the parser substitutes entities, the customer field now holds the contents of /etc/passwd. Display it on a confirmation page or in an error message and the file is leaked. The same trick targets config files with database passwords or internal addresses like http://127.0.0.1/..., turning XXE into SSRF. A third risk is denial of service: the "billion laughs" attack chains entities so a tiny file expands into gigabytes. Modern libxml2 has default limits against this, but LIBXML_PARSEHUGE removes them.

Dangerous and safe settings in PHP

Since libxml2 2.9, external entity loading is disabled by default, which is why PHP 8.0 deprecated libxml_disable_entity_loader(). Developers can still switch the danger back on, usually by copying advice to add LIBXML_NOENT to fix characters like &amp;. Despite its name, this flag enables entity substitution:

<?php
// VULNERABLE: LIBXML_NOENT substitutes entities, including external ones
$xml = simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOENT | LIBXML_DTDLOAD);
echo $xml->customer;

The safe version avoids those flags, forbids network access and rejects any document with a DOCTYPE, which normal business formats never need:

<?php
$dom = new DOMDocument();
// No LIBXML_NOENT, no LIBXML_DTDLOAD; LIBXML_NONET forbids network access
if (!$dom->loadXML($body, LIBXML_NONET)) {
    http_response_code(400); exit('Invalid XML');
}
// Our format never needs a DOCTYPE, so reject any document that has one
if ($dom->doctype !== null) {
    http_response_code(400); exit('DOCTYPE is not allowed');
}
$xml = simplexml_import_dom($dom);

Other languages

Python's documentation warns against using the standard XML modules on untrusted data; use defusedxml instead. In Java, disallowing DOCTYPE declarations is the most robust option:

# Python: use defusedxml instead of the standard parser
from defusedxml.ElementTree import fromstring
root = fromstring(data)

# Python lxml: switch off entity resolution and network access
from lxml import etree
parser = etree.XMLParser(resolve_entities=False, no_network=True)

// Java: forbid DOCTYPE declarations completely
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

Example: a building materials wholesaler in Tashkent

A wholesaler lets dealers upload orders as XML exported from accounting software, then shows the customer name and items on a confirmation page. Years earlier a developer had added LIBXML_NOENT to fix how & appeared in company names. One day a file from an unknown "dealer" displays the site's config file, database password included, where the customer name should be. The flag is removed, files with a DOCTYPE are rejected, the password is rotated, and uploads are limited to logged-in dealers and logged. XXE is often not a sophisticated attack but one misunderstood flag.

Checking your project

Start by searching for risky flags; in almost every case the answer to "is this flag needed?" is no:

grep -rnE "LIBXML_NOENT|LIBXML_DTDLOAD|LIBXML_PARSEHUGE" --include="*.php" .

On sayt.uz hosting you can check and upgrade your PHP version in the ISPmanager panel opened from the cabinet. Newer PHP and libxml2 bring safer defaults, but only a developer can remove dangerous flags from the code.

Related articles

📂 Secrets in a public GitHub repo: what to check before you publish 🚨 Where to report a cyberattack in Uzbekistan 🧩 Subresource Integrity (SRI): protecting external scripts 🌐 What Is SSRF and How to Protect Your Server From It
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓