Improper Neutralization of Special Elements in Data Query Logic

Incomplete Class
Structure: Simple
Description

This vulnerability occurs when an application builds a query for a data store (like a database) but fails to properly sanitize user-controlled input. This allows an attacker to inject special elements that change the query's intended logic, potentially accessing or manipulating data in unauthorized ways.

Extended Description

Attackers can exploit this flaw to manipulate query logic in several harmful ways. They can alter search criteria to return different records, append extra commands, or change the number or order of results. This isn't just about stealing data; if your application logic assumes a specific result—like a single administrative user record—manipulating the query can cause it to incorrectly grant permissions or make flawed decisions based on tainted results. While SQL injection is the most well-known example, this risk applies to many query languages. NoSQL databases, LDAP queries, XPath, and other data querying systems (like HTSQL, DQL, or XQuery) are also vulnerable if input isn't properly neutralized. The core issue is trusting user input within any command that interprets logic, not just traditional SQL.

Common Consequences 1
Scope: ConfidentialityIntegrityAvailabilityAccess Control

Impact: Bypass Protection MechanismRead Application DataModify Application DataVaries by Context

Detection Methods 1
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Demonstrative Examples 3

ID : DX-209

The following code dynamically constructs and executes a SQL query that searches for items matching a specified name. The query restricts the items displayed to those where owner matches the user name of the currently-authenticated user.

Code Example:

Bad
C#
c#
The query that this code intends to execute follows:

Code Example:

Informative
bash
However, because the query is constructed dynamically by concatenating a constant base query string and a user input string, the query only behaves correctly if itemName does not contain a single-quote character. If an attacker with the user name wiley enters the string:

Code Example:

Attack
bash
for itemName, then the query becomes the following:

Code Example:

Attack
bash
The addition of the:

Code Example:

Attack
bash
condition causes the WHERE clause to always evaluate to true, so the query becomes logically equivalent to the much simpler query:

Code Example:

Attack
bash
This simplification of the query allows the attacker to bypass the requirement that the query only return items owned by the authenticated user; the query now returns all entries stored in the items table, regardless of their specified owner.

ID : DX-210

The code below constructs an LDAP query using user input address data:

Code Example:

Bad
Java
java
Because the code fails to neutralize the address string used to construct the query, an attacker can supply an address that includes additional LDAP queries.

ID : DX-211

Consider the following simple XML document that stores authentication information and a snippet of Java code that uses XPath query to retrieve authentication information:

Code Example:

Informative
XML
xml
The Java code used to retrieve the home directory based on the provided credentials is:

Code Example:

Bad
Java
java
Assume that user "john" wishes to leverage XPath Injection and login without a valid password. By providing a username "john" and password "' or ''='" the XPath expression now becomes

Code Example:

Attack
bash
This lets user "john" login without a valid password, thus bypassing authentication.
Observed Examples 5
CVE-2024-50672NoSQL injection in product for building eLearning courses allows password resets using a query processed by the Mongoose find function
CVE-2021-20736NoSQL injection in team collaboration product
CVE-2020-35666NoSQL injection in a PaaS platform using a MongoDB operator
CVE-2014-2503Injection using Documentum Query Language (DQL)
CVE-2014-2508Injection using Documentum Query Language (DQL)
References 2
NoSQL injection
PortSwigger
ID: REF-1454
A Pentester's Guide to NoSQL Injection
The SecOps Group
14-04-2023
ID: REF-1455
Applicable Platforms
Languages:
Not Language-Specific : Undetermined
Modes of Introduction
Implementation
Related Attack Patterns
Alternate Terms

NoSQL Injection, NoSQLi

Injection of data query language into NoSQL databases such as Cassandra, ElasticSearch, MongoDB, Redis, and others
Notes
RelationshipIt could be argued that data query languages are effectively a command language - albeit with a limited set of commands - and thus any query-language injection issue could be treated as a child of Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection'). However, Improper Neutralization of Special Elements in Data Query Logic is intended to better organize query-oriented issues to separate them from fully-functioning programming languages, and also to provide a more precise identifier for the many query languages that do not have their own CWE identifier.