Home/Research/Post

GHSA-25v4-hrwm-qj9f: Mongoose: `sanitizeFilter` bypass via MongoDB internal top-level query operators such as `$_internalSchemaXor`

  • Severity: MEDIUM
  • CVE: CVE-2026-142595
  • Ecosystems: npm, npm, npm, npm
  • Packages: mongoose, mongoose, mongoose, mongoose

### 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`, `$or` and `$nor`;
– throws on `$jsonSchema`, `$where`, `$expr` and `$text` unless the value is wrapped in `mongoose.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.trust…

Source: GitHub Security Advisories · 2026-12-04