Skip to main content

SKY-D238 · Security in code

NoSQL injection

High

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/reset.ts · TypeScriptReported on line 11
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

src/reset.ts · TypeScript
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

src/products.js · JavaScriptReported on line 9
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

src/products.js · JavaScript
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

  1. Checks files that import a MongoDB library (mongoose, mongodb, @nestjs/mongoose, @typegoose/typegoose, mongojs or monk), and elsewhere queries on a collection-shaped receiver (db.ordersCollection, db.collection('x'), a DAO's usersCol, as with MarsDB or a wrapper module) or with a literal $where key. Skips a file that calls express-mongo-sanitize or turns on Mongoose's sanitizeFilter option, which strip or escape $ keys.
  2. Also reports, without request proof, a $where string built from a parameter of the enclosing function, as in a DAO method that receives the request value from its caller.
  3. 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, distinct and similar calls, and to an aggregate $match stage.
  4. 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: Express req.body, req.query and JSON cookies, parsed bodies and JSON.parse() results. Route params, headers and other plain text are not reported.
  5. Reports any request data in a $where condition, text included.
  6. 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.

src/reset.ts · TypeScript · the fix, against the flagged file
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 ValidationPipe or 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()) in server.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 // noqa does not hide HIGH security findings.

    TypeScript
    const user = await User.findOne(filter); // skylos: ignore[SKY-D238] zod-checked
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D238"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.