From f75ce849ddf532f66ba97782b8a3453bdec8ca56 Mon Sep 17 00:00:00 2001 From: seungwoo Date: Thu, 3 Dec 2026 12:32:02 +0900 Subject: [PATCH] querystring: fix overlapping pair separators Match complete pair separators so a failed prefix match cannot hide a separator that starts inside that prefix. Cover overlaps, maxKeys, decoding, and separator precedence. Signed-off-by: seungwoo --- lib/querystring.js | 53 ++++++++----------- .../test-querystring-multichar-separator.js | 11 ++++ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/lib/querystring.js b/lib/querystring.js index c6dc1afcf5f2..6681eed092cc 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -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) { @@ -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; @@ -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 = ''; @@ -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]) { @@ -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 diff --git a/test/parallel/test-querystring-multichar-separator.js b/test/parallel/test-querystring-multichar-separator.js index 720733b1e29e..48308aefdcb6 100644 --- a/test/parallel/test-querystring-multichar-separator.js +++ b/test/parallel/test-querystring-multichar-separator.js @@ -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: '' });