Skip to content

Commit

Permalink
fix(document): disallow setting __proto__ if strict mode false
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Aug 30, 2018
1 parent a738273 commit a3b98f6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,9 @@ Document.prototype.$__set = function(pathToMark, path, constructing, parts, sche
var next = i + 1;
var last = next === l;
cur += (cur ? '.' + parts[i] : parts[i]);
if (parts[i] === '__proto__') {
return;
}

if (last) {
obj[parts[i]] = val;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"kareem": "1.5.0",
"lodash.get": "4.4.2",
"mongodb": "2.2.34",
"mpath": "0.3.0",
"mpath": "0.5.0",
"mpromise": "0.5.5",
"mquery": "2.3.3",
"ms": "2.0.0",
Expand Down
16 changes: 16 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4964,6 +4964,22 @@ describe('document', function() {
done();
});

it('Disallows writing to __proto__', function(done) {
const schema = new mongoose.Schema({
name: String
}, { strict: false });

const Model = db.model('prototest', schema);
const doc = new Model({ '__proto__.x': 'foo' });

assert.strictEqual(Model.x, void 0);
doc.set('__proto__.y', 'bar');

assert.strictEqual(Model.y, void 0);

done();
});

it('Single nested subdocs using discriminator can be modified (gh-5693)', function(done) {
var eventSchema = new Schema({ message: String }, {
discriminatorKey: 'kind',
Expand Down

0 comments on commit a3b98f6

Please sign in to comment.