On this page (7 sections)
Mongoose, the popular MongoDB object modeling library for Node.js, shipped with a gap in its query selector injection defense. According to the GitHub advisory GHSA-pcrw-h46h-c93h, the sanitizeFilter option was not applied to countDocuments() queries in older versions. The issue is tracked as CVE-2026-125120 and rated critical, with a CVSS score of 9.8.
If you run a Node.js application that uses Mongoose and relies on sanitizeFilter to protect against NoSQL operator injection, check your version now. Patched releases exist for every supported major line.
What happened
Mongoose offers a setting called sanitizeFilter. When it is on, objects in a query filter that look like MongoDB operators (for example keys starting with a dollar sign) are treated as literal values instead of operators. This is Mongoose's built-in guard against query selector injection.
The advisory says that in older versions this protection was simply not applied for countDocuments() queries. The maintainer published the advisory on November 2, 2026, the National Vulnerability Database published the CVE record on November 9, and GitHub reviewed and updated the advisory on November 10. The reporter is credited as koyokr.
Technical details
The weakness class is CWE-943: a query is built from input without neutralizing special elements that can change its logic. In MongoDB, filters are objects, so a value that should be a plain string can instead arrive as an object containing an operator. If that object reaches the database as an operator, the query matches different documents than the developer intended.
Per the CVE description, an application that enables sanitizeFilter and passes attacker-controlled filter values to countDocuments() can end up processing operator objects as operators. The count then includes documents that should not match. The description notes this can potentially undermine authorization or application logic that depends on the count.
Who is affected
You are realistically exposed only if all of these are true:
- Your app depends on mongoose in one of the vulnerable version ranges.
- You rely on sanitizeFilter (globally or per query) as a defense.
- Request data, such as query strings or JSON bodies, flows into the filter passed to countDocuments().
- Something meaningful depends on the result, such as pagination, quota checks, existence checks, or access decisions.
Apps that do not enable sanitizeFilter were already not protected by it, so this specific bug changes nothing for them. They may still be open to operator injection through other query methods and deserve a separate review of how input is validated.
How to check your exposure
First, find which Mongoose version each project actually resolves. This read-only command lists the installed mongoose package and any copies pulled in by other dependencies. Run it in each project directory and compare the output against the fixed versions.
npm ls mongooseNext, search your code for the two things that matter: where sanitizeFilter is enabled and where countDocuments() is called. This grep prints matching lines so you can see whether request data reaches those filters.
grep -rn --include=*.js --include=*.ts --exclude-dir=node_modules -e sanitizeFilter -e countDocuments .| Release line | Vulnerable | Upgrade to |
|---|---|---|
| 6.x | below 6.13.11 | 6.13.11 |
| 7.x | 7.0.0 up to below 7.8.12 | 7.8.12 |
| 8.x | 8.0.0 up to below 8.24.3 | 8.24.3 |
| 9.x | 9.0.0 up to below 9.9.2 | 9.9.2 |
What to do now
- Inventory every Node.js service and package that depends on mongoose, including monorepo workspaces and container images.
- Upgrade to the fixed version for your major line: 6.13.11, 7.8.12, 8.24.3 or 9.9.2.
- Commit the updated lockfile and rebuild any container images so the new version is what actually runs.
- If you cannot upgrade immediately, add the vendor's workaround middleware (below) and plan the upgrade.
- Review every countDocuments() call that receives request-derived input and validate types (for example, require strings) before building the filter.
- Redeploy, then confirm the running version with npm ls mongoose inside the deployed environment.
To upgrade within your current major line, install the matching fixed release. This changes your package.json and lockfile, so run your tests before deploying. Example for the 8.x line:
npm install mongoose@8.24.3Workaround if you cannot upgrade yet
The advisory provides a global plugin that forces sanitizeFilter to run before every countDocuments() query. It must be registered before your models are compiled, so it applies to all schemas. The snippet below is taken from the advisory:
mongoose.plugin(function() {
schema.pre('countDocuments', function() {
// This will always apply `sanitizeFilter` on `countDocuments`
mongoose.sanitizeFilter(this.getFilter());
});
});Detection and monitoring
The research does not describe any known indicators of exploitation, so there is no signature to hunt for. You can still look for suspicious input shapes and strengthen your pipeline:
- Search application and reverse proxy logs for request parameters containing dollar-prefixed keys, such as bracketed operator syntax in query strings or JSON bodies with keys beginning with a dollar sign, on endpoints that return counts.
- Watch for unusual jumps in counts, pagination totals or quota results that do not match real data.
- Add npm audit or a dependency scanner to CI so advisories like GHSA-pcrw-h46h-c93h fail builds before release.
- Validate request types at the edge of your app with a schema validator so objects never arrive where strings are expected.
A web application firewall can add a layer of filtering for operator-style payloads, though it is not a substitute for patching. If you want to understand that trade-off, see what a WAF is. For general guidance on keeping dependencies and exposed apps in order, the small business website security guide is a good starting point.
Timeline
| Date | Event |
|---|---|
| Nov 2, 2026 | Advisory published to Automattic/mongoose by the maintainer |
| Nov 9, 2026 | Published by the National Vulnerability Database |
| Nov 10, 2026 | Added to the GitHub Advisory Database, reviewed and updated |
Sources
This article is based on the public sources below. Details can change as vendors and researchers publish more; check the original advisories before acting on version numbers.
