Repository navigation
fix(core,auth): host lease verify always reads the table; auth verifies inside every Better Auth write (#1010) - #1018
Merged
Merged
Conversation
…es inside every Better Auth write (#1010) - HostLease.verify() no longer skips the lease-table read while the process's own monotonic clock says its last heartbeat is under 10 s old. A suspended VM's clock stops with it, so a holder paused just after a heartbeat could resume after another host took over and keep writing. It now reads the rows under the caller's write lock on every write (about 2 us per write, with the plain-connection statements now prepared once). - auth installs a TEMP trigger (auth's connection only) before every INSERT, UPDATE and DELETE on each Better Auth table that calls lease.verify() under that statement's write lock, so a stall between the per-request renew() and Better Auth's statements writes nothing once another host took over (503). - STORE.md: the "refuses its writes" claim is now exact; limits updated (clocks decide when a take-over happens, never whether two hosts write; reads are not guarded; auth tables created while serving are not covered). EXTENSIONS, RIM card, auth README/SECURITY and changelogs updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Fixes #1010
Design
Case 1: a suspended VM resumes and keeps writing.
HostLease.verify()now reads the lease rows under the caller's write lock on every call. Thefresh()shortcut is gone: it skipped the read while the process's own monotonic clock said the last heartbeat was under 10 s old. No clock decides whether a write may happen. A host inserts its row only in a transaction that found no other host's row, so at any committed state the table holds rows of one host at most. A write that passesverify()also holds the write lock until it commits. Clocks now decide only when a take-over happens.renew()still useswriteMs, but only as a cheap per-request gate and to run the rejoin heartbeat. I didn't use a suspend-aware clock: Node doesn't exposeCLOCK_BOOTTIME, and a hypervisor pause would freeze it too.Case 2: auth checks the lease once per request. Hooking
beginwould miss most Better Auth writes, because its Kysely adapter runs manycreate/update/deletecalls in autocommit, outside any transaction. So after joining the lease, auth now installs aTEMPtrigger before every INSERT, UPDATE and DELETE on each Better Auth table. The triggers exist on auth's own connection only. Each one calls a connection function that runslease.verify(). A trigger runs under its statement's write lock, whether the statement is autocommit or insideBEGIN IMMEDIATE. If the check fails, the statement fails, Better Auth returns a 500, and the mount answers 503auth_unavailablewith no cookie. The lease's own table has no trigger. The per-requestrenew()check stays, as a quick 503 and to run the rejoin heartbeat.Per-write cost (Apple-silicon laptop, Node 26, WAL, one-row insert per
BEGIN IMMEDIATEtransaction, 20k iterations, lease table with 4 rows):verify()alone: 4.4 µs before statement caching, 1.65 µs after. The plain-connection path (auth, audit) now prepares each lease statement once. The store already cached its statements.synchronous=FULL: 68.7 µs → 71.1 µs per write (about 3%).synchronous=OFF: 4.2 µs → 6.3 µs.On Linux with a real fsync the commit dominates even more.
Docs. STORE.md's "refuses its writes" sentence is now exact. It explains why (single-host row invariant plus a read under the lock) and gives the cost. "What the lease does not do" now says:
I removed the old "stall inside a single write transaction" limit, because the verified transaction holds the lock. EXTENSIONS.md (verify/renew contract), the RIM-STORE-PERSIST-001 card, auth README/SECURITY and the auth/store/audit changelogs are updated, and llms-full.txt is regenerated.
Tests
test/host-lease.test.ts: A's monotonic clock is frozen while B's runs; B takes over; A resumes 1 s after its last heartbeat withheldstill true;verify()refuses, and only B's row is written. The stale/fresh verify test now asserts a refusal right after a successful heartbeat.packages/auth/test/auth.test.ts: the server's monotonic clock never moves, sorenew()keeps passing. Another host takes over; the next sign-in answers 503auth_unavailablewith no Set-Cookie, and Better Auth wrote no session row.npm run verify(includestest:multiprocess) andnpm run test:packagepass locally on Node 26. Package sizes are within budget.Limitations
🤖 Generated with Claude Code