Home / patterns

NoSQL operator injection: login and search filters built from raw JSON

A young developer at a home desk at night studying blurred curly-brace JSON on an unbranded monitor, a sticky note with a crossed-out dollar sign on the bezel and a coffee mug under a desk lamp, no brand logos
Quick answer: AI-generated Express and Next.js routes often pass req.body or query-string values straight into a MongoDB filter, like User.findOne({ email: req.body.email, password: req.body.password }). If a request sends an object such as {"$ne": null} instead of a string, the database treats it as an operator, which can bypass a login or return records the user should not see. MITRE files this under CWE-943; PortSwigger calls it NoSQL operator injection. The fix is to accept only the types you expect, sanitize filters, and never put user input in $where.

Last updated November 12, 2026

Steps

  1. Search the code for database calls that take request data directly: find, findOne, updateOne, deleteMany, aggregate and countDocuments with req.body, req.query, searchParams or JSON from a Server Action or API route inside the filter.
  2. Validate every input’s type before it reaches a query. With a schema library such as Zod, require z.string() (or a number, enum or ObjectId check) for each field, so an object like {"$ne": ""} is rejected instead of passed through.
  3. If you use Mongoose, turn on the sanitizeFilter option (mongoose.set('sanitizeFilter', true)) or call mongoose.sanitizeFilter() on filters built from user input. It wraps nested objects whose keys start with $ in $eq; mark known-safe operators with mongoose.trusted().
  4. Never build $where clauses, mapReduce functions or other server-side JavaScript from user input. Remove $where entirely if a normal field query can do the job.
  5. Allow-list the field names a client may filter or sort by. Do not let the request choose arbitrary keys, and reject keys that start with $ or contain dots.
  6. Fix login specifically: look the user up by a validated string email only, then compare the password with your password-hashing library’s verify function in code. Never put the password itself in the database filter.
  7. Retest on your own app: send a login request with a JSON body that uses an object instead of a string for the password and confirm it fails with a normal validation error.

Why it matters

CWE-943 (Improper Neutralization of Special Elements in Data Query Logic) covers injection into query languages beyond SQL, and MITRE lists NoSQL injection as an alternate term, citing real CVEs such as a password reset processed through Mongoose’s find function. PortSwigger’s Web Security Academy explains the two forms: syntax injection, where input breaks the query string, and operator injection, where input adds operators like $ne, $in, $regex or $where. Operator injection can bypass authentication, extract data character by character, or slow the server with injected JavaScript. Because JSON bodies happily carry nested objects, AI-written handlers that skip type checks are an easy fit for this bug. This page is for checking and fixing your own app.

Sources

MITRE CWE-943: Improper Neutralization of Special Elements in Data Query Logic — https://cwe.mitre.org/data/definitions/943.html

PortSwigger Web Security Academy: NoSQL injection — https://portswigger.net/web-security/nosql-injection

Mongoose API docs: sanitizeFilter() and trusted() — https://mongoosejs.com/docs/api/mongoose.html

Checked at the source on November 10, 2026.

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.