Skip to content

feat(worker): send the password reset mail from the Worker through Cloudflare Email Sending (E3) - #200

Merged
aRustyDev merged 7 commits into
mainfrom
feat/email-cloudflare
Nov 16, 2026
Merged

aRustyDev merged 7 commits into
mainfrom
feat/email-cloudflare

Conversation

@aRustyDev

@aRustyDev aRustyDev commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

What

The third piece of the mail path for the password reset (E3): the Worker's send_email binding as the EmailSender port, built by thrust-worker and carried by the deploy template.

With this PR the Worker can send the reset mail, when it is given a sender (THRUST_EMAIL_FROM) and a THRUST_PUBLIC_BASE_URL. A Worker with no sender is dark exactly as before. Merging does change what the next staging deploy does: I set [email protected] on the staging and staging-do-lifecycle environments (with your go-ahead), so the next staging deploy turns the reset on at thrust-staging.sow.foo. See the first item under "Things you may want to look at".

Refs #46 (it does not close it).

The plan, and where this PR sits

PR What State
E1 (#169) the EmailSender port, the token table, the two routes, is_smtp_configured from configuration merged
E2 (#176) SmtpEmail on lettre, wired into thrust-server merged
E3 (this) CfEmail on the Worker's send_email binding, the Worker's wiring, the deploy template's binding and sender this PR
E4 the email code (F-A04, P1) not started

What it does

Configuration, read by the Worker on each request (crates/thrust-adapters-cloudflare/src/email.rs; the runbook is docs/src/architecture/infra.md):

THRUST_EMAIL_FROM the sender: one plain address on a domain the account has Email Sending enabled for. Unset is no mail service
THRUST_EMAIL_FROM_NAME the name beside it (Thrust). Optional; refused without a sender
[[send_email]] name = "EMAIL" the binding. There is no relay, password or token: the binding is the credential
  • A half-written configuration is an error the Worker reports on every request and not a Worker that quietly has no reset: a sender with no binding, a sender that is a name or a list, a name with no sender (the OIDC provider's reasoning).
  • The recipient must be one plain address and the subject must have no line break, or the message is refused and nothing is sent (the core makes both from checked values, so this is the second line).
  • The binding's answer: the 21 codes of Cloudflare's own error table are each Rejected (the message or the sender or the recipient is one the service will not use) or Unavailable (a limit, a paused account, an outage, a delivery that failed). A code nobody has met is Unavailable: a mail sent again later is the lesser harm. A refusal with no code, which is how both the local simulator and the real service's refusal of an unowned sender arrived, is told by its words, "not allowed".
  • The mail goes in the request, as on the native server: no queue, no retry (D-041, 5).

Deploy (wrangler.deploy.toml, scripts/render-wrangler.sh, deploy.yml):

  • The template gains the EMAIL binding restricted to the one configured sender (allowed_sender_addresses), which the binding itself enforces whatever the code asks for.
  • THRUST_EMAIL_FROM is a new per-environment GitHub variable with no default, like THRUST_CSRF_ENFORCE. The links in the mails use the THRUST_BASE_URL the smoke already uses, so they point at the origin people reach and cannot disagree with it (no second variable).
  • The renderer checks both values before it writes anything: a sender is one plain lowercase address (no name, list or header) on a domain that is not a placeholder (example.com, .test, ...); an origin is https://<host> with no path, port or trailing slash and not of this machine. Its self-test has a control for each, and it caught one hole in my first draft (a sender with no @).
  • deploy_parity.py compares the template with wrangler.toml for the new binding, requires the template's restriction to be the sender marker, and requires the three mail variables to be the deployment's: wrangler.toml must not set them, so a development run has no mail service nobody asked for.

Things you may want to look at

  • Merging turns the reset on at staging at the next deploy. thrust-staging.sow.foo then answers is_smtp_configured: true, the app draws the reset link, and anyone with an account there who asks is mailed from [email protected]. The only limit is the per-account cooldown (G-37), so many different accounts can be mailed quickly and that spends a real mail budget (3,000 a month are included). Staging's CLOUDFLARE_ACCOUNT_ID is the account that has notify.sow.foo enabled (compared by prefix), which a binding needs: it refuses a sender "not owned by the same account".
  • production has no THRUST_EMAIL_FROM (it has no variables at all yet), so a production deploy refuses to render until someone chooses one. That is the fail-closed shape of THRUST_CSRF_ENFORCE; if you would rather have an explicit "off" for an environment that should not mail, say so.
  • The renderer refuses placeholder sender domains. It is a small opinion, in the spirit of its thrust-spike refusal: a Worker deployed with [email protected] passes every check and refuses every reset at send time.
  • The real send was not made from a deployed Worker. With your approval of the recipient and a throwaway Worker, I used the lighter route: the product's own wasm Worker run by wrangler dev with only the Email Sending binding remote, on the account that has the domain (nothing deployed, so nothing to delete). See Measured.
  • Two new CI cells, worker-memory mail and worker-cf mail (the plan-ci.py planner keeps them whenever thrust-worker or what it depends on changes; checked on this diff). No container; worker-cf has the Postgres container every worker-cf cell has.
  • memory::app takes a small Optional { oidc, email } struct instead of two more parameters (clippy's too_many_arguments).

Tests and controls

  • tests/email.rs (native, 99 tests in the default loop): the 21 codes of Cloudflare's own table, pinned in the test and not taken from the adapter, each read as the table says with every kind of message; a code the table does not list is an outage whatever the message says; a refusal with no code is told by its words; an address that is not one plain address is refused with any of 19 hostile characters at any position, and whatever the guard accepts a second parser (mailparse) reads as exactly that one address; a subject with a line break or another control character, an empty one and one longer than a header line are refused; the configuration is read for every sender with every name (88 combinations).
  • The mail pass on the Worker targets (worker-memory mail, worker-cf mail, 3 tests each): the Worker built for wasm32, run by wrangler dev, whose send_email binding is wrangler's own simulator. It sends nothing and writes each message to the dev server's log and to files, which the tests read from outside the adapter into the shape Mailpit's API has, so the same three tests run on native and Worker targets. The binding in the memory config is restricted to the pass's sender, as the deploy template's is, so the pass also shows that the restriction lets the adapter's named sender through.
  • Found about the simulator (wrangler 4.123.0), not planned: it logs a message a few milliseconds after the response that caused it, so my first run read an empty log, and a "nothing was mailed" check would have passed for want of waiting; the tests now read after a marker message (one more reset, waited for in the log, which is in order). It does not validate an address and lets a line break in a subject through (the adapter's own check does that); a refusal is a plain Error with a message and no code.
  • Planted bugs: four in the adapter (mutant_cf_email_*: a refusal read as an outage and the reverse; an unknown code read as a refusal; the sender taken from the recipient; a recipient that is not one address handed to the binding), each killed by the test aimed at it and each with a scripts/mutants.sh row. One in the Worker (mutant_worker_email_unwired). The Worker's two are hand controls on worker-memory mail, because scripts/mutants.sh does not build the wasm: unwired, the pass is refused by its vacuity guard (exit 5, no test run); with the sender taken from the recipient, the simulator refuses the send, the adapter reads the real JS error as Rejected, and 2 of 3 tests fail.

Measured

Run Result
scripts/contract.sh <target> main: native-memory, worker-memory, worker-cf 70 of 70 on each (EXPECT_MAIN unchanged). native-pg was not run again: nothing here reaches it
scripts/contract.sh <target> mail: native-memory, worker-memory, worker-cf 3 of 3 on each
cargo nextest run --profile ci --workspace 4042 passed (4 skipped), on the tree with main merged in
cargo fmt --all --check, clippy (native every target; the Worker's wasm32 as shipped and with default features; thrust-api-v1 with openapi), cargo test --doc --workspace, cargo deny --locked check clean, on the tree with main merged in
the guards of CI's ci-scripts and deps jobs, all 40 script steps, and the planner on this diff exit 0 on each, on the tree with main merged in (the planner keeps all 25 cells)
render-wrangler.sh --self-test / test-deploy-template.sh / test-deploy-worker.sh 69 / 35 / 149, under sh and dash
the four new mutants.sh rows, on their own all KILLED
Real service, 2026-11-16 (UTC): the product's own Worker under wrangler dev with the Email Sending binding remote, the binding restricted to [email protected] and to one recipient the control, a sender on example.com, was refused by the service (email from example.com not allowed because domain is not owned by the same account, nothing sent, and with no error code, so the words are what classified it; the Worker went on serving). The message from Thrust <[email protected]> was accepted and you confirmed it reached your mailbox

The six cells ran before main was merged in; the merge brings #192's planner and the States OpenAPI work, nothing in the Worker or the mail path, and CI runs the cells again.

CI

Green on 1be5f63 (run 43799363649, attempt 2): 35 jobs successful, mutants (all four new Cloudflare rows killed) and the three mail cells (native-memory, worker-memory, worker-cf) among them. Attempt 1's only failure was one old E1 row of mutants, reset-refusal-without-error-code: its server never started, and the job's log (the diagnostic added in #176) says why, bind: Address already in use, a port collision on the runner and not this change; gh run rerun --failed was green. The one commit after it, 6c5069e, only corrects G-39 in status.md (it listed mutants.sh as not run, and CI has run it); the planner plans from the PR's whole diff, so it ran the full matrix again, green on attempt 2 (35 jobs, gate and the deploy workflow green too). Its attempt 1 failed in two cells that passed on the previous head with the same code, each read from its own log: worker-cf main (2 of 70, the OIDC tests dying at mock_idp.rs:131, the mock IdP's port collision) and worker-cf e2e (the three journeys passed 3 of 3; the empty-list control stopped at create a work item: the new work item is not on screen instead of at see it in the list:). Neither is in what this PR touches, and neither is the mail path. The deploy workflow's pull-request dry-run (run 43799363635) also passed: it renders the template with made-up values and runs wrangler deploy --dry-run under the pinned wrangler 4.151.2.

Not measured

  • A Worker deployed to Cloudflare. The real send above was not one. The first staging deploy after this merges is.
  • The code a refusal carries from a deployed Worker. The refusal above came through wrangler's remote-binding proxy and had none; a deployed Worker's may. The adapter handles both.
  • SPF, DKIM and DMARC verdicts at the receiving provider. The mail arrived; its headers were not inspected.
  • More than one message, and any load. A per-address throttle (G-37 stands).
  • The service's message in the server's log can carry the recipient's address (as G-38 says for the relay's).
  • mdbook build (mdbook-mermaid is not installed here), and the deploy itself: nothing has deployed with the new variable yet (the dry-run above rendered and checked the config, which is not a deploy).

Files

crates/thrust-adapters-cloudflare (src/email.rs, tests/email.rs, four mutant features, mailparse as a dev-dependency), crates/thrust-worker (lib.rs, memory.rs, wrangler.memory.toml, wrangler.deploy.toml, one mutant), crates/thrust-contract-tests/tests/mail.rs, scripts/contract.sh, contract-all.sh, worker-dev.sh, mutants.sh, render-wrangler.sh, .github/workflows/contract-cell.yml and deploy.yml, .github/actions/deploy-worker/action.yml, .github/scripts/tests/ (deploy_parity.py, test-deploy-template.sh, test-deploy-worker.sh), and the docs: infra.md (the runbook), deploy.md, app.md, cicd.md, status.md (G-39), running.md, contract-suite.md, mutation.md, TRACEABILITY.md, QUESTIONS.md, DEVIATIONS.md.

🤖 Generated with Claude Code

aRustyDev and others added 7 commits November 15, 2026 20:42
CfEmail sends one message through the binding, as the sender a deployment configures (THRUST_EMAIL_FROM, and the name beside it in
THRUST_EMAIL_FROM_NAME). What can be decided off wasm32 is plain functions with generative tests (tests/email.rs, native):

- the configuration, read for every sender with every name (a name with no sender, a sender that is a name or a list, a name with
  a quote in it are errors that name the variable);
- which address is one address: any hostile character anywhere in one is refused, and whatever the guard accepts, a parser that is
  not ours (mailparse) reads as exactly that one address;
- what a message becomes before the binding is asked about it: a recipient that is not one plain address, and a subject with a
  line break, are refused and nothing is sent;
- what the binding's refusal means to the port: the 21 codes of Cloudflare's own error table, pinned in the test and not taken from
  the adapter, each a refusal or an outage as the table says; a code nobody has met is an outage; a refusal with no code, which is how
  the local simulator and spike 0002's refusal of an unowned sender read, is told by its words.

Four planted bugs (mutant_cf_email_*) each have a scripts/mutants.sh row and are each killed by the test aimed at it. The glue around
the binding runs only in workerd: the next commit runs it.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… that walks it

The Worker builds the mail service from THRUST_EMAIL_FROM and its EMAIL binding, on both adapter sets, and passes it to the app: with a
sender and a THRUST_PUBLIC_BASE_URL the reset is on, without a sender it is dark as before. A sender with no binding, a sender that is not
one plain address and a name with no sender are errors the Worker reports, not a Worker that quietly has no reset.

The contract suite's mail pass runs on the Worker targets (worker-memory mail, worker-cf mail, two new CI cells): the Worker built for
wasm32, run by wrangler dev, whose send_email binding is wrangler's simulator. The simulator sends nothing and writes each message it is
asked to send to the dev server's log and to files, which the tests read from outside the adapter into the shape Mailpit's API has, so the
same three tests run on both kinds of target. Measured while building it: the simulator logs a message a few milliseconds AFTER the
response that caused it, so the first run read an empty log, and a "nothing was mailed" check would have passed for want of waiting; the
tests now read after a marker message (one more reset, waited for in the log, which is in order). The memory config's binding is
restricted to the sender the pass uses, as the deploy template's will be, so the pass also shows the restriction lets the adapter's
named sender through.

Controls, run by hand because scripts/mutants.sh does not build the wasm: with mutant_worker_email_unwired the pass is refused by its
vacuity guard (exit 5, no test run); with mutant_cf_email_from_is_recipient the simulator refuses the send, the adapter reads the real JS
error as Rejected, and 2 of the 3 tests fail.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…and its binding is restricted to it

The deploy template gains the EMAIL send_email binding, restricted to the one sender the environment is configured with
(allowed_sender_addresses, which the binding enforces whatever the code asks for), and the variables the Worker reads: THRUST_EMAIL_FROM
(a new per-environment GitHub variable with no default, like THRUST_CSRF_ENFORCE, so an environment that mails has chosen who it mails as),
THRUST_EMAIL_FROM_NAME, and THRUST_PUBLIC_BASE_URL, which is filled from the THRUST_BASE_URL the smoke already uses, so the links in the
mails point at the origin people reach and cannot disagree with it.

scripts/render-wrangler.sh checks the two new values before it writes anything: a sender is one plain lowercase address (no name, list or
header) on a domain that is not a placeholder (example.com, .test, ...), an origin is https and a host with no path, port or trailing
slash and not of this machine. Its self-test has a control for each refusal; it caught one hole in the first draft (a sender with no @).
deploy_parity.py compares the template with wrangler.toml for the new binding, requires the template's restriction to be the sender marker,
and requires the three variables to be the deployment's: wrangler.toml must not set them, so a development run has no mail service nobody
asked for. test-deploy-template.sh shows each of those fail on a copy broken that way.

THE NEXT STAGING DEPLOY IS REFUSED BY THE RENDERER until the staging environment (and staging-do-lifecycle) has THRUST_EMAIL_FROM.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…aid the Worker had none

G-39 says what was measured (the adapter's tests and planted bugs, the two Worker cells, two hand controls, what the simulator does that the
documentation does not say) and what was not: any real send, so the service behind the binding (the code a deployed refusal carries, a
deployed binding's sender restriction, SPF, DKIM and DMARC for the sending domain, delivery to a mailbox), the deploy job, and
mdbook build. The runbook, the deploy variables, the contract suite, the CI cells, the mutation list, the port table, F-A03, Q-0073.1 and
D-041 say the Worker has a mail service when it is given a sender; none says a deployment sends mail, because none does yet.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
On the owner's approval of the recipient: the product's own Worker, run by wrangler dev with the Email Sending binding remote, sent one message from
the staging sender and had one send refused. The control, a sender on example.com, was refused by the real service with the words of spike 0002's
refusal and no error code, so the adapter's reading of a refusal with no code by its words is what classified it (seen through wrangler's
remote-binding proxy; a deployed Worker's error may carry a code, which is not measured). The message was accepted and the owner confirmed it reached
their mailbox. G-39, the runbook and Q-0073.1 say so, and say what is still not covered: a Worker deployed to Cloudflare, the code a refusal carries
from one, the receiving provider's SPF, DKIM and DMARC verdicts, and any deploy with the new variable.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
# Conflicts:
#	docs/src/architecture/cicd.md
… dry-run

CI ran the edited scripts/mutants.sh to the end on this PR (all four new Cloudflare rows killed; one old row's server failed on `Address already
in use` in the first attempt and the rerun was green), and the pull request's deploy dry-run rendered the template and passed
`wrangler deploy --dry-run` under the pinned wrangler. G-39 listed the first as not covered and said nothing of the second.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev enabled auto-merge (squash) November 16, 2026 04:24
@aRustyDev
aRustyDev merged commit ce84d85 into main Nov 16, 2026
75 of 78 checks passed
@aRustyDev
aRustyDev deleted the feat/email-cloudflare branch November 16, 2026 04:28
This was referenced Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant