Skip to content

Commit e29578d

Browse files
committed
fix: avoid prototype pollution on init
1 parent b336ed8 commit e29578d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/document.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,10 @@ function init(self, obj, doc, opts, prefix) {
741741

742742
function _init(index) {
743743
i = keys[index];
744+
// avoid prototype pollution
745+
if (i === '__proto__' || i === 'constructor') {
746+
return;
747+
}
744748
path = prefix + i;
745749
schemaType = docSchema.path(path);
746750

test/document.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12212,6 +12212,24 @@ describe('document', function() {
1221212212
const fromDb = await Test.findById(x._id).lean();
1221312213
assert.equal(fromDb.c.x.y, 1);
1221412214
});
12215+
12216+
it('avoids prototype pollution on init', async function() {
12217+
const Example = db.model('Example', new Schema({ hello: String }));
12218+
12219+
const example = await new Example({ hello: 'world!' }).save();
12220+
await Example.findByIdAndUpdate(example._id, {
12221+
$rename: {
12222+
hello: '__proto__.polluted'
12223+
}
12224+
});
12225+
12226+
// this is what causes the pollution
12227+
await Example.find();
12228+
12229+
const test = {};
12230+
assert.strictEqual(test.polluted, undefined);
12231+
assert.strictEqual(Object.prototype.polluted, undefined);
12232+
});
1221512233
});
1221612234

1221712235
describe('Check if instance function that is supplied in schema option is availabe', function() {

0 commit comments

Comments
 (0)