The extensible markup language (XML) is designed to help store, structure, and transfer data. Because of its platform independence, flexibility, and relative simplicity, XML has found use in a wide range of applications. However, because of its versatility, XML is vulnerable to a wide spectrum of attacks, including XML injection.
A user who has the ability to provide input string data that is incorporated into an XML document can inject XML tags. These tags are interpreted by the XML parser and may cause data to be overridden.
An online store application that allows the user to specify the quantity of an item available for purchase might generate the following XML document:
An attacker might input the following string instead of a count for the quantity:
In this case, the XML resolves to the following:
An XML parser may interpret the XML in this example such that the second price field overrides the first, changing the price of the item to $1. Alternatively, the attacker may be able to inject special characters, such as comment blocks and
CDATA delimiters, which corrupt the meaning of the XML.
Noncompliant Code Example
In this noncompliant code example, a client method uses simple string concatenation to build an XML query to send to a server. XML injection is possible because the method performs no input validation.
Compliant Solution (Input Validation)
Depending on the specific data and command interpreter or parser to which data is being sent, appropriate methods must be used to sanitize untrusted user input. This compliant solution validates that
quantity is an unsigned integer:
Compliant Solution (XML Schema)
A more general mechanism for checking XML for attempted injection is to validate it using a Document Type Definition (DTD) or schema. The schema must be rigidly defined to prevent injections from being mistaken for valid XML. Here is a suitable schema for validating our XML snippet:
The schema is available as the file
schema.xsd. This compliant solution employs this schema to prevent XML injection from succeeding. It also relies on the
CustomResolver class defined in IDS17-J. Prevent XML External Entity Attacks to prevent XML external entity (XXE) attacks.
Using a schema or DTD to validate XML is convenient when receiving XML that may have been loaded with unsanitized input. If such an XML string has not yet been built, sanitizing input before constructing XML yields better performance.
Failure to sanitize user input before processing or storing it can result in injection attacks.
|The Checker Framework|
|Tainting Checker||Trust and security errors (see Chapter 8)|
|CERT.IDS16.TDXML||Protect against XML data injection|
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|
CWE-116, Improper Encoding or Escaping of Output
IDS00-J. Prevent SQL Injection LiveLesson
Section 4.4.3, "Included If Validating"