Skip to content

Commit f6a7abf

Browse files
committed
[Fix] parse: enforce arrayLimit on comma-parsed values
1 parent fbc5206 commit f6a7abf

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/parse.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ var parseValues = function parseQueryStringValues(str, options) {
130130
val = isArray(val) ? [val] : val;
131131
}
132132

133+
if (options.comma && isArray(val) && val.length > options.arrayLimit) {
134+
if (options.throwOnLimitExceeded) {
135+
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
136+
}
137+
val = utils.combine([], val, options.arrayLimit, options.plainObjects);
138+
}
139+
133140
if (key !== null) {
134141
var existing = has.call(obj, key);
135142
if (existing && options.duplicates === 'combine') {

test/parse.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,42 @@ test('arrayLimit boundary conditions', function (t) {
13491349
t.end();
13501350
});
13511351

1352+
test('comma + arrayLimit', function (t) {
1353+
t.test('comma-separated values within arrayLimit stay as array', function (st) {
1354+
var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 5 });
1355+
st.ok(Array.isArray(result.a), 'result is an array');
1356+
st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
1357+
st.end();
1358+
});
1359+
1360+
t.test('comma-separated values exceeding arrayLimit convert to object', function (st) {
1361+
var result = qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3 });
1362+
st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
1363+
st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
1364+
st.end();
1365+
});
1366+
1367+
t.test('comma-separated values exceeding arrayLimit with throwOnLimitExceeded throws', function (st) {
1368+
st['throws'](
1369+
function () {
1370+
qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3, throwOnLimitExceeded: true });
1371+
},
1372+
new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
1373+
'throws error when comma-split exceeds array limit'
1374+
);
1375+
st.end();
1376+
});
1377+
1378+
t.test('comma-separated values at exactly arrayLimit stay as array', function (st) {
1379+
var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 3 });
1380+
st.ok(Array.isArray(result.a), 'result is an array when exactly at limit');
1381+
st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
1382+
st.end();
1383+
});
1384+
1385+
t.end();
1386+
});
1387+
13521388
test('mixed array and object notation', function (t) {
13531389
t.test('array brackets with object key - under limit', function (st) {
13541390
st.deepEqual(

0 commit comments

Comments
 (0)