Skip to content

Commit 6a19731

Browse files
vkarpov15shubanker
authored andcommitted
fix(schema): disallow setting __proto__ when creating schema with dotted properties
Fix #12085
1 parent a2ec28d commit 6a19731

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/schema.js

+7
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ Schema.prototype.add = function add(obj, prefix) {
478478
const keys = Object.keys(obj);
479479

480480
for (const key of keys) {
481+
if (utils.specialProperties.has(key)) {
482+
continue;
483+
}
484+
481485
const fullPath = prefix + key;
482486

483487
if (obj[key] == null) {
@@ -663,6 +667,9 @@ Schema.prototype.path = function(path, obj) {
663667
let fullPath = '';
664668

665669
for (const sub of subpaths) {
670+
if (utils.specialProperties.has(sub)) {
671+
throw new Error('Cannot set special property `' + sub + '` on a schema');
672+
}
666673
fullPath = fullPath += (fullPath.length > 0 ? '.' : '') + sub;
667674
if (!branch[sub]) {
668675
this.nested[fullPath] = true;

test/schema.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -2682,4 +2682,14 @@ describe('schema', function() {
26822682
assert.equal(TestSchema.path('testprop.$*').instance, 'Number');
26832683
assert.equal(TestSchema.path('testprop.$*').options.ref, 'OtherModel');
26842684
});
2685+
2686+
it('disallows setting special properties with `add()` or constructor (gh-12085)', async function() {
2687+
const maliciousPayload = '{"__proto__.toString": "Number"}';
2688+
2689+
assert.throws(() => {
2690+
mongoose.Schema(JSON.parse(maliciousPayload));
2691+
}, /__proto__/);
2692+
2693+
assert.ok({}.toString());
2694+
});
26852695
});

0 commit comments

Comments
 (0)