Skip to content

[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 #1010

Closed

Description

@jimhoyd

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:

  1. A suspended VM's monotonic clock does not advance. verify() skips the table read while monotonic() - confirmed < writeMs (10 s). performance.now() is CLOCK_MONOTONIC on Linux and mach_absolute_time on 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 with fresh() still true, and until its next heartbeat fires (up to 5 s of its own time later) every store/audit write passes verify() 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.
  2. Auth checks the lease once per request, before Better Auth runs (packages/auth/src/auth.ts serving() then auth.handler(...)). A stall of more than 20 s between renew() 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_BOOTTIME is 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions