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

[querystring] avoid Object.prototype conflicts including __proto__ #5650

Closed
wants to merge 5 commits into from
Closed
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
87 changes: 61 additions & 26 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const QueryString = exports;
const Buffer = require('buffer').Buffer;
const defineProperty = Object.defineProperty;


// a safe fast alternative to decodeURIComponent
Expand Down Expand Up @@ -267,20 +268,37 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
key = decodeStr(key, decode);
if (valEncoded)
value = decodeStr(value, decode);
// Use a key array lookup instead of using hasOwnProperty(), which is
// slower
if (keys.indexOf(key) === -1) {
obj[key] = value;
keys[keys.length] = key;
// most common case is to define unique keys per query string
// using the `in` operator we speed up the assignment of unique values
// instead of performing an indexOf each time.
// On top of that, we are sure the property is not conflicting
// with those in Object.prototype so it's also safer.
// However, if the object had already a key, even as own property
// we need to check the indexOf instantly after.
if (key in obj) {
// Use a key array lookup instead of using hasOwnProperty(),
// which is slower
if (keys.indexOf(key) === -1) {
keys[keys.length] = key;
defineProperty(obj, key, {
configurable: true,
enumerable: true,
writable: true,
value: value
});
} else {
const curValue = obj[key];
// `instanceof Array` is used instead of Array.isArray() because it
// is ~15-20% faster with v8 4.7 and is safe to use because we are
// using it with values being created within this function
if (curValue instanceof Array)
curValue[curValue.length] = value;
else
obj[key] = [curValue, value];
}
} else {
const curValue = obj[key];
// `instanceof Array` is used instead of Array.isArray() because it
// is ~15-20% faster with v8 4.7 and is safe to use because we are
// using it with values being created within this function
if (curValue instanceof Array)
curValue[curValue.length] = value;
else
obj[key] = [curValue, value];
keys[keys.length] = key;
obj[key] = value;
}
if (--pairs === 0)
break;
Expand Down Expand Up @@ -370,20 +388,37 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
key = decodeStr(key, decode);
if (valEncoded)
value = decodeStr(value, decode);
// Use a key array lookup instead of using hasOwnProperty(), which is
// slower
if (keys.indexOf(key) === -1) {
obj[key] = value;
keys[keys.length] = key;
// most common case is to define unique keys per query string
// using the `in` operator we speed up the assignment of unique values
// instead of performing an indexOf each time.
// On top of that, we are sure the property is not conflicting
// with those in Object.prototype so it's also safer.
// However, if the object had already a key, even as own property
// we need to check the indexOf instantly after.
if (key in obj) {
// Use a key array lookup instead of using hasOwnProperty(),
// which is slower
if (keys.indexOf(key) === -1) {
keys[keys.length] = key;
defineProperty(obj, key, {
configurable: true,
enumerable: true,
writable: true,
value: value
});
} else {
const curValue = obj[key];
// `instanceof Array` is used instead of Array.isArray() because it
// is ~15-20% faster with v8 4.7 and is safe to use because we are
// using it with values being created within this function
if (curValue instanceof Array)
curValue[curValue.length] = value;
else
obj[key] = [curValue, value];
}
} else {
const curValue = obj[key];
// `instanceof Array` is used instead of Array.isArray() because it
// is ~15-20% faster with v8 4.7 and is safe to use because we are
// using it with values being created within this function
if (curValue instanceof Array)
curValue[curValue.length] = value;
else
obj[key] = [curValue, value];
keys[keys.length] = key;
obj[key] = value;
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var qs = require('querystring');
// {{{
// [ wonkyQS, canonicalQS, obj ]
var qsTestCases = [
['__proto__=1',
'__proto__=1',
JSON.parse('{"__proto__":"1"}')],
['foo=918854443121279438895193',
'foo=918854443121279438895193',
{'foo': '918854443121279438895193'}],
Expand Down