Mongoose: `sanitizeFilter` bypass via MongoDB internal top-level query operators such as `$_internalSchemaXor`
Published 4 Dec 2026 · Updated 4 Dec 2026
Affected packages
- Affected
- <6.13.13
- Fixed in
- 6.13.13
npm install mongoose@6.13.13- Affected
- >=7.0.0 <7.8.14
- Fixed in
- 7.8.14
npm install mongoose@7.8.14- Affected
- >=8.0.0 <8.24.10
- Fixed in
- 8.24.10
npm install mongoose@8.24.10- Affected
- >=9.0.0 <9.12.2
- Fixed in
- 9.12.2
npm install mongoose@9.12.2Which mongoose does your project really install?
Works for private projects: the file is read in memory to run the scan and then dropped. Also reads yarn.lock, pnpm-lock.yaml or package.json.
Public repo?
Look after several JavaScript apps? Indie watches up to 15 repos, private ones too, for $5 a month. See Indie
Details
Summary
Mongoose's sanitizeFilter option is meant to block query selector injection. Before this fix, it only refused four top-level operators by name: $jsonSchema, $where, $expr and $text. Any other top-level $ operator was passed to MongoDB unchanged. That includes undocumented internal operators like $_internalSchemaXor and $_internalSchemaCond, which take sub-filters, and sanitizeFilter never inspected those sub-filters. An attacker who controls the keys of a query filter could use them to slip arbitrary query selectors like $ne or $regex past sanitizeFilter.
Details
With sanitizeFilter enabled, Mongoose:
- wraps any object value containing
$keys in$eq, so{ password: { $ne: null } }becomes{ password: { $eq: { $ne: null } } }; - recurses into
$and,$orand$nor; - throws on
$jsonSchema,$where,$exprand$textunless the value is wrapped inmongoose.trusted().
Every other top-level key starting with $ was left alone. MongoDB accepts several internal match expressions at the top level that contain nested filters, for example:
$_internalSchemaXor: [filter1, filter2]$_internalSchemaCond: [ifFilter, thenFilter, elseFilter]
Because sanitizeFilter doesn't recurse into these, the nested filters can contain any query selector.
The fix makes sanitizeFilter refuse every top-level key that starts with $, except $and, $or and $nor, which it recurses into, and values wrapped in mongoose.trusted(). Nested $and/$or/$nor filters are checked the same way. This also means sanitizeFilter now refuses:
$comment: a user-supplied comment ends up in MongoDB's profiler and logs. UseQuery.prototype.comment()instead, which sets the comment outside the filter.$sampleRate,$alwaysTrue,$alwaysFalse, and top-level$ref/$id/$db.
A top-level $-prefixed field name can't be queried with a plain equality filter in the first place: MongoDB rejects { $userId: 42 } with "unknown top level operator". So the fix doesn't prevent any query that previously worked.
Proof of concept
const User = mongoose.model('User', new mongoose.Schema({ username: String, password: String }));
await User.create({ username: 'admin', password: 'hunter2' });
// Attacker controls the filter, e.g. `User.findOne(req.body)`
const login = filter => User.findOne(filter).setOptions({ sanitizeFilter: true });
// Blocked as expected: `$ne` is wrapped in `$eq`, so no match
await login({ username: 'admin', password: { $ne: null } }); // null
// Bypass: login without the password
await login({
username: 'admin',
$_internalSchemaXor: [{ password: { $ne: null } }, { $alwaysFalse: 1 }]
}); // { username: 'admin', password: 'hunter2', ... }
// Bypass: extract the password one character at a time
await login({
username: 'admin',
$_internalSchemaXor: [{ password: { $regex: '^h' } }, { $alwaysFalse: 1 }]
}); // matches, so password starts with 'h'Tested against MongoDB 9.0.2. $_internalSchemaCond works the same way, and both operators also work inside $and/$or/$nor.
Impact
Applications are affected if they rely on sanitizeFilter to protect queries whose filter keys come from users. Common patterns are Model.find(req.body) and Model.find({ ...req.query, owner: userId }). An attacker can inject any query selector, which allows:
- Authentication bypass: match a user record without knowing the password, as in the
$neexample above. - Data extraction: use
$regexas a boolean oracle to recover field values, such as password hashes or tokens, one character at a time. - Unintended writes: make
updateOne(),deleteMany()and similar operations match documents the attacker shouldn't be able to target.
Applications that build filters from individual values, like { username: req.body.username }, are not affected. sanitizeFilter already wraps those values in $eq, so an attacker can't add top-level keys.
Is GHSA-25v4-hrwm-qj9f in any of your client repos?
Checking one package by hand is fine. Checking it across forty client projects is not. NPMscan reads the lockfiles in every GitHub repo you pick, finds each project with an affected mongoose, and gives you the command that fixes it.
More advisories for mongoose
- HighCVE-2026-142596Mongoose: sanitizeProjection can be bypassed to include fields deselected with `select: false` · 4 Dec 2026
- LowCVE-2026-142608Mongoose: Plain objects with a forged _bsontype bypass ObjectId and String casting, leaking and replaying data from other command · 3 Dec 2026
- MediumCVE-2026-125116Mongoose: Discriminator key values matching inherited properties (e.g. __t: 'constructor') bypass schema casting and can poison collections · 10 Nov 2026
- HighCVE-2026-125117Mongoose walks into inherited properties when hydrating, setting, and merging, allowing overwrites of built-in functions (denial of service) and shared schema state · 10 Nov 2026
- MediumCVE-2026-125118Mongoose: Prototype pollution via Schema.prototype.virtual() · 10 Nov 2026
- HighCVE-2026-125119Mongoose: Connection-level sanitizeFilter not applied · 10 Nov 2026
References
- Linkgithub.com/Automattic/mongoose/security/advisories/GHSA-25v4-hrwm-qj9f
- Advisorynvd.nist.gov/vuln/detail/CVE-2026-142595
- Linkgithub.com/Automattic/mongoose/commit/34e76947cc8a9ceb916c233aec01aea060bc3dba
- Linkgithub.com/Automattic/mongoose/commit/3e5f4c8b30ef42db1ab9f2794dc77c95f21d3835
- Linkgithub.com/Automattic/mongoose/commit/4627d2fc572a943ee7afe26a1bcebe493dbd2508
- Linkgithub.com/Automattic/mongoose/commit/c6a4c95f9b15d81a352f2934c91b64ece583c2db
- Packagegithub.com/Automattic/mongoose
- Linkgithub.com/Automattic/mongoose/releases/tag/7.8.14
- Linkgithub.com/Automattic/mongoose/releases/tag/8.24.10
- Linkgithub.com/Automattic/mongoose/releases/tag/9.12.2
Advisory data from OSV.dev (opens OSV.dev in a new tab), refreshed daily.