From 4a778de705dc541ce1107bc383b4cb88d155efd5 Mon Sep 17 00:00:00 2001 From: ThinkerWen <296854007@qq.com> Date: Thu, 9 Oct 2025 11:18:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20URI=20malformed=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js-cookie-monitor-debugger-hook.js | 18 ++++++++++++++++-- src/utils/format.ts | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/old-code-backup/js-cookie-monitor-debugger-hook.js b/docs/old-code-backup/js-cookie-monitor-debugger-hook.js index e61e83a..fb5c0cf 100644 --- a/docs/old-code-backup/js-cookie-monitor-debugger-hook.js +++ b/docs/old-code-backup/js-cookie-monitor-debugger-hook.js @@ -322,6 +322,20 @@ return new CookiePair(key, value, expires ? new Date(expires).getTime() : null) } + /** + * 安全地解码 URI 组件 + * @param str + * @returns {string} + */ + function safeDecode(str) { + try { + return decodeURIComponent(str); + } catch (e) { + // 出错就原样返回,避免 URI malformed + return str; + } + } + /** * 把按照等号=拼接的key、value字符串切分开 * @param s @@ -332,11 +346,11 @@ const keyValueArray = (s || "").split("="); if (keyValueArray.length) { - key = decodeURIComponent(keyValueArray[0].trim()); + key = safeDecode(keyValueArray[0].trim()); } if (keyValueArray.length > 1) { - value = decodeURIComponent(keyValueArray.slice(1).join("=").trim()); + value = safeDecode(keyValueArray.slice(1).join("=").trim()); } return { diff --git a/src/utils/format.ts b/src/utils/format.ts index 8dc0568..3e95207 100644 --- a/src/utils/format.ts +++ b/src/utils/format.ts @@ -13,6 +13,20 @@ export function genFormatArray(messageAndStyleArray: string[]): string { return formatArray.join(""); } +/** + * 安全地解码 URI 组件 + * @param str + * @returns 解码后的字符串 + */ +function safeDecode(str: string) { + try { + return decodeURIComponent(str); + } catch (e) { + // 出错就原样返回,避免 URI malformed + return str; + } +} + /** * 把按照等号=拼接的key、value字符串切分开 * @param s 包含键值对的字符串 @@ -23,11 +37,11 @@ export function splitKeyValue(s: string): KeyValuePair { const keyValueArray = (s || "").split("="); if (keyValueArray.length) { - key = decodeURIComponent(keyValueArray[0].trim()); + key = safeDecode(keyValueArray[0].trim()); } if (keyValueArray.length > 1) { - value = decodeURIComponent(keyValueArray.slice(1).join("=").trim()); + value = safeDecode(keyValueArray.slice(1).join("=").trim()); } return {