Mongoose: Plain objects with a forged _bsontype bypass ObjectId and String casting, leaking and replaying data from other command
Published 3 Dec 2026 · Updated 3 Dec 2026
Affected package
- Affected
- >=6.3.5 <6.13.13
- Fixed in
- 6.13.13
npm install mongoose@6.13.13Which mongoose does your project really install?
Works for private projects: the file is read in memory to run the scan and then dropped. Also reads yarn.lock, pnpm-lock.yaml or package.json.
Public repo?
Look after several JavaScript apps? Indie watches up to 15 repos, private ones too, for $5 a month. See Indie
Details
Summary
From 6.3.5-6.13.12, Mongoose 6 decides whether a value is a BSON type by checking only its _bsontype property. A plain object such as {"_bsontype":"ObjectID","id":""} therefore passes the ObjectId cast. bson 4.x then writes no id bytes but still advances 12 bytes in its serialization buffer, so the value sent to MongoDB is whatever the previous command left at that offset. Request bodies, cookies and Express 4 query strings can all produce such an object. sanitizeFilter doesn’t catch it because it has no $ keys.
Details
lib/helpers/isBsonType.js returned true for any object whose _bsontype matched. This led to three problems:
1. Queries by id replay another request’s id. findById(forged) sends the _id from the last command with the same layout, such as another request’s findById on the same collection. Likewise, findByIdAndDelete(forged) and findByIdAndUpdate(forged) act on the document another request just modified with findOneAndUpdate. This works with sanitizeFilter on.
2. Stored ObjectIds leak command bytes. create(), $set and other writes store 12 bytes of earlier commands, such as filter values and updated fields. Anyone who can read the document can read those bytes. This happens in the default configuration.
3. Forged regexes on String paths. {"_bsontype":"BSONRegExp","pattern":"^a","options":""} passes the String query cast and is sent as a regular expression, bypassing sanitizeFilter.
mongoose.isObjectIdOrHexString() also returns true for the forged object.
The fix makes isBsonType() ignore _bsontype when it is an own property. bson defines _bsontype on the prototype (except for the Timestamp class, which Mongoose does not check), so real bson values still pass.
Proof of concept
mongoose.set('sanitizeFilter', true);
const User = mongoose.model('User', new mongoose.Schema({ email: String, password: String }));
const Comment = mongoose.model('Comment', new mongoose.Schema({ post: mongoose.ObjectId }));
const alice = await User.create({ email: 'alice@example.com', password: 'S3cret' });
const forged = () => JSON.parse('{"_bsontype":"ObjectID","id":""}');
// 1. Query by id: replays the _id from the previous command
await User.findById(alice._id); // another request
await User.findById(forged()); // alice's document
// 2. String path: forged BSONRegExp is sent as a regex
await User.findOne({ password: JSON.parse('{"_bsontype":"BSONRegExp","pattern":"^S3","options":""}') }); // alice's document
// 3. Stored value (default config): stores 12 bytes of the previous command
await User.updateOne({ email: 'victim@example.com' }, { $set: { password: 'resetcode123' } }); // another request
const c = await Comment.create({ post: forged() });
(await Comment.findById(c._id)).post.id.toString('latin1'); // 'il\x00\x13\x00\x00\x00victi'Impact
Applications on 6.3.5 to 6.13.12 are affected if they pass request values into queries or writes on ObjectId or String paths without checking that the values are primitive values. An attacker can:
1. read, modify or delete documents that another request just accessed, even with sanitizeFilter, express-mongo-sanitize or mongo-sanitize enabled; 2. read fragments of other requests’ database commands, such as emails, reset codes or API keys, by storing forged ObjectIds and reading them back; 3. run regex matches against String fields despite sanitizeFilter.
Workaround: check that request values are strings, or use mongoose.isValidObjectId(), before passing them to Mongoose.
Is GHSA-rxm2-55pg-68q6 in any of your client repos?
Checking one package by hand is fine. Checking it across forty client projects is not. NPMscan reads the lockfiles in every GitHub repo you pick, finds each project with an affected mongoose, and gives you the command that fixes it.
More advisories for mongoose
- MediumCVE-2026-125116Mongoose: Discriminator key values matching inherited properties (e.g. __t: 'constructor') bypass schema casting and can poison collections · 10 Nov 2026
- HighCVE-2026-125117Mongoose walks into inherited properties when hydrating, setting, and merging, allowing overwrites of built-in functions (denial of service) and shared schema state · 10 Nov 2026
- MediumCVE-2026-125118Mongoose: Prototype pollution via Schema.prototype.virtual() · 10 Nov 2026
- HighCVE-2026-125119Mongoose: Connection-level sanitizeFilter not applied · 10 Nov 2026
- CriticalCVE-2026-125120Mongoose: sanitizeFilter not applied on countDocuments · 10 Nov 2026
- MediumCVE-2026-73562Mongoose: Prototype pollution in mongoose update casting via __proto__-prefixed dotted path (Schema._getSchema/path getter) · 24 Jul 2026
References
Advisory data from OSV.dev (opens OSV.dev in a new tab), refreshed daily.