{"id":"CVE-2026-142595","aliases":["GHSA-25v4-hrwm-qj9f"],"url":"https://o3.security/vulnerability/CVE-2026-142595","summary":"Mongoose: `sanitizeFilter` bypass via MongoDB internal top-level query operators such as `$_internalSchemaXor`","details":"### Summary\n\nMongoose'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`.\n\n### Details\n\nWith `sanitizeFilter` enabled, Mongoose:\n\n- wraps any object value containing `$` keys in `$eq`, so `{ password: { $ne: null } }` becomes `{ password: { $eq: { $ne: null } } }`;\n- recurses into `$and`, `$or` and `$nor`;\n- throws on `$jsonSchema`, `$where`, `$expr` and `$text` unless the value is wrapped in `mongoose.trusted()`.\n\nEvery 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:\n\n- `$_internalSchemaXor: [filter1, filter2]`\n- `$_internalSchemaCond: [ifFilter, thenFilter, elseFilter]`\n\nBecause `sanitizeFilter` doesn't recurse into these, the nested filters can contain any query selector.\n\nThe 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.trusted()`. Nested `$and`/`$or`/`$nor` filters are checked the same way. This also means `sanitizeFilter` now refuses:\n\n- **`$comment`:** a user-supplied comment ends up in MongoDB's profiler and logs. Use `Query.prototype.comment()` instead, which sets the comment outside the filter.\n- **`$sampleRate`, `$alwaysTrue`, `$alwaysFalse`, and top-level `$ref`/`$id`/`$db`.**\n\nA top-level `$`-prefixed field name can't be queried with a plain equality filter in the first place: MongoDB rejects `{ $userId: 42 }` with \"unknown top level operator\". So the fix doesn't prevent any query that previously worked.\n\n### Proof of concept\n\n```javascript\nconst User = mongoose.model('User', new mongoose.Schema({ username: String, password: String }));\nawait User.create({ username: 'admin', password: 'hunter2' });\n\n// Attacker controls the filter, e.g. `User.findOne(req.body)`\nconst login = filter => User.findOne(filter).setOptions({ sanitizeFilter: true });\n\n// Blocked as expected: `$ne` is wrapped in `$eq`, so no match\nawait login({ username: 'admin', password: { $ne: null } }); // null\n\n// Bypass: login without the password\nawait login({\n  username: 'admin',\n  $_internalSchemaXor: [{ password: { $ne: null } }, { $alwaysFalse: 1 }]\n}); // { username: 'admin', password: 'hunter2', ... }\n\n// Bypass: extract the password one character at a time\nawait login({\n  username: 'admin',\n  $_internalSchemaXor: [{ password: { $regex: '^h' } }, { $alwaysFalse: 1 }]\n}); // matches, so password starts with 'h'\n```\n\nTested against MongoDB 9.0.2. `$_internalSchemaCond` works the same way, and both operators also work inside `$and`/`$or`/`$nor`.\n\n### Impact\n\nApplications are affected if they rely on `sanitizeFilter` to protect queries whose filter keys come from users. Common patterns are `Model.find(req.body)` and `Model.find({ ...req.query, owner: userId })`. An attacker can inject any query selector, which allows:\n\n- **Authentication bypass:** match a user record without knowing the password, as in the `$ne` example above.\n- **Data extraction:** use `$regex` as a boolean oracle to recover field values, such as password hashes or tokens, one character at a time.\n- **Unintended writes:** make `updateOne()`, `deleteMany()` and similar operations match documents the attacker shouldn't be able to target.\n\nApplications that build filters from individual values, like `{ username: req.body.username }`, are not affected. `sanitizeFilter` already wraps those values in `$eq`, so an attacker can't add top-level keys.","published":"2026-12-02T21:47:03.928Z","modified":"2026-12-03T21:02:19.579520919Z","cvss":{"score":6.5,"severity":"MEDIUM","vector":"CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:L/A:N"},"epss":null,"cisaKev":null,"exploitsKnown":null,"affectedPackages":[{"ecosystem":"npm","name":"mongoose","fixedVersion":"6.13.13"},{"ecosystem":"npm","name":"mongoose","fixedVersion":"7.8.14"},{"ecosystem":"npm","name":"mongoose","fixedVersion":"8.24.10"},{"ecosystem":"npm","name":"mongoose","fixedVersion":"9.12.2"}],"fix":{"url":"https://github.com/Automattic/mongoose/commit/34e76947cc8a9ceb916c233aec01aea060bc3dba","label":"Automattic/mongoose@34e7694"},"references":[{"type":"WEB","url":"https://github.com/Automattic/mongoose/releases/tag/7.8.14"},{"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-25v4-hrwm-qj9f"},{"type":"ADVISORY","url":"https://github.com/CVEProject/cvelistV5/tree/main/cves/2026/142xxx/CVE-2026-142595.json"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-142595"},{"type":"FIX","url":"https://github.com/Automattic/mongoose/commit/34e76947cc8a9ceb916c233aec01aea060bc3dba"},{"type":"FIX","url":"https://github.com/Automattic/mongoose/commit/3e5f4c8b30ef42db1ab9f2794dc77c95f21d3835"},{"type":"FIX","url":"https://github.com/Automattic/mongoose/commit/4627d2fc572a943ee7afe26a1bcebe493dbd2508"},{"type":"FIX","url":"https://github.com/Automattic/mongoose/commit/c6a4c95f9b15d81a352f2934c91b64ece583c2db"},{"type":"PACKAGE","url":"https://github.com/Automattic/mongoose"}],"provenance":{"sources":["OSV.dev","FIRST.org (EPSS)"],"lastVerified":"2026-12-03T21:02:19.579520919Z"}}