diff --git a/src/lib/nested_property.js b/src/lib/nested_property.js index f54c2951abe..a057e732bee 100644 --- a/src/lib/nested_property.js +++ b/src/lib/nested_property.js @@ -24,13 +24,20 @@ module.exports = function nestedProperty(container, propStr) { throw 'bad property string'; } - var j = 0; var propParts = propStr.split('.'); var indexed; var indices; - var i; + var i, j; + + for(j = 0; j < propParts.length; j++) { + // guard against polluting __proto__ and other internals + if(String(propParts[j]).slice(0, 2) === '__') { + throw 'bad property string'; + } + } // check for parts of the nesting hierarchy that are numbers (ie array elements) + j = 0; while(j < propParts.length) { // look for non-bracket chars, then any number of [##] blocks indexed = String(propParts[j]).match(/^([^\[\]]*)((\[\-?[0-9]*\])+)$/); diff --git a/test/jasmine/tests/lib_test.js b/test/jasmine/tests/lib_test.js index fcc24c51ad0..88b144d2c24 100644 --- a/test/jasmine/tests/lib_test.js +++ b/test/jasmine/tests/lib_test.js @@ -468,7 +468,9 @@ describe('Test lib.js:', function() { it('should fail on a bad property string', function() { var badStr = [ - [], {}, false, undefined, null, NaN, Infinity + [], {}, false, undefined, null, NaN, Infinity, + // should guard against prototype pollution + 'x.__proto__.polluted', 'x.y.__proto__.polluted' ]; function badProp(i) {