Repository navigation
feat(worker): send the password reset mail from the Worker through Cloudflare Email Sending (E3) - #200
Merged
Conversation
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
enabled auto-merge (squash)
November 16, 2026 04:24
This was referenced Nov 16, 2026
Closed
Merged
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The third piece of the mail path for the password reset (E3): the Worker's
send_emailbinding as theEmailSenderport, built bythrust-workerand 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 aTHRUST_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 thestagingandstaging-do-lifecycleenvironments (with your go-ahead), so the next staging deploy turns the reset on atthrust-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
EmailSenderport, the token table, the two routes,is_smtp_configuredfrom configurationSmtpEmailonlettre, wired intothrust-serverCfEmailon the Worker'ssend_emailbinding, the Worker's wiring, the deploy template's binding and senderWhat it does
Configuration, read by the Worker on each request (
crates/thrust-adapters-cloudflare/src/email.rs; the runbook isdocs/src/architecture/infra.md):THRUST_EMAIL_FROMTHRUST_EMAIL_FROM_NAMEThrust). Optional; refused without a sender[[send_email]] name = "EMAIL"Rejected(the message or the sender or the recipient is one the service will not use) orUnavailable(a limit, a paused account, an outage, a delivery that failed). A code nobody has met isUnavailable: 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".Deploy (
wrangler.deploy.toml,scripts/render-wrangler.sh,deploy.yml):EMAILbinding restricted to the one configured sender (allowed_sender_addresses), which the binding itself enforces whatever the code asks for.THRUST_EMAIL_FROMis a new per-environment GitHub variable with no default, likeTHRUST_CSRF_ENFORCE. The links in the mails use theTHRUST_BASE_URLthe smoke already uses, so they point at the origin people reach and cannot disagree with it (no second variable).example.com,.test, ...); an origin ishttps://<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.pycompares the template withwrangler.tomlfor 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.tomlmust not set them, so a development run has no mail service nobody asked for.Things you may want to look at
thrust-staging.sow.foothen answersis_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'sCLOUDFLARE_ACCOUNT_IDis the account that hasnotify.sow.fooenabled (compared by prefix), which a binding needs: it refuses a sender "not owned by the same account".productionhas noTHRUST_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 ofTHRUST_CSRF_ENFORCE; if you would rather have an explicit "off" for an environment that should not mail, say so.thrust-spikerefusal: a Worker deployed with[email protected]passes every check and refuses every reset at send time.wrangler devwith only the Email Sending binding remote, on the account that has the domain (nothing deployed, so nothing to delete). See Measured.worker-memory mailandworker-cf mail(theplan-ci.pyplanner keeps them wheneverthrust-workeror what it depends on changes; checked on this diff). No container;worker-cfhas the Postgres container everyworker-cfcell has.memory::apptakes a smallOptional { oidc, email }struct instead of two more parameters (clippy'stoo_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).mailpass on the Worker targets (worker-memory mail,worker-cf mail, 3 tests each): the Worker built for wasm32, run bywrangler dev, whosesend_emailbinding 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.Errorwith a message and nocode.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 ascripts/mutants.shrow. One in the Worker (mutant_worker_email_unwired). The Worker's two are hand controls onworker-memory mail, becausescripts/mutants.shdoes 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 asRejected, and 2 of 3 tests fail.Measured
scripts/contract.sh <target> main:native-memory,worker-memory,worker-cfEXPECT_MAINunchanged).native-pgwas not run again: nothing here reaches itscripts/contract.sh <target> mail:native-memory,worker-memory,worker-cfcargo nextest run --profile ci --workspacemainmerged incargo fmt --all --check, clippy (native every target; the Worker's wasm32 as shipped and with default features;thrust-api-v1withopenapi),cargo test --doc --workspace,cargo deny --locked checkmainmerged inci-scriptsanddepsjobs, all 40 script steps, and the planner on this diffmainmerged in (the planner keeps all 25 cells)render-wrangler.sh --self-test/test-deploy-template.sh/test-deploy-worker.shshanddashmutants.shrows, on their ownwrangler devwith the Email Sending binding remote, the binding restricted to[email protected]and to one recipientexample.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 fromThrust <[email protected]>was accepted and you confirmed it reached your mailboxThe six cells ran before
mainwas 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 threemailcells (native-memory,worker-memory,worker-cf) among them. Attempt 1's only failure was one old E1 row ofmutants,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 --failedwas green. The one commit after it,6c5069e, only corrects G-39 instatus.md(it listedmutants.shas 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,gateand thedeployworkflow 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 atmock_idp.rs:131, the mock IdP's port collision) andworker-cf e2e(the three journeys passed 3 of 3; theempty-listcontrol stopped atcreate a work item: the new work item is not on screeninstead of atsee it in the list:). Neither is in what this PR touches, and neither is the mail path. Thedeployworkflow's pull-request dry-run (run 43799363635) also passed: it renders the template with made-up values and runswrangler deploy --dry-rununder the pinned wrangler 4.151.2.Not measured
codea 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.mdbook build(mdbook-mermaidis 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,mailparseas 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.ymlanddeploy.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