From c07741bf54fe0ca46de4e19953d950687ee583af Mon Sep 17 00:00:00 2001 From: RhenCloud Date: Sat, 24 Oct 2026 16:41:29 +0800 Subject: [PATCH] fix(security): harden URL and HTML sanitization --- server/lib/drivers/telegram/index.ts | 2 +- server/lib/drivers/telegram/render.ts | 2 +- server/lib/formatters/helpers.ts | 3 +++ tests/discord-interactions.test.ts | 4 ++-- tests/discord.test.ts | 2 +- tests/telegram-commands.test.ts | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/server/lib/drivers/telegram/index.ts b/server/lib/drivers/telegram/index.ts index 0e22eb8..ab4b236 100644 --- a/server/lib/drivers/telegram/index.ts +++ b/server/lib/drivers/telegram/index.ts @@ -7,7 +7,7 @@ import { renderNeutralMessage } from "./render"; const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false }); function markdownToText(value: string): string { - return markdown.renderInline(value).replace(/<[^>]*>/gu, ""); + return markdown.renderInline(value).replace(/<[^>]*>/gu, "").replace(/ max - 1) { - const rest = text.slice(i).replace(/<[^>]*>/g, ""); + const rest = text.slice(i).replace(/<[^>]*>/g, "").replace(/") .replace(/"/gi, '"') .replace(/'/gi, "'") + // Entity decoding can reveal a tag prefix after the first pass. Remove + // angle brackets again so encoded ` codePoint(parseInt(hex, 16), m)) .replace(/&#(\d+);/g, (m, dec: string) => codePoint(Number(dec), m)) .replace(/&/gi, "&"); diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts index d6e7b24..c752815 100644 --- a/tests/discord-interactions.test.ts +++ b/tests/discord-interactions.test.ts @@ -151,7 +151,7 @@ describe("discord interactions", () => { const url = String(input); if (url.includes("/callback")) { order.push(`callback:${(JSON.parse(String(init?.body)) as { type: number }).type}`); - } else if (url.includes("api.github.com")) { + } else if (new URL(url).hostname === "api.github.com") { order.push("github"); return new Response( JSON.stringify({ html_url: "https://github.com/o/r/issues/7#c", user: { login: "a" } }), @@ -203,7 +203,7 @@ describe("discord interactions", () => { components: [{ components: [{ custom_id: "body", value: "hello" }] }], }, }); - expect(calls.some((u) => u.includes("api.github.com"))).toBe(false); + expect(calls.some((u) => new URL(u).hostname === "api.github.com")).toBe(false); }); it("refuses to save an edit over changes made on GitHub after the modal opened", async () => { diff --git a/tests/discord.test.ts b/tests/discord.test.ts index 3a7367c..069e690 100644 --- a/tests/discord.test.ts +++ b/tests/discord.test.ts @@ -657,7 +657,7 @@ describe("dispatchEvent fallback routing", () => { const sent: string[] = []; const bodies: string[] = []; mockFetch((url, init) => { - if (url.includes("api.github.com") && url.includes("/jobs")) { + if (new URL(url).hostname === "api.github.com" && url.includes("/jobs")) { return Response.json({ jobs: [ { diff --git a/tests/telegram-commands.test.ts b/tests/telegram-commands.test.ts index 594fd96..8e35efe 100644 --- a/tests/telegram-commands.test.ts +++ b/tests/telegram-commands.test.ts @@ -330,7 +330,7 @@ describe("telegram-commands refusals and whitespace", () => { let replies = 0; let github = 0; mockFetch((url) => { - if (url.includes("api.github.com")) github++; + if (new URL(url).hostname === "api.github.com") github++; replies++; return new Response(JSON.stringify({ ok: false, description: "Bad Gateway" }), { status: 502,