NoSQL injection: when input rewrites a query without any SQL
NoSQL injection is an attack in which input sent to an application changes the meaning of a query to a non-relational database. There is no SQL to break, so the usual quote-and-comment tricks are not the point. The query is often an object, and the input can supply an object of its own. Type checks and a query API that does not accept raw input close most of it.
Quick facts
- Weakness
- CWE-943: improper neutralization of special elements in data query logic. It is the parent of CWE-89 (SQL injection)
- OWASP category
- A05:2025 Injection; the Web Security Testing Guide has a page on it
- Two classes
- Syntax injection (breaking the query's structure) and operator injection (supplying query operators), as PortSwigger splits them
- Main fix
- Check that each value has the expected type, and build queries through the driver or ODM rather than from raw input
- Does a WAF stop it?
- Partly. A filter can flag operator-shaped keys and script fragments in a request. It cannot know which types your code expects
Definitions follow CWE-943, the OWASP WSTG page and the OWASP NoSQL Security Cheat Sheet. MongoDB is used as the running example because its documentation covers the topic directly; other document and key-value stores differ in detail.
How it differs from SQL injection
In SQL injection the query is a string, and the attack works by getting input to be read as SQL. Many NoSQL databases do not have one query language. MongoDB takes its queries as structured documents, so a lookup is not a sentence that can be broken by a stray quote; it is an object with fields and conditions.
That changes where the weakness sits. The OWASP Web Security Testing Guide notes that NoSQL databases do not use SQL syntax, yet remain open to injection, and that input crafted for the database's own API may pass sanitisation written for HTML special characters. A filter that strips quotes and angle brackets has not touched the real problem.
CWE-943 is deliberately wider than SQL. Its description covers any product that builds a query to read or change data in a data store and fails to neutralise the elements that can alter the query's logic. CWE-89, the SQL case, is one of its children. Another way a JSON body changes an object is prototype pollution.
Syntax injection and operator injection
The split below comes from PortSwigger's Web Security Academy. The OWASP guide does not use these names; it shows two MongoDB scenarios that line up with them.
- Syntax injection. The application builds a query as text, for instance a string handed to a JavaScript evaluator, and input breaks out of the value it was meant to be. The idea is the SQL one carried over: the database cannot tell the developer's text from the visitor's.
- Operator injection. The query is a structured object, and input supplies a query operator in place of a plain value. In MongoDB, operators are keys that start with a dollar sign. If a login handler expects a string for a password field and receives an object holding a comparison operator, the condition it builds is no longer the one the developer wrote.
The second class is the one specific to this family. It needs no special characters at all; what changes is the type of the value. A JSON body makes it easy, because JSON has objects and arrays natively. Form-encoded data can get there as well: some server-side parsers turn bracketed parameter names into nested objects, so a form post can arrive in the code as the same structure a JSON body would. When a framework does that, the code receives an object even though the form only ever held text. The OWASP guide also points to HTTP parameter pollution as a way to create variables that collide with operator names in PHP.
Server-side JavaScript
The most serious NoSQL cases are the ones where the database runs code. The OWASP guide's first MongoDB example passes unsanitised input into $where, an operator that evaluates JavaScript, and says the result can be arbitrary code execution rather than a query error. The Cheat Sheet adds that a client-controlled $where expression runs untrusted JavaScript inside the database and can use excessive resources.
MongoDB's documentation, in its FAQ on query injection, lists the operations that let you run JavaScript expressions on the server: $where, mapReduce, $accumulator and $function. It says most operations can be written without JavaScript, and that server-side execution can be switched off with --noscripting or by setting security.javascriptEnabled to false in the configuration file (MongoDB FAQ).
Code execution inside the database is a different problem from code execution on the application server, which is command injection territory. They can share a cause, though: input reaching an interpreter.
What it looks like in a request
Traces are structural. A field that normally carries a short string arrives as a nested object. A key beginning with a dollar sign shows up in a body or in bracketed parameter names. A search box receives something that reads as a script expression rather than a word. Repeated requests to a login or lookup endpoint with the same shape and a changing value are what automated probing leaves behind.
False positives exist here too. Some APIs accept operators on purpose: a search endpoint may let clients send a range or a list filter. A rule that flags every operator-looking key would fire on those clients every day, so those endpoints need to be known before anything is refused. Reading the log first is the point of monitor mode.
How to prevent NoSQL injection
The first two items close the hole. The rest narrow what a mistake can reach.
- Check types before building the query. If a field should be a string, reject anything else. The Cheat Sheet says a query object alone is not enough, because a client that supplies an object where a scalar is expected can carry operators into it.
- Validate against a schema. Define the fields and types each endpoint accepts and allow-list the fields clients may query. The Cheat Sheet prefers this to scanning the body for bad strings, which blocks legitimate values and does not check which fields are allowed.
- Go through the driver or ODM. Use the query objects or mapping layer the library offers instead of assembling query strings. As with ORMs on the SQL side, the risk returns wherever code steps outside the layer into raw queries.
- Keep client-controlled operators out. Do not pass a client's JSON through as a query. Where an operator such as a regular expression is needed, validate it and allow it only on the fields that need it.
- Turn off server-side JavaScript when nothing uses it, and avoid
$where,mapReduceandeval-style execution. - Give the database account the least privilege it needs, so a query that goes wrong reads less and changes nothing it should not.
PortSwigger's advice is consistent with this: use parameters rather than concatenating input, restrict incoming keys to an approved list, and check the specific database's own security documentation, since defences vary by product.
Where a WAF helps and where it does not
A filter in front of the application reads each request before the code does. That gives it a few real handles on this attack.
- JSON bodies. A filter that parses JSON can look at keys as well as values, and keys that begin with a dollar sign are worth a record on any endpoint that does not document them. Flagging them in monitor shows which endpoints receive them and from whom.
- Script fragments. Text that looks like a JavaScript expression in a field meant for names or search terms is worth a record.
- Probing volume. Many near-identical requests to a login endpoint can be slowed with rate limiting, which limits guessing even when the query is flawed.
It has limits. A WAF does not know that your handler expects a string, so an operator that is legitimate on one endpoint and dangerous on another looks identical on the wire. Bodies in a format the filter does not parse, a query assembled from values spread across several requests, and stored input that is used later all give it little to match. Most of all it leaves the unchecked type in the code, so a different shape of the same input may get through. The same limit applies to SQL injection; the OWASP Core Rule Set has rules for it, and the Cheat Sheet's caution about body scanning applies to any filter.
The practical order: start in monitor, read what the activity log records, find the endpoints that accept operators legitimately, then move to block for the rest. Fix the type checks in the code in parallel; the filter buys time and the fix removes the cause. The wider category is covered under OWASP Injection, and JSON-heavy interfaces more generally under API security.
Questions
What is NoSQL injection?
NoSQL injection is an attack in which input sent to an application changes the meaning of a query to a non-relational database, letting an attacker read data or bypass checks the developer intended to enforce.
How is NoSQL injection different from SQL injection?
SQL injection breaks a query written as a string. NoSQL queries are often structured objects, so input can supply a query operator in place of a plain value, which needs no special characters. Both are covered by CWE-943, of which SQL injection (CWE-89) is a child.
What are syntax injection and operator injection?
In syntax injection, input breaks out of a value in a query built as text. In operator injection, input supplies a query operator, such as a MongoDB key starting with a dollar sign, so the query behaves differently from what the developer wrote.
How do you prevent NoSQL injection?
Check that each value has the expected type, validate requests against a schema, build queries through the driver or ODM instead of from raw input, keep client-controlled operators out, and turn off server-side JavaScript if it is not needed.
Can a WAF stop NoSQL injection?
A WAF can flag operator-shaped keys in JSON bodies and script fragments in fields, which helps while the code is fixed. It cannot know which types the application expects, so it does not replace type checks and schema validation.