Skip to content

Commit

Permalink
Merge pull request #36 from hhomar/fix-prototype-pollution
Browse files Browse the repository at this point in the history
Fix prototype pollution when pointer is not a string or number
  • Loading branch information
manuelstofer authored Feb 17, 2022
2 parents 9b5ea8e + 47dae1d commit 859c998
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ api.set = function set (obj, pointer, value) {

for (var i = 0; i < refTokens.length - 1; ++i) {
var tok = refTokens[i];
if (typeof tok !== 'string' && typeof tok !== 'number') {
tok = String(tok)
}
if (tok === "__proto__" || tok === "constructor" || tok === "prototype") {
continue
}
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ describe('convenience api wrapper', function() {
expect(obj2.polluted).to.be.undefined();
});

it('should not set __proto__ (array)', function () {
var obj = {}, objPointer = pointer(obj);
expect(obj.polluted).to.be.undefined();
objPointer.set([['__proto__'], 'polluted'], true);
expect(obj.polluted).to.be.undefined();
var obj2 = {};
expect(obj2.polluted).to.be.undefined();
});

it('should not set prototype', function () {
var obj = {}, objPointer = pointer(obj);
expect(obj.polluted).to.be.undefined();
Expand Down

0 comments on commit 859c998

Please sign in to comment.