NoSQL Injection
User input reaches a NoSQL database query as query structure, such as an operator or server-side JavaScript, so it can change which records the query selects.
| Severity | High |
| CVSS 3.1 | 8.2 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:L/A:N Open in the calculator |
| CVSS 4.0 | 8.8 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:L/VA:N/SC:N/SI:N/SA:N Open in the calculator |
| CWE | CWE-943: Improper Neutralization of Special Elements in Data Query Logic |
| MITRE ATT&CK | T1190 Exploit Public-Facing Application |
| Category | Injection |
Description
The application builds queries for a NoSQL database, typically MongoDB or a similar document store, from values supplied by the user without making sure each value is treated as plain data. Where the request parser turns input such as username[$ne]=x, or a JSON body in which a field holds an object rather than a string, into an object, that object is passed into the query and its keys are read as query operators. A condition meant to match one value can then match every record, or stop restricting the query at all. Some NoSQL databases also accept query text, and building that text by concatenation causes the same problem as SQL injection.
An attacker can typically use this to bypass authentication checks that depend on a database lookup and to read records beyond those the query was meant to return, including other users' data and stored credentials or tokens. Depending on the operation affected, they may also be able to change records. No authentication is needed when the affected query sits behind a login form or a public search. Where server-side JavaScript is enabled and input reaches an operator that runs it on the database server, such as $where, injected code is also evaluated; it runs in the database's restricted scripting environment, which can be used to extract data or exhaust the server's resources.
NoSQL injection typically shows itself on a test when an operator supplied in place of a value changes the result: a login that succeeds with $ne in the password field, a search that returns every record, or a response that differs when a $regex condition is true or false, which allows values to be extracted one character at a time. Scanners detect only some forms, so the weakness is usually confirmed manually by replaying requests with operator objects, arrays and changed data types.
Remediation
Make sure every value from outside the application reaches the database as a value of the expected type, never as query structure:
- Validate the type of each input before it is used in a query. Where a string or number is expected, reject objects and arrays and convert the value explicitly, so that operator syntax cannot be supplied.
- Reject or remove keys that begin with
$or contain.in any object built from user input, and validate request bodies against a strict schema that rejects unexpected properties. - Use the driver's or object mapper's query builder with fixed field names and operators, and never build query documents or query text by concatenating user input.
- Avoid operators that evaluate JavaScript, such as
$where,$functionand$accumulator, and disable server-side JavaScript execution in the database configuration where it is not needed. - Run the application with a database account limited to the collections and operations it needs.
As an interim measure, a web application firewall rule that blocks parameter names and JSON keys beginning with $ reduces exposure, but it can be bypassed. Confirm the fix by repeating the original requests with operator objects, arrays and type changes in each affected parameter, and checking that they are rejected or treated as literal values.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.