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 (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 (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 — 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 ($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 (writeup).
  • Password-reset bypass — token: {"$ne": null} on a Mongo-backed reset endpoint resets any account without the token (Intigriti).
  • Prisma on PostgreSQL — password = {"not": ""} returns the user on email match alone, defeating the password check despite a relational backend and strict schema (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.

Sources