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

Last updated November 12, 2026
Steps
- 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.
- 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.
- 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().
- 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.
- 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.
- 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.
- 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
- SQL built from strings in raw queries (SQL injection)
- Mass assignment: users can set fields like role or is_admin
- Prototype pollution from deep-merging user JSON into objects
- Regex denial of service (ReDoS): one crafted input that freezes your app
- Login and signup messages that reveal which accounts exist (account enumeration)
- Running AI or user output with eval, exec or new Function (code injection)
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 StorenTechVibe 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.