# ADR 0006 — Server-side sessions, CSRF and per-request role expansion

**Status:** accepted. **Date:** 2026-12-03.

## Context
Browsers need cookie authentication; integrations need tokens. Revocation must be immediate, and so must role changes.

## Decision
- Sessions in PostgreSQL (`fellow_sessions`, a core migration). The `fellow_session` cookie carries a random 256-bit id; only its SHA-256 is stored.
- HttpOnly, SameSite=Lax cookie, Secure over HTTPS. Signing in discards the previous session (fixation).
- CSRF via synchronizer token (`x-csrf-token`), required only for cookie-authenticated requests with an unsafe method. Bearer tokens are exempt.
- The session and the token store roles; permissions are expanded on every request from the installed manifests.
- OIDC via `openid-client` (code + PKCE, single-use state and nonce in the cache). No homegrown cryptography.
- Per-IP login rate limit in the cache store (Redis in production).

## Consequences
- One database query per cookie-authenticated request (updates `last_seen_at`). Cost not yet measured; a short cache is possible.
- `fellow_sessions` is global (no RLS): the session is resolved before there is a tenant in the context.
- Users with several tenants choose the tenant at login (`accounts.tenant_selection_required`); switching tenants within a session does not exist yet.

## Evidence
`packages/core/tests/services.test.ts` (cookie hash, CSRF, logout revokes, bearer exempt, OIDC with a simulated IdP and single-use state);
tests of the `accounts` module in `examples/records` (wrong credentials, tenant selection, throttling, safe redirect).
