From 27e606516f70ec27ea9060b416c45bcc4793ac79 Mon Sep 17 00:00:00 2001 From: donald Date: Tue, 16 Sep 2025 17:52:31 +0800 Subject: [PATCH] fix(encoding): handle ^, ` and | characters correctly in URL encoding --- src/encoding.ts | 8 ++++++-- test/encoding.test.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/encoding.ts b/src/encoding.ts index 0708453..452a2b5 100644 --- a/src/encoding.ts +++ b/src/encoding.ts @@ -9,6 +9,9 @@ const SLASH_RE = /\//g; // %2F const EQUAL_RE = /=/g; // %3D const IM_RE = /\?/g; // %3F const PLUS_RE = /\+/g; // %2B +const CARET_RE = /\^/g; // %5E +const BACKTICK_RE = /`/g; // %60 +const PIPE_RE = /\|/g; // %7C const ENC_CARET_RE = /%5e/gi; // ^ const ENC_BACKTICK_RE = /%60/gi; // ` @@ -64,9 +67,10 @@ export function encodeQueryValue(input: QueryValue): string { .replace(ENC_SPACE_RE, "+") .replace(HASH_RE, "%23") .replace(AMPERSAND_RE, "%26") - .replace(ENC_BACKTICK_RE, "`") - .replace(ENC_CARET_RE, "^") + .replace(BACKTICK_RE, "%60") + .replace(CARET_RE, "%5E") .replace(SLASH_RE, "%2F") + .replace(PIPE_RE, "%7C") ); } diff --git a/test/encoding.test.ts b/test/encoding.test.ts index c110b23..db9667f 100644 --- a/test/encoding.test.ts +++ b/test/encoding.test.ts @@ -95,7 +95,7 @@ describe("encodeQueryValue", () => { }, { input: String.raw`!@#$%^&*()_+{}[]|\:;<>,./?`, - out: "!@%23$%25^%26*()_%2B%7B%7D%5B%5D|%5C:;%3C%3E,.%2F?", + out: "!@%23$%25%5E%26*()_%2B%7B%7D%5B%5D%7C%5C:;%3C%3E,.%2F?", }, ];