Skip to content

feat(auth): the password reset by email, first half: the port, the token store and the routes, dark until a mail service is wired - #169

Merged
aRustyDev merged 3 commits into
mainfrom
feat/email-recovery
Nov 14, 2026
Merged

aRustyDev merged 3 commits into
mainfrom
feat/email-recovery

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

What

The first half of email recovery (F-A03): the two routes behind the stock sign-in screen's "Forgot your password?" link, built on a new optional EmailSender port and a single-use token store, shipped dark.

  • POST /auth/forgot-password/ asks for a link.
  • POST /auth/reset-password/<uidb64>/<token>/ is the form the link leads to.

Refs #46 (it does not close it).

No mail service is wired in any deployment yet (the SMTP adapter is E2, the Worker's send_email binding is E3), so every deployment answers the request SMTP_NOT_CONFIGURED (5025), as the pin does without EMAIL_HOST, and /api/instances/ says is_smtp_configured: false. The app keeps drawing the "ask your admin" line (D-037, D-038) and not the link. No web change: the app's forgot-password page already posts JSON and its reset page is a native form, so web/ALLOWED-CHANGES stays at its two files.

The plan, and where this PR sits

PR What State
E1 (this) the EmailSender port, the token table, the two routes, is_smtp_configured from configuration, the test seam, the pin recorded this PR
E2 an SMTP adapter on lettre for the self-hosted set, proved against a local mail sink not started
E3 the Worker's send_email binding, wired in wrangler.deploy.toml, and a real send on staging: needs the owner's sender domain and an explicit go-ahead before anything is sent not started
E4 the email code (F-A04, P1) not started

What is already decided and was not re-asked: Q-0073.1 and ADR-0073 (an Email port, Cloudflare Email Sending on the Worker side, lettre SMTP for self-hosting). A real send through the deployed Worker's binding was already made once (spike 0002 step 10, 2026-10-24): the binding accepted it and an unowned sender was refused; delivery to a mailbox was never confirmed.

The pin, measured first

Recorded on a fresh oracle as X10-dark (3 exchanges), X10 (6) and X11 (14); the other 71 fixtures came out byte-identical; the acceptance list is 59 rows.

  • forgot-password with no SMTP host answers 5025 to everything (even a missing address); with one (set in its instance configuration for the rows, removed after): the message, 5060 for an address nobody has, 5005 for a bad, missing or null one. It takes a form body as well as JSON.
  • reset-password answers every outcome with a 302, never signs anyone in, and reads only the urlencoded password: a JSON body is "no password". A weak password leaves the link working. After a reset the link is used up, the old password is refused, the new one works, and the session held before it is over.
  • Recording corrected two readings of the source: a uid that is not text is INVALID_PASSWORD_TOKEN, not EXPIRED_PASSWORD_TOKEN; and the pin's link lifetime is an hour (PASSWORD_RESET_TIMEOUT = 3600 in the fork's settings), which I had first taken for Django's default of three days.

D-041 (owner confirmed 2026-11-14): how Thrust differs

  1. The token is stored (only its SHA-256 digest; password_resets, migration 0017) and used up, where the pin's is stateless: a new request replaces the older link.
  2. A cooldown (60 s, inclusive at the boundary) per account: a request inside it mails nothing and is answered as one that did.
  3. A deactivated account is mailed nothing and told it was.
  4. The link starts with a configured THRUST_PUBLIC_BASE_URL, never the request's Host (a forged Host would mail a victim a working token to another page); the address in it is percent-encoded.
  5. The mail is sent in the request: a refusing or unreachable mail service is the pin's generic 500 here, and leaves no link behind.
  6. Plain text, no HTML part. 7. A relative redirect. 8. EXPIRED_PASSWORD_TOKEN is never answered. 9. set-password and the email code are not built.

Not a difference, kept as the pin has it: the form says whether an account exists (USER_DOES_NOT_EXIST), as the pin's sign-in does, so it is not made uniform.

Tests and controls

  • Adapters: the memory adapter's own tests (25); the Postgres adapter's generated model, extended with insert, find, take, the cooldown, the replacement and the sweep, ten new classes in its coverage guard, run against a Postgres container with the schema snapshot and a digest-length control (the adapter suite is 70 of 70).
  • Core (password_reset.rs, 13): 5 deployments by 6 addresses, 30 worlds against the pin's order; 7 links by 3 passwords, 21 worlds, with the hash work of each and what a refusal leaves; the cooldown at 6 ages and the lifetime at its boundary to the second; a generated loop of any address and any strong passphrase through the mail.
  • Compat (tests/reset.rs, 4) and the recorded rows (parity_x10_*, parity_x11_*, with three parity controls: a cookie, a code-less redirect, a mutated recording). The harness learned: a world that can mail, the pin's 302 projected as the 302 it is and not as D-022's JSON, its form posted as a form, a no-cookie check, and a replay that mints Thrust's own link after the fixture's sign-up.
  • Contract, all four targets (EXPECT_MAIN 76): the dark request, the whole reset against each adapter's token table (Postgres included) through the test seam POST /internal/issue-password-reset (behind THRUST_TEST_ROUTES and the admin token, which no deployment turns on), and a link for its own account and its replacement by a newer one.
  • Planted bugs: seven in the core (a link that works twice, after it expired, for another account, that a weak password burns, a weak password accepted, sessions left alive, no cooldown), two in compat (the mail flag on, a code-less redirect), each failing the tests aimed at it; the recorded X11 fails six of the seven core ones and the recorded S01 fails the flag one; eight new scripts/mutants.sh rows, run by hand on native-memory, each killed (exit 101).
  • A control that did not catch its bug at first: the first generated model passed a planted >=→> in the Postgres adapter's cooldown, because two random inserts are almost never exactly the cooldown apart. A compound op now sends one request on the boundary, and the same bug fails it at its first step.

Measured

Run Result
scripts/contract.sh <target> main, native-memory, native-pg, worker-memory, worker-cf 76 of 76 on each, after the rebase; worker-memory needed one re-run: the first pass failed (16 of 76) while the Mac slept mid-run (power log: maintenance sleep 14:46 to 15:13; the Worker's log shows ~15 min requests), and the re-run alone was 76 of 76
cargo nextest run --profile ci --workspace 3922 passed
the compat crate (-p thrust-compat-plane) 894 of 894
cargo test --doc, cargo fmt --check, clippy native and wasm32, and the deps job's guards (dependency graph, topology and its self-test, Plane 500 and its self-test, acceptance list and its self-test, placement) and the web diff clean
the 8 new scripts/mutants.sh rows by hand each killed, exit 101

Not measured

  • Any real mail. No message leaves any deployment through this code; delivery to a mailbox is unconfirmed even for the one message of 2026-10-24.
  • A deployed Worker (nothing was run against one).
  • A browser journey of the forgot-password and reset pages: the e2e targets have no mail service, so the app does not draw the link; the sign-in journey holds the "ask your admin" line.
  • A per-address throttle (the pin has 10 a minute on this route; here the limit is per account, so many different accounts can still be mailed quickly, which spends the mail budget: G-37).
  • The race of two requests for one account at once: the claim that it leaves two working links and never none is reasoned from Postgres snapshot semantics, and the model is sequential.
  • That the pin's link also dies at a sign-in (by Django's design, not measured).
  • scripts/mutants.sh as a whole (its eight new rows by hand), mdbook build, and this PR's own CI.

Files

crates/thrust-ports (EmailSender, NewPasswordReset), thrust-core (password_reset.rs, config, Ports.email, 7 mutants), thrust-adapters-memory (MemoryEmail, the table), thrust-adapters-postgres (migration 0017 and its methods), thrust-adapters-selfhosted/tests (model, schema snapshot), thrust-compat-plane (reset.rs, the flag, the codes, tests, three fixtures, the harness), thrust-api (the test seam), thrust-server and thrust-worker (configuration; the sender stays None), thrust-contract-tests, scripts/contract.sh and mutants.sh, tools/compat-record (the recorder steps and the acceptance rows), and the docs: DEVIATIONS.md (D-041; D-037 amended), docs/src/testing/status.md (G-37), TRACEABILITY.md, QUESTIONS.md, docs/src/architecture/infra.md (a runbook section), app.md, mutation.md.

🤖 Generated with Claude Code

aRustyDev and others added 3 commits November 14, 2026 15:19
… reset by email

First half of email recovery (F-A03, D-041): what a reset needs under the routes, with no route and no
real mail service yet, so nothing is switched on.

The `EmailSender` port (ADR-0073, Q-0073.1) is optional on `Ports`, like the OIDC provider: a
deployment with none cannot mail, and `MemoryEmail` is the reference implementation that tests read.

Reset tokens are 256 random bits, mailed once and stored as their SHA-256 digest (migration 0017,
`password_resets`; the memory and Postgres adapters; the model test of the Postgres adapter). A token
is single use (`take` deletes it), expires, and a user has at most one that works: an insert replaces
the user's older ones in one statement, and is refused, sending nothing, when the user's last token is
younger than the cooldown (inclusive at the boundary). `find` leaves the token in place, so that a
weak new password does not burn the link.

The use-cases in the core: `request_password_reset` (the pin's order: this deployment can mail; the
address is one; an account has it; then make a link and mail it), `reset_password` (the link is
checked, then the password; the link is used up only when the password passed; every session of the
account ends; nobody is signed in) and `issue_password_reset`, which a test seam will use to get a
token without a mailbox. The link starts with `public_base_url`, a configured setting: never a
request's Host header, which would let a forged one mail a victim a working token to another page.

Controls: seven planted bugs (a link that works twice, after it expired, for another account, that a
weak password burns, a weak password accepted, sessions left alive, no cooldown), each failing the
core's tests. A first run of the generated model did NOT catch an off-by-one in the cooldown boundary
of the Postgres adapter (`>=` written as `>`): two inserts are rarely exactly the cooldown apart. The
compound op `ResetCooldown` now makes one on the boundary, and the same planted bug fails it at its
first step.

Measured: memory adapter tests 25 of 25; Postgres adapter suite 70 of 70 (the model, its class
coverage with the ten new classes, the schema snapshot, the digest-length control); core tests 13 of
13; workspace clippy (native, and the Worker's wasm32) and fmt clean. Not measured: the routes (next
commit), the contract suite, any real mail.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ail service is wired

Second half of email recovery (F-A03, D-041). `POST /auth/forgot-password/` and `POST
/auth/reset-password/<uidb64>/<token>/` are the two routes behind the stock app's "Forgot your
password?" link; the app's own forgot-password page already posts JSON and its reset page is a native
form, so the web needs no change.

Both were measured on the pinned Plane first (a fresh oracle, an SMTP host set in its instance
configuration for the request rows and a link made in its Django shell for the form rows) and
recorded as X10-dark, X10 and X11. The request answers 5025 to everything without a mail service,
then 5005, 5060 and the message; the form answers every outcome with a 302 and signs nobody in.
Recording found two things the source had not said: a uid that is not text is
INVALID_PASSWORD_TOKEN, not EXPIRED_PASSWORD_TOKEN; and a JSON body is no password, because the pin
reads only the form. The pin's own link lifetime is an hour (PASSWORD_RESET_TIMEOUT = 3600), which I
had first taken for Django's three days.

It is dark: no composition root wires a mail service yet (the SMTP adapter is E2, the Worker's
`send_email` binding E3), so every deployment answers 5025 and says `is_smtp_configured: false`,
and the app keeps drawing the "ask your admin" line. The link needs a configured
`THRUST_PUBLIC_BASE_URL`, never a request's Host header.

The contract targets have no mailbox, so a test route `POST /internal/issue-password-reset` (behind
`THRUST_TEST_ROUTES` and the admin token) returns the token a mail would carry; with it the reset form
is exercised against every adapter's token table, Postgres included, on all four targets.

Harness: a world that can mail, the pin's 302 projected as the 302 it is (not D-022's JSON) and its
form posted as a form, a check that a reset sets no cookie, and a replay that mints Thrust's own
link after the fixture's sign-up. X10 and X11 join the acceptance list (59 rows).

Controls: eight new scripts/mutants.sh rows (each killed, exit 101, by the contract test aimed at
it), two compat mutants, and three parity controls (a cookie, a code-less redirect, a mutated
recording). The recorded X11 fails six of the seven core mutants; the recorded S01 fails the flag
one.

Measured: contract 76 of 76 on native-memory, native-pg, worker-memory and worker-cf; compat crate
894 of 894; whole workspace 3928 passed; doctests, fmt, clippy (native and wasm32) and all nine
guard runs of the deps job (dependency graph, topology and its self-test, Plane 500 and its
self-test, acceptance list and its self-test, placement, and the web diff) clean. Not measured: any
real mail, a deployed Worker, a browser journey of the two pages, the whole of scripts/mutants.sh,
and this change's CI.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The owner confirmed the nine differences of the password reset by email from the pin (the token is
stored and used up, and a new request replaces the old link; a cooldown per account; a deactivated
account is mailed nothing and told it was; a configured base URL and a percent-encoded address in
the link; the mail is sent in the request, so a failing mail service is a 500; a plain-text mail;
a relative redirect; EXPIRED_PASSWORD_TOKEN never answered; no set-password and no email code) on
2026-11-14.

Refs: #46
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit 990e1ce into main Nov 14, 2026
35 checks passed
@aRustyDev
aRustyDev deleted the feat/email-recovery branch November 14, 2026 22:58
aRustyDev added a commit that referenced this pull request Nov 15, 2026
…s stale (#181)

`scripts/contract.sh` and `scripts/stage2-contract.sh` each exit 5 when the number of tests that passed is not the number they expect, but only when they are run. Most of contract.sh's passes run in CI cells; `blast`, `budget` and `engine` run in none (contract-all.sh has nine passes and not those, and only mutants.sh runs the last two, against broken builds); and stage2-contract.sh needs a deployed Worker. Its `main` count was 59 until 2026-11-14 while the suite had listed 61 since #151, and #169's three tests made it 64 the same day. Nothing noticed.

`scripts/check-contract-counts.sh` reads every arm of both scripts (13 in stage2, 10 in contract.sh), runs the arm's own filters through libtest (`cargo test -p thrust-contract-tests --test <bin> -- --list --format terse <filters>`, which needs no server and no environment), counts the lines that end in `: test`, and compares. It copies no filter, so a filter cannot drift from its count. It fails closed with exit 2 on an arm it cannot read, a different number of arms read than written, no arm at all, a count that is not a number, an `EXPECT_` variable with no value or two, a listing that fails, and a filter that selects nothing (libtest prints nothing and exits 0 for that).

The self-test (a step of `deps`, no compile) runs it on 16 worlds with a fake lister, each read to be refused for the reason it names: a count one too high and one too low, a filter that selects nothing, a failing listing, a script with nothing to read, an arm with no count, a variable with none and with two, the default arm of contract.sh read as `main`, one pass asked for alone. The real run (a step of `hermetic`, after the suite is built) ends with a control on the real stage2-contract.sh with its `main` count made wrong, which must be refused.

Shown on the real suite: all 23 arms agree today (stage2 `main` 64, contract `main` 70, `bindings` 3 and 5), and renaming one test out of `sweeper_` makes four arms fail with exit 1 (stage2 `main` 65 for 64 and `sweeper` 2 for 3, contract `main` 71 for 70 and `sweeper` 2 for 3). The file was restored and compared. The self-test passes under dash on this Mac, under the Mac's sh, and in an ubuntu:24.04 container (dash with GNU sed and grep).

The prose that had rotted in stage2-contract.sh is fixed with it: its usage line named 6 of its 13 passes, its header said `bindings` runs 5 tests where it runs 3, and `crontrigger`, `blast` and `ratelimit` had no entry. evidence-and-controls.md and contract-suite.md now say what enforces the rule they state, and status.md no longer says the count "has not been kept up".

Not run here: the new `hermetic` step on a runner. `cargo test -p thrust-contract-tests` unifies features differently from `--workspace`, so it may rebuild some dependencies there; the job's timeout is 30 minutes.

Refs: #58

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
This was referenced Nov 15, 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