Skip to content

fix: forwarding on the database session, moves into Sent and Drafts, codes used once - #58

Merged
Madraka merged 4 commits into
mainfrom
fix/route-and-proof-gaps
Nov 18, 2026
Merged

Madraka merged 4 commits into
mainfrom
fix/route-and-proof-gaps

Conversation

@Madraka

@Madraka Madraka commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

What and why

Three gaps on the sign-in and per-mailbox boundaries (B3, B4), one commit each, with documents in a fourth.

  • Forwarding is decided on the session in the database (Closes Forwarding is changed on a session copy up to a minute old #14). The route that saves a mailbox's settings needs fresh proof only when forwarding changes, and it decided on the copy of the session cached in the cookie. A route where only some requests are sensitive is now marked sensitive in the route table, and the guard reads its session from the database for it. Two related gaps came up in review and are closed here too: creating a mailbox could set forwarding without fresh proof, and the raw value was stored instead of the compared one. Nothing forwards mail yet, so neither could be used.
  • One message is moved into Sent never, and into Drafts only as a draft (Closes The route that moves one message still takes Sent and Drafts as a target #31). Drafts takes only a message the mailbox wrote and that never left (writtenHere: no Message-ID, no header list, no original, not unparsed). That is what the app's Undo of deleting a draft needs, and it still works.
  • An authenticator code is used once (Closes An authenticator code can be used twice within its 30 seconds #12). Before a code reaches the sign-in library it is claimed for its person, as a row of the library's own verification table whose primary key is an HMAC of the person and the code. Of two requests with the same code only one row is written, in whatever isolate. The other is answered byte for byte as a wrong code. There is no new table and no migration.

What changes for a person: someone who turned the app on a moment ago signs in with the app's next code, not the one they just typed.

Regression tests (each seen to fail without its control)

  • test/workerd/second-step.test.ts, "forwarding, which needs fresh proof only when it changes":

    • the database calls a session old while the cookie's copy calls it new;
    • a session was ended a moment ago and its copy is still in the cookie;
    • forwarding is sent in an odd form;
    • forwarding is set while creating a mailbox.

    test/unit/fresh.test.ts pins the one sensitive route and holds that only its handler reads the flag, in any of five spellings.

  • test/workerd/move.test.ts: every message is made as the product makes it (delivered, written, sent and let leave). Taking the guard out fails two of its tests. Weakening the draft test to the Message-ID alone fails the received message that has none.

  • test/workerd/second-step.test.ts, "an authenticator code": the code that turned the app on, a code that signed in elsewhere, and a code given by two sign-ins at once. Also: the next code still works, and the claim holds no code. All four fail with the claim taken out. test/e2e/sign-in.spec.ts shows a used code refused in the browser.

Runs on this branch, after the rebase on b62d6a1

  • pnpm check: green.
  • pnpm test:workerd: 28 files, 644 tests.
  • pnpm e2e: 86 passed.

An earlier browser run failed in sign-in.spec.ts: an extra wrong try in the test ran into the sign-in library's limit on second-step requests (429 at the fifth request in ten seconds, read from the trace). The test now makes one wrong try.

Open, not done here

  • A row from the seed app that predates mailbox migrations 4 and 5, if any instance holds one, would count as a draft. No instance in use is known to hold such mail (as in Regroup conversations that were formed before 2026-11-17 #38).
  • USED_CODE_SECONDS (120) fits the library's defaults (30 s period, a window of 1). It must be raised if those are ever changed.

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 (sign-in, authorisation) 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 4 commits November 18, 2026 12:34
The route that saves a mailbox's settings needs fresh proof only when
forwarding changes, and it decided on the flag the guard computed from
the copy of the session cached in the cookie, which can be a minute
old and still says the session is as new as when the copy was made.

A route where only some requests are sensitive is now marked
`sensitive` in the route table, and the guard reads its session from
the database, as for `fresh` and `upgrade`. A unit test pins the one
such route and holds that only its handler reads the flag.

Found in review of this change and closed with it: creating a mailbox
could set forwarding without fresh proof, by someone who manages the
instance and may not read the mailbox; and the forwarding that was
compared was a cleaned-up copy while the raw value was stored. A new
mailbox never forwards, and what is stored is what was compared.

Each Workers-runtime test was seen to fail without its control.

Closes #14

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
…as a draft

A message is in Sent because the sending service took it, and in Drafts
because it is a draft, but the route that moves one message took both
as a target; the tools refused them. Sent is now never a target, and
Drafts takes only a message the mailbox wrote and that never left: no
Message-ID, no header list, no original and not unparsed
(`writtenHere`). That is the app's Undo of deleting a draft, which
still works, as does a message taken back from Scheduled. A request
whose place is not a string is 400 instead of reaching the mailbox.

test/workerd/move.test.ts makes every message as the product does:
delivered to the email handler, written through the drafts route, or
sent and let leave with a send binding of the tests. Taking the guard
out fails two of its tests, and weakening the test of a draft to the
Message-ID alone fails the one with a received message that has none.

Closes #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
The sign-in library accepts a code for its step and the steps on either
side, about ninety seconds, and remembers none, so a code someone saw
being typed could be used again. Before a code reaches the library it
is now claimed for the person it is typed for, as a row of the
library's own `verification` table whose primary key is an HMAC of the
person and the code under the instance secret, kept for two minutes.
Of two requests with the same code only one writes that row, in
whatever isolate; the other is answered byte for byte as a wrong code.
The claim is the last step of the hook, so a request refused for
another reason uses up no code.

Tests in the Workers runtime: the code that turned the app on, one
that signed in another browser, and one given by two sign-ins at once
each let in nobody more; the next code still signs in; the claim holds
no code. All four fail with the claim taken out. The test helpers hand
out each code once, since the old tests typed the same code twice. The
browser test now tries the code that turned the app on, sees it
refused, and signs in with the next one.

Closes #12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
docs/status.md, section 1 gets the three controls and their tests;
section 3 no longer lists them as gaps. The security skill records
the `sensitive` mark, the claim of a code and the rule of the move
route under B3 and B4, and names workers/auth/used-codes.ts on B3.
AGENTS.md and the sign-in design say that a code works once.

Refs #12, #14, #31

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

Read: every source line of the change, the names and shape of the new tests, the documents, against #12, #14 and #31. CI: all six checks green on 436607b. Not done by the auditor: the tests were not run a second time, so "fails without its control" stands on the author's word and on the test's shape.

What I checked and found to hold:

  • Forwarding. The route is marked sensitive, and the guard reads the session from the database for it (disableCookieCache), so the flag the route decides on is never the cookie's copy. What is stored is the value that was compared. A new mailbox cannot be created forwarding: the forced value comes after the spread of what was sent.
  • One message moved. Sent is refused before anything is read. Drafts takes only a row with no Message-ID, no header list, no original and not unparsed. The bulk route already limits its targets to Inbox, Archive, Spam and Trash by schema (BULK_MOVES), so this was the last door. The body is now checked before use.
  • A code used once. The claim runs last in the hook, so a request refused earlier uses up nothing. Two requests with one code: both may delete an expired row, one create wins on the primary key, the other is answered exactly as a wrong code. The person is taken from the session or from the signed cookie of the password step, never from the body, so nobody can use up another person's codes without their password. The only path that takes a code from the app is /two-factor/verify-totp; codes by mail stay refused. Expired claims do not pile up: the sign-in library deletes every expired row of that table whenever it looks one up, and that clean-up is not turned off.

Not blocking, written down as #59: no log line when the claim's write fails for another reason; no label of purpose in what the HMAC signs; the comment "is then replaced" is true only for the same person and digits; the two minutes are not tied to the library's settings by a test.

Left open as the description says: a row older than mailbox migrations 4 and 5 would count as a draft.

@Madraka
Madraka merged commit b434caa into main Nov 18, 2026
6 checks passed
@Madraka
Madraka deleted the fix/route-and-proof-gaps branch November 18, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant