Skip to content

Commit

Permalink
fix(document): disallow setting constructor and prototype if strict m…
Browse files Browse the repository at this point in the history
…ode false
  • Loading branch information
vkarpov15 committed Aug 30, 2018
1 parent b33d8c2 commit fb8b644
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var flatten = require('./services/common').flatten;
var mpath = require('mpath');
var idGetter = require('./plugins/idGetter');

var specialProperties = ['__proto__', 'constructor', 'prototype'];

/**
* Document constructor.
*
Expand Down Expand Up @@ -917,7 +919,7 @@ 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__') {
if (specialProperties.indexOf(parts[i]) !== -1) {
return;
}

Expand Down
6 changes: 5 additions & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4964,7 +4964,7 @@ describe('document', function() {
done();
});

it('Disallows writing to __proto__', function(done) {
it('Disallows writing to __proto__ and other special properties', function(done) {
var schema = new mongoose.Schema({
name: String
}, { strict: false });
Expand All @@ -4977,6 +4977,10 @@ describe('document', function() {

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

doc.set('constructor.prototype.z', 'baz');

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

done();
});

Expand Down

0 comments on commit fb8b644

Please sign in to comment.