CVE-2026-125120: Mongoose: sanitizeFilter not applied on countDocuments
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 mongoosemongoose
- 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.
^9.9.2^8.24.3^7.8.12^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
sanitizeFilternot being applied oncountDocuments()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 callsmongoose.sanitizeFilter(this.getFilter()), as the advisory shows.
References
Read for this analysis:
- https://github.com/Automattic/mongoose/security/advisories/GHSA-pcrw-h46h-c93h
- https://github.com/Automattic/mongoose/commit/7e912af3fac52cb0eea4cd71e24d65318916831a
- https://nvd.nist.gov/vuln/detail/CVE-2026-125120
- https://github.com/Automattic/mongoose/commit/8b10d5760bb181f5bf5d01495cde161c8aed0557
- https://github.com/Automattic/mongoose/commit/b1df045aab076b740fdaec14a15a44c694b85b7d
- https://github.com/Automattic/mongoose/commit/b5003d3915db1cf9209c38a4b93cf3a92cddd710
- https://github.com/Automattic/mongoose/releases/tag/6.13.11
- https://github.com/Automattic/mongoose/releases/tag/7.8.12
- https://github.com/Automattic/mongoose/releases/tag/8.24.3
- https://github.com/Automattic/mongoose/releases/tag/9.9.2
- https://github.com/advisories/GHSA-pcrw-h46h-c93h
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
- Apache Roller CVE-2026-129448: CISA KEV XML-RPC Deserialization RCE
- SAP CVE-2025-42884: CISA KEV JNDI Injection in NetWeaver Enterprise Portal
- Mongoose CVE-2026-125120: sanitizeFilter Skipped on countDocuments
- React CVE-2026-126187: Unauthenticated RSC Remote Code Execution and npm Patches
- CVE-2026-63471 and CVE-2026-63469: fastapi-code-generator Import-Time RCE
More npm vulnerabilities
All npm entriesCVE-2026-125116
Mongoose: Discriminator key values matching inherited properties (e.g. __t: 'constructor') bypass schema casting and can poison collections
medium4.8Nov 10, 2026CVE-2026-125117
Mongoose walks into inherited properties when hydrating, setting, and merging, allowing overwrites of built-in functions (denial of service) and shared schema state
high8.2Nov 10, 2026CVE-2026-125118
Mongoose: Prototype pollution via Schema.prototype.virtual()
medium6.5Nov 10, 2026CVE-2026-125119
Mongoose: Connection-level sanitizeFilter not applied
high7.5Nov 10, 2026CVE-2026-73562
Mongoose: Prototype pollution in mongoose update casting via __proto__-prefixed dotted path (Schema._getSchema/path getter)
medium6.5Jul 24, 2026CVE-2026-42334
Mongoose's Improper Sanitization of $nor in sanitizeFilter May Allow NoSQL Injection
high7.5May 5, 2026
