Skip to content

Commit 81835ff

Browse files
techouseljharb
authored andcommitted
[Fix]: parse: parse encoded square brackets
1 parent db47dcc commit 81835ff

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var parseValues = function parseQueryStringValues(str, options) {
5555
var obj = { __proto__: null };
5656

5757
var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
58+
cleanStr = cleanStr.replace(/%5B/gi, '[').replace(/%5D/gi, ']');
5859
var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
5960
var parts = cleanStr.split(options.delimiter, limit);
6061
var skipIndex = -1; // Keep track of where the utf8 sentinel was found

test/parse.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,15 @@ test('parse()', function (t) {
571571
st.end();
572572
});
573573

574+
t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
575+
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
576+
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
577+
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
578+
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
579+
580+
st.end();
581+
});
582+
574583
t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
575584
st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
576585
st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });

0 commit comments

Comments
 (0)