Skip to content

Host lease survives clock skew and fails closed; sign-out confirms the session is gone - #991

Merged
jimhoyd merged 2 commits into
mainfrom
claude/lease-auth-hardening-978
Sep 29, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/lease-auth-hardening-978

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes #978
Fixes #979
Fixes #980

Problem

Design

  • Liveness without comparing clocks. Every renewal writes a strictly larger heartbeat_at. A process judges another host's row only by whether that value changes, timed on its own monotonic clock: a row unchanged for 20 s is dead. Rows from this host (same kernel, same clock) are also dropped once expires_at passes. Everything stays inside the extension's own SQLite database, with no schema change.
  • Joining. If another host has a row, the joiner watches it for up to 20 s and logs one line saying so. It refuses as soon as the row advances. If the row stays silent, the joiner deletes it and joins.
  • Serving. Every heartbeat re-reads the table. If it finds another host's row, the lease is lost: the process deletes its own row, logs a line naming that host, and never re-inserts beside it. It rejoins by itself once no other host's row is left. Heartbeat failures are logged once each time they start.
  • Writes fail closed.
    • Store: every write transaction calls lease.verify() under the write lock, through StoreDatabase.writeGuard. Host transactions go through the same guard.
    • Audit: ingest calls lease.verify() the same way.
    • What verify() allows: a write goes ahead if the last committed heartbeat began less than 10 s ago. After that it goes ahead only if the process's own row is present and no other host's row is. A peer needs 20 s of silence before it can take over, so no write from the old holder can land after the take-over.
    • Refusals: store answers 503 storage_unavailable; audit answers 503 audit_unavailable, and producers keep their events. Auth cannot wrap Better Auth's statements, so it calls lease.renew() once per request and answers 503 auth_unavailable.
  • [Bug]: auth activation that fails after joining the host lease leaves the auth_servers row and a heartbeat timer running #979. Auth closes the lease before its connection in the activation catch. Audit closes it when close() races the first activation.
  • [Bug]: auth: sign-out answers 200 and clears the cookie when deleting the session fails, leaving the session valid #980.
    • After a successful /sign-out, the mount reads the session back from the database (disableCookieCache, disableRefresh). It answers 503 with no Set-Cookie unless the session is gone.
    • I checked the review's unverified claim, and it is true: Better Auth's getSessionFromCtx turns a storage error into "no session", so its session endpoints answer 401. A test now pins that upstream behavior. The mount confirms a 401 from /list-sessions, the revoke endpoints and /change-password the same way.
    • /revoke-session(s) and /revoke-other-sessions already fail with a 500, which the mount turns into a 503.

Remaining limits (documented in STORE.md)

  • The lease detects a second host within one heartbeat or one write. It does not stop both hosts from opening the file, and over a network filesystem the lease's own statements can fail too.
  • A monotonic clock running slow by a large factor, or a stall inside a single write transaction, is not covered.
  • Auth checks the lease per request, not inside Better Auth's transactions.
  • A process that lost the lease keeps reading, and keeps watching the table: that takes the write lock once per heartbeat.
  • A restart after an unclean shutdown waits up to 20 s when the stale row is "another host". That includes a reboot of the same machine, because Linux changes the boot id.

Validation

This is not an independent security review.

🤖 Generated with Claude Code

…ms revocation (#978, #979, #980)

Host lease (core joinHostLease, shared by store, auth and audit):
- Liveness never compares two hosts' wall clocks. Each renewal writes a
  strictly larger heartbeat_at; an observer judges another host's row by
  whether that value changes, timed on its own monotonic clock (dead after
  20 s unchanged). Rows of this host are also dropped once expires_at passed.
- Joining watches another host's row: refuses once it advances, deletes it
  and joins once it stays silent for the TTL. A joiner whose clock is ahead
  no longer evicts a live holder; a crashed host whose clock was ahead blocks
  for 20 s, not the skew.
- Every heartbeat re-reads the table. Another host's row loses the lease:
  delete the own row, log, never re-insert beside it, rejoin by itself once
  no other host's row is left. Heartbeat failures are logged.
- Writes fail closed: HostLease.verify() runs inside each store write
  transaction and audit ingest (503 storage_unavailable / audit_unavailable);
  auth checks renew() once per request (503 auth_unavailable).

#979: auth releases the lease when activation fails after joining; audit
releases it when close races the first activation.

#980: the auth mount confirms against the database that a successful
/sign-out removed the session, and that a 401 from the session endpoints
means no session (Better Auth's getSessionFromCtx reads a storage failure
as no session, pinned by a test); otherwise 503 with no Set-Cookie.

Docs: STORE, OPERATIONS, CAPACITY, EXTENSIONS, FRAMEWORK, the RIM card,
package READMEs, SECURITY and CHANGELOGs. Package budgets raised per the
package-audit policy with measurements.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 29, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment