Skip to content
Docs
Changelog

Server reads the session from the cookie cache

The repository's architectural decision, rationale and consequences.

Markdown

Status: accepted · Date: 2026-11-09 · Amends: ADR-0003

Enforcement: packages/api/src/platform/rpc/request-authority.test.ts, packages/api/src/platform/rpc/session-authority.test.ts, packages/auth/src/auth-round-trips.test.ts, packages/api/src/platform/rpc/session-revocation.test.ts.

Every signed-in request previously read the Session and User from Postgres. A mobile scoped call read it twice. The files gateway read it twice and then read membership. Issue #189 and PRs #192–#194 reduced the browser presentation read; the owner accepted a 60 second revocation window for ordinary data access to remove the remaining serial session read.

Decision

  1. SSR, oRPC and the files gateway read the request session from Better Auth's signed compact cookie snapshot, at most 60 seconds old, or from Postgres on a miss.
  2. Procedures that create, reveal or redirect credentials or standing channels, change account control, administer Users, touch billing or push, or approve a pending agent tool call that lets a run continue (agentTools.resolve) read the database session. Resolving a stored proposal (proposals.resolve) is a one-shot data write, like the manual path, and uses the request session. The authority middlewares and session-authority.test.ts classify every procedure (until 2026-11-28 a markAuthoritative marker). Admin and sensitive Better Auth endpoints remain authoritative.
  3. The membership reader checks member on every request. RLS checks app.org_id() on every tenant statement.
  4. The Better Auth handler strips the snapshot from every endpoint except the presentation GET /get-session.
  5. The cookie integration forwards Set-Cookie on a miss. A hit does not re-issue the snapshot.
  6. The mobile scope header is compared with the request session. Authoritative procedures compare it again with the database read.

Consequences

Session reads records the revocation behavior. A copied cookie can continue to access ordinary data for at most 60 seconds. An SSE request keeps the identity read at its start. Presigned download and upload URLs require a database session check before minting. A revoked session cannot mint a new URL; one minted before revocation remains usable until expiry, at most 15 minutes. Bump session.cookieCache.version when fields used by the server change. A mobile RPC response does not store the re-issued cookie, so subsequent requests can miss again. Local measurements are recorded in the measurement report.

Alternatives

  • The status quo retains a serial database round trip on every request.
  • KV secondaryStorage is eventually consistent, has no atomic operations, and moves sessions out of Postgres.
  • Isolate-local memory has no cross-isolate invalidation.
  • A shorter maxAge reduces the window but increases misses.

Amendment (2026-11-28): one request authority

The six rules above were implemented in three places (the procedure middlewares, the files gateway, the tool authorization) and had drifted. packages/api/src/platform/rpc/request-authority.ts now owns them: a caller asks for the acting User or the Member's tenant at the request authority (rule 1) or the database authority (rule 2). What that settled:

  • The database session must be the request session's User and Session, and for a tenant it must name the same Active Organization. The files gateway already refused otherwise and the tools refused a changed Active Organization; uploads.prepare, uploads.resume, uploads.signPart and documents.prepareText used the database's Active Organization silently and now refuse (FORBIDDEN, "The active organization changed.") like the others.
  • Rule 3 at the database authority: member is read again, not taken from the request's memo, so a tool call inside a long assistant stream sees a membership revoked since the stream began. A request-level ask still reads it once per request.
  • Rule 6: the scope is compared once with the database read (it was compared twice).
  • An OAuth access token is refused by the authority middlewares, as before; it may still act through the tool catalog, where the token's User is what the database vouches for.
  • Only two middlewares read the database session, requireDatabaseSession and requireDatabaseTenant; a feature can no longer mark its own.

Amendment (2026-11-29) — Scoped RPC refusals

At either authority, a captured tuple with no live Session returns UNAUTHORIZED (401), so a client can start reauthentication. A different live tuple or an anonymous scope against a live Session returns SCOPE_MISMATCH (409). A malformed header returns BAD_REQUEST (400).

A keyed collection write uses request authority for its first admitted attempt. Before returning a stored receipt, it asks database authority for the live Session and membership. Each replay asks independently, including inside a batch.

Edit on GitHubDocumentation is in English.
Cookie settings
We use essential cookies to make our app work. With your consent, we also use optional cookies to improve your experience.

Read our Cookie Policy and Privacy Policy.