Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] parsing comma delimited str with percent encoded comma #336

Merged
merged 1 commit into from Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/parse.js
Expand Up @@ -85,7 +85,15 @@ var parseValues = function parseQueryStringValues(str, options) {
val = options.strictNullHandling ? null : '';
} else {
key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
val = options.decoder(part.slice(pos + 1), defaults.decoder, charset, 'value');
var encodedVal = part.slice(pos + 1);
if (options.comma && encodedVal.indexOf(',') !== -1) {
val = encodedVal.split(',')
.map(function (encodedFragment) {
return options.decoder(encodedFragment, defaults.decoder, charset, 'value');
});
} else {
val = options.decoder(encodedVal, defaults.decoder, charset, 'value');
}
}

if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
Expand Down
8 changes: 8 additions & 0 deletions test/parse.js
Expand Up @@ -429,6 +429,14 @@ test('parse()', function (t) {
st.end();
});

t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: ['a', 'b'] });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb this test is wrong, it should return "a,b". It's the main point of the pull request and the related issue.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, i understand. my bad.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #361 to proceed with a proper fix.

st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });

st.end();
});

t.test('parses an object in dot notation', function (st) {
var input = {
'user.name': { 'pop[bob]': 3 },
Expand Down