Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/path-toolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ var wildCardMatch = function(template, str){
return match;
};

/**
* Returns true, if given key is included in the blacklisted
* keys.
* @param {String} key key for check, string.
* @returns {Boolean}.
*/
function isPrototypePolluted(key) {
return ['__proto__', 'prototype', 'constructor'].includes(key);
}

/**
* Inspect input value and determine whether it is an Object or not.
* Values of undefined and null will return "false", otherwise
Expand Down Expand Up @@ -1035,7 +1045,10 @@ var PathToolkit = function(options){
obj[tk[i]] = {};
}
}
obj = obj[tk[i++]];
if (!isPrototypePolluted(tk[i])) {
obj = obj[tk[i]];
}
i++;
}
return obj;
};
Expand Down
10 changes: 10 additions & 0 deletions test/path-toolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,16 @@ describe( 'PathToolkit', function(){
expect(result).to.be.true;
expect(ary.join(',')).to.equal('NEW,NEW,NEW');
});

it('should prevent prototype pollution', function() {
var str = '__proto__.polluted';
var obj = {};
var result = ptk.set(obj, str, 'Yes, its polluted.');
expect(result).to.be.true;
expect(obj.polluted).to.be.equal('Yes, its polluted.');
expect({}.polluted).to.not.be.equal('Yes, its polluted.');
expect({}.polluted).to.be.equal(undefined);
})
});

describe( 'find', function(){
Expand Down