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.

The advisory and CVE text describe the impact as a wrong count influencing application logic. The 9.8 score reflects the CVSS vector assigned in the record (network, low complexity, no privileges, high confidentiality, integrity and availability). Real-world impact depends heavily on how your code uses the count, so treat the score as a prompt to patch, not as a statement about your specific app.

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.

List installed Mongoose versions (run in the project root)Shell
npm ls mongoose

Next, 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.

Find sanitizeFilter and countDocuments usage in source (run in the project root)Shell
grep -rn --include=*.js --include=*.ts --exclude-dir=node_modules -e sanitizeFilter -e countDocuments .
Vulnerable and fixed versions per Mongoose release line
Release lineVulnerableUpgrade to
6.xbelow 6.13.116.13.11
7.x7.0.0 up to below 7.8.127.8.12
8.x8.0.0 up to below 8.24.38.24.3
9.x9.0.0 up to below 9.9.29.9.2

What to do now

  1. Inventory every Node.js service and package that depends on mongoose, including monorepo workspaces and container images.
  2. Upgrade to the fixed version for your major line: 6.13.11, 7.8.12, 8.24.3 or 9.9.2.
  3. Commit the updated lockfile and rebuild any container images so the new version is what actually runs.
  4. If you cannot upgrade immediately, add the vendor's workaround middleware (below) and plan the upgrade.
  5. Review every countDocuments() call that receives request-derived input and validate types (for example, require strings) before building the filter.
  6. 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:

Upgrade Mongoose on the 8.x line (swap the version for your major line)Shell
npm install mongoose@8.24.3
Caution. Upgrading a database library can change behavior. Run your test suite and a staging check before deploying to production, especially if you are jumping several minor versions.

Workaround 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:

Vendor workaround: always apply sanitizeFilter on countDocuments (from the advisory)JavaScript
mongoose.plugin(function() {
  schema.pre('countDocuments', function() {
    // This will always apply `sanitizeFilter` on `countDocuments`
    mongoose.sanitizeFilter(this.getFilter());
  });
});
Tip. Treat the workaround as a stopgap. Upgrading removes the need to carry custom middleware and keeps you on a supported line.

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

Dates as listed in the GitHub advisory
DateEvent
Nov 2, 2026Advisory published to Automattic/mongoose by the maintainer
Nov 9, 2026Published by the National Vulnerability Database
Nov 10, 2026Added to the GitHub Advisory Database, reviewed and updated
As of this writing, the research for this story consists of the GitHub advisory and the CVE record. No independent technical analysis or reports of in-the-wild exploitation were found, so details beyond the vendor's description remain unconfirmed.

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.

  1. CVE-2026-125120 - GitHub Advisory Databasegithub.com
  • Mongoose
  • Node.js
  • NoSQL injection
  • npm
  • CVE-2026-125120