SKY-D238 · Security in code
NoSQL injection
MongoDB queries are objects, and keys that start with $ are operators. When a JSON body or a query string is parsed, a field that should be text can arrive as an object such as {"$ne": null}. SKY-D238 reports request data that can be an object reaching a MongoDB or Mongoose query filter, and request data in a $where JavaScript condition.
- Languages
- TypeScript/JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
A password-reset route that runs findOne({ resetToken: req.body.token }) matches the first user who has any token when the body sends {"token": {"$ne": null}}. That is an account takeover with one request. $where text built from input runs JavaScript inside the database.
What Skylos flags
import express from "express";import mongoose from "mongoose"; const userSchema = new mongoose.Schema({ email: String, resetToken: String });const User = mongoose.model("User", userSchema); const app = express();app.use(express.json()); app.post("/reset", async (req, res) => { const user = await User.findOne({ resetToken: req.body.token }); if (!user) { res.status(404).end(); return; } res.json({ email: user.email });}); app.listen(3000);What Skylos leaves alone
import express from "express";import mongoose from "mongoose"; const userSchema = new mongoose.Schema({ email: String, resetToken: String });const User = mongoose.model("User", userSchema); const app = express();app.use(express.json()); app.post("/reset", async (req, res) => { const token = String(req.body.token ?? ""); const user = await User.findOne({ resetToken: token }); if (!user) { res.status(404).end(); return; } res.json({ email: user.email });}); app.listen(3000);Why Skylos leaves it alone
String() turns any value, objects included, into plain text, so the filter can only compare resetToken with a string. Skylos treats the result of String(), template literals and .toString() as text.
More languages
JavaScript
Flagged
const express = require("express");const mongoose = require("mongoose"); const Product = mongoose.model("Product", new mongoose.Schema({ price: Number }));const app = express(); app.get("/products", async (req, res) => { const max = req.query.max; const items = await Product.find({ $where: `this.price < ${max}` }); res.json(items);}); app.listen(3000);Not flagged
const express = require("express");const mongoose = require("mongoose"); const Product = mongoose.model("Product", new mongoose.Schema({ price: Number }));const app = express(); app.get("/products", async (req, res) => { const max = Number(req.query.max); const items = await Product.find({ price: { $lt: max } }); res.json(items);}); app.listen(3000);Why Skylos leaves it alone
The condition uses the $lt operator instead of JavaScript text, and Number() turns the input into a number. Neither a number nor a fixed operator can change the query's shape.
How Skylos decides
- Checks files that import a MongoDB library (
mongoose,mongodb,@nestjs/mongoose,@typegoose/typegoose,mongojsormonk), and elsewhere queries on a collection-shaped receiver (db.ordersCollection,db.collection('x'), a DAO'susersCol, as with MarsDB or a wrapper module) or with a literal$wherekey. Skips a file that callsexpress-mongo-sanitizeor turns on Mongoose'ssanitizeFilteroption, which strip or escape$keys. - Also reports, without request proof, a
$wherestring built from a parameter of the enclosing function, as in a DAO method that receives the request value from its caller. - Follows request data inside one route handler (Express, Fastify, Koa, Hapi, NestJS, Next.js and others) to the filter of
find,findOne,findOneAndUpdate,updateOne,deleteMany,countDocuments,where,distinctand similar calls, and to anaggregate$matchstage. - Reports HIGH when the filter is the request object itself (
find(req.body)), a spread of it, or a field whose value can be an object: Expressreq.body,req.queryand JSON cookies, parsed bodies andJSON.parse()results. Route params, headers and other plain text are not reported. - Reports any request data in a
$wherecondition, text included. - Stops at
String(),Number(),typeof x === "string"checks whose failing branch returns or throws, ObjectId checks, and calls into other modules such as a zod.parse()validator.
How to fix it
Check that each value has the type you expect before it goes into a filter, or validate the whole body with a schema library. Never build $where text from input; use query operators instead.
import express from "express";import mongoose from "mongoose"; const userSchema = new mongoose.Schema({ email: String, resetToken: String });const User = mongoose.model("User", userSchema); const app = express();app.use(express.json()); app.post("/reset", async (req, res) => {Removed: const user = await User.findOne({ resetToken: req.body.token });Added: const token = req.body.token;Added: if (typeof token !== "string") {Added: res.status(400).end();Added: return;Added: }Added: const user = await User.findOne({ resetToken: token }); if (!user) { res.status(404).end(); return; } res.json({ email: user.email });}); app.listen(3000);When it can be wrong
- A global validation layer, such as a NestJS
ValidationPipeor an Express middleware that checks every body, is not seen in an untyped handler, so its routes can still be reported. - Sanitising middleware set up in another file, such as
app.use(mongoSanitize())inserver.ts, is not seen when the route lives in its own file.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line and say why. A bare
// noqadoes not hide HIGH security findings.TypeScript const user = await User.findOne(filter); // skylos: ignore[SKY-D238] zod-checkedAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D238"]turns the rule off for the whole project.