[Fix] parse: mark overflow objects for indexed notation exceeding `… · ljharb/qs@6addf8c (original) (raw)
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -199,6 +199,9 @@ var parseObject = function (chain, val, options, valuesParsed) { | ||
| 199 | 199 | obj[index] = leaf; |
| 200 | 200 | } else if (isValidArrayIndex && options.throwOnLimitExceeded) { |
| 201 | 201 | throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.'); |
| 202 | +} else if (isValidArrayIndex) { | |
| 203 | +obj[index] = leaf; | |
| 204 | +utils.markOverflow(obj, index); | |
| 202 | 205 | } else if (decodedRoot !== '__proto__') { |
| 203 | 206 | obj[decodedRoot] = leaf; |
| 204 | 207 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -147,6 +147,17 @@ var merge = function merge(target, source, options) { | ||
| 147 | 147 | } else { |
| 148 | 148 | acc[key] = value; |
| 149 | 149 | } |
| 150 | + | |
| 151 | +if (isOverflow(source) && !isOverflow(acc)) { | |
| 152 | +markOverflow(acc, getMaxIndex(source)); | |
| 153 | +} | |
| 154 | +if (isOverflow(acc)) { | |
| 155 | +var keyNum = parseInt(key, 10); | |
| 156 | +if (String(keyNum) === key && keyNum >= 0 && keyNum > getMaxIndex(acc)) { | |
| 157 | +setMaxIndex(acc, keyNum); | |
| 158 | +} | |
| 159 | +} | |
| 160 | + | |
| 150 | 161 | return acc; |
| 151 | 162 | }, mergeTarget); |
| 152 | 163 | }; |
| @@ -323,6 +334,7 @@ module.exports = { | ||
| 323 | 334 | isBuffer: isBuffer, |
| 324 | 335 | isOverflow: isOverflow, |
| 325 | 336 | isRegExp: isRegExp, |
| 337 | +markOverflow: markOverflow, | |
| 326 | 338 | maybeMap: maybeMap, |
| 327 | 339 | merge: merge |
| 328 | 340 | }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1484,5 +1484,29 @@ test('mixed array and object notation', function (t) { | ||
| 1484 | 1484 | st.end(); |
| 1485 | 1485 | }); |
| 1486 | 1486 | |
| 1487 | +t.test('mixed notation produces consistent results when arrayLimit is exceeded', function (st) { | |
| 1488 | +var expected = { a: { 0: 'b', 1: 'c', 2: 'd' } }; | |
| 1489 | + | |
| 1490 | +st.deepEqual( | |
| 1491 | +qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: -1 }), | |
| 1492 | +expected, | |
| 1493 | +'arrayLimit -1' | |
| 1494 | +); | |
| 1495 | + | |
| 1496 | +st.deepEqual( | |
| 1497 | +qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 0 }), | |
| 1498 | +expected, | |
| 1499 | +'arrayLimit 0' | |
| 1500 | +); | |
| 1501 | + | |
| 1502 | +st.deepEqual( | |
| 1503 | +qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 1 }), | |
| 1504 | +expected, | |
| 1505 | +'arrayLimit 1' | |
| 1506 | +); | |
| 1507 | + | |
| 1508 | +st.end(); | |
| 1509 | +}); | |
| 1510 | + | |
| 1487 | 1511 | t.end(); |
| 1488 | 1512 | }); |