Skip to content

Commit 52afe00

Browse files
committed
[Robustness] parse: throw the arrayLimit error before splitting oversized comma values
With `comma` and `throwOnLimitExceeded`, a single large comma-separated value was fully split into an array before the `arrayLimit` check ran, so an oversized value allocated the whole array (and could exhaust memory) before the `RangeError` was thrown. Count commas up to the limit and throw before splitting, for flat (non-bracket) values. Bracket `[]=` comma groups are intentionally one nested element, so they are unaffected. The post-split comma throw is now redundant (`utils.combine` already throws when `throwOnLimitExceeded` is set) and is removed.
1 parent 963e538 commit 52afe00

2 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎lib/parse.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,19 @@ var interpretNumericEntities = function (str) {
3636
});
3737
};
3838

39-
var parseArrayValue = function (val, options, currentArrayLength) {
39+
var parseArrayValue = function (val, options, currentArrayLength, isFlatArrayValue) {
4040
if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) {
41+
if (isFlatArrayValue && options.throwOnLimitExceeded) {
42+
var commaCount = 0;
43+
var commaIndex = val.indexOf(',');
44+
while (commaIndex > -1) {
45+
commaCount += 1;
46+
if (commaCount >= options.arrayLimit) {
47+
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
48+
}
49+
commaIndex = val.indexOf(',', commaIndex + 1);
50+
}
51+
}
4152
return val.split(',');
4253
}
4354

@@ -114,7 +125,8 @@ var parseValues = function parseQueryStringValues(str, options) {
114125
parseArrayValue(
115126
part.slice(pos + 1),
116127
options,
117-
isArray(obj[key]) ? obj[key].length : 0
128+
isArray(obj[key]) ? obj[key].length : 0,
129+
part.indexOf('[]=') === -1
118130
),
119131
function (encodedVal) {
120132
return options.decoder(encodedVal, defaults.decoder, charset, 'value');
@@ -132,9 +144,6 @@ var parseValues = function parseQueryStringValues(str, options) {
132144
}
133145

134146
if (options.comma && isArray(val) && val.length > options.arrayLimit) {
135-
if (options.throwOnLimitExceeded) {
136-
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
137-
}
138147
val = utils.combine([], val, options.arrayLimit, options.plainObjects, options.throwOnLimitExceeded);
139148
}
140149

‎test/parse.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,64 @@ test('parse()', function (t) {
13921392
sst.end();
13931393
});
13941394

1395+
st.test('throws before splitting when a single comma value exceeds arrayLimit', function (sst) {
1396+
sst['throws'](
1397+
function () {
1398+
qs.parse('a=1,2,3,4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1399+
},
1400+
new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
1401+
'a flat comma value over the limit throws'
1402+
);
1403+
1404+
sst['throws'](
1405+
function () {
1406+
qs.parse('a=1,2', { comma: true, arrayLimit: 1, throwOnLimitExceeded: true });
1407+
},
1408+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1409+
'singular message at arrayLimit 1'
1410+
);
1411+
1412+
sst['throws'](
1413+
function () {
1414+
qs.parse('a[b]=1,2,3,4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1415+
},
1416+
new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
1417+
'a non-bracket nested key comma value over the limit throws'
1418+
);
1419+
sst.end();
1420+
});
1421+
1422+
st.test('does not throw for a single comma value within arrayLimit', function (sst) {
1423+
sst.deepEqual(
1424+
qs.parse('a=1,2,3', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true }),
1425+
{ a: ['1', '2', '3'] },
1426+
'within the limit'
1427+
);
1428+
sst.deepEqual(
1429+
qs.parse('a=1,2,3,4,5', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true }),
1430+
{ a: ['1', '2', '3', '4', '5'] },
1431+
'exactly at the limit'
1432+
);
1433+
sst.end();
1434+
});
1435+
1436+
st.test('does not throw for a bracketed comma group within arrayLimit', function (sst) {
1437+
var result = qs.parse('a[]=1,2,3,4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1438+
sst.deepEqual(result, { a: [['1', '2', '3', '4', '5', '6']] }, 'a bracketed comma group is a single element');
1439+
sst.end();
1440+
});
1441+
1442+
st.test('throws for a bracketed comma group when arrayLimit is 0', function (sst) {
1443+
sst['throws'](
1444+
function () {
1445+
qs.parse('a[]=1,2,3', { comma: true, arrayLimit: 0, throwOnLimitExceeded: true });
1446+
},
1447+
new RangeError('Array limit exceeded. Only 0 elements allowed in an array.'),
1448+
'a single bracketed element still exceeds arrayLimit 0'
1449+
);
1450+
sst.end();
1451+
});
1452+
13951453
st.end();
13961454
});
13971455

0 commit comments

Comments
 (0)