Skip to main content
HighCodeOWASP A03:2021CWE-89

How to fix: Mongoose find with user JSON

Model.find(JSON.parse(req.body)) passes a user-supplied object directly into a Mongo query. Attackers can inject query operators like $where, $regex, or $ne to bypass access control.

Why it matters

Attacker injects NoSQL operators to bypass filters, leak other users' documents, or trigger $where JavaScript.

The `code-sqli-mongoose-find-user` check verifies that the server does not expose the `sqli-mongoose-find-user` weakness in the code category. The detection runs against the response headers, body, or auxiliary probes (DNS, TLS, async fetch) as appropriate.

How to fix it

  1. 1Use an explicit schema (Joi/Zod) to strip unknown keys before reaching find().
  2. 2Apply express-mongo-sanitize or mongo-sanitize to remove $-prefixed operators.
  3. 3Validate types: ensure ids are strings, numbers, or ObjectIds only.

Code examples

Use Mongoose with strict equality, not raw where

TypeScript
// BAD: using $where with user input
User.findOne({ $where: `this.name == "${name}"` });

// GOOD: standard Mongoose query (auto-escaped)
User.findOne({ name: name, active: true });

References

Detected automatically by VulnRadar. The check id is code-sqli-mongoose-find-user, stable across every scan so you can filter or diff on it.

See every code check

Questions

How severe is Mongoose find with user JSON?
VulnRadar rates this high severity in the code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id code-sqli-mongoose-find-user, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Mongoose find with user JSON map to?
It maps to CWE-89. In the OWASP Top 10 it falls under A03:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for mongoose find with user json

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: Mongoose find with user JSON | VulnRadar