|
1 | | -<h2>Noncompliant Code Example</h2> |
| 1 | +<p>XML standard allows the use of entities, declared in the DOCTYPE of the document, which can be <a |
| 2 | +href="https://www.w3.org/TR/xml/#sec-internal-ent">internal</a> or <a href="https://www.w3.org/TR/xml/#sec-external-ent">external</a>.</p> |
| 3 | +<p>When parsing the XML file, the content of the external entities is retrieved from an external storage such as the file system or network, which may |
| 4 | +lead, if no restrictions are put in place, to arbitrary file disclosures or <a |
| 5 | +href="https://www.owasp.org/index.php/Server_Side_Request_Forgery">server-side request forgery (SSRF)</a> vulnerabilities.</p> |
| 6 | +<pre> |
| 7 | +<?xml version="1.0" encoding="utf-8"?> |
| 8 | +<!DOCTYPE person [ |
| 9 | + <!ENTITY file SYSTEM "file:///etc/passwd"> |
| 10 | + <!ENTITY ssrf SYSTEM "https://internal.network/sensitive_information"> |
| 11 | +]> |
| 12 | + |
| 13 | +<person> |
| 14 | + <name>&file;</name> |
| 15 | + <city>&ssrf;</city> |
| 16 | + <age>18</age> |
| 17 | +</person> |
| 18 | +</pre> |
| 19 | +<p>It’s recommended to limit resolution of external entities by using one of these solutions:</p> |
| 20 | +<ul> |
| 21 | + <li> If DOCTYPE is not necessary, completely disable all DOCTYPE declarations. </li> |
| 22 | + <li> If external entities are not necessary, completely disable their declarations. </li> |
| 23 | + <li> If external entities are necessary then: |
| 24 | + <ul> |
| 25 | + <li> Use XML processor features, if available, to authorize only required protocols (eg: https). </li> |
| 26 | + <li> And use an entity resolver (and optionally an XML Catalog) to resolve only trusted entities. == Noncompliant Code Example </li> |
| 27 | + </ul> </li> |
| 28 | +</ul> |
2 | 29 | <p>For <a href="https://docs.oracle.com/javase/9/docs/api/javax/xml/parsers/DocumentBuilderFactory.html">DocumentBuilder</a>, <a |
3 | 30 | href="https://docs.oracle.com/javase/9/docs/api/javax/xml/parsers/SAXParserFactory.html">SAXParser</a>, <a |
4 | 31 | href="https://docs.oracle.com/javase/9/docs/api/javax/xml/stream/XMLInputFactory.html">XMLInput</a>, <a |
|
0 commit comments