# NoSQL Injection
> Injection against document/query-operator databases (MongoDB and friends) where untrusted input becomes a query *operator* or server-side JavaScript rather than a value — and which also hits relational ORMs that expose the same operator API.
## How it works
NoSQL queries are often structured objects, not strings, so the classic "break out of the quotes" model doesn't apply. The vulnerability is **operator injection**: an input expected to be a scalar is supplied as an object like `{"$ne": null}` or `{"$gt": ""}`, changing the query's meaning ([[raw/intigriti.com/nosql-injection-advanced-exploitation-guide-4793|Intigriti guide]]). A login that does `findOne({ email, password })` becomes an auth bypass when `password` is `{"$ne": null}` — the predicate now matches any non-null password. Same root cause as SQLi (unsanitized input concatenated/merged into a query), different grammar.
Crucially this is **not MongoDB-specific**: Prisma, Sequelize and TypeORM expose query operators even on PostgreSQL/MySQL, and validate *field names* but accept operator objects as valid input to multi-record functions (`findFirst`, `findMany`, `updateMany`, `deleteMany`). Prisma's string-based operators make it especially easy to reach by accident ([[raw/aikido.dev/prisma-and-postgresql-vulnerable-to-nosql-injection-aikido-4395|Prisma + PostgreSQL operator injection]]). Single-record functions (`findUnique`) reject objects and are safe.
## Techniques / payloads
- **Operator injection** — `$ne`, `$gt`, `$gte`, `$regex`, `$exists`, `$eq`. Used for auth bypass and to widen filters (`(id gt 0)` returned other tenants' records in a real Synack finding — [[raw/synack.com/exploits-explained-nosql-injection-returns-private-information-4794|NoSQLi returns private info]]).
- **Form-encoded operator smuggling** — when the app parses `x-www-form-urlencoded`, bracket syntax injects operators without JSON: `token[$ne]=null`. Many body parsers build nested objects from `param[key]`.
- **Blind extraction via `$regex`** — anchor a regex to test a value prefix character-by-character, using a content differential as the oracle (the NoSQL analogue of boolean-blind SQLi).
- **Time-based and server-side JS via `$where`** — `$where` accepts a JavaScript expression; a conditional `sleep()` gives a timing oracle, and unsanitized input concatenated into a `$where` string allows arbitrary server-side JS and tautology auth bypass ([[raw/intigriti.com/nosql-injection-advanced-exploitation-guide-4793|$where exploitation]]).
- **Second-order NoSQLi** — stored operator payload executed when later read into a query (parallels [[Second-Order SQL Injection]]).
## Notable cases
- **Synack Red Team finding** — a `$filter` (OData-style `eq`/`gt`) parameter on `/CustomerLogins` leaked the administrator's email, username, password hash and phone number by swapping `eq 2` for `gt 0` ([[raw/synack.com/exploits-explained-nosql-injection-returns-private-information-4794|writeup]]).
- **Password-reset bypass** — `token: {"$ne": null}` on a Mongo-backed reset endpoint resets any account without the token ([[raw/intigriti.com/nosql-injection-advanced-exploitation-guide-4793|Intigriti]]).
- **Prisma on PostgreSQL** — `password = {"not": ""}` returns the user on email match alone, defeating the password check despite a relational backend and strict schema ([[raw/aikido.dev/prisma-and-postgresql-vulnerable-to-nosql-injection-aikido-4395|Aikido]]).
## Defenses
Reject non-scalar input where a scalar is expected: cast to string/validate type before it reaches the query. Disable or avoid `$where`/server-side JS. Prefer single-record ORM functions (`findUnique`) or operator-object APIs that can't be synthesized from JSON. Sanitize `param[key]` body parsing so bracket operators can't be smuggled. Tooling: **NoSQLMap** automates discovery.
## Related
- [[SQL Injection]]
- [[ORM Injection]]
- [[Second-Order SQL Injection]]
- [[Broken Access Control]]
## Sources
- [[raw/intigriti.com/nosql-injection-advanced-exploitation-guide-4793|NoSQL Injection: Advanced Exploitation Guide (Intigriti)]]
- [[raw/synack.com/exploits-explained-nosql-injection-returns-private-information-4794|Exploits Explained: NoSQL Injection Returns Private Information]]
- [[raw/aikido.dev/prisma-and-postgresql-vulnerable-to-nosql-injection-aikido-4395|Prisma and PostgreSQL Vulnerable to NoSQL Injection]]
- [[raw/github.com/sqli-payloads-classic-blind-error-based-time-based-waf-bypass-2835|SQLi/NoSQLi payloads and tools collection]]
- [[raw/github.com/web-attack-cheat-sheet-4992|Web Attack Cheat Sheet]]