Skip to content

Commit 963e538

Browse files
committed
[Fix] parse: enforce throwOnLimitExceeded for cumulative array growth via combine/merge
`throwOnLimitExceeded` was only honored on the per-part paths in `parse`. When an array grew past `arrayLimit` through duplicate-key combination (`utils.combine`) or through `utils.merge`'s overflow branches, the value was silently converted to an overflow object instead of throwing. Thread `throwOnLimitExceeded` into `utils.combine` and guard the overflow branches in `utils.merge`, so the option throws consistently wherever an array would otherwise be converted to an overflow object. This also covers the `arrayLimit: 1` scalar-to-array boundary for plain duplicate keys, bracket arrays, and mixed index/key notation.
1 parent 59da434 commit 963e538

4 files changed

Lines changed: 154 additions & 4 deletions

File tree

‎lib/parse.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var parseValues = function parseQueryStringValues(str, options) {
135135
if (options.throwOnLimitExceeded) {
136136
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
137137
}
138-
val = utils.combine([], val, options.arrayLimit, options.plainObjects);
138+
val = utils.combine([], val, options.arrayLimit, options.plainObjects, options.throwOnLimitExceeded);
139139
}
140140

141141
if (key !== null) {
@@ -145,7 +145,8 @@ var parseValues = function parseQueryStringValues(str, options) {
145145
obj[key],
146146
val,
147147
options.arrayLimit,
148-
options.plainObjects
148+
options.plainObjects,
149+
options.throwOnLimitExceeded
149150
);
150151
} else if (!existing || options.duplicates === 'last') {
151152
obj[key] = val;
@@ -180,7 +181,8 @@ var parseObject = function (chain, val, options, valuesParsed) {
180181
[],
181182
leaf,
182183
options.arrayLimit,
183-
options.plainObjects
184+
options.plainObjects,
185+
options.throwOnLimitExceeded
184186
);
185187
}
186188
} else {

‎lib/utils.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ var merge = function merge(target, source, options) {
9090
if (isArray(target)) {
9191
var nextIndex = target.length;
9292
if (options && typeof options.arrayLimit === 'number' && nextIndex > options.arrayLimit) {
93+
if (options.throwOnLimitExceeded) {
94+
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
95+
}
9396
return markOverflow(arrayToObject(target.concat(source), options), nextIndex);
9497
}
9598
target[nextIndex] = source;
@@ -129,6 +132,9 @@ var merge = function merge(target, source, options) {
129132
}
130133
var combined = [target].concat(source);
131134
if (options && typeof options.arrayLimit === 'number' && combined.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+
}
132138
return markOverflow(arrayToObject(combined, options), combined.length - 1);
133139
}
134140
return combined;
@@ -320,9 +326,12 @@ var isBuffer = function isBuffer(obj) {
320326
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
321327
};
322328

323-
var combine = function combine(a, b, arrayLimit, plainObjects) {
329+
var combine = function combine(a, b, arrayLimit, plainObjects, throwOnLimitExceeded) {
324330
// If 'a' is already an overflow object, add to it
325331
if (isOverflow(a)) {
332+
if (throwOnLimitExceeded) {
333+
throw new RangeError('Array limit exceeded. Only ' + arrayLimit + ' element' + (arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
334+
}
326335
var newIndex = getMaxIndex(a) + 1;
327336
a[newIndex] = b;
328337
setMaxIndex(a, newIndex);
@@ -331,6 +340,9 @@ var combine = function combine(a, b, arrayLimit, plainObjects) {
331340

332341
var result = [].concat(a, b);
333342
if (result.length > arrayLimit) {
343+
if (throwOnLimitExceeded) {
344+
throw new RangeError('Array limit exceeded. Only ' + arrayLimit + ' element' + (arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
345+
}
334346
return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), result.length - 1);
335347
}
336348
return result;

‎test/parse.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,73 @@ test('parse()', function (t) {
13251325
sst.end();
13261326
});
13271327

1328+
st.test('throws when cumulative comma + duplicate-key combine exceeds arrayLimit', function (sst) {
1329+
sst['throws'](
1330+
function () {
1331+
qs.parse('a=1,2,3&a=4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1332+
},
1333+
new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
1334+
'throws when comma groups within the limit cumulatively exceed it across duplicate keys'
1335+
);
1336+
1337+
sst['throws'](
1338+
function () {
1339+
qs.parse('a=v,v,v,v,v&a=v,v,v,v,v&a=v,v,v,v,v', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1340+
},
1341+
new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
1342+
'throws on a subsequent part once the cumulative array is already over the limit'
1343+
);
1344+
sst.end();
1345+
});
1346+
1347+
st.test('throws when plain duplicate keys combine past arrayLimit at the boundary', function (sst) {
1348+
sst['throws'](
1349+
function () { qs.parse('a=x&a=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
1350+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1351+
'duplicate scalar keys'
1352+
);
1353+
1354+
sst['throws'](
1355+
function () { qs.parse('a[]=x&a[]=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
1356+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1357+
'duplicate bracket keys'
1358+
);
1359+
sst.end();
1360+
});
1361+
1362+
st.test('throws when mixed index and key notation merge past arrayLimit', function (sst) {
1363+
sst['throws'](
1364+
function () { qs.parse('a=x&a[0]=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
1365+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1366+
'scalar then index that overflows on merge'
1367+
);
1368+
1369+
sst['throws'](
1370+
function () { qs.parse('a[0]=1&a[1]=2&a=3', { arrayLimit: 1, throwOnLimitExceeded: true }); },
1371+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1372+
'indexed array then scalar that overflows on merge'
1373+
);
1374+
sst.end();
1375+
});
1376+
1377+
st.test('does not throw when cumulative comma combine stays within arrayLimit', function (sst) {
1378+
var result = qs.parse('a=1,2,3&a=4', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1379+
sst.deepEqual(result, { a: ['1', '2', '3', '4'] }, 'combined array within limit is preserved');
1380+
sst.end();
1381+
});
1382+
1383+
st.test('silently combines to an overflow object when throwOnLimitExceeded is not set', function (sst) {
1384+
var result = qs.parse('a=1,2,3&a=4,5,6', { comma: true, arrayLimit: 5 });
1385+
sst.deepEqual(result, { a: { 0: '1', 1: '2', 2: '3', 3: '4', 4: '5', 5: '6' } }, 'converts to object without throwing');
1386+
sst.end();
1387+
});
1388+
1389+
st.test('does not throw for comma groups nested under bracket notation, counting each group as one element', function (sst) {
1390+
var result = qs.parse('a[]=1,2,3&a[]=4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
1391+
sst.deepEqual(result, { a: [['1', '2', '3'], ['4', '5', '6']] }, 'nested comma groups count as one element each');
1392+
sst.end();
1393+
});
1394+
13281395
st.end();
13291396
});
13301397

‎test/utils.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ test('merge()', function (t) {
153153
s2t.end();
154154
});
155155

156+
st.test('throws instead of merging primitive into over-limit array when throwOnLimitExceeded is set', function (s2t) {
157+
s2t['throws'](
158+
function () { utils.merge(['a', 'b', 'c'], 'd', { arrayLimit: 1, throwOnLimitExceeded: true }); },
159+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
160+
'throws rather than converting to an overflow object'
161+
);
162+
s2t['throws'](
163+
function () { utils.merge(['a', 'b', 'c'], 'd', { arrayLimit: 2, throwOnLimitExceeded: true }); },
164+
new RangeError('Array limit exceeded. Only 2 elements allowed in an array.'),
165+
'uses the plural message when arrayLimit is not 1'
166+
);
167+
s2t.end();
168+
});
169+
170+
st.test('throws instead of merging array into primitive when throwOnLimitExceeded is set', function (s2t) {
171+
s2t['throws'](
172+
function () { utils.merge('a', ['b', 'c'], { arrayLimit: 1, throwOnLimitExceeded: true }); },
173+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
174+
'throws rather than converting to an overflow object'
175+
);
176+
s2t['throws'](
177+
function () { utils.merge('a', ['b', 'c', 'd'], { arrayLimit: 2, throwOnLimitExceeded: true }); },
178+
new RangeError('Array limit exceeded. Only 2 elements allowed in an array.'),
179+
'uses the plural message when arrayLimit is not 1'
180+
);
181+
s2t.end();
182+
});
183+
156184
st.end();
157185
});
158186

@@ -274,6 +302,47 @@ test('combine()', function (t) {
274302
st.end();
275303
});
276304

305+
t.test('with throwOnLimitExceeded', function (st) {
306+
st.test('throws when concatenation exceeds the limit', function (s2t) {
307+
s2t['throws'](
308+
function () { utils.combine(['a', 'b', 'c'], 'd', 3, false, true); },
309+
new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
310+
'throws instead of converting to an overflow object'
311+
);
312+
s2t['throws'](
313+
function () { utils.combine([], 'a', 0, false, true); },
314+
new RangeError('Array limit exceeded. Only 0 elements allowed in an array.'),
315+
'throws with the correct count at arrayLimit 0'
316+
);
317+
s2t.end();
318+
});
319+
320+
st.test('throws when adding to an existing overflow object', function (s2t) {
321+
var overflow = utils.combine(['a', 'b'], 'c', 0, false);
322+
s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');
323+
324+
s2t['throws'](
325+
function () { utils.combine(overflow, 'd', 0, false, true); },
326+
new RangeError('Array limit exceeded. Only 0 elements allowed in an array.'),
327+
'throws rather than appending to the overflow object'
328+
);
329+
s2t['throws'](
330+
function () { utils.combine(overflow, 'd', 1, false, true); },
331+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
332+
'uses the singular message at arrayLimit 1'
333+
);
334+
s2t.end();
335+
});
336+
337+
st.test('does not throw when within the limit', function (s2t) {
338+
var combined = utils.combine(['a'], 'b', 5, false, true);
339+
s2t.deepEqual(combined, ['a', 'b'], 'returns the array unchanged when under the limit');
340+
s2t.end();
341+
});
342+
343+
st.end();
344+
});
345+
277346
t.test('with existing overflow object', function (st) {
278347
st.test('adds to existing overflow object at next index', function (s2t) {
279348
// Create overflow object first via combine: 3 elements (indices 0-2) with limit 0

0 commit comments

Comments
 (0)