From 40671bfa50d86d6ef29d822d32690f35a3a61254 Mon Sep 17 00:00:00 2001 From: XZH Date: Sun, 23 Aug 2026 18:51:31 -0700 Subject: [PATCH 1/2] fix(chat): decode dense non-ASCII unicode escapes for display --- web/lib/markdown-display.ts | 45 ++++++++++++++++++++++++++++-- web/tests/markdown-display.test.ts | 35 +++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/web/lib/markdown-display.ts b/web/lib/markdown-display.ts index 0d451cc42..edcab6d0b 100644 --- a/web/lib/markdown-display.ts +++ b/web/lib/markdown-display.ts @@ -151,6 +151,7 @@ const MATH_SPAN_REGEX = // two delimiters the author meant to pair (see repairStrongEmphasisLine). const MALFORMED_STRONG_EMPHASIS_REGEX = /(? + INDENTED_CODE_LINE_REGEX.test(line) + ? line + : line.replace(ESCAPED_UNICODE_RUN_REGEX, decodeEscapedUnicodeRun), + ) + .join("\n"); + return fenced.restore(math.restore(inline.restore(decoded))); +} + +function decodeEscapedUnicodeRun(escaped: string): string { + try { + const value = JSON.parse(`"${escaped}"`) as string; + const containsNonAscii = Array.from(value).some( + (character) => character.charCodeAt(0) > 0x7f, + ); + return containsNonAscii ? value : escaped; + } catch { + return escaped; + } +} + export function normalizeMarkdownForDisplay(content: string): string { if (!content) return ""; - const normalized = stripInvisibleCharacters(String(content)) - .replace(/\r\n/g, "\n") + const normalized = decodeEscapedUnicodeRuns( + stripInvisibleCharacters(String(content)).replace(/\r\n/g, "\n"), + ) .replace(EMPTY_DETAILS_REGEX, "") .replace(EMPTY_SUMMARY_REGEX, "") .replace(EMPTY_PROGRESS_REGEX, "") diff --git a/web/tests/markdown-display.test.ts b/web/tests/markdown-display.test.ts index 7a93220e9..c714eaff8 100644 --- a/web/tests/markdown-display.test.ts +++ b/web/tests/markdown-display.test.ts @@ -85,6 +85,41 @@ test("normalizeMarkdownForDisplay removes empty details blocks", () => { assert.equal(normalizeMarkdownForDisplay(input), "Before\n\nAfter"); }); +test("normalizeMarkdownForDisplay decodes dense non-ASCII JSON escapes", () => { + const input = "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"; + assert.equal(normalizeMarkdownForDisplay(input), "「数制转换」"); +}); + +test("normalizeMarkdownForDisplay keeps isolated and ASCII unicode escape examples", () => { + const inputs = [ + "A JSON string can encode A as \\u0041.", + "Three ASCII escapes: \\u0041\\u0042\\u0043.", + ]; + + for (const input of inputs) { + assert.equal(normalizeMarkdownForDisplay(input), input); + } +}); + +test("normalizeMarkdownForDisplay keeps unicode escapes inside code verbatim", () => { + const input = [ + "Escaped text: `\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d`", + "", + "```json", + '"label": "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"', + "```", + ].join("\n"); + + assert.equal(normalizeMarkdownForDisplay(input), input); +}); + +test("normalizeMarkdownForDisplay keeps unicode escapes in indented code verbatim", () => { + const input = + 'Example:\n\n "label": "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"'; + + assert.equal(normalizeMarkdownForDisplay(input), input); +}); + test("normalizeMarkdownForDisplay removes raw html control placeholders", () => { const input = 'Before\n\n\n\n\n\nAfter'; From 20f95999c317ccd798c7ad00c91b901113d82d03 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Wed, 26 Aug 2026 01:29:58 +0800 Subject: [PATCH 2/2] fix(chat): strip decoded invisible unicode controls --- web/lib/markdown-display.ts | 9 +++++---- web/tests/markdown-display.test.ts | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/web/lib/markdown-display.ts b/web/lib/markdown-display.ts index edcab6d0b..cd6cf6687 100644 --- a/web/lib/markdown-display.ts +++ b/web/lib/markdown-display.ts @@ -1,6 +1,7 @@ "use client"; -const ZERO_WIDTH_REGEX = /[\u200B-\u200D\uFEFF]/g; +const INVISIBLE_CONTROL_REGEX = + /[\u200B-\u200F\u202A-\u202E\u2060\u2066-\u2069\uFEFF]/g; const EMPTY_DETAILS_REGEX = /]*)?>\s*(]*)?>\s*(?: |\s|)*\s*<\/summary>\s*)?<\/details>/gi; const EMPTY_SUMMARY_REGEX = @@ -16,7 +17,7 @@ const EMPTY_HTML_BLOCK_REGEX = const HTML_TABLE_REGEX = /]*)?>[\s\S]*?<\/table>/gi; function stripInvisibleCharacters(value: string): string { - return value.replace(ZERO_WIDTH_REGEX, ""); + return value.replace(INVISIBLE_CONTROL_REGEX, ""); } // Tags that the renderer (rehype-raw + react-markdown) is allowed to render @@ -668,8 +669,8 @@ function decodeEscapedUnicodeRun(escaped: string): string { export function normalizeMarkdownForDisplay(content: string): string { if (!content) return ""; - const normalized = decodeEscapedUnicodeRuns( - stripInvisibleCharacters(String(content)).replace(/\r\n/g, "\n"), + const normalized = stripInvisibleCharacters( + decodeEscapedUnicodeRuns(String(content).replace(/\r\n/g, "\n")), ) .replace(EMPTY_DETAILS_REGEX, "") .replace(EMPTY_SUMMARY_REGEX, "") diff --git a/web/tests/markdown-display.test.ts b/web/tests/markdown-display.test.ts index c714eaff8..a30557490 100644 --- a/web/tests/markdown-display.test.ts +++ b/web/tests/markdown-display.test.ts @@ -90,6 +90,17 @@ test("normalizeMarkdownForDisplay decodes dense non-ASCII JSON escapes", () => { assert.equal(normalizeMarkdownForDisplay(input), "「数制转换」"); }); +test("decoded unicode cannot reintroduce invisible or bidi controls", () => { + assert.equal( + normalizeMarkdownForDisplay("\\u200b\\u4e2d\\u6587"), + "中文", + ); + assert.equal( + normalizeMarkdownForDisplay("\\u202e\\u0061\\u0062"), + "ab", + ); +}); + test("normalizeMarkdownForDisplay keeps isolated and ASCII unicode escape examples", () => { const inputs = [ "A JSON string can encode A as \\u0041.",