Skip to content

fix: the text of a message's HTML is made by one scanner, not regular expressions - #60

Merged
Madraka merged 3 commits into
mainfrom
fix/text-of-html
Nov 18, 2026
Merged

Madraka merged 3 commits into
mainfrom
fix/text-of-html

Conversation

@Madraka

@Madraka Madraka commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

What and why

The first run of code scanning on this repository raised twenty alerts. Each was followed to where its value ends up; none is a way in. This change corrects the nine that pointed at code worth correcting; the other eleven are dismissed on the alerts themselves, each with its reason.

Opened by the auditor from the builder's pushed branch and report; the code and tests are the builder's.

Corrected here

  • The text of a message's HTML was made in three places with regular expressions over markup (workers/lib/email-helpers.ts, app/components/EmailPanel.tsx, app/lib/utils.ts). All three now use the one scanner the project already owns (shared/html-text.ts): the content of a script or a style is never text, however its tags are written, and each character reference is decoded once. The old helper decoded twice, which was a correctness defect (&amp;lt; became <), not an injection: every place that uses the text as HTML escapes it first, and the others put it in a text node, give it to a model, or send it as the text part of a message.
  • Tooling and a test: scripts/pins.ts replaces every / of a package name, scripts/dev-setup.ts creates .dev.vars only if it is not there in one step, scripts/e2e-server.ts reads without asking first, and the script tag pattern of test/unit/content-policy.test.ts matches a closing tag with a space.

Dismissed on the alert, with the reason there

  • Eight "file sent over the network" and one "message without an origin check": browser-test code that sends its own fixtures to the local server it started, and the tests' recorder. Used in tests.
  • One "file system race": scripts/checks/copies.ts --fix on the developer's own checkout. Won't fix.
  • One "regular expression without an anchor": the expression finds a mention of a host in source so that a static check can forbid it; it checks no URL. False positive.

Regression tests

test/unit/html-text.test.ts, "the text Podbay makes of a stranger's HTML": for each of the callers, script and style content is never kept and no tag survives; each character reference is decoded once; a quoted reply shows what was written as text and holds no element of it.

Runs (the builder's, one at a time, on this branch)

pnpm check green (725 unit tests), Workers runtime 630 of 630, browser 86 of 86.

Checklist

  • pnpm check and pnpm test:workerd are green.
  • Original work. Nothing here comes from another mail product: no code, schema, test, text, translation or asset. No dependency under AGPL or GPL.
  • It touches a trust boundary (what is made of a stranger's HTML) and ships the regression tests named above.
  • It relies on no Cloudflare fact.
  • Commits are conventional and signed off (git commit -s).

🤖 Generated with Claude Code

Madraka and others added 2 commits November 18, 2026 13:41
… 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 `&amp;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 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
…a test

- scripts/pins.ts encodes every slash of a package name, not the
  first; a scoped name has one, so nothing changes today.
- scripts/dev-setup.ts creates .dev.vars only when it is not there
  (`wx`), instead of asking first and writing after.
- scripts/e2e-server.ts reads the built Worker's variables without
  asking first whether the file is there.
- test/unit/content-policy.test.ts finds the document's scripts with
  any case and with a space before the end of the closing tag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka Madraka added this to the Milestone 3: version 0.1 milestone Nov 18, 2026
@Madraka Madraka added bug Something isn't working security Touches a trust boundary. A vulnerability is never reported in an issue: see SECURITY.md area: app The single-page app under app/ area: worker The Worker: routes, inbound mail, configuration labels Nov 18, 2026
Comment thread test/unit/content-policy.test.ts Fixed
@Madraka

Madraka commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

audit: one thing to correct, then pass

Read: both commits line by line, and every caller of the three changed functions to where its value ends up. Tests and the two analyses are green; the code scanning gate is not.

  • The gate fails on one new alert, in test/unit/content-policy.test.ts:87: the corrected pattern still does not match an end tag written as </script followed by whitespace and more before >. The test reads the project's own built page, so nothing can be made of it, but the ruleset is right to stop here: the correction of alert chore(deps): Bump better-auth from 1.7.23 to 1.7.25 #2 produced the same alert again. Match the end tag as <\/script\b[^>]*> (or dismiss the new alert as used in tests, with the reason; the first is cleaner).

What I checked and found to hold:

  • stripHtml (app) and stripHtmlToText (Worker) now decode character references, which they did not before. So a body that holds &lt;script&gt; now yields the text <script>. Every use was followed: the two quoted-reply builders escape it before it becomes HTML (escapeHtml(...) in app/lib/utils.ts, app/hooks/useComposeForm.ts, workers/lib/email-helpers.ts); the thread header and the list snippets put it in a text node; the rest is given to a model or a tool as text, or sent as the text part of a message. No use puts it into markup unescaped.
  • The scanner itself is unchanged; only decodeEntities is exported and a one-line variant added. Each reference is decoded once (a single pass over the text).
  • scripts/dev-setup.ts now creates .dev.vars with an exclusive create, so a developer's file is never written over; scripts/e2e-server.ts reads without asking first.
  • The eleven dismissals are on the alerts with their reasons; I read each flagged line before dismissing.

Not done by the auditor: the suites were not run a second time locally; CI ran them.

…g in it

Code scanning still flagged the pattern of the previous commit: an end
tag such as `</script bar>` 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 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka

Madraka commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

audit: pass

The end tag of the test's pattern is corrected in 0e2d1fe as asked, and the code scanning gate now passes with the other five checks. Nothing else changed since my reading above. To be merged with a merge commit.

@Madraka
Madraka merged commit 070226b into main Nov 18, 2026
6 checks passed
@Madraka
Madraka deleted the fix/text-of-html branch November 18, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: app The single-page app under app/ area: worker The Worker: routes, inbound mail, configuration bug Something isn't working security Touches a trust boundary. A vulnerability is never reported in an issue: see SECURITY.md

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants