Security rules
NGB080: NoSQL injection in server code
Reports MongoDB / Mongoose queries in server code whose filter takes request values that can be objects — the request body, qs-parsed query strings and JSON cookies — without converting them to a primitive: User.findOne({ email: req.body.email }), collection.find(req.query), and any $where built from request data..
| Property | Value |
|---|---|
| Rule ID | NGB080 |
| Category | Angular SSR security |
| Default severity | high |
| CWE | CWE-943 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports MongoDB / Mongoose queries in server code whose filter takes request values that can be objects — the request body, qs-parsed query strings and JSON cookies — without converting them to a primitive: User.findOne({ email: req.body.email }), collection.find(req.query), and any $where built from request data.
Why it matters
Express parses ?password[$ne]=x and JSON bodies into objects. Passed into a query filter, { "$ne": null } matches every document: login checks are bypassed and records of other users are returned or modified (operator injection). $where evaluates JavaScript on the database server.
Detection behavior and supported patterns
Model.find/findOne/updateOne/deleteMany/findOneAndUpdate/countDocuments/exists …(filter)on Mongoose models or MongoDB collections (typed receivers, or files importingmongoose/mongodb)- The whole filter from request data (
find(req.body)) or a top-level filter property from request body / query / cookies (high) $wherewith request data in its value (critical)- Not reported:
String(x)/Number(x)/ template literals /x + ""(at the use or in theconstinitializer),mongo-sanitize, an appliedexpress-mongo-sanitizemiddleware, nested operator objects such as{ email: { $eq: v } }, route params and headers (always strings)
Example
const user = await User.findOne({ email: req.body.email, password: req.body.password });Safer alternative
const user = await User.findOne({ email: String(req.body.email) });const ok = user && await bcrypt.compare(String(req.body.password), user.passwordHash);Remediation
Convert request values to the expected primitive (String(req.body.email)), validate the request schema, wrap untrusted values in { $eq: value }, enable Mongoose sanitizeFilter or apply express-mongo-sanitize. Never build $where from request data.
False-positive considerations
- Requests validated by a schema middleware that guarantees strings are reported; add an explicit
String()conversion or document an exception. - Mongoose
sanitizeFilter: truemakes operator injection ineffective for that query or connection.
Known limitations
- Only filters written as object literals (or passed whole) at the call are inspected; filters assembled elsewhere are traced only as a whole.
- Schema-level casting (Mongoose casts to the schema type for many paths, and
sanitizeFilter) is not evaluated — such queries are reported with the same confidence. - Validation libraries (zod, class-validator, joi) applied in middleware are not recognized.
References
Run it
ngbastion explain NGB080