Skip to content

Commit 4f67ea1

Browse files
mscdexjoelostrowski
authored andcommitted
querystring: don't inherit from Object.prototype
This commit safely allows querystring keys that are named the same as properties that are ordinarily inherited from Object.prototype such as __proto__. Additionally, this commit provides a bit of a speed improvement (~25% in the querystring-parse 'manypairs' benchmark) when there are many unique keys. Fixes: nodejs#5642 PR-URL: nodejs#6055 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f71fb18 commit 4f67ea1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/querystring.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
const QueryString = exports;
66
const Buffer = require('buffer').Buffer;
77

8+
// This constructor is used to store parsed query string values. Instantiating
9+
// this is faster than explicitly calling `Object.create(null)` to get a
10+
// "clean" empty object (tested with v8 v4.9).
11+
function ParsedQueryString() {}
12+
ParsedQueryString.prototype = Object.create(null);
13+
814

915
// a safe fast alternative to decodeURIComponent
1016
QueryString.unescapeBuffer = function(s, decodeSpaces) {
@@ -216,7 +222,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
216222
sep = sep || '&';
217223
eq = eq || '=';
218224

219-
const obj = {};
225+
const obj = new ParsedQueryString();
220226

221227
if (typeof qs !== 'string' || qs.length === 0) {
222228
return obj;

test/parallel/test-querystring.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var qs = require('querystring');
99
// {{{
1010
// [ wonkyQS, canonicalQS, obj ]
1111
var qsTestCases = [
12+
['__proto__=1',
13+
'__proto__=1',
14+
JSON.parse('{"__proto__":"1"}')],
15+
['__defineGetter__=asdf',
16+
'__defineGetter__=asdf',
17+
JSON.parse('{"__defineGetter__":"asdf"}')],
1218
['foo=918854443121279438895193',
1319
'foo=918854443121279438895193',
1420
{'foo': '918854443121279438895193'}],

0 commit comments

Comments
 (0)