From 7ca422d2586e311d0032f47708fdf48481c59ae9 Mon Sep 17 00:00:00 2001 From: brick30llc-ctrl Date: Sun, 12 Jul 2026 08:26:21 -0500 Subject: [PATCH] fix(sse): escape backslash in ChatGPT-web citation link text (#6569) (#6944) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sse): escape backslash in ChatGPT-web citation link text (#6569) markdownLinkText() escaped [ and ] but not the backslash itself, so a citation label ending in (or containing) a backslash produced a broken Markdown link — e.g. [Path C:\](url), where the trailing \ escapes the closing bracket and consumes the link. Escape the backslash first, then the brackets. Clears the CodeQL js/incomplete-sanitization alerts at open-sse/executors/chatgpt-web/citations.ts:52 (2 of the 9 new alerts on the v3.8.47 release PR). Regression guard: tests/unit/chatgpt-web-citations-escape.test.ts (trailing backslash, backslash-before-bracket, bracket-only, plain). * chore(changelog): add changelog.d fragment for #6944 Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: brick30llc-ctrl Co-authored-by: Diego Rodrigues de Sa e Souza Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --- ...4-chatgpt-web-citation-backslash-escape.md | 1 + open-sse/executors/chatgpt-web/citations.ts | 10 +++- .../unit/chatgpt-web-citations-escape.test.ts | 50 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 changelog.d/fixes/6944-chatgpt-web-citation-backslash-escape.md create mode 100644 tests/unit/chatgpt-web-citations-escape.test.ts diff --git a/changelog.d/fixes/6944-chatgpt-web-citation-backslash-escape.md b/changelog.d/fixes/6944-chatgpt-web-citation-backslash-escape.md new file mode 100644 index 0000000000..cefb3d8800 --- /dev/null +++ b/changelog.d/fixes/6944-chatgpt-web-citation-backslash-escape.md @@ -0,0 +1 @@ +- **fix(sse):** escape backslash before brackets in ChatGPT-web citation link text, preventing a citation label containing `\` from corrupting the generated Markdown link ([#6944](https://github.com/diegosouzapw/OmniRoute/pull/6944)) — thanks @brick30llc-ctrl diff --git a/open-sse/executors/chatgpt-web/citations.ts b/open-sse/executors/chatgpt-web/citations.ts index 395a18317c..2f61830ad8 100644 --- a/open-sse/executors/chatgpt-web/citations.ts +++ b/open-sse/executors/chatgpt-web/citations.ts @@ -49,7 +49,15 @@ function asArray(value: unknown): unknown[] { } function markdownLinkText(value: string): string { - return value.replace(/\[/g, "\\[").replace(/\]/g, "\\]").replace(/\n/g, " ").trim(); + // Escape the backslash first — otherwise a label ending in (or containing) a + // backslash leaks past the `[…]` escaping and breaks the generated link, e.g. + // `[Path C:\](url)` where the trailing `\` escapes the closing bracket. + return value + .replace(/\\/g, "\\\\") + .replace(/\[/g, "\\[") + .replace(/\]/g, "\\]") + .replace(/\n/g, " ") + .trim(); } function markdownUrl(value: string): string { diff --git a/tests/unit/chatgpt-web-citations-escape.test.ts b/tests/unit/chatgpt-web-citations-escape.test.ts new file mode 100644 index 0000000000..540183e959 --- /dev/null +++ b/tests/unit/chatgpt-web-citations-escape.test.ts @@ -0,0 +1,50 @@ +// ChatGPT-web citation link-text escaping (CodeQL js/incomplete-sanitization, +// PR #6569 release-blocker). +// +// `markdownLinkText()` builds the `[text]` half of a Markdown link from an +// untrusted citation label. It escaped `[` and `]` but NOT the backslash +// itself, so a label ending in (or containing) a backslash produced a broken +// link: e.g. `[Path C:\](url)` — the trailing `\` escapes the closing `]`, +// consuming the link's bracket. The escape character must be escaped first. + +import test from "node:test"; +import assert from "node:assert/strict"; + +const { cleanChatGptText } = await import( + "../../open-sse/executors/chatgpt-web/citations.ts" +); + +const S = ""; // marker start +const SEP = ""; // marker separator +const E = ""; // marker end + +// Build a raw `url` citation marker:  url 