Skip to content
Draft
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
10 changes: 8 additions & 2 deletions lib/internals/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ function encodeString(str) {
throw new Error("URI malformed");
}

const c2 = str.charCodeAt(i) & 0x3ff;
const c2 = str.charCodeAt(i);

// The second code unit must be a low surrogate (0xDC00–0xDFFF),
// otherwise the input is not a well-formed UTF-16 string.
if ((c2 & 0xfc00) !== 0xdc00) {
throw new Error("URI malformed");
}

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)] +
Expand Down
4 changes: 4 additions & 0 deletions test/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ test("invalid surrogate pair should throw", () => {
assert.throws(() => qs.stringify({ foo: "\udc00" }), "URI malformed");
});

test("high surrogate followed by non-surrogate should throw", () => {
assert.throws(() => qs.stringify({ foo: "a\ud800b" }), "URI malformed");
});

test("should omit nested values", () => {
const f = qs.stringify({
a: "b",
Expand Down
Loading