Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential prototype pollution in nestedProperty #6704

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib/nested_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]*\])+)$/);
Expand Down
4 changes: 3 additions & 1 deletion test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down