Repository navigation
Description
Runtime revision and environment
main at 21b4dfd (after #991). Node 24, macOS. Reproduced against packages/core/src/host-lease.ts directly with injected probes (the same verify() the store installs as its writeGuard, and the renew() auth calls per request).
Summary
docs/STORE.md ("Several serving processes on one host") says: a holder that stalled for longer than the TTL (a suspended VM, SIGSTOP, a blocked event loop) and resumes after another host took over refuses its writes. Two cases where that does not hold:
- A suspended VM's monotonic clock does not advance.
verify()skips the table read whilemonotonic() - confirmed < writeMs(10 s).performance.now()isCLOCK_MONOTONICon Linux andmach_absolute_timeon macOS; neither counts system suspend, and a hypervisor pause (QEMU/KVM restores kvmclock on resume) freezes the guest's clock too. So a holder paused within 10 s of its last heartbeat resumes withfresh()still true, and until its next heartbeat fires (up to 5 s of its own time later) every store/audit write passesverify()without reading the table, while the other host (which timed 20 s of silence on its own running clock, deleted the row and joined) also writes. - Auth checks the lease once per request, before Better Auth runs (
packages/auth/src/auth.tsserving()thenauth.handler(...)). A stall of more than 20 s betweenrenew()and Better Auth's own statements (SIGSTOP, a blocked event loop, a slow await) lets another host take over and the resumed request still writes. The docs mention the per-request check, but the "What the lease does not do" list only excludes "a stall inside a single write transaction", which does not describe an auth request.
Both need the unsupported two-host topology, so this is a correctness/docs gap in a misconfiguration check, not an exploitable issue.
Minimal reproduction
Case 1 (A's clock frozen while B's advances):
import { DatabaseSync } from 'node:sqlite';
import { mkdtempSync } from 'node:fs';
import { join } from 'node:path';
import { joinHostLease } from './packages/core/src/host-lease.ts';
const dir = mkdtempSync('/tmp/lease-'); const open = () => { const d = new DatabaseSync(join(dir, 'x.sqlite'), { timeout: 2000 }); d.exec('PRAGMA journal_mode=WAL'); return d; };
const dbA = open(), dbB = open(); dbA.exec('CREATE TABLE data(v TEXT)');
let monoA = 1_000_000, monoB = 5_000_000;
const A = await joinHostLease(dbA, { table: 't_servers', what: 'test', log: () => {}, probe: { hostname: () => 'a', bootId: async () => 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', monotonic: () => monoA, sleep: async () => {} } });
// A's VM pauses: monoA frozen. B watches A's row for 20 s of its own time, deletes it, joins.
const B = await joinHostLease(dbB, { table: 't_servers', what: 'test', log: () => {}, probe: { hostname: () => 'b', bootId: async () => 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', monotonic: () => monoB, sleep: async ms => { monoB += ms; } } });
monoA += 1000; // A resumes
dbA.exec('BEGIN IMMEDIATE'); A.verify(); dbA.prepare("INSERT INTO data VALUES ('A')").run(); dbA.exec('COMMIT');
console.log('B.held', B.held, '- A verify() passed and A wrote');Case 2: same setup, call A.renew() (true), then join B with a sleep that advances both clocks (a 25 s SIGSTOP of A); A's next statement is not checked by anything.
Relevant output
B joined, B.held = true rows: [ 'b' ]
A verify() passed and A WROTE while B holds
request on A: renew() = true
B holds: true ; A resumes and Better Auth writes the session row (no lease check on this path)
session rows written by both hosts: [ 'A', 'B' ]
Suggested direction (unverified)
- For case 1, either read the table in every
verify()(one indexed read of a tiny table under the lock the write already holds), or add the suspended-VM case to "What the lease does not do" and remove it from the "refuses its writes" sentence.CLOCK_BOOTTIMEis not reachable from Node, and it would not cover a hypervisor pause anyway. - For case 2, state in STORE.md's limits (and packages/auth/SECURITY.md) that for auth a stall between the per-request check and Better Auth's statements is not covered.
Not verified: the frozen-clock premise on a real hypervisor pause or laptop suspend (reasoned from the clock sources, not measured).
Activity
fix(core,auth): host lease verify always reads the table; auth verifi…
One serving process per database: replace the host lease with an OS l…