Skip to content

Commit

Permalink
Merge branch 'vkarpov15/avoid-prototype-pollution'
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 10, 2023
2 parents 2188458 + cc722a1 commit 02699fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,10 @@ function init(self, obj, doc, opts, prefix) {

function _init(index) {
i = keys[index];
// avoid prototype pollution
if (i === '__proto__' || i === 'constructor') {
return;
}
path = prefix + i;
schemaType = docSchema.path(path);

Expand Down
30 changes: 30 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12233,6 +12233,36 @@ describe('document', function() {
assert.deepStrictEqual(doc.elements[0].modifiedPaths(), []);
assert.deepStrictEqual(doc.elements[1].modifiedPaths(), []);
});

it('avoids prototype pollution on init', async function() {
const Example = db.model('Example', new Schema({ hello: String }));

const example = await new Example({ hello: 'world!' }).save();
await Example.findByIdAndUpdate(example._id, {
$rename: {
hello: '__proto__.polluted'
}
});

// this is what causes the pollution
await Example.find();

const test = {};
assert.strictEqual(test.polluted, undefined);
assert.strictEqual(Object.prototype.polluted, undefined);

const example2 = await new Example({ hello: 'world!' }).save();
await Example.findByIdAndUpdate(example2._id, {
$rename: {
hello: 'constructor.polluted'
}
});

await Example.find();
const test2 = {};
assert.strictEqual(test2.constructor.polluted, undefined);
assert.strictEqual(Object.polluted, undefined);
});
});

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

0 comments on commit 02699fa

Please sign in to comment.