Skip to content

Commit

Permalink
Prevent Object prototype pollution.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwakerman committed Apr 27, 2018
1 parent 9653940 commit 3543599
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/deep-extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) {
}

Object.keys(obj).forEach(function (key) {
src = target[key]; // source value
val = obj[key]; // new value
src = safeGetProperty(target, key); // source value
val = safeGetProperty(obj, key); // new value

// recursion prevention
if (val === target) {
Expand Down Expand Up @@ -142,3 +142,7 @@ var deepExtend = module.exports = function (/*obj_1, [obj_2], [obj_N]*/) {

return target;
}

function safeGetProperty(object, property) {
return property === '__proto__' ? undefined : object[property];
}
8 changes: 8 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,12 @@ describe('deep-extend', function () {
});
});

// Vulnerability reported via hacker1: https://hackerone.com/reports/311333
it('should not modify Object prototype (hacker1 #311333)', function () {
var a = {};
extend({}, JSON.parse('{"__proto__":{"oops":"It works!"}}'))
should.not.exist(a.oops);
should.not.exist(Object.prototype.oops);
});

});

0 comments on commit 3543599

Please sign in to comment.