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/scripts/dev-setup.ts b/scripts/dev-setup.ts index 313156b..6e1e321 100644 --- a/scripts/dev-setup.ts +++ b/scripts/dev-setup.ts @@ -9,18 +9,24 @@ */ import { execFileSync } from "node:child_process"; import { randomBytes } from "node:crypto"; -import { existsSync, readFileSync, writeFileSync } from "node:fs"; +import { readFileSync, writeFileSync } from "node:fs"; const DEV_VARS = ".dev.vars"; function ensureDevVars(): string { - if (!existsSync(DEV_VARS)) { - const secret = randomBytes(32).toString("hex"); - const setup = randomBytes(24).toString("hex"); - writeFileSync(DEV_VARS, `AUTH_SECRET=${secret}\nSETUP_TOKEN=${setup}\n`, { mode: 0o600 }); + const secret = randomBytes(32).toString("hex"); + const setup = randomBytes(24).toString("hex"); + try { + // Created only if it is not there: the developer's own file is never written over. + writeFileSync(DEV_VARS, `AUTH_SECRET=${secret}\nSETUP_TOKEN=${setup}\n`, { + mode: 0o600, + flag: "wx", + }); process.stdout.write( `dev-setup: wrote ${DEV_VARS} with a new session secret and setup token\n`, ); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; } const match = /^SETUP_TOKEN=(.+)$/m.exec(readFileSync(DEV_VARS, "utf8")); return match?.[1]?.trim() ?? ""; diff --git a/scripts/e2e-server.ts b/scripts/e2e-server.ts index 5d7386f..c1e99eb 100644 --- a/scripts/e2e-server.ts +++ b/scripts/e2e-server.ts @@ -26,6 +26,16 @@ const QUIET = { ...process.env, CI: "1", WRANGLER_SEND_METRICS: "false" }; /** What the build put next to the Worker, so it can be put back when the tests are done. */ let builtVarsBefore: string | null = null; +/** A file's text, or null when it is not there: read without asking first, so nothing can change between. */ +function readIfThere(path: string): string | null { + try { + return readFileSync(path, "utf8"); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; + throw error; + } +} + function restore(): void { if (builtVarsBefore !== null) writeFileSync(BUILT_VARS, builtVarsBefore, { mode: 0o600 }); else rmSync(BUILT_VARS, { force: true }); @@ -45,7 +55,7 @@ function prepare(): void { }; // The build copied the developer's local variables next to the Worker. They are replaced // for the run and put back afterwards. - builtVarsBefore = existsSync(BUILT_VARS) ? readFileSync(BUILT_VARS, "utf8") : null; + builtVarsBefore = readIfThere(BUILT_VARS); writeFileSync( BUILT_VARS, `AUTH_SECRET=${secrets.authSecret}\nSETUP_TOKEN=${secrets.setupToken}\n`, diff --git a/scripts/pins.ts b/scripts/pins.ts index a0a26b5..a5b3797 100644 --- a/scripts/pins.ts +++ b/scripts/pins.ts @@ -81,7 +81,7 @@ export function newestSettled( } async function settled(name: string, now: number): Promise { - const res = await fetch(`${REGISTRY}/${name.replace("/", "%2F")}`, { + const res = await fetch(`${REGISTRY}/${name.replaceAll("/", "%2F")}`, { headers: { accept: "application/json" }, }); if (!res.ok) throw new Error(`${name}: registry answered ${res.status}`); 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/content-policy.test.ts b/test/unit/content-policy.test.ts index 4e139b4..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>/g)].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

", 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