Skip to content

Commit 305ce4f

Browse files
committedJul 11, 2023
fix: avoid prototype pollution on init
1 parent 35e59eb commit 305ce4f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎lib/document.js

+4
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ function init(self, obj, doc, opts, prefix) {
740740

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

‎test/document.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -12278,6 +12278,24 @@ describe('document', function() {
1227812278
assert.equal(fromDb.obj.subArr.length, 1);
1227912279
assert.equal(fromDb.obj.subArr[0].str, 'subArr.test1');
1228012280
});
12281+
12282+
it('avoids prototype pollution on init', async function() {
12283+
const Example = db.model('Example', new Schema({ hello: String }));
12284+
12285+
const example = await new Example({ hello: 'world!' }).save();
12286+
await Example.findByIdAndUpdate(example._id, {
12287+
$rename: {
12288+
hello: '__proto__.polluted'
12289+
}
12290+
});
12291+
12292+
// this is what causes the pollution
12293+
await Example.find();
12294+
12295+
const test = {};
12296+
assert.strictEqual(test.polluted, undefined);
12297+
assert.strictEqual(Object.prototype.polluted, undefined);
12298+
});
1228112299
});
1228212300

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

0 commit comments

Comments
 (0)
Please sign in to comment.