Skip to content

fix(core,auth): host lease verify always reads the table; auth verifies inside every Better Auth write (#1010) - #1018

Merged
jimhoyd merged 1 commit into
mainfrom
claude/lease-verify-1010
Sep 30, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/lease-verify-1010

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

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. The fresh() 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 passes verify() also holds the write lock until it commits. Clocks now decide only when a take-over happens. renew() still uses writeMs, 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 expose CLOCK_BOOTTIME, and a hypervisor pause would freeze it too.

Case 2: auth checks the lease once per request. Hooking begin would miss most Better Auth writes, because its Kysely adapter runs many create/update/delete calls in autocommit, outside any transaction. So after joining the lease, auth now installs a TEMP trigger 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 runs lease.verify(). A trigger runs under its statement's write lock, whether the statement is autocommit or inside BEGIN IMMEDIATE. If the check fails, the statement fails, Better Auth returns a 500, and the mount answers 503 auth_unavailable with no cookie. The lease's own table has no trigger. The per-request renew() 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 IMMEDIATE transaction, 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:

  • Clocks decide when a take-over happens, never whether two hosts write. A fast clock makes a process evict a live holder early, and the holder then refuses its writes.
  • Reads are not guarded.
  • Auth tables created while serving get no trigger until the next activation.

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 with held still 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, so renew() keeps passing. Another host takes over; the next sign-in answers 503 auth_unavailable with no Set-Cookie, and Better Auth wrote no session row.
  • Both new tests fail on main (verified by reverting the source changes: the auth sign-in answered 200) and pass with the fix.
  • npm run verify (includes test:multiprocess) and npm run test:package pass locally on Node 26. Package sizes are within budget.

Limitations

  • The frozen-clock premise for a real laptop suspend or hypervisor pause is reasoned from the clock sources, not measured.
  • The triggers rely on a user function calling back into the same connection during a statement. I verified this on Node 26 only; CI's Node 22.13 leg is the check for older versions.
  • This is a check for a misconfiguration (the unsupported two-host topology), not an adversary control: the lease rows are ordinary rows in the file.

🤖 Generated with Claude Code

…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]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 30, 2026 00:23
@jimhoyd
jimhoyd merged commit f14fe2a into main Sep 30, 2026
25 checks passed
@jimhoyd
jimhoyd deleted the claude/lease-verify-1010 branch September 30, 2026 00:29
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.

[Bug]: Host lease: a resumed suspended VM passes verify() without reading the table; auth's per-request check misses a mid-request stall; docs overclaim

1 participant