Skip to content

Commit 85743c0

Browse files
Myles Borinsrvagg
Myles Borins
authored andcommitted
querystring: check that maxKeys is finite
There was a very subtle change in behavior introduced with 27def4f In the past if querystring.parse was given Infinity for maxKeys, everything worked as expected. Check to see is maxKeys is Infinity before forwarding the value to String.prototype.split which causes this regression PR-URL: #5066 Reviewed-By: Evan Lucas <[email protected]> Reviewed By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 3175f74 commit 85743c0

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/querystring.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
215215
}
216216

217217
// maxKeys <= 0 means that we should not limit keys count
218-
if (maxKeys > 0) {
218+
if (maxKeys > 0 && isFinite(maxKeys)) {
219219
qs = qs.split(sep, maxKeys);
220220
} else {
221221
qs = qs.split(sep);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
// This test was originally written to test a regression
3+
// that was introduced by
4+
// https://github.com/nodejs/node/pull/2288#issuecomment-179543894
5+
require('../common');
6+
7+
const assert = require('assert');
8+
const parse = require('querystring').parse;
9+
10+
/*
11+
taken from express-js/body-parser
12+
https://github.com/expressjs/body-parser/
13+
blob/ed25264fb494cf0c8bc992b8257092cd4f694d5e/test/urlencoded.js#L636-L651
14+
*/
15+
function createManyParams(count) {
16+
var str = '';
17+
18+
if (count === 0) {
19+
return str;
20+
}
21+
22+
str += '0=0';
23+
24+
for (var i = 1; i < count; i++) {
25+
var n = i.toString(36);
26+
str += '&' + n + '=' + n;
27+
}
28+
29+
return str;
30+
}
31+
32+
const count = 10000;
33+
const originalMaxLength = 1000;
34+
const params = createManyParams(count);
35+
36+
// thealphanerd
37+
// 27def4f introduced a change to parse that would cause Inifity
38+
// to be passed to String.prototype.split as an argument for limit
39+
// In this instance split will always return an empty array
40+
// this test confirms that the output of parse is the expected length
41+
// when passed Infinity as the argument for maxKeys
42+
const resultInfinity = parse(params, undefined, undefined, {maxKeys: Infinity});
43+
const resultNaN = parse(params, undefined, undefined, {maxKeys: NaN});
44+
const resultInfinityString = parse(params, undefined, undefined, {
45+
maxKeys: 'Infinity'
46+
});
47+
const resultNaNString = parse(params, undefined, undefined, {maxKeys: 'NaN'});
48+
49+
// Non Finite maxKeys should return the length of input
50+
assert.equal(Object.keys(resultInfinity).length, count);
51+
assert.equal(Object.keys(resultNaN).length, count);
52+
// Strings maxKeys should return the maxLength
53+
// defined by parses internals
54+
assert.equal(Object.keys(resultInfinityString).length, originalMaxLength);
55+
assert.equal(Object.keys(resultNaNString).length, originalMaxLength);

0 commit comments

Comments
 (0)