NewApp Monitoring is liveSign up free
AgentbeamBeta
Open-source package vulnerability · npm

CVE-2026-125120: Mongoose: sanitizeFilter not applied on countDocuments

criticalCVSS 3.1 9.8GHSA-pcrw-h46h-c93h

Mongoose skipped `sanitizeFilter` on `countDocuments()` queries, so apps that rely on it to stop NoSQL selector injection were still open on count queries. An unauthenticated remote attacker can send operators like `$where` or `$ne` in request data. Fixed in mongoose 6.13.11, 7.8.12, 8.24.3 and 9.9.2.

Published Nov 10, 2026 · Analysis Nov 10, 2026

What an attacker can do

sanitizeFilter is Mongoose's guard against query selector injection. When on, it wraps user-supplied operator objects in $eq so they match literally instead of acting as operators. In the affected releases countDocuments() never applied it. The fix in lib/query.js routes _countDocuments and the deprecated _count through _castConditions(), where sanitizing happens. The same commit makes cursor() cast conditions the same way.

An attacker needs only network access to an endpoint that passes request JSON or query-string objects straight into Model.countDocuments(filter). A body like {"role": {"$ne": null}} then changes the filter. The result is a count oracle: the attacker can probe field values one condition at a time. The advisory scores it 9.8 (C:H/I:H/A:H, CWE-943). Whether write impact follows depends on what the app does with the count, for example gating logins or quotas on it. A $where clause in the test suite shows operator injection is the case the patch covers.

Who is affected

Four npm release lines are affected: mongoose before 6.13.11, 7.x before 7.8.12, 8.x before 8.24.3, and 9.x before 9.9.2. Your app is exposed only if it calls countDocuments() (or the deprecated count()) with filters built from untrusted input and relies on sanitizeFilter instead of validating input. Check the installed version:

npm ls mongoose

mongoose

Ecosystem
npm
Affected
< 6.13.11
Fixed in
6.13.11
Check
npm ls mongoose

mongoose

Ecosystem
npm
Affected
>= 7.0.0, < 7.8.12
Fixed in
7.8.12
Check
npm ls mongoose

mongoose

Ecosystem
npm
Affected
>= 8.0.0, < 8.24.3
Fixed in
8.24.3
Check
npm ls mongoose

mongoose

Ecosystem
npm
Affected
>= 9.0.0, < 9.9.2
Fixed in
9.9.2
Check
npm ls mongoose

How to fix it

Upgrade to the patched version on your line: 6.13.11, 7.8.12, 8.24.3 or 9.9.2.

npm install [email protected]

The advisory also gives a workaround: a pre('countDocuments') middleware that calls mongoose.sanitizeFilter(this.getFilter()), registered through a global plugin so it covers every schema. Also validate and cast request input before it reaches a filter, so it is never an object when you expect a string.

How to tell if you were hit

Review code for countDocuments( calls whose filter comes from req.body, req.query or similar. In access logs and application logs, look for request parameters or JSON containing $ne, $gt, $regex, $where or $or aimed at endpoints that return counts, such as pagination totals or availability checks. Repeated requests to one endpoint with slowly varying operator values suggest probing.

Original advisory

From the GitHub Advisory Database (CC BY 4.0).

Impact

sanitizeFilter is Mongoose's mechanism for protecting against query selector injection attacks. In older versions of Mongoose, sanitizeFilter was not applied for countDocuments() queries.

Patches

Patches have been released for all supported Mongoose major release lines. Please upgrade to the following versions depending on which major version of Mongoose applications are using.

  1. ^9.9.2
  2. ^8.24.3
  3. ^7.8.12
  4. ^6.13.11

Workarounds

Middleware:

mongoose.plugin(function() {
  schema.pre('countDocuments', function() {
    // This will always apply `sanitizeFilter` on `countDocuments`
    mongoose.sanitizeFilter(this.getFilter());
  });
});

Credited to koyokr.

Questions

Is my app affected if I never enabled sanitizeFilter?

This advisory is about sanitizeFilter not being applied on countDocuments() when you turned it on. If you never enabled it, you have no protection on any query. Upgrading does not change that, so validate input yourself.

Which Mongoose versions fix CVE-2026-125120?

6.13.11, 7.8.12, 8.24.3 and 9.9.2, one per supported major line. Mongoose 5 and earlier get no listed fix.

Is there a workaround besides upgrading?

Yes. Register a global plugin with a pre('countDocuments') hook that calls mongoose.sanitizeFilter(this.getFilter()), as the advisory shows.

References

How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. The analysis is written by an AI model from the sources listed above and checked by automated rules before it is published. Check versions against the vendor's advisory before you act. Methodology

Coverage on the Beam blog

More npm vulnerabilities

All npm entries