Skip to content

Let a setup-token retire win over a finish still in flight - #1143

Merged
nedtwigg merged 2 commits into
mainfrom
relay-setup-retire-race
Oct 16, 2026
Merged

nedtwigg merged 2 commits into
mainfrom
relay-setup-retire-race

Conversation

@nedtwigg

@nedtwigg nedtwigg commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

A retire now wins over a setup finish still in flight; previously a failing finish could restore a token the owner had just retired, leaving it redeemable until it expired.

Fix

POST /api/setup/finish now claims its setup token in place instead of removing it, so POST /api/setup/retire still finds a token a finish holds and kills it, answering 204. The finish re-checks its claim immediately before the registration write and registers nothing without it, and its failure path only clears a claim that is still there, so it never puts a retired token back. Single use and the original expiry are unchanged.

  • Self-host Relay (relay/src/setup-token.ts, relay/src/app.ts): SetupTokenIssuer keeps each token as a live / claimed / committed slot, with claim, commit, settle, restore, and retire. A claimed token counts toward the Burrow cap like any other, and one the cap evicts stays dead.
  • Hosted Relay (hosted/server/relay-api.ts): migration 006_relay_setup_token_claim.sql adds claimed to dormouse_relay_setup_tokens (the relay role gets UPDATE (claimed)). begin ignores claimed rows. The passkey transaction re-reads the claim FOR UPDATE and deletes the token when it registers. Restore just clears the flag, and retire deletes the token whether it is claimed or not.

The HTTP contract stays the same shape: retire still answers 204 or 401 SETUP_TOKEN_INVALID_ERROR, so Pocket needs no change.

Tests

  • relay/test/setup-token.test.mjs: two route-level regressions suspend a finish (one failing, one valid) on a deferred promise after it claims the token, retire it, then release the finish. The retire must answer 204, the finish must register nothing, and the token must not be redeemable afterwards. Issuer-level tests cover retire against claimed and committed tokens and cap eviction of a claimed token. Mutation-checked: each new test goes red when the retire skips claimed tokens, when it leaves them for restore, or when the commit check is removed.
  • hosted/server/tests/relay.test.ts: the same interleaving against real Postgres, holding the account's passkey lock so the finish waits with its claim held. Mutation-checked against each of the three fix lines. The restore tests are rewritten for claim-in-place, and the migration and role pins are updated.

Specs

  • docs/specs/security-remote.md → "Trust boundary": a new FAIL IF, appended so nothing shifts and claimed by the remote-trust domain. Budget ratcheted.
  • docs/specs/security-hosted.md → "Relay boundary": the restore FAIL IF now fails a retire that misses a claimed token, a refused finish that does more than clear its claim, and a passkey insert that does not first re-read the claim FOR UPDATE.
  • docs/specs/relay.md → "Setup tokens and the pairing QR" and docs/specs/hosted.md → "Relay": updated to match.

🤖 Generated with Claude Code

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Deploying mouseterm with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4139304
Status: ✅  Deploy successful!
Preview URL: https://b445e437.mouseterm.pages.dev
Branch Preview URL: https://relay-setup-retire-race.mouseterm.pages.dev

View logs

A retire now wins over a setup finish still in flight; previously a failing
finish could restore a token the owner had just retired, leaving it
redeemable until it expired.

`finish` now claims its token in place instead of removing it, so a retire
still finds a token a finish holds and kills it (204). The finish re-checks
its claim immediately before the registration write and registers nothing
without it, and its failure path only clears a claim that is still there,
so it can never put a retired token back.

- Self-host Relay: `SetupTokenIssuer` tracks live / claimed / committed
  slots (`claim`, `commit`, `settle`, `restore`, `retire`).
- Hosted Relay: migration 006 adds `claimed` to the setup-token table; the
  passkey transaction re-reads the claim `FOR UPDATE` and deletes the token
  on registering; restore clears the flag; retire deletes claimed or not.
- Specs: a new FAIL IF under security-remote "Trust boundary", the hosted
  FAIL IF tightened, relay.md and hosted.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@dormouse-bot dormouse-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Hosted FAIL IF that replaces the restore rule now states the restore and FOR UPDATE requirements as descriptions, not conditions, so the nightly audit would not flag a regression in either. There is a suggested rewording inline.

Comment thread docs/specs/security-hosted.md Resolved
@nedtwigg
nedtwigg merged commit eb517fa into main Oct 16, 2026
11 checks passed
@nedtwigg
nedtwigg deleted the relay-setup-retire-race branch October 16, 2026 23:02
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.

2 participants