XML External Entity (XXE) Injection
vulnerabilities occur when XML data is taken from a user-controlled input without properly sanitizing or safely parsing it, which may allow us to use XML features to perform malicious actions.
XML
Extensible Markup Language (XML)
is a common markup language (similar to HTML and SGML) designed for flexible transfer and storage of data and documents in various types of applications.
XML DTD
XML Document Type Definition (DTD)
allows the validation of an XML document against a pre-defined document structure. The pre-defined document structure can be defined in the document itself or in an external file. The following is an example DTD for the XML document we saw earlier:
As we can see, the DTD is declaring the root email
element with the ELEMENT
type declaration and then denoting its child elements. After that, each of the child elements is also declared, where some of them also have child elements.
The above DTD can be placed within the XML document itself, right after the XML Declaration
in the first line. Otherwise, it can be stored in an external file (e.g. email.dtd
), and then referenced within the XML document with the SYSTEM
keyword, as follows:
XML Entities
We can define custom entities in XML DTDs to allow refactoring of variables and reduce repetitive data.
This can be done with the use of the ENTITY
keyword:
We can reference XML entity as such:
-
SYSTEM
: It specifies that the entity is an external reference. It can point to a remote URL (likehttp://localhost/company.txt
) or a local file path (likefile:///var/www/html/signature.txt
). -
company
andsignature
: These are the names of the external entities. Wherever these entities are used in the XML document, their value will be replaced with the contents of the specified external resource (in this case, a URL or file).