Skip to content

feat(email): an SMTP adapter, and the native server sends the reset mail when it has a relay - #176

Merged
aRustyDev merged 5 commits into
mainfrom
feat/email-smtp
Nov 15, 2026
Merged

aRustyDev merged 5 commits into
mainfrom
feat/email-smtp

Conversation

@aRustyDev

@aRustyDev aRustyDev commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

What

The second half of the mail path for the password reset (E2): an SMTP adapter for the EmailSender port that #169 added, built from the environment by thrust-server.

With this PR the native server can send the reset mail, when it is given a relay (THRUST_SMTP_*) and a THRUST_PUBLIC_BASE_URL. Without them nothing changes: a deployment answers SMTP_NOT_CONFIGURED, says is_smtp_configured: false, and the app keeps drawing the "ask your admin" line. The Worker is still dark (its send_email binding is E3), so no deployment you run today starts sending mail when this merges.

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 (this) SmtpEmail on lettre, wired into thrust-server, proved against a scripted server and against Mailpit this PR
E3 the Worker's send_email binding, wired in wrangler.deploy.toml, and a real send on staging: needs your sender domain and your explicit go-ahead before anything is sent not started
E4 the email code (F-A04, P1) not started

What it does

Configuration, read once when the server starts (crates/thrust-adapters-selfhosted/src/smtp.rs, and docs/src/architecture/infra.md for the runbook):

variable
THRUST_SMTP_HOST the relay; unset is no mail service, and any other THRUST_SMTP_* without it stops the server at start
THRUST_SMTP_FROM the sender, required with a host
THRUST_SMTP_TLS starttls (default; required, a relay that cannot upgrade is not used in the clear), tls (port 465), or none
THRUST_SMTP_PORT by the mode: 587, 465, 25
THRUST_SMTP_USERNAME, THRUST_SMTP_PASSWORD both or neither; a secret, environment only; refused with tls=none
THRUST_SMTP_CA_FILE a PEM CA; set, the relay's certificate must chain to it and to nothing else, and name the host
THRUST_SMTP_TIMEOUT_SECS the most one message may take (10)
  • A half-written configuration (a port with no host, a username with no password, a mode that is not one of the three, a CA file that is missing, empty or not a certificate) stops the server at start and names the variable; it never prints a password (Debug redacts it too).
  • One connection per message, no queue and no retry (D-041, 5: the mail goes in the request). A relay's permanent refusal (5xx) is Rejected; a 4xx, a relay that cannot be reached or does not answer in time, and a failed handshake are Unavailable. Either is the pin's generic 500 and leaves no link behind (shown by hand on the real server: both requests tried to send, neither was held back by a link that was never delivered).
  • The recipient must be a bare address: a list, a name with an address, or a line break with a header or a command after it is refused before a connection is made. The id the port returns is the Message-ID the adapter wrote.
  • The server logs one line when it starts with a relay (mail: SMTP relay <host>:<port>, tls=<mode>, from <address>, ...), and says so if there is no base URL, because then the reset stays off.

Things you may want to look at

  • deny.toml gains 0BSD. lettre brings quoted_printable (0BSD), and mailparse, a dev-dependency, is 0BSD too. It is permissive with no attribution clause; the licence list is a policy file, so I am saying it here and not burying it.
  • New dependencies: lettre (rustls on ring, as reqwest already is, Mozilla roots; no native-tls, no pool, no hostname lookup) and its four small ones (email-encoding, email_address, nom 8, quoted_printable). scripts/check-deps.sh now names lettre among the crates core must not depend on and the Worker graph must not contain (it is absent from both).
  • THRUST_SMTP_CA_FILE trusts only that CA when set. I chose "only", as the Postgres adapter's verify-ca does, over "added to the Mozilla roots": a self-hosted relay with a private CA should not also be trusted for whatever a public CA signs. The cost is that a deployment that wants both cannot have both. Say if you want it the other way.
  • A new CI cell, native-memory mail (and a Docker step in the pg-adapter job for smtp_sink). The cell is generated from scripts/contract-all.sh; it pulls axllent/mailpit:v1.33.1 from Docker Hub as postgres:16-alpine is pulled. The required-contexts set is unchanged (a new cell is not a required context).

Tests and controls

  • tests/smtp_wire (hermetic, in the default loop; a scripted SMTP server in the test's own process, 9 tests): every reply code the grammar has at every step of the conversation (600 cases) read by its first digit; a relay that is silent, hangs up or is not there; a relay that cannot do TLS not used when TLS is asked for; a generated property that what arrives is what was given, read by mailparse and not by lettre (one sender, one recipient, text, subject, the returned id, and no header nobody meant, with line breaks and Bcc: in the subject); every address core's is_valid_email accepts is one the adapter sends to; a recipient that is not one bare address refused with nothing sent.
  • tests/smtp_config (hermetic, 4 tests): all 54,000 combinations of the eight settings against an independently written reference; the password in no error and no Debug; a bad CA file.
  • tests/smtp_sink (Docker, 7 tests, Mailpit): the message as a server that is not ours reads it; Mailpit's own 4xx and 5xx; STARTTLS, implicit TLS and a login, each with controls (the server requires TLS, so a plain client is refused by the server; another CA, a certificate that does not name the host, and the Mozilla roots alone are refused; no login and a wrong password are refused).
  • The mail pass (native-memory mail and native-pg mail, 3 tests): the real server beside a Mailpit; request, message, link, and the reset; a forged Host does not change the link; a second request inside the cooldown mails nothing. The pass refuses to run unless /api/instances/ says mail is on, every other pass refuses to run unless it says off and unsets THRUST_SMTP_* first, so a developer's shell cannot turn the dark test into a test of something else.
  • Planted bugs (nine new scripts/mutants.sh rows, and one old row converted: see Measured): the adapter's six (a refusal read as an outage and the reverse; the sender taken from the recipient; a wrong id returned; a password allowed in the clear; any certificate trusted; STARTTLS made opportunistic), thrust-server's mutant_smtp_unwired (the relay read and not handed to the app: the mail pass refuses to run, exit 5), and two core mutants run against the mail pass, one of which (mutant_reset_ignores_cooldown) could until now only be killed by the core's own tests.
  • Found by the tests, not planned: the first domain of reply codes was wrong (560 to 599 are not codes, and lettre says so); a run of spaces in a subject sent as encoded words comes back as none (RFC 2047 6.2, cosmetic); lettre writes a BEL into a header as it is (not an injection; the only text a person controls is the address, which is checked); Mailpit's published ports took connections a moment after its log said it was ready (the helper waits for the 220 and the API).

Measured

Run Result
scripts/contract.sh <target> main: native-memory, native-pg, worker-memory, worker-cf 76 of 76 on each (EXPECT_MAIN unchanged), with the new guard that holds is_smtp_configured at false
scripts/contract.sh native-memory mail, native-pg mail 3 of 3 on each
cargo nextest run --profile ci --workspace 3935 passed (4 skipped); 13 of them are the SMTP adapter's hermetic tests (9 in smtp_wire, 4 in smtp_config)
cargo test -p thrust-adapters-selfhosted --test smtp_sink (Docker, Mailpit) 7 of 7, twice in a row
cargo test --doc --workspace, cargo fmt --all --check, clippy (native, every target; the Worker's wasm32), cargo deny --locked check clean
the guards of CI's deps and ci-scripts jobs, all 30 steps (the dependency graph, topology and its self-test, Plane 500 and its self-test, the acceptance list and its self-test, placement, the render and workflow lints, the web diff, the 19 helper-script tests) exit 0 on each; the dependency graph, acceptance list, Plane 500 guard, ci-scripts and deny were run again on the finished tree
scripts/mutants.sh as a whole, once, on the tree before its last edit 43 rows: 39 killed, 3 refused by a vacuity guard, 1 that survived: smtp-flag-always-on, a server that says mail is on though nothing can send it, which the new main-pass guard now refuses (exit 5) before a test runs, so the row's "exit 101" test failed. The defect is still caught (by the guard, by tests/reset.rs and by the recorded S01), so I made it a refusal row and ran that row by hand: refused. The script as edited ran to the end in CI (below)
all nine new mutants.sh rows the six adapter rows killed (1 failed test each, except 2 for the configuration matrix and 3 for the TLS tests), both mail-pass rows killed, and the mail-pass guard refused: in that whole run
the real server with an SMTP relay that is not there, by hand the pin's generic 500 for the request, the cause in the server's log, and a second request at once is not held back

The six contract cells ran before the last two edits (a cfg-gated mutant in the adapter, and a formatting change in a test); neither changes what a build without the mutant does, and smtp_wire, smtp_sink and the workspace were run again after them.

CI

Green on ab6045f (run 43678220241, first attempt of that run): 34 jobs successful, mutants (12 minutes) and the native-memory mail cell among them. The two runs before it are described under Not measured.

Not measured

  • Any real relay or mailbox. The only relay these ran against is Mailpit: no provider, no SPF, DKIM or DMARC, no delivery to a real mailbox, and the success of the Mozilla roots against a public relay (only their refusal of a private CA is shown).
  • The Worker (E3), and anything deployed.
  • A per-address throttle (G-37 stands: the limit is per account, so many different accounts can still be mailed quickly, and now that spends a real mail budget).
  • Concurrency: each message is its own connection (no pool); nothing was run with many at once.
  • The relay's reply text in the server's log can carry the recipient's address (stated in infra.md, not removed).
  • mdbook build.
  • The cause of two CI failures of this PR. Two attempts of the CI run on the updated branch failed in mutants, and a new run after the commit below is green: the first attempt had two rows of the new mail pass that never started (exit 3 and exit 4), the second had one old row that never started (reset-token-reusable, exit 4), a different row each time, and the job's log said nothing more because a row's own output is thrown away. That is the "server never came up" failure this job has shown before on rows unrelated to a PR; I have not established its cause, and cannot say whether the nine rows added here (three of them start a Docker container) make it likelier. The last commit makes the job print the end of a row's output when it is not killed, so the next one explains itself.

Files

crates/thrust-adapters-selfhosted (src/smtp.rs; tests/smtp_wire, smtp_config, smtp_sink; six mutant features), crates/thrust-server (composition root, one mutant), crates/thrust-contract-tests/tests/mail.rs, scripts/contract.sh (the mail pass, the guard, the unsets), contract-all.sh, mutants.sh, check-deps.sh, verify-all.sh, .github/workflows/ci.yml and contract-cell.yml, .config/nextest.toml, deny.toml, and the docs: infra.md (the runbook), app.md, cicd.md, status.md (G-38), running.md, contract-suite.md, mutation.md, TRACEABILITY.md, QUESTIONS.md.

🤖 Generated with Claude Code

aRustyDev and others added 2 commits November 14, 2026 18:48
SmtpEmail, on lettre (rustls on ring, the Mozilla roots, no native-tls, no pool), for the self-hosted set. It
reads THRUST_SMTP_* through SmtpConfig::from_lookup: a host, a sender, a TLS mode (starttls, which is required
and never falls back to the clear, tls, or none), a port by the mode, a login (both or neither, refused with
none: the password would go in the clear), a CA file that is then the only trust, and a deadline for the whole
message. A half-written configuration is an error that names the variable and never carries a password, and
Debug redacts it.

A 5xx from the relay and a message lettre cannot make are Rejected; a 4xx, an unreachable or silent relay and a
failed handshake are Unavailable. The recipient must be one bare address, so a list, a name or a line break and
a header after it is refused before a connection is made. The id the port returns is the Message-ID it wrote.

Tests: smtp_wire (hermetic, a scripted server in the test's own process: all 600 reply codes by first digit, a
relay that cannot do TLS not used when TLS is asked for, a generated property that what arrives is what was
given as read by mailparse, and every address core accepts is one it can send to), smtp_config (hermetic, 54,000
combinations against a reference) and smtp_sink (Docker: Mailpit, with STARTTLS, implicit TLS and a login, each
beside its controls). Six planted bugs, one per defect, each fails the suite aimed at it.

deny.toml allows 0BSD (quoted_printable, under lettre; mailparse, a dev-dependency, is 0BSD too).
check-deps.sh names lettre among what core must not depend on and what the Worker graph must not contain.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
thrust-server builds the SMTP adapter from THRUST_SMTP_* and hands it to the app, and logs one line saying which
relay it will use (and that the reset stays off if THRUST_PUBLIC_BASE_URL is not set). With a relay and a base
URL, is_smtp_configured is true and the reset mails a link; without them nothing changes. The Worker still has no
mail service (its send_email binding is E3), so a deployed Worker is dark.

A new contract pass, mail (native targets, a cell in contract-all.sh): the real server beside a Mailpit
container, and tests/mail.rs walks request, message, link and reset against a mail server that parses the
message for itself, with a forged Host that must not change the link and a second request inside the cooldown
that must mail nothing. scripts/contract.sh now asks /api/instances/ before every pass: true for mail, false for
all the others, which also unset THRUST_SMTP_* and THRUST_PUBLIC_BASE_URL first, so a developer's shell cannot
turn the dark test into a test of something else.

scripts/mutants.sh: nine new rows (the adapter's six, thrust-server's mutant_smtp_unwired, and two core mutants
run against the mail pass, one of which, the cooldown, could until now only be killed by core's own tests).
smtp-flag-always-on, which the new guard refuses before a test runs, is a refusal row now. The CI Docker job runs
smtp_sink, and nextest keeps it out of the hermetic loop as it does pg_adapter.

Records: G-38, the runbook in infra.md (every THRUST_SMTP_* variable), the port row in app.md, F-A03 and
Q-0073.1, the pass and its cell in running.md, contract-suite.md and cicd.md, and the mutants in mutation.md.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@socket-security

socket-security Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcargo/​lettre@​0.11.2310010093100100
Addedcargo/​mailparse@​0.18.010010093100100

View full report

aRustyDev and others added 3 commits November 14, 2026 23:19
A mutants row that was neither killed nor refused said only "SURVIVED or did not run (exit=4)", and on a runner
the row's own output is thrown away with the job, so the one thing a red job needed, the reason, was missing:
contract.sh says why it stopped (a server that exited early, one that never became healthy, a mail server that
did not come up, a build that failed) in that output.

On PR #176 the job failed twice, on different rows each time (two rows of the new mail pass with exit 3 and 4 in
the first attempt, an old row with exit 4 in the second), and nothing in the log could tell a startup flake from a
fault of the new rows. A row that is not killed now prints the end of its own output to the job log, and a killed
row prints nothing.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
G-38 recorded that the edited scripts/mutants.sh had not run to the end. It has now: green in CI on ab6045f.
The record also says what the road to that was: two attempts of the CI run on the updated branch failed in the
mutants job on rows that never started (exit 3 and 4 of the two new mail rows in the first, exit 4 of an old row
in the second, a different row each time), the startup failure this job has shown before, whose cause is not
established; the script now prints the end of a row's own output when a row is not killed.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
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