Skip to content

Auth SQLite: WAL, busy timeout, BEGIN IMMEDIATE, database rate limit; 503 not false 401 (#927) - #934

Merged
jimhoyd merged 2 commits into
mainfrom
claude/auth-sqlite-multiprocess
Sep 29, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/auth-sqlite-multiprocess

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

The first two items of #927, which also fix a bug that affects single-server sites.

Found

  • No WAL and no busy timeout: auth.sqlite was opened with neither. A second process writing (for example urlcode-auth create-user beside a running server) got "database is locked" at once, and authorize() turned that into a false 401.
  • Sign-ups failed under concurrent writes (reproduced):
    • Better Auth's node:sqlite dialect begins transactions with a deferred begin. Sign-up reads, hashes the password with scrypt, then inserts.
    • Under WAL, a commit from another process during the hash makes the insert fail at once with SQLITE_BUSY_SNAPSHOT (517), which the busy timeout does not retry.
    • With a child process writing every 2 ms: 40 of 40 sign-ups failed before this change, and 40 of 40 succeeded after it.
  • Rate limit per process: Better Auth's limiter kept its counters in a process-local Map, so the effective limit grew with the number of processes.

Changes (packages/auth/src/auth.ts)

  • openAuthDatabase(), used by the server and the CLI:
    • the file is created 0600 in a 0700 directory, and a file that isn't private, isn't regular or has several links is refused;
    • allowExtension is off, the busy timeout is 2000 ms, and trusted_schema=OFF, journal_mode=WAL and synchronous=FULL are set.
  • BEGIN IMMEDIATE: a small documented DatabaseSync subclass rewrites exactly begin to BEGIN IMMEDIATE, so Better Auth keeps its own dialect. The cost: the write lock is held for the length of the password hash, and other processes wait for it up to the timeout instead of failing.
  • Rate limit in the database: rateLimit.storage: 'database' is the default, so the limit is shared across processes; an operator can override it. migrate creates the rateLimit table, and activation refuses a database without it.
  • authorize(): a Better Auth 4xx or null stays 401. Any storage or I/O error now answers 503 {"error":"auth_unavailable"} with Retry-After: 1 and no detail.

Evidence (macOS, Node 26)

  • 503 vs 401: a refresh blocked by a held lock gets 503; the same cookie gets 200 once the lock is released, and a forged cookie gets 401.
  • Shared limit: two serving child processes on one auth.sqlite, with 12 sign-ins alternated between them, give 10 × 401 then 429.
  • create-user beside a server: three CLI create-user runs succeed beside a serving process that commits on every request, and every request returns 200.
  • Migration and file checks: a database missing the rateLimit table is refused, a migrated file is 0600 and in WAL mode, and a 0644 file is refused.
  • Negative controls: without the rewrite, the create-user test fails with 517; without database storage, the shared-limit test fails.
  • Proof adjustment: the proof test clears rateLimit before the MCP runners, and gives the third fixture run fresh data, because counts now persist across processes.
  • Results: npm run verify passes (core 1226, auth 8, store 172, proof 12, proof:authjs 13, ecosystem 12); npm run test:package passes.

Existing sites

Run npx urlcode-auth migrate once to create the rateLimit table, and chmod 600 data/auth.sqlite if the file was created 0644. There is no compatibility path, since the owner is the only user.

Follow-ups

🤖 Generated with Claude Code

jimhoyd and others added 2 commits September 29, 2026 11:53
…IN IMMEDIATE, database rate limit, 503 on storage errors (#927)

- Open auth.sqlite 0600 (refuse a non-private file) with a 2 s busy timeout,
  WAL and synchronous=FULL, mirroring the store and audit.
- Better Auth transactions begin IMMEDIATE. Its sign-up reads, hashes, then
  writes; under WAL a deferred transaction failed with SQLITE_BUSY_SNAPSHOT
  (errcode 517) whenever another process committed during the hash:
  reproduced 40/40 create-user failures beside a steady writer, 0/40 after.
- rateLimit.storage 'database' by default; migrate creates the rateLimit
  table and activation refuses a database without it.
- authorize() answers 503 auth_unavailable (Retry-After: 1) on a storage
  failure instead of a false 401; a missing or forged session stays 401.
- Tests: two serving child processes share one sign-in budget (10 x 401,
  then 429); create-user beside a continuously committing server; 503 vs 401.
- The private-requests proof gives its third fixture run fresh data and
  clears rateLimit before the MCP runners, since the budget now persists.
- Docs: auth README/SECURITY/CHANGELOG, EXTENSIONS, RESILIENCE backups
  (-wal/-shm), proof README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant