From 8e95b8264a70a79a6a732421b3dd6f3d3614540f Mon Sep 17 00:00:00 2001 From: Madraka <8898640+Madraka@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:41:07 +0300 Subject: [PATCH 1/3] fix: the text of a message's HTML is made by one scanner, not regular expressions Three helpers made text of HTML with regular expressions: the Worker's text for a model, a tool and a quoted reply, the app's text part of a draft that is sent, and the app's decoding of character references. They missed an end tag written with a space, a line break or capitals, and decoded `&lt;` twice. Each result was text: escaped before it became HTML, put into a text node, given to a model, or not used by the server while the message has HTML. So none could inject markup, but the text was wrong, and code scanning flagged all three. All of them now use shared/html-text.ts, which already scans markup once and never keeps script or style content: `oneLineTextOf` for one line, `plainTextOf` with line breaks, each decoding a reference once. test/unit/html-text.test.ts gives each of the four makers the forms the expressions missed and a reference written twice, and quotes a message whose script is written with a space. Six of its tests fail with the old helpers. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com> --- app/components/EmailPanel.tsx | 4 +-- app/lib/utils.ts | 28 +++++-------------- shared/html-text.ts | 11 +++++++- test/unit/html-text.test.ts | 51 +++++++++++++++++++++++++++++++++++ tsconfig.test.json | 1 + workers/lib/email-helpers.ts | 15 +++++------ 6 files changed, 77 insertions(+), 33 deletions(-) diff --git a/app/components/EmailPanel.tsx b/app/components/EmailPanel.tsx index ae6e3c6..4f682af 100644 --- a/app/components/EmailPanel.tsx +++ b/app/components/EmailPanel.tsx @@ -27,7 +27,7 @@ import { useShortcuts } from "~/hooks/useShortcuts"; import { useUIStore } from "~/hooks/useUIStore"; import { plural, t } from "~/i18n"; import { unsubscribeOf } from "~/lib/unsubscribe"; -import { splitEmailList, toEmailListValue } from "~/lib/utils"; +import { htmlToPlainText, splitEmailList, toEmailListValue } from "~/lib/utils"; import { useDeleteEmail, useEmail, useThreadReplies } from "~/queries/emails"; import { useMailbox } from "~/queries/mailboxes"; import api from "~/services/api"; @@ -322,7 +322,7 @@ export default function EmailPanel({ emailId, siblings = [], inPane = false }: E from, subject: target.subject || t("(no subject)"), html: target.body || "", - text: target.body ? target.body.replace(/<[^>]*>/g, "").trim() : "", + text: target.body ? htmlToPlainText(target.body) : "", }; // The mailbox takes it; the notes that follow, Undo and what became of it, are the // hook's. Nothing here says "sent" (ADR-0016). diff --git a/app/lib/utils.ts b/app/lib/utils.ts index 0f0315e..93e2677 100644 --- a/app/lib/utils.ts +++ b/app/lib/utils.ts @@ -11,7 +11,7 @@ */ import DOMPurify from "dompurify"; import { formatQuotedDate } from "shared/dates"; -import { htmlToText } from "shared/html-text"; +import { oneLineTextOf, plainTextOf } from "shared/html-text"; import { quoteLanguage, t } from "../i18n"; export { @@ -57,37 +57,23 @@ export function toEmailListValue(addresses: string[]): string | string[] | undef * Convert HTML content to plain text, keeping line breaks between blocks. */ export function htmlToPlainText(html: string): string { - // No DOM and no HTML injection: script and style content is never text (shared/html-text). - return decodeHtmlEntities(htmlToText(html, { keepLineBreaks: true })); + // No DOM and no HTML injection: script and style content is never text, and each character + // reference is decoded once (shared/html-text). + return plainTextOf(html); } /** * Strip all HTML tags from a string. */ export function stripHtml(html: string): string { - return htmlToText(html); -} - -function decodeHtmlEntities(text: string): string { - return text - .replace(/&#(\d+);/g, (_match: string, code: string) => String.fromCharCode(Number(code))) - .replace(/&#x([0-9a-f]+);/gi, (_match: string, hex: string) => - String.fromCharCode(Number.parseInt(hex, 16)), - ) - .replace(/&/g, "&") - .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/"/g, '"') - .replace(/'/g, "'") - .replace(/'/g, "'") - .replace(/ /g, " "); + return oneLineTextOf(html); } export function getSnippetText(snippet?: string | null, maxLength = 100): string { if (!snippet) return ""; - // htmlToText never shows script or style content, even when the snippet is cut mid-element. - const clean = decodeHtmlEntities(htmlToText(snippet)).replace(/\s+/g, " ").trim(); + // Never script or style content, even when the snippet is cut mid-element. + const clean = oneLineTextOf(snippet); if (!clean) return ""; return clean.length > maxLength ? `${clean.slice(0, maxLength)}...` : clean; diff --git a/shared/html-text.ts b/shared/html-text.ts index 23be5cd..fcf9cca 100644 --- a/shared/html-text.ts +++ b/shared/html-text.ts @@ -136,7 +136,7 @@ const NAMED: Record = { }; /** Character references back to characters, each once: what one makes is not read again. */ -function decodeEntities(text: string): string { +export function decodeEntities(text: string): string { return text.replace( /&(#\d{1,7}|#x[0-9a-f]{1,6}|lt|gt|quot|apos|nbsp|amp);/gi, (match: string, name: string) => { @@ -159,3 +159,12 @@ function decodeEntities(text: string): string { export function plainTextOf(html: string): string { return decodeEntities(htmlToText(html, { keepLineBreaks: true })); } + +/** + * The text of an HTML fragment on one line, its character references decoded once: for what a + * model, a tool or a quoted reply is given. Text, never markup: whatever uses it as HTML + * escapes it first. + */ +export function oneLineTextOf(html: string): string { + return decodeEntities(htmlToText(html)); +} diff --git a/test/unit/html-text.test.ts b/test/unit/html-text.test.ts index 8a922a6..81c2ad3 100644 --- a/test/unit/html-text.test.ts +++ b/test/unit/html-text.test.ts @@ -1,5 +1,7 @@ import { describe, expect, it } from "vitest"; +import { getSnippetText, htmlToPlainText, stripHtml } from "../../app/lib/utils.ts"; import { htmlToText } from "../../shared/html-text.ts"; +import { buildQuotedReplyBlock, stripHtmlToText } from "../../workers/lib/email-helpers.ts"; describe("htmlToText", () => { it("keeps the text and drops the tags", () => { @@ -77,3 +79,52 @@ describe("htmlToText with line breaks kept", () => { expect(text.endsWith("b")).toBe(true); }); }); + +describe("the text Podbay makes of a stranger's HTML", () => { + // The forms a regular expression over markup misses: an end tag with a space or a line + // break in it, capitals, a tag inside a tag, and a character reference written twice. + const HOSTILE = [ + "

before

after

", + "

before

after

", + "

before

after

"; + const makers = { + "the Worker's text for a model, a tool and a quote": stripHtmlToText, + "the app's one-line text": stripHtml, + "the app's list snippet": (html: string) => getSnippetText(html, 500), + "the app's text part of a message": htmlToPlainText, + }; + + for (const [who, make] of Object.entries(makers)) { + it(`${who}: never keeps script or style content, and never a tag`, () => { + for (const html of HOSTILE) { + const text = make(html); + expect(text, html).toContain("before"); + expect(text, html).not.toContain("steal()"); + expect(text, html).not.toContain("p{}"); + expect(text, html).not.toMatch(/<\/?[a-z]/i); + } + expect(make(NESTED)).not.toMatch(/<\/?[a-z]/i); + }); + + it(`${who}: decodes each character reference once`, () => { + expect(make("

a &lt;b&gt; c

")).toBe("a <b> c"); + expect(make("

café & co

")).toBe("café & co"); + }); + } + + it("a quoted reply shows what was written as text, and the reply holds no element of it", () => { + const quoted = buildQuotedReplyBlock({ + sender: "ada@sender.test", + date: "2026-11-18T10:00:00Z", + body: "

<script>steal()</script>

", + }); + expect(quoted).toContain("<script>steal()</script>"); + expect(quoted).not.toContain(" and ` closes a script in a browser too. The end tag is now matched as the start tag is, by its name and up to `>`. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com> --- test/unit/content-policy.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/unit/content-policy.test.ts b/test/unit/content-policy.test.ts index c4df5ab..3a999cd 100644 --- a/test/unit/content-policy.test.ts +++ b/test/unit/content-policy.test.ts @@ -84,7 +84,9 @@ describe("the frame's sizing script", () => { allowRemote: false, nonce, }); - const scripts = [...html.matchAll(/]*>([\s\S]*?)<\/script\s*>/gi)].map((m) => m[1]); + const scripts = [...html.matchAll(/]*>([\s\S]*?)<\/script\b[^>]*>/gi)].map( + (m) => m[1], + ); expect(scripts).toEqual([MAIL_FRAME_SCRIPT]); const without = mailDocument({ sanitizedBody: "

Hi

",