Skip to content
Open
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
53 changes: 23 additions & 30 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ function charCodes(str) {
ret[i] = StringPrototypeCharCodeAt(str, i);
return ret;
}
const defSepCodes = [38]; // &
const defEqCodes = [61]; // =

function addKeyVal(obj, key, value, keyEncoded, valEncoded, decode) {
Expand Down Expand Up @@ -451,9 +450,10 @@ function parse(qs, sep, eq, options) {
return parseAmpEq(qs, obj);
}

const sepCodes = (!sep ? defSepCodes : charCodes(String(sep)));
const separator = sep ? String(sep) : '&';
const sepLen = separator.length;
const sepCode = sepLen === 1 ? StringPrototypeCharCodeAt(separator, 0) : -1;
const eqCodes = (!eq ? defEqCodes : charCodes(String(eq)));
const sepLen = sepCodes.length;
const eqLen = eqCodes.length;

let pairs = 1000;
Expand All @@ -474,7 +474,8 @@ function parse(qs, sep, eq, options) {
const customDecode = (decode !== qsUnescape);

let lastPos = 0;
let sepIdx = 0;
// Find complete separators so failed prefixes are processed as field data.
let nextSep = sepLen > 1 ? StringPrototypeIndexOf(qs, separator) : -1;
let eqIdx = 0;
let key = '';
let value = '';
Expand All @@ -486,39 +487,31 @@ function parse(qs, sep, eq, options) {
const code = StringPrototypeCharCodeAt(qs, i);

// Try matching key/value pair separator (e.g. '&')
if (code === sepCodes[sepIdx]) {
if (++sepIdx === sepLen) {
// Key/value pair separator match!
const end = i - sepIdx + 1;
if (eqIdx < eqLen) {
// We didn't find the (entire) key/value separator
if (lastPos < end) {
// Treat the substring as part of the key instead of the value
key += StringPrototypeSlice(qs, lastPos, end);
} else if (key.length === 0) {
// We saw an empty substring between separators
if (--pairs === 0)
return obj;
lastPos = i + 1;
sepIdx = eqIdx = 0;
continue;
}
} else if (lastPos < end) {
value += StringPrototypeSlice(qs, lastPos, end);
if (code === sepCode || i === nextSep) {
const end = i;
i += sepLen - 1;
const emptyPair = eqIdx < eqLen && lastPos === end && key.length === 0;
if (!emptyPair) {
if (lastPos < end) {
const part = StringPrototypeSlice(qs, lastPos, end);
if (eqIdx < eqLen)
key += part;
else
value += part;
}

addKeyVal(obj, key, value, keyEncoded, valEncoded, decode);

if (--pairs === 0)
return obj;
keyEncoded = valEncoded = customDecode;
key = value = '';
encodeCheck = 0;
lastPos = i + 1;
sepIdx = eqIdx = 0;
}
if (--pairs === 0)
return obj;
lastPos = i + 1;
eqIdx = 0;
if (sepLen > 1)
nextSep = StringPrototypeIndexOf(qs, separator, lastPos);
} else {
sepIdx = 0;
// Try matching key/value separator (e.g. '=') if we haven't already
if (eqIdx < eqLen) {
if (code === eqCodes[eqIdx]) {
Expand Down Expand Up @@ -585,7 +578,7 @@ function parse(qs, sep, eq, options) {
if (lastPos < qs.length) {
if (eqIdx < eqLen)
key += StringPrototypeSlice(qs, lastPos);
else if (sepIdx < sepLen)
else if (sepLen > 0)
value += StringPrototypeSlice(qs, lastPos);
} else if (eqIdx === 0 && key.length === 0) {
// We ended on an empty substring
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-querystring-multichar-separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ check(qs.parse('foo==>bar, bar==>baz', ', ', '==>'),

check(qs.stringify({ foo: 'bar', bar: 'baz' }, ', ', '==>'),
'foo==>bar, bar==>baz');

// A failed prefix match can overlap the start of the separator.
check(qs.parse('a=--_b=2', '-_'), { a: '-', b: '2' });
check(qs.parse('a=---_b=2', '--_'), { a: '-', b: '2' });
check(qs.parse('a=--_b=2', '-_', '=', { maxKeys: 1 }), { a: '-' });

// A partial separator in field data is still subject to decoding.
check(qs.parse('a=++_b=2', '+_'), { a: ' ', b: '2' });
check(qs.parse('a=%2D%_b=2', '%_'), { a: '-', b: '2' });

check(qs.parse('foo-_bar', '-_', '-_'), { foo: '', bar: '' });