From 481adc6ecbd4ef047d299e568833df3947ccf3dd Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 31 Jul 2025 07:05:26 -0400 Subject: [PATCH] fix: Anchor with some UTF8 characters not correctly decoding (#9778) --- app/utils/urls.test.ts | 50 ++++++++++++++++++++++++++++++++++++++++++ app/utils/urls.ts | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/utils/urls.test.ts b/app/utils/urls.test.ts index 443546e71c..63f76712ab 100644 --- a/app/utils/urls.test.ts +++ b/app/utils/urls.test.ts @@ -10,4 +10,54 @@ describe("decodeURIComponentSafe", () => { expect(decodeURIComponentSafe("%7D")).toBe("}"); expect(decodeURIComponentSafe("%2F")).toBe("/"); }); + + test("to handle malformed percent encoding", () => { + // Single % at end of string + expect(decodeURIComponentSafe("test%")).toBe("test%"); + + // % followed by single hex digit + expect(decodeURIComponentSafe("test%A")).toBe("test%A"); + + // % followed by non-hex characters + expect(decodeURIComponentSafe("test%GG")).toBe("test%GG"); + expect(decodeURIComponentSafe("test%XY")).toBe("test%XY"); + + // % followed by space + expect(decodeURIComponentSafe("test% ")).toBe("test% "); + + // Multiple malformed % symbols + expect(decodeURIComponentSafe("%test%")).toBe("%test%"); + expect(decodeURIComponentSafe("%A%B")).toBe("%A%B"); + }); + + test("to handle mixed valid and invalid percent encoding", () => { + // Valid encoding mixed with invalid + expect(decodeURIComponentSafe("%20test%")).toBe(" test%"); + expect(decodeURIComponentSafe("%7D%invalid")).toBe("}%invalid"); + expect(decodeURIComponentSafe("valid%20%")).toBe("valid %"); + + // Complex mixed case + expect(decodeURIComponentSafe("%20%A%2F%")).toBe(" %A/%"); + }); + + test("to handle edge cases", () => { + // Empty string + expect(decodeURIComponentSafe("")).toBe(""); + + // Only % symbols + expect(decodeURIComponentSafe("%%%")).toBe("%%%"); + + // Language specific characters + expect(decodeURIComponentSafe("%E3%81%AB%E3%81%BB%E3%82%93%E3%81%94")).toBe( + "にほんご" + ); + + // % at various positions + expect(decodeURIComponentSafe("%start")).toBe("%start"); + expect(decodeURIComponentSafe("mid%dle")).toBe("mid%dle"); + expect(decodeURIComponentSafe("end%")).toBe("end%"); + + // Already encoded %25 should decode to % + expect(decodeURIComponentSafe("%25%25")).toBe("%%"); + }); }); diff --git a/app/utils/urls.ts b/app/utils/urls.ts index 66e3b374ff..7a0297cc98 100644 --- a/app/utils/urls.ts +++ b/app/utils/urls.ts @@ -30,7 +30,7 @@ export function isHash(href: string) { export function decodeURIComponentSafe(text: string) { try { return text - ? decodeURIComponent(text.replace(/%(?![0-9][0-9a-fA-F]+)/g, "%25")) + ? decodeURIComponent(text.replace(/%(?![0-9a-fA-F]{2})/g, "%25")) : text; } catch (_) { return text;