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

Patch fixes prototype pollution issue #17 #19

Closed
wants to merge 5 commits into from
Closed
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
25 changes: 21 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ var decode = function(str) {
}
}

/**
* Ensure any potential prototype pollution can be sanitized.
*
* @param {String} str
* @return {String}
* @api public
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not part of the public API.

Suggested change
* @api public
* @api private

*/

var sanitizeObjKey = function(str) {
if (str && ["__proto__", "constructor", "prototype"].indexOf(str.toLowerCase()) > -1) {
return str.toUpperCase()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why convert to uppercase? If the user (for some weird reason) wanted the value of prototype, they'd get PROTOTYPE instead, which would either return unexpected (incorrect) results or undefined. Consider the following:

var q = querystring.parse('prototype=hi&PROTOTYPE=bye')
console.log(q.prototype)
// => bye
console.log(q.PROTOTYPE)
// => bye

IMO we should either error or return undefined.

}

return str;
}

/**
* Parse the given query `str`.
*
Expand All @@ -59,16 +75,17 @@ exports.parse = function(str){
var pairs = str.split('&');
for (var i = 0; i < pairs.length; i++) {
var parts = pairs[i].split('=');
var key = decode(parts[0]);
var key = sanitizeObjKey(decode(parts[0]));
var m;

if (m = pattern.exec(key)) {
obj[m[1]] = obj[m[1]] || [];
obj[m[1]][m[2]] = decode(parts[1]);
var objectKey = sanitizeObjKey(m[1])
obj[objectKey] = obj[objectKey] || [];
obj[objectKey][m[2]] = decode(parts[1]);
continue;
}

obj[parts[0]] = null == parts[1]
obj[key] = null == parts[1]
? ''
: decode(parts[1]);
}
Expand Down