From e94572922d6993ec43be3889a9d13bf86847f26d Mon Sep 17 00:00:00 2001 From: Vianney Carel Date: Mon, 2 Jun 2025 14:07:48 +0200 Subject: [PATCH] Fix decoding queries encoded twice --- packages/router-core/src/qss.ts | 8 ++------ packages/router-core/src/utils.ts | 17 ----------------- packages/router-core/tests/qss.test.ts | 6 ++++++ 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/packages/router-core/src/qss.ts b/packages/router-core/src/qss.ts index cb1912b8468..2611389f375 100644 --- a/packages/router-core/src/qss.ts +++ b/packages/router-core/src/qss.ts @@ -7,7 +7,6 @@ * (namely URLSearchParams) and TypeScript while still * maintaining the original functionality and interface. */ -import { hasUriEncodedChars } from './utils' /** * Encodes an object into a query string. @@ -42,11 +41,8 @@ export function encode(obj: any, pfx?: string) { * // Example input: toValue("123") * // Expected output: 123 */ -function toValue(mix: any) { - if (!mix) return '' - const str = hasUriEncodedChars(mix) - ? decodeURIComponent(mix) - : decodeURIComponent(encodeURIComponent(mix)) +function toValue(str: unknown) { + if (!str) return '' if (str === 'false') return false if (str === 'true') return true diff --git a/packages/router-core/src/utils.ts b/packages/router-core/src/utils.ts index 5033dbfe9ef..5beb9d5bb99 100644 --- a/packages/router-core/src/utils.ts +++ b/packages/router-core/src/utils.ts @@ -446,20 +446,3 @@ export function shallow(objA: T, objB: T) { } return true } - -/** - * Checks if a string contains URI-encoded special characters (e.g., %3F, %20). - * - * @param {string} inputString The string to check. - * @returns {boolean} True if the string contains URI-encoded characters, false otherwise. - * @example - * ```typescript - * const str1 = "foo%3Fbar"; - * const hasEncodedChars = hasUriEncodedChars(str1); // returns true - * ``` - */ -export function hasUriEncodedChars(inputString: string): boolean { - // This regex looks for a percent sign followed by two hexadecimal digits - const pattern = /%[0-9A-Fa-f]{2}/ - return pattern.test(inputString) -} diff --git a/packages/router-core/tests/qss.test.ts b/packages/router-core/tests/qss.test.ts index 8bff5c3a64d..ae2516c1fe8 100644 --- a/packages/router-core/tests/qss.test.ts +++ b/packages/router-core/tests/qss.test.ts @@ -104,4 +104,10 @@ describe('decode function', () => { const decodedObj = decode(queryString) expect(decodedObj).toEqual({ q: 'red+yellow orange' }) }) + + it('should decode once percent characters (%) encoded twice', () => { + const queryString = 'q=%2540' + const decodedObj = decode(queryString) + expect(decodedObj).toEqual({ q: '%40' }) + }) })