Entity declarations define shortcuts to commonly used text or special characters. An entity declaration may define either an internal or external entity. For internal entities, the content of the entity is given in the declaration. For external entities, the content is specified by a Uniform Resource Identifier (URI).
Entities may be either parsed or unparsed. The contents of a parsed entity are referred to as called its replacement text. An unparsed entity is a resource whose contents may or may not be text, and if text, may be other than XML. Parsed entities are invoked by name using an entity reference; unparsed entities are invoked by name.
According to XML W3C Recommendation [W3C 2008], section 4.4.3, "Included If Validating" [W3C 2008]:
When an XML processor recognizes a reference to a parsed entity, to validate the document, the processor MUST include its replacement text. If the entity is external, and the processor is not attempting to validate the XML document, the processor MAY, but need not, include the entity's replacement text.
...
An XML external entity (XXE) attack occurs when XML input containing a reference to an external entity is processed by an improperly configured XML parser. An attacker might use an XXE attack to gain access to sensitive information by manipulating the URI of the entity to refer to files on the local file system containing sensitive data such as passwords or and private user data. An attacker might launch a denial-of-service attack, for example, by specifying /dev/random
or /dev/tty
as input URIs can URIs, which can crash or indefinitely block a program.
...
This compliant solution defines a CustomResolver
class that implements the interface org.xml.sax.EntityResolver
. This interface enables a SAX application to customize handling of external entities. The customized handler uses a simple whitelist for external entities. The resolveEntity()
method returns an empty InputSource
when an input fails to resolve to any of the specified, safe entity source paths.
Code Block | ||
---|---|---|
| ||
import java.io.IOException; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; class CustomResolver implements EntityResolver { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { // Check for known good entities String entityPath = "file:/Users/onlinestore/good.xml"; if (systemId.equals(entityPath)) { System.out.println("Resolving entity: " + publicId + " " + systemId); return new InputSource(entityPath); } else { // Disallow unknown entities by returning a blank path return new InputSource(); } } } |
The setEntityResolver()
method registers the instance with the corresponding SAX driver. When parsing malicious input, the empty InputSource
returned by the custom resolver causes a java.net.MalformedURLException
to be thrown. Note that you must create an XMLReader
object on which to set the custom entity resolver.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
IDS00-J | Medium | Probable | Medium | P8 | L2 |
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
Fortify | 1.0 | Missing_XML_Validation | Implemented |
Related Vulnerabilities
CVE-2008-2370 describes a vulnerability in Apache Tomcat 4.1.0 through 4.1.37, 5.5.0 through 5.5.26, and 6.0.0 through 6.0.16. When a RequestDispatcher
is used, Tomcat performs path normalization before removing the query string from the URI, which allows remote attackers to conduct directory traversal attacks and read arbitrary files via a ..
(dot dot) in a request parameter.
...
SEI CERT Perl Coding Standard | IDS33-PL. Sanitize untrusted data passed across a trust boundary |
Injection [RST] | |
CWE-116, Improper encoding Encoding or escaping Escaping of outputOutput |
Android Implementation Details
This rule uses MS Microsoft SQL Server as an example to show a database connection. However, on Android, DatabaseHelper
from SQLite is used for a database connection. Because Android apps may receive untrusted data via network connections, the rule is applicable.
...
A Guide to Building Secure Web Applications and Web Services | |
IDS17-J. Prevent XML External Entity Attacks LiveLesson | |
[W3C 2008] | Section 4.4.3, "Included If Validating" |
...