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

querystring: fix up lastPos usage #14151

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ function parse(qs, sep, eq, options) {
if (lastPos < end) {
// Treat the substring as part of the key instead of the value
key += qs.slice(lastPos, end);
if (keyEncoded)
key = decodeStr(key, decode);
} else {
} else if (key.length === 0) {
// We saw an empty substring between separators
if (--pairs === 0)
return obj;
Expand All @@ -319,15 +317,15 @@ function parse(qs, sep, eq, options) {
continue;
}
} else {
if (lastPos < end) {
if (lastPos < end)
value += qs.slice(lastPos, end);
if (valEncoded)
value = decodeStr(value, decode);
}
if (keyEncoded)
key = decodeStr(key, decode);
}

if (key.length > 0 && keyEncoded)
key = decodeStr(key, decode);
if (value.length > 0 && valEncoded)
value = decodeStr(value, decode);

// Use a key array lookup instead of using hasOwnProperty(), which is
// slower
if (keys.indexOf(key) === -1) {
Expand Down Expand Up @@ -422,13 +420,13 @@ function parse(qs, sep, eq, options) {
key += qs.slice(lastPos);
else if (sepIdx < sepLen)
value += qs.slice(lastPos);
} else if (eqIdx === 0) {
} else if (eqIdx === 0 && key.length === 0) {
// We ended on an empty substring
return obj;
}
if (keyEncoded)
if (key.length > 0 && keyEncoded)
key = decodeStr(key, decode);
if (valEncoded)
if (value.length > 0 && valEncoded)
value = decodeStr(value, decode);
// Use a key array lookup instead of using hasOwnProperty(), which is slower
if (keys.indexOf(key) === -1) {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/url-searchparams.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = [
['=', '=', [['', '']]],
['+', '+=', [[' ', '']]],
['+=', '+=', [[' ', '']]],
['+&', '+=', [[' ', '']]],
['=+', '=+', [['', ' ']]],
['+=&', '+=', [[' ', '']]],
['a&&b', 'a=&b=', [['a', ''], ['b', '']]],
Expand All @@ -56,6 +57,14 @@ module.exports = [
['a=&a=value&a=', 'a=&a=value&a=', [['a', ''], ['a', 'value'], ['a', '']]],
['foo%20bar=baz%20quux', 'foo+bar=baz+quux', [['foo bar', 'baz quux']]],
['+foo=+bar', '+foo=+bar', [[' foo', ' bar']]],
['a+', 'a+=', [['a ', '']]],
['=a+', '=a+', [['', 'a ']]],
['a+&', 'a+=', [['a ', '']]],
['=a+&', '=a+', [['', 'a ']]],
['%20+', '++=', [[' ', '']]],
['=%20+', '=++', [['', ' ']]],
['%20+&', '++=', [[' ', '']]],
['=%20+&', '=++', [['', ' ']]],
[
// fake percent encoding
'foo=%©ar&baz=%A©uux&xyzzy=%©ud',
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,16 @@ const qsTestCases = [
['a=b&=c&d=e', 'a=b&=c&d=e', { a: 'b', '': 'c', d: 'e' }],
['a=b&=&c=d', 'a=b&=&c=d', { a: 'b', '': '', c: 'd' }],
['&&foo=bar&&', 'foo=bar', { foo: 'bar' }],
['&', '', {}],
['&&&&', '', {}],
['&=&', '=', { '': '' }],
['&=&=', '=&=', { '': [ '', '' ]}],
['=', '=', { '': '' }],
['+', '%20=', { ' ': '' }],
['+=', '%20=', { ' ': '' }],
['+&', '%20=', { ' ': '' }],
['=+', '=%20', { '': ' ' }],
['+=&', '%20=', { ' ': '' }],
['a&&b', 'a=&b=', { 'a': '', 'b': '' }],
['a=a&&b=b', 'a=a&b=b', { 'a': 'a', 'b': 'b' }],
['&a', 'a=', { 'a': '' }],
Expand All @@ -91,6 +98,14 @@ const qsTestCases = [
['a=&a=value&a=', 'a=&a=value&a=', { a: [ '', 'value', '' ] }],
['foo+bar=baz+quux', 'foo%20bar=baz%20quux', { 'foo bar': 'baz quux' }],
['+foo=+bar', '%20foo=%20bar', { ' foo': ' bar' }],
['a+', 'a%20=', { 'a ': '' }],
['=a+', '=a%20', { '': 'a ' }],
['a+&', 'a%20=', { 'a ': '' }],
['=a+&', '=a%20', { '': 'a ' }],
['%20+', '%20%20=', { ' ': '' }],
['=%20+', '=%20%20', { '': ' ' }],
['%20+&', '%20%20=', { ' ': '' }],
['=%20+&', '=%20%20', { '': ' ' }],
[null, '', {}],
[undefined, '', {}]
];
Expand Down