CRITICAL
Rule Definition
XML documents optionally contain a Document Type Definition (DTD), which, among other features, enables the definition of XML entities. It is possible to define an entity by providing a substitution string in the form of a URI. The XML parser can access the contents of this URI and embed these contents back into the XML document for further processing.
By submitting an XML file that defines an external entity with a file:// URI, an attacker can cause the processing application to read the contents of a local file. For example, a URI such as "file:///c:/winnt/win.ini" designates (in Windows) the file C:\Winnt\win.ini, or file:///etc/passwd designates the password file in Unix-based systems. Using URIs with other schemes such as http://, the attacker can force the application to make outgoing requests to servers that the attacker cannot reach directly, which can be used to bypass firewall restrictions or hide the source of attacks such as port scanning.
Once the content of the URI is read, it is fed back into the application that is processing the XML. This application may echo back the data (e.g. in an error message), thereby exposing the file contents.
Remediation
Use function libxml_set_external_entity_loader()
Violation Code Sample
libxml_disable_entity_loader(false);
// OR
simplexml_load_file($file_path, options: LIBXML_NOENT);
Fixed Code Sample
libxml_set_external_entity_loader(null);
simplexml_load_file($file_path, options: LIBXML_NOENT);
Reference
https://cwe.mitre.org/data/definitions/611.html
https://www.owasp.org/index.php/Top_10-2017_A4-XML_External_Entities_(XXE)
https://owasp.org/Top10/fr/A05_2021-Security_Misconfiguration/
https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#php
https://www.php.net/manual/en/libxml.constants.php
Related Technologies
Technical Criterion
CWE-611 - Improper Restriction of XML External Entity Reference
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.