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
8 changes: 5 additions & 3 deletions lib/internal/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ function encodeStr(str, noEscapeTable, hexTable) {
// This branch should never happen because all URLSearchParams entries
// should already be converted to USVString. But, included for
// completion's sake anyway.
if (i >= len)
if (i >= len || c >= 0xDC00)
throw new ERR_INVALID_URI();

const c2 = StringPrototypeCharCodeAt(str, i) & 0x3FF;
const c2 = StringPrototypeCharCodeAt(str, i);
if (c2 < 0xDC00 || c2 >= 0xE000)
throw new ERR_INVALID_URI();

lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
c = 0x10000 + (((c & 0x3FF) << 10) | (c2 & 0x3FF));
out += hexTable[0xF0 | (c >> 18)] +
hexTable[0x80 | ((c >> 12) & 0x3F)] +
hexTable[0x80 | ((c >> 6) & 0x3F)] +
Expand Down
29 changes: 19 additions & 10 deletions test/parallel/test-querystring-escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ assert.strictEqual(qs.escape({}), '%5Bobject%20Object%5D');
assert.strictEqual(qs.escape([5, 10]), '5%2C10');
assert.strictEqual(qs.escape('Ŋōđĕ'), '%C5%8A%C5%8D%C4%91%C4%95');
assert.strictEqual(qs.escape('testŊōđĕ'), 'test%C5%8A%C5%8D%C4%91%C4%95');
assert.strictEqual(qs.escape(`${String.fromCharCode(0xD800 + 1)}test`),
'%F0%90%91%B4est');
Comment on lines -13 to -14
Copy link
Copy Markdown
Member

@ChALkeR ChALkeR Apr 19, 2026

Choose a reason for hiding this comment

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

Note that this assertion is changed to throw, how encodeURIComponent behaves.

Copy link
Copy Markdown
Member

@ChALkeR ChALkeR Apr 19, 2026

Choose a reason for hiding this comment

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

It was introduced in #12163 and was just increasing coverage / documenting behavior, not by design likely.

assert.strictEqual(
qs.escape(`${String.fromCharCode(0xD800 + 1)}${String.fromCharCode(0xDC00)}`),
'%F0%90%90%80');

assert.throws(
() => qs.escape(String.fromCharCode(0xD800 + 1)),
{
code: 'ERR_INVALID_URI',
name: 'URIError',
message: 'URI malformed'
}
);
for (const invalid of [
String.fromCharCode(0xD800 + 1),
`${String.fromCharCode(0xD800 + 1)}test`,
`${String.fromCharCode(0xDC00)}test`,
`${String.fromCharCode(0xD800 + 1)}${String.fromCharCode(0x41)}`,
`${String.fromCharCode(0xD800 + 1)}${String.fromCharCode(0xD800)}`,
]) {
assert.throws(
() => qs.escape(invalid),
{
code: 'ERR_INVALID_URI',
name: 'URIError',
message: 'URI malformed',
},
);
}

// Using toString for objects
assert.strictEqual(
Expand Down
Loading