8 Cross-Cutting Concepts

Authentication and Authorization

OIDC-only (no local accounts): Every user authenticates via an OIDC-conformant provider (Keycloak, Azure AD, Dex, Google). No local passwords, no shadow directory.

Token flow (single Elixir application, ADR-021):

Matrix Client → Nebu (Nebu.Web.Plugs.OidcPlug):
  Authorization: Bearer <oidc_token>
  → OidcPlug validates the token against the cached OIDC JWKS (signature + exp + iss + aud + sub)
  → extracts: user_id (@sub:server.name), system_role
  → consults the token denylist (revoked tokens) and provisions the user row idempotently

OidcPlug → domain (in-process):
  conn.assigns.current_user = "@user:server.name"  (+ access_token, oidc_claims)
  the Matrix handlers call the domain functions directly — no metadata hop, no separate process

There is no Go layer in front of the application; the OIDC plug is the single auth + provisioning point. Token revocation, rate limiting, and security headers are all enforced in Nebu.Web on the same node.

Components in Nebu.Web (the shared HTTP plumbing):

  • Nebu.Web.Plugs.OidcPlug — extracts Authorization: Bearer, enforces a strict asymmetric alg allowlist (RS*/ES*/PS* only — alg:none and HMAC HS* are rejected before any key selection, blocking alg-confusion attacks, MEDIUM-2), selects the JWK by the token kid, and verifies signature + exp + iss + aud + sub via Assent.JWTAdapter.verify/3. Issuer and audience are validated fail-closed: a missing oidc_issuer/oidc_audience config rejects every token rather than failing open (MEDIUM-3). After a token cryptographically verifies, the plug consults Nebu.Web.TokenDenylist (see below) and rejects a revoked token. On success it assigns conn.assigns.current_user = "@<sub>:<server_name>" (plus access_token and oidc_claims, so POST /logout can revoke exactly the token that authenticated the request) and idempotently provisions the user row (TTL-cached, 60s, kept off the /sync hot path). On failure it returns a Matrix-error JSON envelope (M_MISSING_TOKEN / M_UNKNOWN_TOKEN) and halts. The verification core, OidcPlug.verify_token/2, accepts optional :issuer and :jwks overrides; only the bootstrap operator-login callback passes them (to verify against the bootstrap draft's issuer + locally-fetched keys before server_config exists). All checks remain fail-closed and the single verification code path is preserved — the overrides change only where the issuer/keys come from, not the validation.

  • Nebu.Web.TokenDenylist (logout revocation, F-1) — OidcPlug returns the verified OIDC token verbatim as the access_token, so destroying the session GenServer alone would NOT stop the token from re-validating on the next request; without a denylist, logout would be a no-op until the token's own exp. POST /logout adds the presented token to this denylist; OidcPlug consults it on every authenticated request (after crypto verification, so an attacker cannot flood it with unsigned strings) and rejects a revoked token with 401 M_UNKNOWN_TOKEN. Entries are keyed by the JWT jti claim when present, otherwise by the SHA-256 hash of the raw compact token (Go pgTokenHash parity; the raw token is never stored), and self-expire at the token's exp (lazy delete on lookup, so the table cannot grow unbounded). Persistence strategy (fix-jwt-denylist-persistence): ETS is a write-through cache; the pre-existing PostgreSQL invalidated_tokens table (token_hash TEXT PRIMARY KEY, expires_at BIGINT) is the persistent truth source. revoke/3 writes to ETS and then to DB (best-effort — logs a warning on failure, never crashes). revoked?/2 checks ETS first; on a miss it falls back to a DB SELECT so tokens revoked before a node restart are still rejected after ETS is cleared. DB keys use a flat-string encoding disjoint from ETS tagged tuples: "jti:<value>" for jti-keyed entries, bare 64-char SHA-256 hex otherwise. DB results are not back-populated into ETS (write-through only — avoids stale-cache issues in rolling-restart / multi-node scenarios).

  • Nebu.Web.Plugs.RateLimit (per-IP rate limiting, F-3) — a classic per-IP token-bucket limiter (ETS), porting the Go gateway's NewIPRateLimiter/strictRL so the brute-force / verification-DoS controls survive the migration. POST /login is limited to 30 req/min burst 10 (Go strictRL parity); the long-poll GET /sync to a generous 600 req/min burst 100 (caps a single IP hammering the endpoint to exhaust BEAM long-poll processes without impeding a normal client). On exhaustion it responds 429 with the Matrix envelope {"errcode":"M_LIMIT_EXCEEDED","error":"Rate limit exceeded"} plus a Retry-After header and halts. The bucket key is conn.remote_ip (the directly connected peer — no trusted-proxy/XFF config yet, so the safe default: a client cannot spoof its own bucket key). Buckets are volatile ETS (reset on restart — a restart cannot bypass a sustained attack because the steady-state rate still bounds throughput). NEBU_RATE_LIMIT_DISABLED=true makes it a no-op (test/dev escape hatch, mirroring the Go gateway flag).

  • Security header (F-5) — Nebu.Web.Router registers a before_send callback at the front of the pipeline that sets X-Content-Type-Options: nosniff on every response — including those produced by the forwarded sub-routers and CORSPlug's halted preflight. All Nebu responses are application/json Matrix envelopes, so this prevents any MIME-sniffing edge case on a misbehaving client.

  • Nebu.Web.OidcConfig — a GenServer + ETS cache for OIDC issuer, audience, JWKS URI, and (since story fix-and-configure-sso-redirect-schemes) the Admin-UI-managed sso_extra_redirect_schemes string (additional trusted redirectUrl deep-link schemes for SsoLogin.redirect_url_allowed?/1, beyond the hardcoded defaults) and (story legacy-sso-redirect-confirmation) the sso_redirect_allowed_origins list (https origins whose legacy SSO logins skip the confirmation page, read by Nebu.Matrix.SsoRedirect.listed?/2; absent or unreadable means empty, i.e. fail closed to the confirmation page). ADR-022: OIDC is configured EXCLUSIVELY via the Bootstrap Wizard (server_config) — there is no environment-variable OIDC config path. OidcPlug, JwksCache, SsoLogin, and Admin.Auth resolve issuer/audience/JWKS-URI/SSO-schemes solely through this cache (and the bootstrap draft during the wizard flow). Caching in ETS :nebu_http_oidc_config avoids per-request DB lookups on the hot OIDC and SSO-redirect paths. The cache is refreshed both by the Bootstrap Wizard and by Nebu.Admin.DB.upsert_server_config/1 (for post-bootstrap Admin UI edits, e.g. /admin/config) after configuration is written to server_config, and cluster-aware invalidation via :pg ensures all nodes stay synchronized (ADR-020, M-3). When server_config has no OIDC config (pre-bootstrap), the cache returns nil and every consumer fails closed (F-4 — all Bearer tokens rejected with 401; admin → /bootstrap); the SSO-schemes field independently defaults to [] (only the hardcoded scheme allowlist governs redirectUrl validation) rather than failing closed, since it is an additive allowlist extension, not an auth-critical value. A test-only :oidc_config_source seam injects values without a live DB.

  • Nebu.Web.JwksCache — a GenServer that fetches the OIDC provider JWKS on start and refreshes it on a TTL (NEBU_OIDC_JWKS_TTL, default 300s — a cache-tuning knob, not OIDC config). The JWKS URI is resolved (ADR-022) from server_config via OidcConfig.jwks_uri/0; when only the issuer is configured (no explicit JWKS URI) it is auto-discovered from the issuer's /.well-known/openid-configuration document. The discovered jwks_uri is validated to use https:// or http:// scheme only (SSRF guard). On a refresh failure it keeps the last-known-good keys rather than blanking the cache, so transient JWKS outages do not reject otherwise-valid tokens. The JWKS HTTP fetch uses TLS peer verification for https endpoints. Its state is volatile and is recovered by a fresh fetch when the supervisor restarts it. A public helper JwksCache.fetch_keys_for/2 performs an explicit, TLS-verifying fetch for a given jwks_uri/issuer without mutating the global cache — used by the bootstrap operator-login callback so it can verify an ID token against the bootstrap draft's keys before server_config exists, without warming the global cache and thereby breaking the fail-closed-until-bootstrap-completes invariant (F-4).

  • Nebu.Matrix.OidcMetadata — MSC2965 discovery endpoint (bugfix-oidc-public-endpoint-urls): GET /_matrix/client/unstable/org.matrix.msc2965/auth_metadata proxies (the stable GET /_matrix/client/v1/auth_metadata is NOT part of this passthrough any more: since epic oauth-broker / ADR-033 it is Nebu's own RFC 8414 document served by Nebu.OAuth.Http, 404 M_UNRECOGNIZED while oauth_enabled is false; likewise auth_issuer and this unstable auth_metadata path serve Nebu's own issuer/document while oauth_enabled is true and fall back to this passthrough only while it is false, Nebu.Web.Plugs.OAuthDiscovery) and caches (5-min TTL) the OIDC provider's /.well-known/openid-configuration discovery document, with two architectural principles:

    1. Honest issuer over response rewriting (config honesty): the IdP is configured with its OWN publicly-resolvable issuer value (issuer in dev Dex's config.yaml, or a Keycloak realm URL), independent of how it is reached from Nebu's perspective (whether as an internal Docker-Compose service name or a public hostname). Dex/Keycloak bakes this SAME issuer into every URL in its discovery document (authorization_endpoint, token_endpoint, jwks_uri, issuer field itself, etc.), so the document is already honest and publicly-reachable by construction. This design was chosen INSTEAD of response rewriting (a rejected v1 approach that rewrote the document's origin fields at serve time) because rewriting is fragile: it desyncs the advertised issuer from the real iss claim baked into tokens, requires a hand-maintained per-IdP field allowlist, and still leaves unfixed endpoints that Dex never exposes. By contrast, honest configuration makes every URL correct at the source.

    2. MSC2965 field synthesis (when absent): Dex's discovery document genuinely omits 3 fields the MSC2965 profile (and the Matrix Dart SDK) require: registration_endpoint, response_modes_supported, revocation_endpoint. Nebu.Matrix.OidcMetadata.ensure_msc2965_fields/2 fills in ONLY these three when missing (never rewriting any field the upstream document provides). response_modes_supported is a spec constant (["query", "fragment"] — no origin dependency). registration_endpoint and revocation_endpoint are synthesized as <configured_issuer>/<placeholder_path> (e.g. http://dex.localtest.me:5556/dex/clients/register) using the CONFIGURED issuer value from server_config.oidc_issuer (a trusted local value), NOT the upstream document's issuer field (which is attacker-influenced network input); preserving the issuer's path (e.g. /dex, /realms/nebu) ensures the synthesized URL stays inside the IdP's mount point. A malformed configured issuer → synthesis is skipped (fail-closed, synthesis omitted rather than serving a garbage URL).

    On any fetch/parse failure, auth_metadata returns 503 M_UNAVAILABLE without attempting to synthesize a fallback — no incorrect endpoint URLs are served. The raw discovery document is otherwise passed byte-for-byte when fetch succeeds, upholding the honest-issuer principle: Dex's response shapes exactly what browsers/clients see.

  • GET /_matrix/client/v3/auth_issuer (ADR-022): a simple endpoint returning {"issuer": <configured_issuer>} read from the OidcConfig cache (the same server_config value) — fail-closed (503) until bootstrap configures it. This endpoint's behavior mirrors auth_metadata in architecture: it surfaces the configured issuer without rewriting, by construction.

  • Router scoping — Nebu.Web.Router keeps GET /health open, forwards the unauthenticated Matrix routes (POST /login, GET /profile, MSC2965 discovery) to Nebu.Matrix.HttpPublic, and forwards everything else under /_matrix to the protected sub-router Nebu.Matrix.Http.Protected whose pipeline runs OidcPlug.

Both request-time ETS tables (token denylist, rate-limit buckets) are created eagerly at Nebu.Application boot so the lazy ensure_table fast path never has to race under concurrent first requests (closing a brief fail-open window, R-2). Config validated per-operation, not at boot (ADR-022): runtime.exs reads no OIDC config — the server boots without OIDC and fails closed until the Bootstrap Wizard writes server_config. NEBU_DB_URL and NEBU_PII_ENCRYPTION_KEY remain mandatory at boot. When server_config has no OIDC config, OidcPlug fails closed (F-4) and rejects all Bearer tokens with 401; the wizard validates required fields (issuer, single client id/secret, audience, JWKS URI) at form submission to prevent completing into a broken state.

Relevant configuration (server service): all OIDC config (issuer, audience, JWKS URI, the single client id/secret, claim mappings) is set via the Bootstrap Wizard (server_config), never env. Remaining env knobs: NEBU_OIDC_JWKS_TTL (JwksCache refresh interval), NEBU_OIDC_DISPLAY_NAME (advertised IdP name for POST /login m.login.sso), NEBU_RATE_LIMIT_DISABLED, and NEBU_SERVER_NAME (used to build the @<sub>:<server_name> Matrix user ID and to derive the admin /admin/callback redirect_uri). Additional trusted SSO client redirect schemes (beyond the hardcoded defaults element/schildichat/im.fluffychat.auth) are bootstrap-only, configured via the Admin UI (/admin/config, server_config.sso_extra_redirect_schemes) — no env var (story fix-and-configure-sso-redirect-schemes). The same holds for the trusted https redirect origins (server_config.sso_redirect_allowed_origins, story legacy-sso-redirect-confirmation, ADR-022: no env var). The OAuth 2.0 authorization server switch (server_config.oauth_enabled, oauth_issuer_url) is written from the same Admin page (/admin/config, section "OAuth 2.0 authorization server", story admin-oauth-switch, ADR-033 AS1-AS12) or by SQL: it has the protection of the SSO trust lists (strict fetch metadata, a fresh admin sign-in, an activation acknowledgement, no framing) and one audit row oauth_switch_changed per change; other nodes follow within NEBU_OAUTH_CONFIG_CACHE_MS.

SSO redirect scheme rejection: content-negotiated response (story sso-redirect-error-page/html-error-page-for-rejected-sso-redirect-scheme): when SsoLogin.redirect_url_allowed?/1 rejects a client's redirectUrl scheme, GET .../sso/redirect and .../sso/redirect/{idpId} now content-negotiate the rejection response on the request's Accept header — a real browser (the Matrix SSO flow always opens one here) with an HTML-preferring Accept header gets a minimal static HTML error page naming only the rejected scheme (never the full redirectUrl/host/path/query), pointing to /admin/config → "SSO redirect schemes" to request support. Every other Accept header (missing, application/json, bare */*, or any ambiguous/tied preference) still gets the exact pre-existing JSON M_INVALID_PARAM error, byte-for-byte — this remains the guaranteed default per the CS API's standard-error-response rule. The scheme is the only interpolated value and is explicitly escaped via Plug.HTML.html_escape/1; the HTML response carries Content-Security-Policy: default-src 'none'. This is a rejection-response-format change only — the allow/deny decision of that story was untouched (story legacy-sso-redirect-confirmation later tightened the syntax rules behind redirect_url_allowed?/1, see "Legacy SSO redirect confirmation" below; the rejection bodies stay byte-identical), as are uia_fallback/1 and the OIDC callback.

Bootstrap mode: First OIDC login automatically receives instance_admin. Bootstrap mode is permanently disabled after the first admin setup. No default password, no insecure fallback.

Single OIDC client (ADR-022): ONE OIDC client serves both Matrix Bearer-token validation and the admin operator login. The wizard configures it (server_config.oidc_client_id + AES-256-GCM-encrypted oidc_client_secret); the IdP-side client registers all Nebu redirect URIs (Matrix SSO redirect, Element auth.html, and /admin/callback). There is no separate admin client and no env credential fallback — SsoLogin.resolve_client_credentials/0 and Admin.Auth.oidc_config/1 read the single client from server_config only (or the bootstrap draft during the wizard flow), decrypting the at-rest secret on use. The admin /admin/callback redirect_uri is server-derived from :server_name

  • :admin_session_secure (never from request headers).

OIDC scopes (ADR-022): the admin/bootstrap authorization request asks for openid + the operator-configured extra scopes (empty by default — no extra scopes unless the operator configured them via Bootstrap Wizard or /admin/config); the Matrix SSO authorization request asks for openid + operator-configured extra scopes (same: empty by default). Both add offline_access when their configured refresh mode is "offline" (read live from server_config via the :matrix_oidc_refresh_mode_reader / admin_oidc_refresh_mode seams). openid is always mandatory (cannot be removed); all other scopes are operator-configurable via the Bootstrap Wizard and /admin/config (Story configurable-oidc-scopes, server_config keys matrix_oidc_extra_scopes / admin_oidc_extra_scopes). There is no NEBU_OIDC_EXTRA_SCOPES env var (ADR-022: bootstrap-only config).

Nebu.Web.OidcScopes (Story centralize-scope-resolution, epic oidc-simplification, 2026-07-01): scope resolution + validation for BOTH Nebu.Matrix and Nebu.Admin is centralized in a single Nebu.Web shared-infra module (server/lib/nebu/web/oidc_scopes.ex), analogous to OidcConfig / JwksCache:

  • resolve_scope(:matrix | :admin) :: String.t() — "openid" + that client's OWN configured extra scopes + "offline_access" iff that client's OWN refresh mode is "offline", deduplicated, space-separated. Reads through Nebu.Web.OidcConfig's extended ETS cache (see below) — no ad-hoc SQL in OidcScopes itself.
  • validate_scope_string(String.t()) :: :ok | {:error, String.t()} — the ONE validator (~r/^[a-zA-Z0-9_:.\-]+\z/ per token), replacing the two previously-duplicated regex copies in bootstrap_controller.ex and admin/auth.ex.
  • Nebu.Matrix.SsoLogin.scope_list/0 and Nebu.Admin.Auth.admin_oidc_scope/0 are now THIN DELEGATES to OidcScopes.resolve_scope/1; bootstrap_controller.ex/config_controller.ex call OidcScopes.validate_scope_string/1 instead of a local regex.
  • The existing four per-domain reader seams (:{matrix,admin}_oidc_{extra_scopes,refresh_mode}_reader) migrated house to OidcScopes UNCHANGED in name/shape, so the pre-existing regression suites keep passing without modification.

This supersedes the "Admin/Matrix identity unification" scope-union fold below: Nebu.Matrix.matrix_oidc_extra_scopes/0 (the Public-API facade delegate that fold used) is removed — see the dedicated note further down.

A matching warning is emitted for client_secret. This ensures operators can detect a misconfigured SSO flow immediately from startup logs before the first login attempt.

LoginHandler API change: LoginConfig gains two optional loader fields:

resolveClientID(ctx) and resolveClientSecret(ctx) methods on LoginHandler delegate to the loader when set, otherwise return the static fields. All usages in sso.go (GetSSORedirect, GetSSOCallback) and login.go (PostLogin verifier) call these methods — the static-field fallback preserves backward compatibility for test setups and deployments without a Bootstrap DB.

OIDC Scopes Law (bugfix-remove-default-scopes): extra scopes default to "" (empty). When server_config has no value (nil or missing row), the scope is openid only — no profile, email, or groups defaults. Operators configure scopes explicitly via the Bootstrap Wizard or /admin/config; only then are the configured scopes appended to openid.

ClientRuntime key in server_configDefault (no row)
Matrix SSOmatrix_oidc_extra_scopes"" (openid only)
Admin loginadmin_oidc_extra_scopes"" (openid only)

Bootstrap Wizard writes explicit values; /admin/config allows runtime update without redeploy.

Configurable OIDC Refresh Mode (Story oidc-refresh-mode-config):

Whether the Matrix and Admin OIDC flows request a refresh token (offline_access) is configurable per client rather than hardcoded. Two server_config keys, each offline | online, drive it:

KeyDefaultBuilder reading it liveoffline_access requested?
matrix_oidc_refresh_modeofflineNebu.Matrix.SsoLogin.scope_list/0 → Nebu.Web.OidcScopes.resolve_scope(:matrix)iff mode = offline
admin_oidc_refresh_modeonlineNebu.Admin.Auth.admin_oidc_scope/0 → Nebu.Web.OidcScopes.resolve_scope(:admin)iff mode = offline
  • Set at bootstrap (Bootstrap Wizard) and changeable at runtime via /admin/config (Nebu.Admin.ConfigLive). Runtime changes apply to subsequent logins — no redeploy.
  • Post-centralize-scope-resolution, Nebu.Web.OidcScopes reads the mode through Nebu.Web.OidcConfig's shared ETS cache (or the injectable :{matrix,admin}_oidc_refresh_mode_reader seam in unit tests) instead of an ad-hoc per-call SQL query — an Admin-Config write to any of the 4 scope/refresh-mode keys now forces a synchronous cache refresh + :pg cluster broadcast via Nebu.Admin.DB.upsert_server_config/1 (mirroring the oidc_user_id_claim cache-invalidation pattern), so a runtime change is visible on the very next login without a stale-cache window, even on multi-node deployments. openid is always mandatory; offline_access is appended only when the client's mode is offline.
  • The admin client no longer force-requests offline_access — its online default means no special IdP client approval is needed for standard admin sign-in.
  • IdP caveat: Dex issues a refresh token only when offline_access is requested, so admin online mode disables silent refresh on Dex. Keycloak supports session-bound online refresh, so online mode there can still keep the session alive.

Configurable OIDC Extra Scopes (Story configurable-oidc-scopes):

Which OIDC scopes Nebu requests beyond the mandatory openid is operator-configurable per client, set in the Bootstrap Wizard (Step 3, before refresh-mode) and changeable at runtime via /admin/config. Two server_config keys drive it:

KeyDefaultScope builder
matrix_oidc_extra_scopes"" (empty)Nebu.Matrix.SsoLogin.scope_list/0 → Nebu.Web.OidcScopes.resolve_scope(:matrix)
admin_oidc_extra_scopes"" (empty)Nebu.Admin.Auth.admin_oidc_scope/0 → Nebu.Web.OidcScopes.resolve_scope(:admin)
  • openid is always injected by Nebu.Web.OidcScopes.resolve_scope/1 — it cannot be removed by operator config, and no literal scope beyond "openid" is ever hardcoded there (OIDC Scopes Law, mechanically pinned by a grep test in oidc_scopes_test.exs).
  • offline_access is still controlled by {matrix,admin}_oidc_refresh_mode, and — as of centralize-scope-resolution — is scoped strictly to each client's OWN refresh mode; it can no longer be requested by one client via the other's *_extra_scopes configuration (see the superseded identity-unification note below for the leak this closes).
  • Scope tokens are validated against [a-zA-Z0-9_:.\-]+ (anchored with \z — trailing-newline safe) via the ONE validator, Nebu.Web.OidcScopes.validate_scope_string/1, at every write boundary (bootstrap wizard, admin config page, bootstrap promotion) — no local regex copies remain.
  • Migration 000013 extends the config_update_mutable RLS policy to include both new keys, mirroring the 000007 pattern for refresh-mode keys.
  • Runtime injection via :{matrix,admin}_oidc_extra_scopes_reader seams (0-arity fun) mirrors the :{matrix,admin}_oidc_refresh_mode_reader pattern for test isolation; both pairs of seams now live on Nebu.Web.OidcScopes (unchanged name/shape) rather than being duplicated per domain module.

Bootstrap-mode scope carve-out (bugfix-bootstrap-scope-ignored, 2026-07-09): the table above (admin_oidc_extra_scopes → server_config) only applies to the admin client's post-bootstrap login. During the wizard itself, server_config.admin_oidc_extra_scopes does not exist yet — it is only written by do_complete_bootstrap at the very end — so Nebu.Admin.Auth.oidc_config("bootstrap") cannot use admin_oidc_scope/0 (it would always see an empty value and request openid only, ignoring whatever the operator just typed into Step 3's "Admin client scopes" field). It instead calls Nebu.Web.OidcScopes.resolve_scope_with_extra(:admin, draft["admin_oidc_extra_scopes"]), reading the value straight from the in-progress bootstrap_draft. resolve_scope_with_extra/2 composes the scope identically to resolve_scope/1 (same openid + extras + refresh-mode-gated offline_access rule) but takes the extras value as an argument instead of reading a fixed seam — and, since that argument is no longer forced through a validated trust boundary, it re-validates it itself (validate_scope_string/1, fail-closed to openid-only on an invalid string) rather than trusting every call site. Bootstrap-mode's refresh-mode side is unaffected by this fix and still reads server_config/the reader (a known, deliberately out-of-scope, same-class gap for a future story).

Admin/Matrix identity unification (bugfix-unify-admin-matrix-oidc-identity, 2026-07-01) — SUPERSEDED by story centralize-scope-resolution (same date): Because matrix_oidc_extra_scopes and admin_oidc_extra_scopes are independently configurable, an operator could set them to different values — e.g. Matrix requests profile (so preferred_username/name resolve) while Admin requests only groups. The SAME real person then gets ID tokens with different claim sets from the two flows, and Nebu.Matrix.UserId.from_claims/2 (claim-priority: configured claim → preferred_username → name → sub) derives two different Matrix user_ids for one person — a split identity, visible as two rows in the admin Users list.

Original fix: Nebu.Admin.Auth.admin_oidc_scope/0 unioned its own configured extra scopes with the Matrix flow's (Nebu.Matrix.matrix_oidc_extra_scopes/0, exposed via the Nebu.Matrix facade per ADR-020), guaranteeing the admin ID token was always a claim-superset of the Matrix ID token.

This fold is REMOVED as of centralize-scope-resolution (2026-07-01): it was itself the root cause of a Kassandra LOW finding (AC3 of that story) — offline_access configured under matrix_oidc_extra_scopes could leak into the Admin OIDC request via the unfiltered fold, even when the admin client's own refresh mode was "online". Nebu.Admin.Auth.union_scopes/2 and the Nebu.Matrix.matrix_oidc_extra_scopes/0 facade delegate are both deleted; Nebu.Web.OidcScopes resolves each client's scope from ONLY that client's own server_config keys — no cross-client union. An operator who independently configures matrix_oidc_extra_scopes and admin_oidc_extra_scopes can once again see two different Matrix user_ids for the same person (the original bug this fold fixed); identity/claim unification across independently-scoped flows is tracked as a separate, claim-mapping-centric follow-up (see centralize-scope-resolution's Non-Goals: "Kein Claim-Mapping"), not re-solved via scope folding.

Known limitation (historical, from the original fix): it prevented NEW divergence only, never retroactively merged identities that already split under the old drift in an existing deployment (room memberships, devices, and audit-log actor rows tied to a stray opaque-sub user_id remain untouched either way). Kassandra's non-blocking MEDIUM about the admin admin_claim?/1 any-key scan seeing a wider claim set is now largely moot for admin logins (the admin scope no longer widens via the Matrix fold); the underlying any-key-scan risk itself is RESOLVED by story scope-admin-claim-to-path (2026-07-02, see the "Path-scoped gate" note above) for deployments that have (re-)run bootstrap claim selection since — pre-existing deployments remain on the documented fallback until they do.

Bootstrap-completion session identity fix (Story fix-bootstrap-admin-session-identity, epic oidc-simplification, 2026-07-01):

A second, independent matrix_user_id-divergence class from the one above: Nebu.Admin.Auth. bootstrap_confirm_claim/2 (the handler for POST /admin/bootstrap/select-claim, which completes the setup wizard) minted the admin session via a second, hand-rolled user map (sub/email/admin/session_exp/refresh_grace_exp) that never included "matrix_user_id" — unlike put_current_user/2 (used by the ordinary normal_callback/2 login), which derives it via Nebu.Matrix.UserId.from_claims/2. An operator who created a room via the Admin UI immediately after finishing bootstrap (before ever logging in as a Matrix client) was attributed as the raw opaque OIDC sub instead of their canonical Matrix user_id — RoomsController.actor_id/1 / UsersController.actor_id/1 both fall back to sub when matrix_user_id is absent/empty.

Fix: bootstrap_confirm_claim/2 now mints the session via the SAME put_current_user/2 builder normal_callback/2 uses — no second map construction. Making this possible required extending what the bootstrap draft carries: bootstrap_callback/2 (the OIDC-callback leg, mode "bootstrap") now additionally stashes the FULL verified ID-token claims map as JSON under a new draft key bootstrap_full_claims (the pre-existing bootstrap_claims key is a dot-path-flattened projection built for the claim-picker UI and is not usable as input to UserId.from_claims/2's flat-key lookups; both keys are kept, serving their separate purposes). A private bootstrap_claims/1 decodes bootstrap_full_claims, falling back to reconstructing a flat claims map from the legacy flattened bootstrap_claims list (bare/non-dot-path keys only) for a bootstrap that was already in-flight when this fix deployed — this keeps such an in-flight setup from being bricked. The reconstruction/decode-failure guard is checked in bootstrap_confirm_claim/2's cond chain BEFORE complete_bootstrap/2 runs, so bootstrap_completed can never be set "true" from a state that would then fail to mint a session.

Because both login paths now converge on one builder with the same claims-derivation logic, the guarantee that a bootstrap-completion session's matrix_user_id equals a subsequent normal re-login's is now structural (one shared code path), not incidental — the same closure this story's sibling bugfix-unify-admin-matrix-oidc-identity/centralize-scope-resolution sought for scope-driven claim divergence, but for a session-construction duplication instead.

Considered and rejected: a defense-in-depth fallback in RoomsController.actor_id/1 / UsersController.actor_id/1 that would derive matrix_user_id from a synthetic sub/email-only claims map when the session lacks matrix_user_id (e.g. a stale session minted before this fix deploys), instead of falling back to the raw sub. Rejected in code review: for an instance with a non-default oidc_user_id_claim (e.g. preferred_username), that claim is absent from a stale session, so the fallback would have silently derived a plausible but wrong Matrix user_id — worse than today's visibly-wrong raw-sub fallback, which at least fails obviously. actor_id/1 in both controllers ships unchanged. The residual — a session minted before this fix deploys still shows the pre-fix behavior until the operator's next login — is bounded (sessions are TTL-capped) and self-healing (every session minted after deployment now carries matrix_user_id), not a new or worse exposure. Kassandra SEC Gate 1: CLEAN (see docs/stories/oidc-simplification/security-reports/fix-bootstrap-admin-session-identity-security-review-2026-07-01.md).

Configurable Nebu session-lifetime ceiling (Story e2e-session-refresh-suites):

The Nebu-controlled session ceiling — the Matrix opaque-session expiry (min(oidc_exp, ceiling) on login and the /v3/refresh re-mint) and the admin session cap — defaults to 8h, read through Nebu.Web.SessionConfig.{matrix,admin}_session_ttl_seconds/0. Operators may shorten it at boot via NEBU_MATRIX_SESSION_TTL_SECONDS / NEBU_ADMIN_SESSION_TTL_SECONDS (parsed by SessionConfig.parse_ttl/1: only a strictly-positive integer applies; unset/junk/≤0 → the 8h default, never crashes the login path). The Matrix ceiling can also be SAVED in the Admin UI (/admin/config, server_config.matrix_session_ttl_seconds, 1 hour to 90 days; it wins over the env value; story session-lifetime-setting) and is read through the per-node config cache with a last-known-good rule (a failed read never lengthens it). The env value stays the fallback and the test seam: make test-e2e-refresh sets a short TTL so the silent-refresh loop fires within a browser-test window. The login ceiling stays bounded by the OIDC exp; the /v3/refresh re-mint remains now + ceiling (the pre-existing trade-off, now configurable). Migration 000007 adds matrix_oidc_refresh_mode / admin_oidc_refresh_mode to the server_config config_update_mutable RLS allowlist so the refresh-mode runtime change (above) actually persists after bootstrap (the keys are enum-validated offline|online at every write — no scope injection).

SSO Nonce-Based Replay Prevention (Story 11-7, hardened Story 12.7):

GetSSORedirect generates a 16-byte cryptographically random nonce (32 hex chars) per SSO flow. The nonce is stored in globalSSOState alongside the PKCE verifier and forwarded to Dex via oauth2.SetAuthURLParam("nonce", nonce). In GetSSOCallback, the nonce claim is extracted from the returned id_token and compared against the stored nonce using crypto/subtle.ConstantTimeCompare (timing-attack safe, Story 12.7 LOW-7). A mismatch results in 403 M_FORBIDDEN "SSO nonce mismatch" - no loginToken is issued. The server nonce is never logged on mismatch (Story 12.7 LOW-8). This prevents Dex's server-side token caching from replaying a previously invalidated JWT as a new access_token.

Denylist Check at Login (Story 11-7):

PostLogin calls h.store.IsInvalidated(rawJWT) before issuing an access_token. If the submitted JWT is in the denylist, it returns 403 M_FORBIDDEN "Token has been logged out". This is defence-in-depth against direct POST /login calls that submit a previously invalidated JWT (bypassing the SSO nonce check). Status 403 M_FORBIDDEN (not 401) is intentional: POST /login is an authentication attempt, not a session validation request. LoginHandler.store is nil-safe - deployments without a denylist skip the check.

Deactivated User Login Rejection (Story 14.4):

PostLogin calls validate_token (in-process) which checks is_active in the DB. When the Elixir domain returns :permission_denied with message "user account is deactivated", PostLogin responds 403 M_USER_DEACTIVATED (Matrix CS API v1.18 required errcode). This covers the case where a user was deleted via GDPR erasure but attempts to re-authenticate via OIDC. All other validate_token errors remain 500 M_UNKNOWN (defensive - no internals leaked).

Cache-Control: no-store on SSO Redirect (Story 11-7):

GetSSORedirect sets Cache-Control: no-store before the http.Redirect call. This prevents Safari (and any intermediate cache) from storing the 302 redirect response. Without this header, Safari can replay a cached redirect on re-login, causing the already-consumed state parameter to be rejected (400 M_UNKNOWN "Invalid or expired SSO state").

Rate limiting (per IP) - API Gateway:

TierRateEndpoints
strict30/min, burst 10POST /login (brute-force risk)
compliance10/min, burst 10All compliance/* and admin key/anonymize
admin60/min, burst 20Login UI, bootstrap wizard
medium30/min, burst 10SSO redirect/callback, public profile
loose300/min, burst 100Discovery, capabilities, unauthenticated stubs

Implementation: Nebu.Web.Plugs.RateLimit - a per-IP token-bucket limiter backed by ETS, keyed on conn.remote_ip (the directly connected peer, not a spoofable X-Forwarded-For). The buckets are volatile ETS (reset on restart; steady-state rate still bounds throughput). NEBU_RATE_LIMIT_DISABLED=true makes it a no-op (test/dev). The tier values above are historical Go-gateway tiers; the consolidated app enforces equivalent limits in-process (e.g. POST /login 30/min, GET /sync 600/min).

Rate limiting (per IP) - media endpoints:

The media endpoints are guarded by the same Nebu.Web.Plugs.RateLimit token-bucket limiter as the rest of the Matrix API (e.g. preview_url 30/min, upload 600/min). The limiter keys on Nebu.Web.ClientIP.get/1: the direct peer, or, only when that peer is listed in NEBU_TRUSTED_PROXY_CIDRS, the rightmost untrusted X-Forwarded-For element, so an attacker cannot rotate the bucket key by sending a header. On exhaustion it returns 429 with {"errcode":"M_LIMIT_EXCEEDED",...} + Retry-After. NEBU_RATE_LIMIT_DISABLED=true disables it (dev/test escape hatch). (Historically these limits lived in a standalone Go media gateway with a sync.Map limiter and a NEBU_TRUSTED_PROXY gate, superseded by the CIDR allowlist NEBU_TRUSTED_PROXY_CIDRS; the Go media service is gone — ADR-021.)

Rate limiting (per user) - room membership tiers (writes and the members_read read tier; story rate-limits-join-invite-create-room):

Tier (bucket key)RateBurstCharged by
create_room ({create_room, user})10/min201 per POST /createRoom
invite ({invite, user})20/min501 per POST /rooms/{id}/invite, n per POST /createRoom with n distinct invitees
join ({join, user})12/min301 per POST /join/{roomIdOrAlias}
moderation ({moderation, user})30/min601 per POST /rooms/{id}/kick, /ban or /unban (story moderation-routes-atomic-ban) and per PUT /rooms/{id}/state/m.room.power_levels (story power-levels-enforcement); a divergence from the spec's "Rate-limited: No"
members_read ({members_read, user})60/min301 per GET /rooms/{id}/members or /initialSync (story membership-state-model, security cycle 1; a divergence from the spec, which lists no rate limit; the initial /sync is not charged)
room_keys ({room_keys, user})60/min51 per write of /room_keys/** (POST, PUT, DELETE; story server-side-key-backup); charged after the authentication and before the body is read
room_keys_bulk_read ({room_keys_bulk_read, user})30/min31 per GET /room_keys/keys or /room_keys/keys/{roomId}; besides it one slot per user and two per node (RoomKeysReadGate)
room_keys_version_read ({room_keys_version_read, user})120/min301 per GET /room_keys/version or /room_keys/version/{version} (a count over the version's rows); the single-session GET is not charged

The tiers are keyed by the authenticated user (never by IP, so a NAT address is not shared) and live in the same ETS table as the per-IP tiers, per node (the buckets are not shared between nodes): with N nodes a user can spend up to N x the budget. POST /rooms/{id}/upgrade spends one create_room unit as well (it creates a room). The caps of PENDING invitations (500 per inviter, 100 per invitee, 10 per inviter-invitee pair, 403 M_FORBIDDEN) are counted and written in PostgreSQL under advisory locks of the inviter and the invitee, so they are a cluster-wide count bound that concurrent client invitations cannot exceed; the invitee total ignores rows of deactivated inviters and of inviters on the invitee's m.ignored_user_list; rows of upgrade_room and the Admin force-add are counted but not refused (since story membership-state-model the upgrade writes a real invite event in the upgrader's name next to each row, still uncapped, gi (c)). GET /rooms/{id}/members and /initialSync draw from the per-user read tier members_read (60 per minute, burst 30, story membership-state-model security cycle 1; slice reads are also limited to 3 concurrent per node, node-local, security cycle 2: their cost is O(member events of the room); the initial /sync is not charged) and their member read runs under a 2 s statement_timeout. The rooms.invite section of a /sync response has a byte budget (an optional stripped event above 64 KiB, then the largest optional events above 8 MiB in total, are left out; never an invitation; an m.room.create above 64 KiB is reduced to its spec keys). The optional stripped state is bounded; create and member content stays unbounded until ep. The pending count reads the invitee's ignore list once per invitee (PostgreSQL 16+), and busy invitation locks (more than 2 s) answer 503 M_LIMIT_EXCEEDED. All values are tunable (NEBU_RATE_LIMIT_*, NEBU_INVITE_MAX_PENDING_*, see 07-deployment.md).

OAuth 2.0 Authorization Server (Broker Model, ADR-033)

Nebu can act as an OAuth 2.0 authorization server standing in front of the corporate OIDC provider (broker model O2, see Scenario 4 in 06-runtime.md). This feature is disabled by default and must be explicitly enabled via /admin/config (server_config key oauth_enabled).

Architectural role (ADR-033):

  • Authorization Server + Resource Server: Nebu issues Matrix access and refresh tokens directly. The corporate OIDC provider (Keycloak, Dex, Azure AD) is identity-only: it authenticates users; Nebu mediates the flow and controls token issuance.
  • Broker model (O2): Third-party OAuth 2.0 clients (Matrix SDK apps, Element Web) discover Nebu as the issuer via RFC 8414 metadata and authenticate users through Nebu's authorization endpoint (GET /oauth2/authorize, story oauth-authorize-idp-delegation-code). Nebu talks to the corporate IdP internally (OIDC) but clients talk only to Nebu.
  • Dynamic Client Registration (RFC 7591): Clients register themselves via unauthenticated POST /oauth2/register. Every registration starts pending and becomes usable only after admin approval (no auto-approve). Registrations are metadata-hash deduplicated (ON CONFLICT (metadata_hash) DO NOTHING), so identical registrations resolve to one row and are idempotent.
  • https-for-all-URLs: All OAuth endpoints and metadata URLs must be https. A non-https oauth_issuer_url configuration makes both metadata and discovery endpoints answer 404 M_UNRECOGNIZED (fail closed) instead of advertising insecure URLs.

Pending registration lifecycle (ADR-033, "Pending registrations" section):

  1. Creation: Client POSTs to POST /oauth2/register. Nebu validates metadata (RFC 7591), computes a SHA-256 hash of the normalized, filtered metadata JSON, and inserts a row with status = 'pending'.
  2. Deduplication: If the same metadata hash already exists (whether pending, approved, or rejected), the INSERT is a no-op (atomic dedup via ON CONFLICT (metadata_hash) DO NOTHING). The existing client_id is returned idempotently.
  3. Approval window: An admin navigates to /admin/oauth/registrations and sees all pending registrations. Admin clicks "Approve" → status = 'approved', approved_by = <admin_user_id>, approved_at = now(). Once approved, the client is usable in authorization flows (/oauth2/authorize).
  4. Rejection: Admin clicks "Reject" → status = 'rejected', rejected_by = <admin_user_id>, rejected_at = now(). The client cannot be used.
  5. TTL purge: Pending rows older than oauth_pending_ttl_days (default 30) are deleted hourly by the Oban cron job Nebu.Workers.OAuthPendingPurgeWorker. Approved and rejected rows are never purged.
  6. Database guards (ADR-033, "Database invariants"):
    • RLS INSERT policy admits only status = 'pending' with nullable approved_by, approved_at, rejected_by, rejected_at.
    • Trigger enforces final decisions: once a decision is made, status, approved_*, and rejected_* fields cannot be changed (no re-approval, no re-attribution).
    • CHECK constraint ties approved/rejected rows to their *_by/*_at columns and keeps pending rows free of them.

Rate limiting (three bucket families, ADR-033, "Registration rate limits"):

  1. Invalid bucket (junk only): 600/hour per IP, 5000/hour globally. Charged by unparsable/oversize/invalid request bodies or requests while oauth_enabled = false. When exhausted, the junk request is answered 429 Too Many Requests instead of its normal 4xx.
  2. Loose bucket (every valid request): 600/hour per IP, no global limit. Charged by every syntactically valid registration request (regardless of whether it's new or a dedup hit).
  3. Strict bucket (new registrations only): 50/hour per IP, 500/hour globally. Charged only when a request would INSERT a new oauth_clients row (unknown metadata hash). Dedup hits (known hash) skip this budget.

Pending registration caps (ADR-033, "Pending registrations"):

  • Global cap: At most oauth_pending_cap (default 5000) pending rows. A new registration above the cap returns 503 temporarily_unavailable with Retry-After: 3600. Dedup hits bypass the cap (idempotency).
  • Per-host cap: At most oauth_pending_host_cap (default 20) pending rows per client_uri host. Same 503 response. GATE (security review finding N-1): the host is unverified (attacker-chosen), so an attacker can fill this cap for a real host (e.g. element.company.com) and block legitimate registrations for 30 days. Fix (before oauth_enabled): key the cap on (client_host, created_from_ip) (requires trusted-proxy resolution, item 1).

CORS: Both GET /_matrix/client/v1/auth_metadata and POST /oauth2/register respond to OPTIONS preflight and set Access-Control-Allow-Origin: *, allowing browser clients (Element Web, Dart SDK, etc.).

Payload redaction: Secrets of the OAuth flow (code, code_verifier, refresh_token, client_secret) are redacted from request/response logs via the existing Nebu.Web.RequestLogger.PayloadRedactor extension.

Authorization flow state and codes (story oauth-authorize-idp-delegation-code, ADR-033 D1-D23):

  • Flow state lives in the database, never node-local (D3, cluster safety ADR-020). Table oauth_authz_requests (migration 000041, TTL 10 min, key = state_hash) holds the validated client request, the IdP PKCE verifier (SecretBox-encrypted), the IdP nonce and the browser-binding hash. It is consumed by ONE atomic DELETE ... RETURNING ..., (expires_at > now()) AS live; the node-local SsoStore (ETS) is NOT used for the OAuth leg. At most 10 000 live rows (soft cap inside the INSERT, temporarily_unavailable above).
  • Shared IdP callback (D1/D2). The IdP leg reuses the one IdP client and its fixed redirect URI /_matrix/client/v3/login/sso/redirect/oidc (ADR-022: no new operator-side IdP configuration). The state tag o2. + 43 base64url chars selects the OAuth branch before any legacy logic. Module boundary (D22, ADR-020): the IdP leg stays in Nebu.Matrix (idp_authorize_location/2, idp_complete_login/4, provision_login_user/1); Nebu.OAuth is called only via oauth_idp_state?/1 and handle_idp_callback/1.
  • Browser binding (D4, RFC 9700 4.7). Per-flow cookie nebu_oa_<16 hex of state_hash> (32 random bytes, HttpOnly; SameSite=Lax, path = the callback, Max-Age=600, Secure on https); only its SHA-256 is stored; verified in constant time at the callback and cleared. Assumes authorize and callback share the host.
  • Hash-only secrets at rest (D2/D16). The IdP-leg state is stored as state_hash (SHA-256 hex); the authorization code only as code_hash; the IdP refresh token only SecretBox-encrypted (idp_rt_enc); plaintext codes exist only in the client redirect.
  • Single-use codes with reuse detection (D17). oauth_codes (TTL 60 s) is consumed by UPDATE ... SET used_at = now() WHERE code_hash = $1 AND used_at IS NULL AND expires_at > now() RETURNING ...; exactly one consumer wins across the cluster, a second presentation yields {:reused, row} (the token endpoint revokes the derived session, proof-gated, T8). Database enforcement: guard trigger (only used_at NULL -> NOT NULL), RLS refuses deleting a code earlier than 24 h after expiry. Rows are purged by Nebu.Workers.OAuthFlowPurgeWorker (every 10 min).
  • Interactive login is forced as far as the IdP allows (D5). The OAuth leg always sends prompt=login AND max_age=0 (prompt alone is advisory); client prompt=create is forwarded only if the IdP advertises it; unknown client parameters (login_hint, max_age, claims, ...) are never forwarded. No consent screen: admin approval of the registration is the consent gate. auth_time freshness check and consent remain gate items in follow-ups.md.
  • Error discipline. Errors before client and redirect URI are trusted are accessible HTML pages (Nebu.OAuth.Pages, strict CSP, no script/forms, never a redirect); later errors are RFC 6749 4.1.2.1 redirects with state and iss (RFC 9207, advertised as authorization_response_iss_parameter_supported). Authorize/callback responses carry no-store/no-referrer and no CORS headers.
  • Abuse bounds and logging (D19/D20). Node-local per-IP buckets {"oauth_authorize", ip} / {"oauth_callback", ip} (600/h each, before any DB access; subject to the trusted-proxy gate item 1). No audit_log rows for authorize/callback (like Matrix logins); INFO logs carry only client_id, outcome and user_id/device_id; RequestLogger logs the query strings of /oauth2/authorize and of the callback with parameter names only (No Sensitive Data law).
  • Scopes (D6-D8, D23). Both Matrix prefixes (stable and unstable msc2967, never mixed); api:* and exactly one valid device:<id> required; unknown RFC 6749 tokens ignored; openid and the client nonce are kept in the flow/code rows (never sent to the IdP); the token endpoint (story 3) issues the Nebu-signed id_token from them.

Token endpoint, OAuth sessions and signing keys (story oauth-token-endpoint, ADR-033 T1-T33):

  • OAuth sessions vs. legacy sessions (T2/T27). Both live in matrix_sessions (one row per token pair, a session = all rows of (user_id, device_id), at most one active row), distinguished by the auth_api column ('legacy' = DEFAULT and every pre-existing row, 'oauth'). Logout, device deletion, admin deactivation and the OidcPlug opaque path therefore work unchanged for both. Isolation is enforced in SQL: legacy POST /refresh, the RefreshWorker and the legacy store's refresh/extension queries touch only legacy rows, the OAuth store only oauth rows. An OAuth row has two expiries: expires_at (access token) and session_expires_at (absolute, never extended, T3). Tokens are opaque (64 lowercase hex), stored only as SHA-256 hashes; the IdP refresh token only as SecretBox ciphertext.
  • One device, one API (T7/T25/T26). A device is never served by both APIs: OAuth issuance deletes every earlier row of the device and denylists raw IdP JWTs bound to it, a legacy /login with an OAuth device's device_id takes the device over, and raw IdP JWTs are refused for OAuth devices in three layers (issuance kill switch, DeviceBinding.put/4, OidcPlug). User-level raw-JWT access without an OAuth device remains the legacy exception (kept in story 5 as accepted parallel-operation risk, C14; removed with the legacy deprecation, follow-up u).
  • Single-use and cluster safety (ADR-020). Code consumption, refresh rotation and revocation are atomic SQL (UPDATE ... WHERE ... RETURNING, FOR UPDATE, the devices row lock as the common serialization point for issuance and rotation); no node-local state decides correctness. The per-IP buckets are node-local abuse control only.
  • Signing keys (T17/T18). id_tokens are signed with ES256 (P-256, a code constant: HS256 cannot work for public clients, RS256 and EdDSA were rejected for size/compatibility). The key is generated by Nebu, stored in oauth_signing_keys (private JWK only as SecretBox ciphertext, public JWK as jsonb, kid = RFC 7638 thumbprint), created lazily with a single winner across the cluster (INSERT ... ON CONFLICT DO NOTHING against the one-active partial UNIQUE index) and verified on load against kid AND public_jwk. Retirement is an UPDATE guarded by a trigger; the JWKS publishes the active and unexpired retired keys. Rotation tooling/runbook and an undecryptable-key runbook are follow-ups (b, p). Signing-key compromise would allow forged id_tokens for clients that trust them (see 11-risks.md).
  • 5xx {} contract for oidc-client-ts (T20/T22). matrix-js-sdk's refresh path (oidc-client-ts) turns ANY JSON body with an error member into an ErrorResponse and then logs the user out, while a body without it is retried. Hence 4xx ({"error","error_description"}, never errcode) are reserved for definitive outcomes, and every 5xx (transient DB or IdP failure, rescued exception, exhausted refresh bucket, unreadable config) is {} with Retry-After. The kill-switch 404 of token/JWKS/openid-configuration is {"errcode":"M_UNRECOGNIZED"} (no error), so switching OAuth off does not log refreshing clients out; the stable auth_metadata endpoint and /oauth2/register keep their Matrix-shaped body.
  • CORS scoping (T32). POST /oauth2/token, GET /oauth2/jwks and GET /.well-known/openid-configuration answer Access-Control-Allow-Origin: * and never Access-Control-Allow-Credentials (CORSPlug runs with credentials: false for exactly these paths, also on preflights, which run no endpoint logic); every other route keeps the global CORS behaviour unchanged. Authorize/callback stay CORS-free.
  • Discovery document under the issuer (T31). Nebu serves its own /.well-known/openid-configuration (byte-identical to auth_metadata, never the IdP passthrough) because matrix-js-sdk resolves it relative to the issuer; hence the Nebu issuer must have an empty path. The metadata advertises jwks_uri and id_token_signing_alg_values_supported: ["ES256"] (T30).
  • Logging (T23/T29). The token request body is read once by the handler and never captured (JsonBody skips the path, RequestLogger logs query parameter names only and never the response body of /oauth2/token, which carries tokens and the granted scope); id_token joins the PayloadRedactor key lists. INFO logs carry grant type, client id, outcome and user/device; WARNING covers reuse revocations, client mismatch and IdP rejections (RefreshErrors.describe/1 only). No audit_log rows, like Matrix logins.

Revocation and session teardown (story oauth-token-revocation-logout, ADR-033 R1-R24):

  • Revocation = Matrix logout semantics (R2/R3). POST /oauth2/revoke accepts an access OR refresh token in any state (active, expired, superseded, consumed, expired session; token_type_hint ignored) and ends the whole device session: every matrix_sessions row of (user_id, device_id) (the IdP refresh-token ciphertext dies with them) plus the devices row with its device, one-time and fallback keys. Only code-reuse and refresh-reuse revocations (T8/T11) keep the device.
  • Teardown invariants (R11/R17). One transaction per device: plain SELECT, then the devices row FOR UPDATE (the lock issuance and rotation take), a re-check after the lock, DELETE of all rows of the device (a new statement, so a rotation that committed meanwhile is deleted too), device tables via a raising variant. Lock order is always devices -> matrix_sessions; DELETE /devices and deactivation are single statements, so there is no cycle. A login committing after a device's teardown survives whole (point-in-time semantics; in-flight codes of at most 60 s are not invalidated). The post-commit S3 JWT kill-switch deletes the bindings only after every denylist write was confirmed.
  • /logout/all is all-or-500 (R17). OAuth sessions, then the legacy part, then every device (union of devices rows and matrix_sessions device ids) are torn down; 200 {} only when every part succeeded, a failed part => 500 M_UNKNOWN while the others still ran, and the caller's raw JWT must be denylisted durably (TokenDenylist.revoke_checked/3). Cross-signing keys stay. Legacy POST /logout keeps its best-effort contract and is a recorded spec deviation (R16, gate item r).
  • No client authentication, no oracle (R4/R5/R10). The unauthenticated endpoint answers byte-identically for revoked, unknown, malformed and legacy tokens; a malformed value costs no DB access; client_id is never looked up. This follows the Matrix spec (still revoke) over the RFC 7009 client binding; the rate limit {"oauth_revoke", ip} (60/min, burst 600) is charged before any DB access and is subject to the trusted-proxy gate.
  • {} contract and CORS (R6/R7/R14). Success and unknown token answer 200 {} with application/json (oidc-client-ts throws on a text/* type); 4xx use the RFC 6749 shape, every 5xx is {} + Retry-After (T20), 401 invalid_client and unsupported_token_type are unreachable. /oauth2/revoke joins the credentials-free CORS branch (T32): ACAO * on every status, preflights answered by CORSPlug. Unlike the other OAuth endpoints it is exempt from the T22 kill switch (R13).
  • :auth_api assign (R15). OidcPlug records at authentication time whether the token is an OAuth or a legacy credential, so POST /logout ends only the OAuth device in the first case and keeps the legacy wipe otherwise.
  • Logging/redaction (R21/R22). Only outcome=revoked|ignored and client_id_match plus the stored user/device/client id are logged; the presented client_id, token, hint and hashes never; /oauth2/revoke joins the names-only query paths; no audit rows.

Legacy coexistence (story oauth-legacy-coexistence, ADR-033 C1-C20):

  • auth_api is the source of truth (decision 6). Every matrix_sessions row carries auth_api = legacy | oauth; a session only ever uses the API that issued it. OidcPlug assigns :auth_api at authentication time and handlers branch on it (device deletion refusal C7, /logout R15). Refresh paths are isolated by the filter: /refresh, the RefreshWorker, refresh_auth_time/2 (C9) act on legacy rows only; /oauth2/token refresh on oauth rows only. /whoami, media routers, admin deactivation and erasure are shared.
  • Parallel operation rules. One liveness predicate (Nebu.OAuth.Config.live_state/0, facade Nebu.OAuth.oauth_aware?/0, C1/C2) decides whether OAuth is offered: the kill switch, auth_metadata, the login flags and the SSO action can never disagree; the Matrix domain never names Nebu.OAuth.Config. Switching oauth_enabled off removes the flags and action at once; existing OAuth sessions keep authenticating but die at their next refresh (T22 kill switch, C16); revoke and /logout stay live. A raw IdP JWT can never act as an OAuth device (C11/C13/T25), and the one kill-switch DeviceBinding.kill_device_jwts/3 is fail closed (a failed denylist write keeps the binding, and lookup/1 refuses a binding whose device has an OAuth session or no devices row). All non-preserved device deletions run through the one atomic locked teardown Nebu.Matrix.end_device_session/2 (C12; the legacy rotate_tokens/1 takes no devices lock, follow-up x).
  • Accepted, documented parallel-operation residuals. The unbound raw-JWT path stays for legacy (C14, follow-up u); device-id take-over across APIs needs an interactive login of the same user (C15, gate item i); legacy POST /logout still wipes every session of the user (R16, gate item r; to be closed by the bugfix legacy-logout-per-device before oauth_enabled; the story-9 flip code is dormant); account endpoints were unrouted in story 5 (C10; the account page is routed since story oauth-account-management, next section) (C10); the admin device list shows no owning API (follow-up v).
  • Deprecation path (C20, documented, not implemented). Deprecating legacy adds a legacy_login_enabled switch (GET /login stops advertising m.login.token / SSO for new logins, POST /login refuses new legacy logins, /refresh and RefreshWorker are removed, legacy sessions are not migrated and run out at expires_at, the raw-JWT path goes with it); deprecating OAuth is oauth_enabled=false.
  • Logging (C6). action is attacker-controlled: the SSO-redirect query capture masks the value of every key beginning with action / org.matrix.msc3824.action; redirectUrl stays visible; no new secret fields, no migration, no audit rows (C19).

Account management page and m.oauth (story oauth-account-management, ADR-033 A1-A27):

  • Account session (A8). A browser session of the account page, NOT a Matrix device: table oauth_account_sessions, key = SHA-256 of a 256-bit random cookie value (plaintext never stored or logged), idle timeout 900 s (sliding) and absolute lifetime 3600 s (CHECK expires_at <= created_at + 1 hour), both enforced in ONE atomic UPDATE ... RETURNING so every node answers (ADR-020). Every login or re-auth creates a new session and deletes the replaced one in the same transaction (session fixation); at most 20 sessions per user (the oldest is deleted, a user is never locked out). The session never authenticates /_matrix/* (OidcPlug ignores the cookie), a deactivated user's next request deletes it. Cookie names: __Host-nebu_account on https (Path=/; Secure; HttpOnly; SameSite=Lax, the only name read there: cookie tossing from a sibling subdomain is ignored), nebu_account (Path=/account) on plain http; SameSite=Lax because the cookie is set on a callback reached through a cross-site redirect chain.
  • Own state tag, shared IdP client (A6). The sign-in uses the ONE IdP client, the shared callback and the OIDC Scopes Law scope; only the state tag (o2.acct.) selects Nebu.OAuth.Account (handle_idp_callback/1 dispatches it first; an account state can never match Authorize's anchored state regex). Flow state is database-backed (oauth_account_login_requests, TTL 10 min, atomic consume), the redirect target is built server-side from the consumed row (no open redirect), and the IdP refresh token is discarded. No provisioning (A7): an unknown user gets no_account, so the page serves existing accounts only, including legacy-only users.
  • CSRF derivation (A9). The page renders csrf_token = HMAC-SHA256("nebu-account-csrf-v1", session cookie value) (256 bits only the browser knows, stable across tabs and the back button); the session stores only csrf_hash = sha256(token), verified with a constant-time compare. A stored hash alone could not be rendered into later forms, hence the derivation. Additional layers: Origin, when present, must equal the issuer origin; Sec-Fetch-Site, when present, must not be cross-site; on the destructive POSTs fetch metadata, when present, must describe a user-activated document navigation (navigate / document / ?1; Safari sends no ?1 and proves the click through the form field _click_proof, ADR-033 SO9). Any failure => 403 forbidden, nothing changed. CSRF is checked BEFORE freshness; GET never mutates; every POST ends in a 303 with a fixed notice code (the one exception is POST /account/logout).
  • Referrer-Policy: same-origin (A4/A9, review cycle 1 M1). Under no-referrer browsers send Origin: null for the page's OWN form POSTs, which would make every legitimate POST fail the Origin check; same-origin keeps cross-origin referrers away while same-origin POSTs carry the real origin. Origin: null is still accepted ONLY together with Sec-Fetch-Site: same-origin and the valid token (privacy settings that strip the header); this must be confirmed in a real browser before the gate (Playwright follow-up).
  • Re-authentication freshness (A7/A10/A17). Destructive actions (device delete, reset approve / decline) need now - session.auth_time < 300 s; the confirmation page of a stale session offers a link (an <a>: the CSP form-action 'self' would block a redirecting form) to GET /account/reauth, which runs a new IdP login with prompt=login + max_age=0 and returns to the same page; the POST re-checks. auth_time is the id_token claim ONLY (iat is no substitute), accepted when >= login request - 60 s (a future value is clamped to now). When the IdP omits the claim despite max_age=0 (OIDC Core 3.1.2.1 obliges it; Dex reportedly does not) the session is accepted with the callback time and one WARNING, so the re-auth then proves no freshness: gate item (y), BLOCKING before oauth_enabled (recommendation: absent = never fresh, fail closed), same for gate (ag). The account re-auth does NOT refresh matrix_sessions.auth_time (A17).
  • UIA approval bound to the keys hash (A14-A16). The m.oauth approval is the uia_sessions row op = "cross_signing_reset" (stage_complete = approved on the page). params = canonical JSON of {device_id, keys_sha256} where keys_sha256 is the SHA-256 of the canonical JSON (RFC 8785, Nebu.Matrix.CanonicalJson.sha256_hex/1) of ALL supplied keys: the approval cannot be replayed from another device nor for swapped self-signing / user-signing keys; a mismatching retry gets a new challenge. Single use is the atomic consumed = TRUE WHERE consumed = FALSE inside the same transaction as the key write (a failed write rolls the consume back); an approved PARTIAL replacement keeps the stored keys that were not supplied. The challenge is serialized per (user, device) with pg_advisory_xact_lock, reuses the caller's pending session (Element makes two auth-less calls and retries without polling) and keeps at most one pending request per device and 5 per user. The legacy m.login.sso UIA fallback is separated from this op (complete/2 / load/1 exclude it). Request-first (an approval names ONE concrete session; no user-level "allow resets" window); the page shows device, app (the app's own claim), last activity, times and a short reference and, while SEVERAL requests are pending, no Approve button.
  • Device list and deletion (A12/A13). Nebu.Matrix.account_devices/1 is error-aware (an outage is a 503, never "no devices"); the OAuth app is the registration's client_name (ClientStore.client_names/1, escaped, labelled "name claimed by the app"). Deletion reuses the atomic locked teardown Nebu.Matrix.end_device_session/2 (C12) for devices of either API and is idempotent; ownership is membership in the user's own device list, so unknown and foreign ids get one answer (no IDOR / existence oracle).
  • Page security and look (A4/A20). A standalone renderer (not the admin layout): auto-escaped EEx, ONE CSS block allowed by its hash in default-src 'none'; form-action 'self'; base-uri 'none'; frame-ancestors 'none', X-Frame-Options: DENY, Cache-Control: no-store, no script, no external resource, no link to a client-controlled URL, WCAG 2.1 AA, no CORS (/account* is browser navigation only).
  • Abuse bounds, logging, redaction (A19/A21/A22). Bucket {"oauth_account", ip} (600/h, before any DB access; trusted-proxy gate item 1), at most 10 000 live login requests. INFO lines name only user id (and device id); never a cookie value, CSRF token, state, IdP code / token / claim, UIA session id or hash. PayloadRedactor redacts csrf_token and the session of a UIA challenge; query strings of /account* are logged names-only. No audit rows (T29 / R21 / C19 consistency; follow-up ab).
  • Deliberate residuals. Same-origin approval risk (gate ag): /account shares the origin with Element Web, so a script injected there could read the CSRF token and approve a reset; dedicated account origin (MAS pattern) or an accepted risk plus a release note, decided before oauth_enabled. A legacy replacement of cross-signing keys still answers 403 instead of a UIA challenge (pre-existing); the corrected "unchanged" rule (supplied keys only, master_key required) also turned a legacy master-only re-upload from 403 into 200.

OAuth phase 2: cross-cutting concepts (ADR-033 "Phase 2 summary")

  • Client IP trust. One resolution point (Nebu.Web.Plugs.ClientIP): X-Forwarded-For counts only from a direct peer inside NEBU_TRUSTED_PROXY_CIDRS (default none = the direct peer; parsing fails closed; Forwarded / X-Real-IP are ignored). Trust exactly YOUR proxies (a /32 of the pinned nginx container, never a client network or the Docker subnet); a proxy must append or overwrite. An empty or invalid list only leaves shared buckets (safe against spoofing, NOT safe for availability); a too-broad list is the unsafe case. With the variable set, last_seen_ip / created_from_ip hold real addresses (personal data).
  • Client-IP misconfiguration warnings are aids, not a gate (story client-ip-misconfiguration-warnings, G3-2). The shared bucket (an empty or wrong list, a proxy that does not pass the client address) is safe against spoofing, NOT safe for availability: every client behind the proxy shares one rate-limit bucket and per-source cap (consent 20, authorize / account login 300, pending registrations 100), so one anonymous source can lock every user out of login; the negative spoof test passes anyway. Nebu therefore warns where a misconfiguration is reliably visible: W1 (one throttled WARNING per node and 10 minutes when X-Forwarded-For arrives from an untrusted peer; ambiguous, a client can send the header itself, and it cannot see the alb collapse because nginx is a trusted peer), W2 (6-hourly cron over devices: at least 10 devices of at least 5 users on at most 2 addresses; statistical, a real single-NAT deployment looks the same), W3 (OpenTofu check block in the reference templates, deterministic). The POSITIVE client-IP test (two networks, distinct last_seen_ip through the real public path) stays a MANUAL, GATING precondition for oauth_enabled; the absence of a warning proves nothing. There is deliberately NO in-server block of activation: the only state that proves the collapse is the deployment topology, which Nebu cannot see (the load balancer address is a private address, indistinguishable from an intranet client), W1 and W2 are ambiguous or statistical, activation is an SQL write with no server code path that could refuse it, and a block built on a guess would either miss real collapses or block correct deployments. No log line carries an address or a header value (personal data); the throttle is node-local and decides nothing (cluster safety, ADR-020 / TP11).
  • Rate-limit buckets. Nebu.Web.ClientIP.bucket/1 keys every per-IP RateLimit bucket (IPv4 per address, IPv6 per /64); per-source CAPS on live rows (authorize, account login, consent, pending registrations) use the hashed Nebu.OAuth.SourceKey and are refuse-only (nothing is evicted). Discovery limits exist only where the Matrix spec allows a 429 (GET /login, live openid-configuration); auth_metadata, auth_issuer and /versions rely on the cache. Idle buckets are swept per node every 10 minutes.
  • Config cache exception to ADR-020. The switch state (oauth_enabled, issuer) is cached per node in ETS for at most NEBU_OAUTH_CONFIG_CACHE_MS (default 2 s, max 5 s); only successful reads are cached (an outage still fails closed); the owner is the supervised Nebu.OAuth.CacheOwner. This is bounded staleness like OidcConfig and the 60 s JWT denylist cache; single-use codes, rotation and revocation stay atomic SQL.
  • Fetch metadata / BrowserPost. State-changing browser POSTs (consent, /account, admin approve / reject / revoke / bulk removal) check Origin, Sec-Fetch-Site, Mode: navigate, Dest: document, User: ?1 and a CSRF token BEFORE anything is consumed; admin actions also need a fresh IdP re-authentication bound to one client and one action (single-use marker). Residual SO7: a script on the shared origin that waits for a user click is not stopped; the class is removed only by serving Element Web on its own origin. The consent page adds a hash-pinned arming script (double clickjacking) and Cross-Origin-Opener-Policy: same-origin.
  • Freshness. Server-owned and fail closed: only the raw auth_time claim counts, absent means not fresh; consent on every authorization (unless the user's remembered Allow applies, see persistent-consent-grants) compensates at login, /account destructive actions stay unavailable without auth_time.
  • Revocation and teardown. Every device-ending path (revoke, logout, /logout/all, device delete, client revocation, legacy logout) uses the SAME locked per-device teardown (lock order clients -> devices -> sessions); a revoked client registration is final for its metadata hash.
  • Signing keys and secrets. ES256 id_token keys rotate in two calls with a database-enforced 900 s pre-publication; one internal secret still protects all secrets at rest (SecretBox v2 design deferred, HY17).

Legacy SSO redirect confirmation (story legacy-sso-redirect-confirmation, epic SEC gate 3 G3-3; ADR-033)

  • Concept. A legacy m.login.sso flow ends in a single-use loginToken that POST /login exchanges for a full session, so the redirectUrl decides who receives an account. Following the Matrix spec's security considerations (and Synapse's model), Nebu hands a token directly only to trusted targets and asks the signed-in user first for every other https target: after the IdP login and BEFORE a token exists, a confirmation page names the target origin and the Matrix user; only Continue mints the token. It applies with oauth_enabled on or off.
  • One classification (Nebu.Matrix.SsoRedirect.classify/1). The same raw string is checked at the redirect leg, stored in the SSO state and used for the Location, so there is no checked-versus-used differential. Strict syntax (length 2048, no control bytes, backslash, userinfo, % in the authority, only canonical IPv4 for numeric hosts, ASCII hosts so the page, the CSP form-action and the allowlist all see the same value) rejects with the unchanged 400; :direct targets (loopback http://localhost|127.0.0.1, default deep-link schemes, operator sso_extra_redirect_schemes) never see the page. loginToken parameters already present in the redirectUrl are removed before the new one is appended (spec "should").
  • Trust sources (exact origin, no wildcard, fail closed). https://<server_name> (never the request Host; with .well-known delegation the apex host is trusted automatically, so it must be an operator-controlled origin), the Admin list sso_redirect_allowed_origins (validated in ONE place, SsoRedirect.normalize_origin_list/1, reached by the Admin domain only through Nebu.Matrix.normalize_sso_redirect_origins/1), and Nebu.OAuth.trusted_redirect_url?/2: the oauth_issuer_url origin plus URLs equal to or below a REGISTERED https redirect URI of an approved OAuth client (registered URI, not origin: an approval of one path must not trust a whole shared-hosting origin). The decision is made at callback time with the then-current list; any error answers "not trusted".
  • ADR-020 facade use. The Matrix confirmation never names OAuth internals: it calls the public Nebu.OAuth functions trusted_redirect_url?/2, parse_form_post/1 (hardened raw form parser; JsonBody skips the path so the CSRF token is never captured) and check_fetch_metadata/2 (lenient level). The Admin form reaches the Matrix validator only through the Nebu.Matrix facade.
  • Pending entry and binding. The verified IdP login is parked in Nebu.Matrix.SsoStore (:confirm tag, 300 s, single use per node, volatile, pg-replicated best effort like the loginToken tag; recorded deviation from the "atomic SQL" rule because the entry guards a less powerful artefact than the token it precedes). It is bound to the browser by a cookie (__Host-nebu_sso_confirm, HttpOnly; SameSite=Lax) whose SHA-256 is the key; the CSRF token is derived from the cookie and stored nowhere. Order of checks (story sso-confirm-bucket-order, follow-up cv): the cookie first (none or malformed: invalid_state, nothing charged), then the live entry (HIT: per-binding bucket, MISS: per-address miss bucket, so an anonymous flood can never lock the page for users with a live binding), then on POST parse, decision, Origin + fetch metadata, CSRF, consume.
  • No COOP, decided. The confirmation routes deliberately send no Cross-Origin-Opener-Policy (unlike the consent page, which keeps COOP same-origin on purpose: story consent-arming-hardening D5, the OAuth clients of this epic need no opener through the consent page; a popup-based OAuth web client is a manual verification item, follow-up cr): popup-based web clients (FluffyChat web) open the flow with window.open and read the result from window.opener, which COOP same-origin would sever. Framing stays blocked (X-Frame-Options: DENY, frame-ancestors 'none'); the opener-based risk is covered by the shared arming script (Nebu.Web.ArmScript: Continue stays aria-disabled until dwell time and real interaction elsewhere, reset on hashchange) and by the page naming the target. Accepted residual, ADR-033.
  • Audit. Saving the origin list writes one sso_redirect_origins_changed row (registered in AuditWriter.@known_actions; without that entry the action would be silently dropped). Logs name only the validated ASCII host (redirect confirmation shown|confirmed|declined host=<host>), never the token, the cookie, the CSRF token or the URL path / query.

Media Served In-Process by the Elixir Application (ADR-021)

Media handling lives in Nebu.Media.* and serves the Matrix /_matrix/media/v3/* and /_matrix/client/v1/media/* endpoints (upload, download, download/{fileName}, thumbnail, preview_url) on the same Bandit/Plug pipeline behind OidcPlug. There is no separate media process. The cross-cutting controls are:

  • At-rest encryption (AES-256-GCM, ADR-011 managed E2EE key escrow): Nebu.Media.Crypto encrypts every media object with AES-256-GCM. The per-object AES key + nonce are stored hex in the media_files table (key escrow — operator/server can decrypt, consistent with ADR-011's managed model). The encryption is byte-compatible with the prior Go implementation. Because GCM authenticates the whole blob, download decrypts the entire object in memory and then serves it (including HTTP Range responses) over the decrypted plaintext.
  • Access control = local OTP/DB call (no gRPC): Nebu.Media.Membership checks room_members via inline SQL inside the same node, replacing the former gRPC CheckMembership RPC — one fewer cross-process hop. Note: media_files has no room_id, so the current check is member-of-any-room; per-media room scoping is a documented accepted follow-up.
  • Object storage = ExAws.S3 against MinIO (ADR-013): Nebu.Media.Storage uses ExAws.S3. New deps: ex_aws, ex_aws_s3, finch, floki, sweet_xml, hackney. A Finch pool is started in Nebu.Application. server/config/runtime.exs reads the MinIO credentials from file-based Docker secrets.
  • SSRF guard for outbound preview fetches: preview_url (Nebu.Media.HttpFetch) fetches the target page via Finch and parses Open Graph tags with Floki. Nebu.Media.SSRF guards the outbound request: a scheme allowlist plus blocking of loopback, RFC1918, link-local, CGNAT, cloud-metadata, IPv4-mapped, and IPv6-ULA addresses, with resolve-once IP-pinning for http. A 1 MiB streamed body cap bounds the response. Residual accepted risk: the https path is subject to an SNI/DNS-rebind TOCTOU window.
  • Rate limiting extended to media: the existing Nebu.Web.Plugs.RateLimit (per-IP token bucket) now also guards preview_url (30/min) and upload (600/min).
  • Locality of Behavior (ADR-018): the media code uses inline SQL with no schema/repository layer; boundary seams (DB, storage, HTTP fetch) are injected via Application.get_env for testability.

Matrix Session Lifecycle - Opaque Access Tokens (Story oidc-session-refresh)

Stack note (ADR-021): This concept is now realised in the Elixir/OTP stack. Earlier revisions of this section described a Go gateway (auth.go, middleware structs, a cleanup goroutine, GetSSOCallback). Those are superseded — the modules below are the current Elixir implementation. The Go gateway is gone.

Problem: Matrix clients connected via OIDC were force-logged-out when the OIDC id_token expired (5 minute default in Keycloak). The login handler previously returned the OIDC JWT directly as the Matrix access_token, tying the Matrix session lifetime to the OIDC provider's short access-token expiry ("force-logout every 5 min" regression).

Solution: Nebu now manages Matrix sessions server-side using opaque access tokens + refresh tokens (Matrix Client-Server API v1.3), decoupled from OIDC token lifetime. The session lifetime is bounded at min(oidc_exp, now + 8h) (8h: the default of the Admin-configurable session lifetime, see "Configurable Nebu session-lifetime ceiling" above).

Dual session model (backward compatible): POST /login issues opaque tokens only when the client sends refresh_token: true in the request body; otherwise the raw OIDC JWT is still returned as access_token (the original stateless behaviour). Both token shapes are accepted on subsequent requests — dispatch is by the absence of a dot (opaque tokens are 64-hex; JWTs always contain two dots).

Architecture: Nebu.Matrix.Session.MatrixSessionStore (inline SQL over the matrix_sessions table, ADR-018 — no Ecto schema/repository) maintains a matrix_sessions row with:

access_token_hash  ← PK. Lowercase hex SHA-256 of the opaque access token (the token itself — 32 random bytes =
                     64 hex chars, no dots — is returned to the client once and NEVER stored)
refresh_token_hash ← UNIQUE NOT NULL. Lowercase hex SHA-256 of the opaque refresh token (rotated by each client
                     /refresh, never by the worker; the plaintext is never stored)
oidc_refresh_token ← AES-256-GCM encrypted OIDC RT from Keycloak (via Nebu.Web.SecretBox; optional, for future OIDC refresh)
user_id            ← Matrix user ID (@user:server.name)
device_id          ← From JWT "did" claim or SSO device registration
expires_at         ← Absolute wall-clock time (Nebu controls expiry: NOW() + the Matrix session lifetime, 8 hours by default; Admin setting `matrix_session_ttl_seconds`)
revoked_at         ← Set when a client /refresh rotates the row away (old half of a rotation); logout/invalidations DELETE rows
grace_until        ← NULL on a rotated-away row = PENDING (old RT valid until the new AT/RT is used); set to NOW() = CONSUMED
created_at         ← Session creation timestamp

Token validation: Nebu.Web.Plugs.OidcPlug dispatches by token shape — an opaque token is any bearer value with no . (JWTs always contain two dots). The JWT path is unchanged.

Hashed token storage (story token-hashing-matrix-sessions, migration 000038): matrix_sessions stores only SHA-256 hashes of the access and refresh token (like jwt_device_bindings already does), so a read-only DB leak (backup, replica, SQL injection) yields no usable session. Both tokens are 256-bit random values, so an unsalted hash is sufficient (no KDF/pepper). The store API keeps taking plaintext tokens at its boundary; session maps it returns carry only :access_token_hash / :refresh_token_hash. Code that knows a row only by its hash (RefreshWorker, the /refresh rotation) uses hash-addressed variants (get_by_access_token_hash/1, extend_in_place_by_hash/3, delete_by_access_token_hash/1, rotate_tokens/1 with :old_access_token_hash); these are internal (@doc false), accept only values read from the DB and must never receive a client-supplied value (pass-the-hash); rotate_tokens/1 rejects params carrying both :old_access_token and :old_access_token_hash. Multi-row sibling updates lock in access_token_hash order. Hashes and tokens are never logged.

Migration 000038 invalidates ALL sessions (forced one-time re-login). The up migration TRUNCATEs matrix_sessions (no backfill): DROP COLUMN would leave the plaintext bytes in the table file and a backfill UPDATE would leave old row versions, so keeping the sessions would keep plaintext residue on disk, in backups, WAL and replicas. TRUNCATE gives the table a new file, and since every session is dead, any residue is worthless. Every user logs in again once. The down migration also deletes all sessions (plaintext cannot be restored from hashes) and, after a rollback, code of the previous release writes plaintext tokens again.

This release must be deployed stop-the-world (stop ALL nodes, run the migration, start the new version). In a rolling / multi-node deploy, nodes still on the old code query the dropped access_token column and answer every opaque-token request with a hard 401 (no soft_logout) and fail logins with 500 until they are replaced.

When the bearer token is opaque:

  1. Look up the row via MatrixSessionStore.get_by_access_token/1, which hashes the presented token (MatrixSessionStore.hash_token/1, the single hashing seam) and compares by indexed equality on access_token_hash — presenting the stored hash as a bearer therefore does not authenticate
  2. If not found: 401 M_UNKNOWN_TOKEN "Invalid token"
  3. If found but revoked_at IS NOT NULL: 401 M_UNKNOWN_TOKEN "Token has been logged out"
  4. If found but expired (re-check is_active / expires_at < NOW()): 401 M_UNKNOWN_TOKEN with soft_logout: true so the client can silently re-auth without dropping the device
  5. If found and valid: populate conn.assigns (user_id, device_id) from the session row

When the bearer token contains a dot, routing continues on the existing OIDC JWT path (backward compat); if the raw JWT has expired, call_jwt/2 returns 401 M_UNKNOWN_TOKEN with soft_logout: true (matching the opaque-session expiry path's behavior), allowing clients to recover via re-authentication.

Device assigned on EVERY authenticated request (Story s2-device-lifecycle-at-login): OidcPlug now populates conn.assigns[:device_id] for both session shapes — opaque sessions read it from matrix_sessions.device_id, raw-JWT sessions read it from Nebu.Matrix.Session.DeviceBinding.get_device/1 (the jwt_device_bindings row keyed by SHA-256(token)). This closes the prior gap where raw-JWT sessions had no server-side device. The device a request acts as is therefore server-authoritative for all sessions (see Device Identity vs Device Keys below).

Session creation (POST /login):

When the client sends refresh_token: true:

  1. Validate the OIDC token in-process (existing login logic)
  2. If an OIDC refresh token is available, encrypt it with Nebu.Web.SecretBox (AES-256-GCM) before storing it in oidc_refresh_token
  3. Generate opaque access_token and refresh_token (32 random bytes each → 64-hex)
  4. Compute expires_at = min(oidc_exp, NOW() + 8h) — decouples the Matrix session from Keycloak's short access-token lifetime
  5. Insert the matrix_sessions row and return {access_token, refresh_token, expires_in_ms, …}

When the client omits refresh_token: true, the raw OIDC JWT is returned as access_token and no session row is written (original stateless behaviour).

Requested-but-not-granted fallback (Story fix-matrix-login-missing-refresh-token): if the client DOES send refresh_token: true but the IdP's token response carries no refresh_token despite offline_access having been requested (e.g. the Keycloak client lacks the "Offline Access" capability — an IdP-side misconfiguration, not a Nebu bug per RFC 6749 §3.3, granting fewer scopes than requested is legal), the login silently degrades to the same JWT-only response as the omitted-flag case above (no session row, no refresh_token/expires_in_ms in the response — both spec-OPTIONAL per Matrix v1.3). This was previously a silent WARNING with no actionable detail; respond_login/7's fallback now logs a message with the same information content as the admin-side parity message (Nebu.Admin.Auth.store_refresh_token/2): "...ensure the IdP grants offline_access to the matrix client" — naming the specific client an operator needs to reconfigure, without logging any token/id_token content. Investigating this diagnosability gap confirmed Nebu.Web.OidcScopes.resolve_scope/1 and Nebu.Matrix.SsoLogin.extract_id_token/1 were already correct (no scope-construction or parsing bug existed) — the fix is diagnosis-only, not behavioral. A distinct follow-up story (matrix-refresh-revalidates-with-idp, now implemented — see below) tracked the separately-identified observation that /refresh extended sessions purely locally and never re-contacted the OIDC token endpoint.

Session refresh (POST /_matrix/client/v3/refresh) — now IdP-revalidating (story matrix-refresh-revalidates-with-idp):

Registered OUTSIDE the auth pipeline (unauthenticated by spec) but rate-limited via the per-IP Nebu.Web.Plugs.RateLimit token bucket. /refresh no longer just extends the Nebu-local opaque session — it now re-validates against the OIDC provider on every refresh, closing the Matrix-side "Nebu loses its OIDC-resource-server posture" gap the Oracle flagged: the admin refresh path (Nebu.Admin.Auth.refresh_admin_session/1) already re-contacted the IdP on every silent refresh; Matrix previously did not, leaving up to 8h between a deprovisioning event at the IdP and Nebu noticing it (deactivate_user's immediate opaque-session hard-delete was the only other kill-switch). That detection-latency gap is now "next refresh attempt", matching the admin side:

  1. Extract refresh_token from the request body and classify it (MatrixSessionStore.resolve_refresh_token/1): current RT, PENDING old RT (replay, see "Refresh token validity" below), CONSUMED RT, or unknown. Unknown → 401 M_UNKNOWN_TOKEN WITHOUT soft_logout. Consumed → token reuse: the device's session is deleted and the response is 401 M_UNKNOWN_TOKEN WITHOUT soft_logout (the session is destroyed, so the client must discard its state).

  2. (AC1) IdP re-validation, BEFORE any local rotation: decrypt the session's stored oidc_refresh_token (Nebu.Web.SecretBox) and redeem it at the IdP token endpoint (grant_type=refresh_token) via the shared Nebu.Web.OidcTokenExchange.refresh/2 — the SAME HTTP-POST + response-parse + Nebu.Web.Plugs.OidcPlug.verify_token/2 logic Nebu.Admin.Auth.exchange_refresh_token/1 uses (extracted once, called by both domains — no second independent copy). A :matrix_oidc_refresh_override test seam (1-arity fun over the decrypted RT, mirroring :admin_oidc_refresh_override) short-circuits the HTTP+JWT-verify leg in tests.

  3. (AC2/AC7) Failures are classified by Nebu.Matrix.Session.RefreshErrors.transient?/1 — the single source of truth shared with the background worker:

    • Transient ({:transient_check_failed, _} e.g. DB hiccup, {:token_endpoint_unreachable, _}, {:discovery_failed, _}, IdP status >= 500 or 429): 503 with a Retry-After header (M_UNKNOWN, or M_LIMIT_EXCEEDED + retry_after_ms for an IdP 429). The session row is left untouched and a later retry with the same refresh token succeeds. Reason: Matrix clients log out on any 4xx and retry on 5xx, so a transient problem must never surface as 4xx.
    • Confirmed rejection (IdP 400/401/other, invalid_grant, :deactivated, decrypt failure, no stored OIDC RT): 401 M_UNKNOWN_TOKEN WITHOUT soft_logout and the device's rows are hard-deleted (MatrixSessionStore.delete_for_user_device/2). soft_logout: true is reserved for an expired access token on an authenticated request while the session still exists.

    There is no local-only fallback in either branch.

  4. (AC3) On success, the new opaque tokens' lifetime is computed via the SAME session_expires_at/1 function login uses (min(fresh_id_token.exp, now + ceiling)), fed the FRESH claims returned by the IdP round-trip — not a flat ceiling. This restores login/refresh lifetime symmetry (previously an asymmetry the Oracle had flagged) as a side effect of the IdP now genuinely being asked on every refresh.

  5. (AC4) A provider-rotated OIDC refresh token (some IdPs rotate on refresh, some don't) is re-encrypted and threaded through MatrixSessionStore.rotate_tokens/1's pre-existing :new_oidc_refresh_token param; when the IdP returns none, the previous encrypted OIDC RT is carried over unchanged (that carry-over was already rotate_tokens/1's behavior — this story is the first caller to ever actually populate the fresh-RT side of it).

  6. Rotate atomically (rotate_tokens/1, single transaction): soft-revoke the active row (revoked_at = NOW(), grace_until = NULL = pending) and insert the new row. Redeeming the CURRENT refresh token additionally consumes every older pending refresh token of the device (consume_pending_siblings). A concurrent /refresh that lost the row race is re-resolved (bounded retries) and served as a replay or denied. A replayed pending token is re-validated inside the rotation transaction (row lock): if the client consumed it during the IdP round trip, nothing is issued and the device is invalidated (token reuse). If the retries are exhausted while the session is intact, the answer is 503 + Retry-After (never a 4xx, which would make clients discard their state).

  7. Return 200 {"access_token", "refresh_token", "expires_in_ms"}.

Shared IdP token exchange (Nebu.Web.OidcTokenExchange): extracts the ~20-line grant_type=refresh_token HTTP-POST + JSON-parse + verify_token/2 leg out of Nebu.Admin.Auth.exchange_refresh_token/1 into a module both the Admin and Matrix refresh paths call with their own resolved config (issuer/client_id/client_secret) — same pattern as centralize-scope-resolution/centralize-claim-resolution, applied to the refresh grant. Each domain keeps its OWN test seam (:admin_oidc_refresh_override / :matrix_oidc_refresh_override) since audience/client-config context can differ between the two flows.

Server-paced background refresh worker (Nebu.Matrix.Session.RefreshWorker, story server-paced-session-refresh/background-session-refresh-worker):

The client-driven /refresh flow above only extends a session when the client actually calls it. Element Web's legacy SSO redirect + token-login path never does — it never sends refresh_token: true at login and never calls /refresh — so an opaque session issued that way was previously fire-and-forget: it simply expired at its initial expires_at bound with no further extension.

RefreshWorker is a plain GenServer (ADR-018/ADR-021 — inline SQL, no Repository/Service layer, Process.send_after scheduling loop, same template as Nebu.Matrix.Session.Sweeper), registered in the supervision tree alongside Sweeper under Nebu.Repo's children (server/lib/nebu/application.ex), so neither starts without a DB. Each cycle:

  1. Selects candidates — matrix_sessions rows with revoked_at IS NULL and expires_at within a configurable lookahead window of NOW() (NEBU_MATRIX_REFRESH_WORKER_LOOKAHEAD_SECONDS, default 1h). Since rotate_tokens/1 always soft-revokes the old row in the same transaction as inserting the new one, at most one non-revoked row exists per device at a time — a plain revoked_at IS NULL filter already excludes every rotated-away (pending/consumed) sibling, so a stale row is never touched.
  2. Re-checks per row (TOCTOU-safe) immediately before calling the IdP — re-selects by access_token_hash and skips silently (no-op, no resurrection) if the row was deleted or revoked since step 1 (e.g. by delete_one_device or a racing client-driven refresh).
  3. Redeems via the SAME reused logic as the client-driven path — HttpPublic.revalidate_with_idp/1 — no second, parallel IdP-exchange implementation.
  4. On success, extends the SAME row IN PLACE (MatrixSessionStore.extend_in_place_by_hash/3: UPDATE ... SET expires_at, oidc_refresh_token WHERE access_token_hash = $1 AND revoked_at IS NULL). The worker NEVER rotates: the client never learns rotated tokens, so rotating would log it out. The opaque access and refresh token stay unchanged; no new row, no revoked_at, no grace window. The new expires_at is computed by the SAME session_expires_at/1 cap login//refresh already use (min(fresh_oidc_exp, now + ceiling)) — never unbounded. In addition (F-1) the extension is capped absolutely at created_at + matrix_session_ttl_seconds of the row (GREATEST(expires_at, LEAST($new_expires_at, created_at + ttl)), strictly extending, never shortening): a device that never refreshes itself cannot be kept alive forever through the worker (a stolen opaque AT of a dormant device dies at the cap). A client /refresh creates a new row with a fresh created_at, so well-behaved clients never hit the cap; at the cap the worker simply stops extending and the normal expiry/soft_logout path applies. Rows at/over the cap (created_at + ttl <= NOW()) are not even selected as candidates (N-3), so there is no pointless IdP redemption that would keep resetting the IdP idle timer; the client's own /refresh re-validates. Rows that an earlier worker version extended beyond the cap keep their expires_at (GREATEST): deploy logs out nobody; only clients inside the OLD 60 s grace window at deploy time are logged out once (rows with a time-valued grace_until count as consumed). Note: the stale COMMENT on matrix_sessions.grace_until in the applied migration 000001_init.up.sql is intentionally not edited (applied migration); the semantics are NULL = pending, NOT NULL = consumed. Worker and client /refresh therefore never invalidate each other (both are atomic single-row statements guarded by revoked_at IS NULL).
  5. On failure, only a CONFIRMED upstream rejection/deprovisioning signal (e.g. :deactivated, an explicit IdP rejection of that refresh token, decrypt failure) hard-deletes the session row — scoped to the EXACT row just acted on (MatrixSessionStore.delete_by_access_token/1, guarded WHERE access_token = $1 AND revoked_at IS NULL), not the wider delete_for_user_device/2. This narrower scope matters specifically for this worker: under clustering (ADR-020), multiple nodes may race to redeem the same rotating upstream refresh token, and a losing node's failure must not be able to reach across and delete a row another node//refresh already superseded. A merely TRANSIENT error (DB hiccup, IdP unreachable/discovery failure, IdP 5xx/429) is NOT treated as rejection — the row is left intact and simply expires via the bounded TTL if the transient condition persists, avoiding a brief IdP outage or rolling restart turning into a mass, self-inflicted hard-delete of every near-expiry session in that cycle.

Orthogonal to the per-request liveness check: the existing ≤60s deactivation/revocation check in OidcPlug.call_opaque/1 (via provision/2) is completely unmodified — the worker's interval does not replace it, lengthen it, or otherwise interact with it. Delete-on-deactivation remains the fast path; the worker's bounded rolling TTL remains the fail-safe ceiling if a delete is ever missed. No new ADR: this is additive hardening of the existing legacy-login session-refresh path, not a new architectural decision.

Refresh token validity (Matrix CS API v1.18, "Refreshing access tokens"):

The spec has NO fixed grace window: the old refresh token "remains valid until the new access token or refresh token is used, at which point the old refresh token is revoked". Nebu implements exactly that:

  • After a client /refresh, the old row is PENDING (revoked_at set, grace_until IS NULL). A client that lost the response may retry with the old refresh token at ANY later time (not just 60 s): the server rotates the device's active row again (replay) and returns a fresh pair. The old token is never valid beyond the session's own expires_at (the device must still have an unexpired active row).
  • The old token becomes CONSUMED (grace_until = NOW()) the first time the new access token authenticates a request (OidcPlug.call_opaque/1 → mark_access_token_used/1) or the new refresh token is redeemed.
  • Presenting a consumed refresh token is treated as token reuse (hardening borrowed from the OAuth refresh grant): 401 M_UNKNOWN_TOKEN WITHOUT soft_logout and only THAT device's session is deleted; other devices of the user are unaffected. The detection is BOUNDED (F-3): it only fires while the consumed row is younger than the session TTL ceiling (created_at + matrix_session_ttl_seconds) AND the device still has an active row (any non-revoked row, i.e. exactly what the :current path would redeem, even if its access token has already expired; N-7). An older leaked token, or a token of a dead device, is a plain 401 that deletes nothing (otherwise any historical RT would be a remote-logout vector).
  • Takeover handling (F-2 / N-1): a stolen, still-PENDING old RT can be replayed to rotate the device's active row, which supersedes the victim's tokens (soft-revoked). OidcPlug.call_opaque/1 answers EVERY soft-revoked access token (pending or already consumed) with 401 M_UNKNOWN_TOKEN + soft_logout: true and NEVER deletes or detects reuse itself (no time window, no wall-clock heuristic; a legitimate in-flight request, e.g. a /sync long-poll that started before a rotation, simply makes the client refresh). Reuse detection lives only on the bounded /refresh path (F-3). Outcomes: a PASSIVE attacker (replays RT0, does not use the session) -> the victim gets soft_logout, refreshes with its still-pending RT1 and wins the device back (attacker's tokens die). An ACTIVE attacker (uses his new AT2 immediately, which consumes the victim's pending RT1) -> the victim still gets soft_logout, refreshes with the now-consumed RT1 -> reuse -> the device is deleted -> BOTH parties are logged out; that visible logout is the end of the takeover.
  • An expired access token with a still-valid refresh token keeps 401 M_UNKNOWN_TOKEN + soft_logout: true.
  • POST /refresh keeps its own per-IP rate limit (429 M_LIMIT_EXCEEDED with retry_after_ms, spec-defined); the limit (30/min, burst 10) is far above legitimate refresh traffic.

Logout: POST /logout deletes the user's matrix_sessions rows (immediate, no grace window).

Admin deactivation: deactivate_user also deletes the target user's matrix_sessions rows, so an admin-deactivated user's opaque sessions are invalidated immediately.

Comparison to Admin Sessions:

AspectAdmin SessionsMatrix Sessions
StorageSigned Phoenix session cookie (no DB row)matrix_sessions table
Token formatEncrypted claims/RT in the signed cookieOpaque hex (64-hex, no dots)
Encrypted OIDC RTYes — AES-256-GCM (Nebu.Web.SecretBox) in the cookie; used for active refreshYes (future: allows Keycloak session binding)
Lifetime controlsession_exp = min(id_token.exp, now + 8h)min(oidc_exp, now + lifetime) for all Matrix clients (lifetime: 8h default, Admin-configurable)
Refresh patternWhen admin_oidc_refresh_mode = offline: silent OIDC RT exchange on HTTP requests within a 120 s pre-expiry window, admin-group gate re-evaluated each refresh. Default online → no offline_access, no silent refresh (see refresh-mode config below)On-demand via /refresh endpoint; matrix_oidc_refresh_mode (default offline) controls whether offline_access is requested
Grace periodNone (refresh failure / lost group → clear session, redirect /admin/login)No fixed window: the old refresh token stays PENDING (grace_until IS NULL) until the new access token is used or the new refresh token is redeemed, then CONSUMED (grace_until set); reuse of a consumed token deletes that device's session

Cryptography

Two key pairs per user (ADR-007):

Key PairAlgorithmPurpose
Signing KeyEd25519Message signatures, non-repudiation
Encryption KeyX25519 (ECDH)PII encryption, GDPR deletion

Generated at user registration via Erlang/OTP 27 :crypto - no external Hex packages.

Event signing: All Matrix events are signed with the sender's Ed25519 private key before storage. Nebu.EventId.generate/1 computes $<base64url(SHA-256(canonical_json(event)))>.

GDPR Right-to-be-Forgotten: Delete both private keys → sensitive PII (email, IdP subject) is permanently irrecoverable. Audit log integrity is preserved - message signatures remain verifiable via the permanent public key.

Compliance key: A separate Ed25519 keypair in server_config (AES-256-GCM encrypted) signs compliance export tokens. Survives Elixir restarts (unlike the ephemeral :nebu_signing_key).

Audit Logging

All administrative actions and compliance access events are written to an append-only audit_log table. Entries are Ed25519-signed for tamper evidence. Retention is configurable (default: 2555 days / 7 years).

Nebu.Compliance.AuditWriter validates every action against a @known_actions allowlist and rejects an unregistered action with {:error, :audit_unknown_action} before any DB call — so a new audited operation MUST add its action there or its entries are silently dropped. The Admin device-management operations (Story s4) registered device_renamed and device_deleted accordingly. Story power-levels-enforcement registered room_power_levels_changed: one row per COMMITTED change through PUT /rooms/{id}/state/m.room.power_levels (metadata changed_keys (only the ten keys of the v10 content), users_changed (at most 50 user ids, sorted; strict ids, except that a REMOVED stored entry of a relaxed-grammar id (a port or above 255 bytes) is listed too), users_changed_count, plus other_keys_changed_count when keys of the client's own choosing were altered: their names are never recorded, so the row carries no client free text), best effort after the room process committed (like the moderation rows; residual follow-up fs); no row for the idempotent no-op, a refusal, createRoom or an upgrade copy. Story state-writer-redaction registered room_event_redacted: one row per COMMITTED redaction of ANOTHER user's event (actor = the redacting user, resource room, the room id, metadata exactly event_id (the target) and target_sender; never the reason, never message content), best effort after the room process committed; no row for an own-event redaction (the signed redaction event is the record), the idempotent replay or a refusal. An export of the audit log (CSV / spreadsheet) must escape its cells: user ids start with @ (a formula prefix in some spreadsheet programs).

Append-only enforcement model (Story 13-14):

The audit_log table uses ENABLE ROW LEVEL SECURITY with two blocking policies - audit_log_no_update USING (false) and audit_log_no_delete USING (false). FORCE ROW LEVEL SECURITY is intentionally not set in the squashed single-role migration.

Deployment modelMechanismRationale
Single-role (nebu_app = table owner)Application-layer enforcement + RLS policies block non-owner sessions; nebu_app as table owner bypasses RLS naturally (no FORCE RLS). audit_log_purge() SECURITY DEFINER works without BYPASSRLS.FORCE RLS would require BYPASSRLS on nebu_app for audit_log_purge() to succeed - this privilege escalation is avoided by design.
Dual-role (nebu_migrate = function owner with BYPASSRLS)FORCE RLS + SECURITY DEFINER escalation - supported by legacy incremental deployments.Dual-role BYPASSRLS privilege on the migration role is not exposed in the squashed migration.

Immutability guarantee in single-role mode: The nebu_app application role is the table owner. Direct DELETE/UPDATE on audit_log issued deliberately by the application owner will succeed - the RLS policies block only non-owner roles. Defence-in-depth is provided by REVOKE UPDATE, DELETE ON audit_log FROM nebu_app (no-op for owner, meaningful for non-owner sessions in dual-role deployments) and by application-layer invariants: the gateway never issues DELETE or UPDATE on audit_log outside of audit_log_purge().

audit_log_purge(retention_days INT) is a SECURITY DEFINER function that deletes rows older than retention_days (bounds: 1–36500). EXECUTE is restricted to nebu_app; PUBLIC access is revoked. The BEFORE INSERT trigger audit_log_event_time_force_trigger forces event_time = NOW() on every insert, preventing backdating.

Failure audit trail pattern (Story 9-27): Multi-step operations (e.g. room upgrade) that can fail partway through wrap their entire body in try/rescue. The rescue clause writes a "failure" audit entry (including an "error" metadata field with the exception message) before reraising the original exception. A nested try/rescue around the audit write itself ensures an outage in the audit writer never masks the original error. This guarantees that any partially-applied operation leaves a forensic record even when the in-process call ultimately returns an error to the client.

Operational Logging

Separation from audit logs: Nebu.Web.LogStore manages a mutable, pruneable operational log store (operational_logs table) that is EXPLICITLY SEPARATE from Nebu.Compliance.AuditLog (the immutable, tamper-evident audit trail). Audit logs have a compliance contract (legal hold, tamper evidence); operational logs are transient diagnostics for operator troubleshooting — merging them would force audit logs to inherit a TTL deletion policy (bad for compliance) or operational logs to inherit expensive immutability (unnecessary complexity under churn).

Central logger handler: instead of scattered Logger.* calls deciding independently whether to persist, a single Erlang :logger handler (attached at boot via Nebu.Web.LogStore.attach_handler/0) intercepts every Logger.* call app-wide and is the sole decision point:

  1. Always: broadcasts via Phoenix.PubSub.broadcast/3 to topic "operational_logs" (feeds live-view subscriptions, independent of persistence)
  2. Conditionally: queues entries via GenServer.cast/2 to Nebu.Web.LogStore.Writer for batched insertion, scoped by server_config policy

The handler runs synchronously in the calling process (standard :logger behaviour) but performs NO blocking DB I/O — instead, it delegates to a buffering GenServer (non-blocking cast) so that Logger calls on hot paths (e.g. inside Nebu.Repo.QueryTelemetry's telemetry callback, where a live DB read would deadlock the connection pool) remain fast.

Levels and persistence:

  • WARN and ERROR: always persisted (no toggle). Since story admin-logs-retention-settings, these two levels also get a hardcoded, non-admin-configurable 90-day safety-net TTL (no server_config key, no UI control — AC6 explicitly forbids one) so this tier is no longer unbounded, closing a gap flagged during the operational-log-store review.
    • ERROR-always-persists exception (story trace-map-node-controls): Entry level is checked before endpoint-tier policy — ERROR entries persist to operational_logs unconditionally, regardless of whether the endpoint is in the long-polling "off-by-default" list. This ensures a silently-off zone never hides failures: if an off-list endpoint encounters an error, the error is logged and propagates to the Trace Map (node renders with error status), making the silent zone visibly alarming to operators.
  • INFO and DEBUG: persisted only if enabled via server_config keys (log_collect_info, log_collect_debug); off-list endpoints (long-polling: sync, keepalive, health) suppress DEBUG/INFO collection by default
  • Retention per level: TTL (log_retention_*_days) + max-count cap, purged hourly via Nebu.Matrix.Session.Sweeper's existing sweep cycle. The max-count cap was DEBUG-only at first; story admin-logs-retention-settings added the symmetric log_retention_info_max_count key so INFO now has the same two-dimensional (days + count) retention control as DEBUG.

Request ID and route template propagation: Nebu.Web.Plugs.RequestLogger calls Logger.metadata(request_id: request_id, route_template: route_template) early in the pipeline so all downstream Logger.* calls automatically carry them. route_template is normalized via position-based pattern matching (room/user/event ID segments → placeholders, e.g. /rooms/:room_id/send/:event_type/:txn_id) rather than fragile regex matching, enabling endpoint-level log aggregation.

Policy cache: Nebu.Web.LogStore.PolicyCache caches the server_config log-policy keys (6, since admin-logs-retention-settings added log_retention_info_max_count) in persistent_term to avoid a Nebu.Repo connection-pool re-entrancy bug (the handler runs synchronously inside Nebu.Repo.QueryTelemetry's own telemetry callback, so a synchronous DB read there would deadlock the pool). The cache is refreshed on-write and on a periodic timer.

Schema:

exclusive_scope in war_room_sessions (Story war-room-dedicated-page): When an operator pre-scopes a session with "exclusive mode" (AC9), the exclusive_scope column holds a non-empty list of route_template strings to EXCLUDE from capture. When Nebu.Web.LogStore.WarRoom.check/1 evaluates an entry for tagging, it checks:

  1. Does the entry's route_template match the session's scope (as before, inclusive scope)?
  2. Is the route_template in exclusive_scope (AC10)? If yes, SKIP (do not tag).
  3. Does the filter (if present) match metadata["user_id"]/["device_id"] AND-wise?

If all three conditions pass, the entry is tagged with war_room_id and persisted. This avoids the common "I only wanted /sync excluded" scenario by allowing an operator to say "capture everything except these noisy endpoints" rather than laboriously enumerating what they do want. Pre-populated defaults from Nebu.Admin.LogsLive.TraceMap.long_polling_endpoints/0 (the canonical endpoint list) are offered as checkbox options in the War Room form; an operator may also type a custom route if needed (validated server-side — untrusted client input, ADR-018 defense-in-depth).

Admin UI monitoring consumption layer (Nebu.Admin.LogsLive, story admin-logs-monitoring-view):

The operational log store's live broadcast is consumed by the Admin UI at /admin/logs (Nebu.Admin.LogsLive LiveView), which groups operational logs by request_id and presents one row per request, enabling operators to diagnose production issues without leaving the admin dashboard. The view subscribes to the "operational_logs" PubSub topic and applies log filters with two dimensions: Level (flat bubble list) and Source (flat bubble list). The filter state is a real, persistent LiveView socket assign, @active_filters — a map of dimension => [values] supporting same-dimension multi-select (e.g. level=error OR level=warning). Each bubble's phx-click triggers both instant client-side visual feedback via Phoenix.LiveView.JS.dispatch (zero perceived lag for interactivity) AND a JS.push("toggle_filter"/"remove_filter", ...) that updates the server-side assign via new event handlers in LogsLive. Filter state is also reflected in the page URL as comma-joined query params (e.g. ?level=error,warning) via client-side history.pushState, making the filtered view reloadable and shareable. Browser back/forward triggers a new handle_event("sync_filters", ...) push to keep the server assign in sync with URL-driven navigation. A shared reject_off_tier/3 helper guards all three write paths (mount-time URL seeding, toggle_filter, sync_filters) against activating a dimension value not currently collected per the retention policy. This fixes a latent bug in the prior pure-client-side design: unrelated re-renders silently reset filters since the server never tracked click state. The view streams requests sorted by severity then recency. A detail panel shows the full chronological thread for a request_id. The Endpoint dimension (hierarchical tree filtering) was retired in story trace-map-leitstand and is now a dedicated, full-page LiveView at /admin/logs/trace-map. Additionally, this story fixes two logging-pipeline gaps: (1) Nebu.Web.Plugs.RequestLogger now places method/status/duration_ms into structured Logger.metadata/1 so these fields reach the operational log store as metadata (not just inspect'd message text), and (2) Nebu.Web.LogStore.Handler.source_from_meta/1 now derives the calling module from Logger's :mfa metadata tuple rather than a nonexistent bare :module key, so "http"/"ecto" source classification works correctly in production log ingestion.

Admin UI collection-policy configuration layer (Nebu.Admin.LogsController, story admin-logs-retention-settings):

Where Nebu.Admin.LogsLive (above) is a read-only view of the collection policy, GET /admin/logs/config / POST /admin/logs/config/retention (Nebu.Admin.LogsController + Nebu.Admin.LogsConfigHTML) let an operator actually change it — reached via a "Sammlung konfigurieren →" link on the monitoring page, deliberately nested under /admin/logs rather than the generic /admin/config page (config UI lives next to where it's used, not in a catch-all). It is a deliberate, literal plain Phoenix Controller, not a LiveView, following Nebu.Admin.ConfigController's exact GET/POST/flash/redirect pattern. WARN and ERROR render as non-interactive, always-on bubbles (no UI control exists for them, matching their hardcoded safety-net TTL above); INFO and DEBUG render as clickable toggle buttons that, when turned on, reveal inline retention fields (days, max entries) — DEBUG additionally shows a fixed warning about payload volume and high-frequency capture, referencing a not-yet-built "War Room mode" by name as a forward-compatible placeholder. Because there is no LiveView socket on this page, all of the toggle/reveal/pre-fill/warning interactivity (AC7/AC8/AC11) is implemented client-side in a new wireLogsConfigToggles() function in server/priv/static//assets/app.js, using the same delegated-listener, CSP-safe pattern (script-src 'self', no inline handlers) as the page's other JS-driven admin widgets; the actual persistence round-trip is a real native form POST, validated and normalized server-side (blank or "0" always means "no limit" for that dimension, unconditionally — the 7-day/1000-entry defaults from AC8 are a client-side pre-fill convenience only, never a server-side fallback). Cross-tab consistency between this settings page and the monitoring view's bubble state (AC17) is implemented as Option A for this story: no PubSub, the monitoring page reflects a changed policy only on its next manual reload.

War Room Mode — bounded, self-terminating diagnostic sessions (story war-room-mode, final story of the enhanced-logging epic):

The DEBUG collection toggle above (log_collect_debug) is a persistent, global switch — safe only if an operator remembers to turn it off, and unsafe on a high-frequency endpoint like /sync (calls continuously, every connected client, every few seconds — an unbounded DEBUG-on window can fill the DB in seconds). War Room Mode is a distinct, orthogonal mechanism: a bounded, tagged, self-terminating diagnostic session, not a policy toggle. Exactly one may be active at a time (war_room_sessions table; started_by/reason/scope/filter JSONB/max_entries/max_duration_s/ended_at/ended_reason/entries_collected).

Enforcement lives upstream of persistence, not inside the Writer's batch flush. The obvious-looking design — "tag entries with war_room_id inside Writer.flush_buffer/1, right before the INSERT" — does not work, for two reasons: (1) Writer only ever sees entries Handler.log/2 already decided to persist per the ambient policy, but AC4 requires war-room-matched entries to persist regardless of that policy (e.g. a DEBUG entry even when log_collect_debug=false); (2) Writer's batching (every 5s or 100 entries) is far too coarse for single-entry budget granularity — a burst of matching entries inside one flush window would blow straight through max_entries before the batch is even inserted. Instead, Nebu.Web.LogStore.WarRoom (a new GenServer, sibling to PolicyCache) intercepts inside Handler.log/2, before the should_persist? gate: it holds the one cached active session, fast-paths to a no-op when none is active (a persistent_term flag, mirroring PolicyCache's own "never block the hot path" design), and on a match performs one atomic UPDATE ... entries_collected = entries_collected + 1[, ended_at = now(), ended_reason = 'budget_exhausted'], serialized through the single GenServer — genuinely single-entry-granular, no read-then-write race, no batch-window overshoot.

Two independent termination paths, both scoped to exactly one active session by construction (AC2's "exactly one active" is enforced both app-level, in Nebu.Admin.WarRoomManager, and at the DB level via a partial unique index war_room_sessions_single_active_idx ON ((1)) WHERE ended_at IS NULL — the app check gives a friendly conflict message, the index is the real backstop against a race, and a unique_violation maps to the same {:error, :conflict, ...} tuple either way): (1) budget exhaustion — the atomic increment above; (2) duration timeout — a single Process.send_after scheduled on session load, checked against the current cached session id (so a stale timer message from a since-ended session is a no-op), re-scheduled with the remaining time on every load — including at app boot (do_load_active/1), which immediately ends an already-past-deadline session rather than silently leaving it active forever across a restart. This closes the exact "forgot to turn it off" failure mode that motivated bounding the feature in the first place.

Scope/filter matching (AC3/AC9): scope is "all"/nil (match everything) or an exact route_template string (e.g. /_matrix/client/v3/sync — verified the only sync route this codebase serves, no r0/v1 alias exists, so exact-string matching is complete here); filter is an optional {user_id, device_id} JSONB map, AND-matched against the log entry's metadata["user_id"]/["device_id"]. Those two metadata fields did not exist anywhere in the logging pipeline before this story — Nebu.Web.Plugs.RequestLogger now also propagates them via Logger.metadata/1, extracted through a new extract_user_id/1 helper that reduces conn.assigns.current_user to a bare identifier string regardless of route (Matrix routes: already a string; /admin/* routes, served by the SAME RequestLogger plug: Nebu.Admin.Auth's current_user is a MAP containing an admin's email — extract_user_id/1 exists specifically to never let that map, or its email, reach Logger.metadata/1; this was a security-review-driven fix after code-review caught the naive version leaking admin emails into operational_logs on admin routes).

UI integration is a LiveView, not a new HTTP controller. The story's own Dev Notes assumed a separate war_room_controller.ex with JSON-shaped endpoints (POST /admin/war-room/start → 409/session-id) — written before admin-logs-monitoring-view had shipped. In reality /admin/logs is Nebu.Admin.LogsLive, a LiveView, and the admin app's :browser pipeline is HTML-only with no JSON-API convention anywhere under /admin. The War Room panel (Nebu.Admin.LogsLive.WarRoomPanel) is therefore a component of that same LiveView: handle_event("start_war_room"/"stop_war_room", ...) calls straight into Nebu.Admin.WarRoomManager (the Admin-domain public API + inline SQL, ADR-018 — start_session/1, stop_session/1, status/0; no Plug.Conn awareness, directly unit-testable), gated by the same on_mount :require_admin as every other event in that LiveView. WarRoomManager.start_session/1 also enforces max_entries/max_duration_s numeric bounds directly at this API boundary — added after a Kassandra security-review MEDIUM finding that the LiveView form's own client-side bounds check was the only place those bounds were enforced, insufficient defense-in-depth for a feature whose entire purpose is bounding runaway collection. A 2-second self-tick (independent of log traffic) refreshes the live "N/max entries · time remaining" status bar; the panel's outer wrapper carries aria-live="polite" (mirroring the retention-settings page's own established "stable wrapper, dynamic child" pattern) so screen-reader users get feedback on every start/stop/auto-terminate transition. Nebu.Admin.LogsLive.mount/3 additionally accepts an optional war_room_id query param, seeding the initial log load from Nebu.Web.LogStore.get_logs(%{war_room_id: id}) (its @allowed_filter_keys was extended to accept it — the SQL already selected the column, added by operational-log-store's migration as a forward-looking seam) instead of the default unfiltered 200-row cap, giving a bookmarkable "show me everything tagged with this session" URL without reimplementing the stream component.

Redaction is untouched — no new leak path. WarRoom.check/1 and the tagging it performs read/write only route_template, metadata["user_id"/"device_id"], and war_room_id — it never touches entry.message or any payload content. The two-category redaction (PayloadRedactor, credentials + chat message content) still runs entirely upstream, inside RequestLogger's Logger.debug(fn -> ... end) closure, before an entry ever reaches Handler.log/2; elevating collection via a war room changes whether an already-redacted entry gets persisted, never what is in it.

War Room Indicator — Cross-Page Recording Status Visibility (Story war-room-dedicated-page)

Problem: When an operator starts a War Room session, the UI signals (recording indicator, countdown) were visible only on the page where the session was started (/admin/logs when embedded, later the dedicated /admin/logs/war-room page). An operator navigating away would lose real-time awareness that a recording session was active, and could inadvertently perform unmonitored diagnostic work or trigger a full page reload thinking the session had ended.

Solution: Nebu.Admin.WarRoomIndicator is a shared, stateless assign helper that every admin LiveView (DashboardLive, AuditLogLive, LogsLive, LogsLive.TraceMap, LogsLive.WarRoom) calls in its mount/3 to poll Nebu.Admin.WarRoomManager.status/0 every 2 seconds. Each page receives a live @war_room_active assign (boolean or session metadata), updated by a self-tick independent of user interaction. Nebu.Admin.Layouts.live/1 consumes this assign to render:

  • Sidebar recording dot: a pulsing red dot on the "War Room" nav item (children item under "Logs" group, populated by the admin-sidebar-nesting story) when @war_room_active is truthy.
  • Topbar recording pill: a fixed-width pill showing "War Room recording · mm:ss" in the header, with the user who initiated the session and elapsed time, updated every second on the client via a lightweight JS hook (no server tick needed — the expiry state is already in @war_room_active's {started_at, max_duration_s} tuple or similar).

This pattern is CSP-safe (no inline JS; the hook is registered in app.js like the nav-section toggle) and survives LiveView morphdom patches (the dot/pill are part of the shared Layouts chrome, not re-rendered on each view mount). An operator viewing any admin page sees a single source of truth for "is a recording active right now" without needing to navigate back to the War Room page — closing the situational-awareness gap the Trace Map Leitstand UX gate identified as a required closure for the feature to be production-safe.

Temporary Capture — Time-Limited Per-Endpoint Diagnostic Collection (Story trace-map-node-controls)

Rationale: Long-polling endpoints (sync, keepalive, health) are rendered off-by-default in the Trace Map to keep the signal-to-noise ratio high — DEBUG/INFO collection is suppressed unless an operator explicitly enables temporary capture. This complements War Room Mode: where War Room is a global, bounded session with optional scope/filter, Temporary Capture is a fine-grained, one-click per-endpoint toggle for a fixed 5-minute window.

Architecture:

  • Storage: admin_temporary_captures table (endpoint TEXT PRIMARY KEY, enabled_at TIMESTAMP, expires_at TIMESTAMPTZ, started_by TEXT) — a shared DB row is the single source of truth for timer state (cluster-safe, survives node restarts, deterministic server-time-based expiry).

  • Module: Nebu.Admin.TemporaryCaptureManager (public API: enable/2, disable/2, active?/1, cleanup_expired/0; inline SQL, ADR-018). The 5-minute duration is configurable via Application.get_env(:nebu, :temporary_capture_duration_s, 300) for testing.

  • Cache: Nebu.Web.LogStore.CaptureCache (persistent_term-backed, mirroring PolicyCache pattern). 2s polling refresh + on-demand invalidation (PubSub broadcast on capture enable/disable). Holds a map of {endpoint → {enabled_at, expires_at}} so should_persist?/2's enforcement avoids a per-request DB query on the hot path.

  • Enforcement in LogStore: Handler.log/2 invokes should_persist?/2 after checking for ERROR (which always persists) and after the war-room check. should_persist?/2 now consults CaptureCache: if a capture is active and not expired for the endpoint, persist the entry (DEBUG/INFO level) regardless of the global policy. Expired captures are cleaned up lazily on cache refresh.

  • UI: The Trace Map inspector panel gains "Enable capture" button for off-list endpoints. On click, Nebu.Admin.LogsLive.TraceMap.handle_event("enable_capture", ...) → TemporaryCaptureManager.enable/2 → PubSub broadcast → CaptureCache refresh. A countdown display (mm:ss, client-side computed from expires_at) updates every second; on expiry, the node reverts to off state. "Disable now" button calls handle_event("disable_capture", ...) → disable/2 for early termination.

  • Audit trail: Capture enable/disable are recorded as admin actions in compliance_audit_log (following the retention-settings pattern): action: "enable_temporary_capture" / "disable_temporary_capture", resource_type: "endpoint", resource_id: endpoint_name, admin_user: current_user, timestamp: now().

  • Cascade for domain/nebu nodes: Inspector selects allow per-endpoint level/retention changes and also cascade those changes to all satellite endpoints under a domain or nebu (batch upsert_server_config calls). Temporary capture is per-endpoint only; cascade does not apply to capture enable/disable (clicking an off node enables capture for that node alone, not its entire domain).

No new ADR: this is an operational feature layered atop the existing policy cache and handler pattern, not a new architectural decision.

Trace Map Zoom/Pan — Client-Owned Viewport State (Story trace-map-zoom-pan)

Interaction model: the Trace Map Leitstand SVG supports Google-Maps-style navigation via four redundant input paths, each covering both zoom and pan:

  • Wheel — cursor-centered zoom (the point under the cursor stays fixed), computed via getScreenCTM().inverse() against a real SVG point rather than raw pixel deltas, so it stays correct under the viewBox's preserveAspectRatio="xMidYMid meet" letterboxing.
  • Drag — click-and-drag pans the background; a pointerdown originating on a node ([data-testid="trace-map-node"]) sets a null guard for the whole gesture so a click (or even a large intentional drag) started on a node never pans and never blocks the node's own click handler.
  • Keyboard — arrow keys pan (32px/press), +/=/PageUp and -/_/PageDown zoom, scoped to event.target === mapSvg so it never shadows the page's other keyboard interactions (node activation, the stream resizer). Keyboard/button zoom deliberately holds pan fixed (no re-centering) — only wheel zoom is cursor-centered by design.
  • Buttons — a trace-map-zoom-controls group (zoom in/out/reset) and a trace-map-pan-controls group (up/down/left/right), both plain <button type="button"> wired via addEventListener in the TraceMapLeitstand hook (server/priv/static//assets/app.js) — no phx-click, no inline handlers (CSP script-src 'self'). The pan buttons call the exact same panByStep/2 the keyboard path uses, not a parallel implementation, so the two stay behaviorally identical by construction.

Zoom is bounded 1.0–4.0 (step 0.1); pan is clamped so the viewport can never scroll past the map's edges at any zoom level. State does not persist across reload (by design) and lives only in the LiveView hook's JS-side instance state.

Client-owned state survives LiveView patches: this is the second feature in this epic to need this pattern, after the stream-resizer's height. Any client-set attribute the server never renders (the SVG's viewBox, here) is silently stripped by morphdom on the next patch, because the DOM node's identity is preserved but its non-server-tracked attributes are not. The hook's updated() callback reasserts the current zoom/panX/panY into the viewBox on every patch — the same fix shape as the resizer's streamHeight reassertion — rather than the server tracking viewport state as a socket assign (which would require a round-trip per zoom/pan input and defeat the point of client-side responsiveness).

Accessibility: the SVG root is tabindex="0" with an extended aria-label describing the keyboard controls, and carries an explicit :focus-visible CSS rule (server//assets/css/app.css) matching the existing node-ring/resizer focus-ring convention — a newly-focusable element needs its own explicit rule rather than relying on the browser's UA-default outline. Both zoom and pan have a dedicated on-screen button group specifically so a user who cannot perform a pointer drag, or whose assistive technology may not reliably enter "focus mode" for a bare role="group" container, still has a fully discoverable, keyboard-operable path to every viewport operation.

No new ADR: this is a client-side interaction enhancement to an existing visualization, not a new architectural component.

GDPR Article 17 Right to Erasure - Deletion Orchestration (Story 14.4)

DELETE /api/v1/admin/users/{userId} - instance_admin only (four-eyes: caller ≠ target).

The GDPR deletion handler in Nebu.Compliance.* chains the existing building blocks into a single atomic-from-the-caller's-perspective pipeline (all steps are in-process calls within the one application):

  1. DeactivateUser - is_active=false + all sessions destroyed (NOT_FOUND → 404 stops pipeline; AlreadyExists → idempotent continue)
  2. DeleteUserKeys - private_key = NULL for signing + encryption keys (best-effort: failure logged, pipeline continues)
  3. DB anonymization TX - profiles.displayname = 'Deleted User', profiles.avatar_url = NULL, users.anonymized_at = <unix_ms> (failure → 500, PII erasure MUST NOT be silently skipped)
  4. Audit emit - gdpr_deletion event with keys_deleted: "confirmed"|"failed_best_effort" metadata (never-raise: audit failure does not block 200)
  5. 200 {"user_id": "...", "status": "gdpr_deleted"}

Login behavior after deletion: POST /_matrix/client/v3/login - when token validation finds the user "deactivated", the login handler responds 403 M_USER_DEACTIVATED (Matrix CS API v1.18 compliant).

Room history preservation: Messages in room event timelines are NOT deleted - per Matrix spec and GDPR recital 65 (legitimate archive interest). The sender's profile is anonymized; message content remains.

Runbook: docs/compliance/gdpr-deletion-runbook.md

Three PII Tiers

TierDataAt RestGDPR Deletion
Operational PIIDisplay name, avatarEncrypted at rest with the SERVER-WIDE pii_encryption_key (NOT with a user key: deleting the user's keys does not shred it)The erasure transaction sets the ciphertext columns of users to NULL and overwrites profiles with "Deleted User" (story gdpr-erasure-global-data)
Sensitive PIIEmail, IdP subjectEncrypted with user's X25519 keyDelete private key (DeleteUserKeys, Story 5.7); the ciphertext columns are NULLed as well. Irrecoverable only once backups and WAL within the retention have expired (runbook)
Message ContentChat messagesPlain in DB (audit requirement)Not deleted; sender anonymized

Per-Device Sync Token Isolation (Story 9-22)

Matrix clients identify themselves with a device_id extracted from the "did" claim in the JWT access token. The Elixir Server maintains independent sync checkpoints per (user_id, device_id) in the sync_tokens table (composite PK, migration 000041).

Design invariants:

  • A single user with N active devices has N independent rows in sync_tokens.
  • Each device's since token is advanced only by that device's sync responses.
  • On logout, only the (user_id, device_id) row is removed; other devices are unaffected.
  • Legacy clients (no device_id in JWT) fall back to the device_id = '' row; a token mismatch triggers a full initial sync (safe degradation).
  • Story sync-token-per-device (follow-up fd, closed): GET /sync passes the AUTHENTICATED device (opaque session or bound raw IdP JWT, never a request parameter), so the per-device rows are actually used; only an unbound raw IdP JWT (device-less) keeps the '' row (follow-up cj (c)). Each device row also carries account_data_pos and cross_signing_pos (migration 000052): the global account data and the own cross-signing change are delivered per device by position, not by the consume-once flags (those stay for device-less requests).
  • persist_since_token/4 and get_since_token/2 are the device-aware arities in Nebu.Session.PgStore.Postgres; /3 and /1 remain for backward compatibility.

Token cleanup on logout: POST /logout invalidates the session in-process via SessionSupervisor.destroy_session/2 (a DB transaction that deletes the sync_tokens + sessions rows for that device). ETS is NOT evicted (other devices may still be active).

Account Data - Global and Room-Scoped (Stories account-data-durability, room-account-data-and-read-markers)

Matrix account data ("Client Config") is private, per-user, opaque client state in two scopes.

ScopeStoreTable/sync locationIncremental delivery
GlobalNebu.Matrix.AccountDataStoreaccount_data (000011)top-level account_data.eventsmonotonic stream_pos (sequence sync_global_pos_seq, 000052) vs. the device's sync_tokens.account_data_pos (per DEVICE, story sync-token-per-device); device-less requests keep the consume-once per-USER changed flag
RoomNebu.Matrix.RoomAccountDataStoreuser_room_account_data (000039)rooms.join.<roomId>.account_data.eventsmonotonic stream_pos vs. the position in the client's since token (per DEVICE)

Both stores are PostgreSQL + inline SQL (ADR-018, cluster-correct per ADR-020) and keep the content VERBATIM as TEXT (ADR-011, byte-exact round trip; for room scope the raw request body is captured by Nebu.Web.Plugs.RawBodyReader). The Room store is type-agnostic so the follow-up room-tags story (m.tag) can reuse it. A dormant legacy table room_account_data (000001/000002; JSONB, forced RLS) is intentionally NOT used.

Room account data endpoints and error matrix

PUT/GET /_matrix/client/v3/user/{userId}/rooms/{roomId}/account_data/{type}. Check order (everything before the room/membership lookup, so nothing leaks): {userId} != caller 403 M_FORBIDDEN (admins too); PUT of m.fully_read/m.push_rules 405 M_BAD_JSON (server managed, MUST, global and room scope; GET stays allowed); invalid roomId 400 M_INVALID_PARAM; body > limit 413 M_TOO_LARGE (the parser's RequestTooLargeError is mapped for these routes in Nebu.Web.Plugs.JsonBody); malformed / non-object body 400 M_NOT_JSON. Then membership: an unknown room and a room the user is not in both answer 403 M_FORBIDDEN (same response, no existence leak; Matrix does not require membership, but unrestricted writes would allow room-id probing and storage abuse). GET of an unset type is 404 M_NOT_FOUND (no fallback to the global value).

Limits (abuse protection): content body <= 65536 bytes (NEBU_ROOM_ACCOUNT_DATA_MAX_BYTES), at most 1000 rows per user (NEBU_ROOM_ACCOUNT_DATA_MAX_ROWS_PER_USER; a new row beyond the cap is refused with 403 M_RESOURCE_LIMIT_EXCEEDED, overwriting an existing row always works). The cap is enforced under a per-user advisory lock, so concurrent writers cannot overshoot it.

Incremental /sync per device (the position in the token)

Nebu's next_batch/since token is <core token>[.<room-account-data position>]. The core token is the opaque value the sync core stores per authenticated device since story sync-token-per-device (per user only for a device-less client, an unbound raw IdP JWT); a mismatching/stale core token triggers the documented fallback to a full initial sync. The decimal suffix is the highest stream_pos of the user's room account data that THIS client has received. Every insert/update of a row draws a new stream_pos from a sequence; /sync returns rows with stream_pos > since_pos for the rooms the user is joined to. Because the position lives in each client's own token (not in a server-side flag), any number of devices each receive every change exactly once; this also holds on the fallback-to-initial path (the position is taken from the client's token, not from the stored core token). A token WITHOUT suffix (issued before this feature) means position 0, i.e. the first sync after the upgrade re-delivers all room account data once. Writers hold a per-user advisory lock while drawing the position, so per user position order equals commit order and a reader that saw position N cannot miss a still-uncommitted smaller one. A room whose ONLY change is account data is added to rooms.join with empty timeline/state; rooms newly joined in a delta get ALL their account data. A PUT wakes the user's long-polling /sync ({:account_data_changed, room} via the user:<id> :pg group) and /sync skips long-polling when changes are already pending.

read_markers

POST /rooms/{roomId}/read_markers checks membership like /receipt (404 unknown room, 403 non-member), validates that an m.fully_read event belongs to the room (400 M_INVALID_PARAM) before writing anything, sends m.read/m.read.private through the same Server.send_receipt/2 path as /receipt, and stores m.fully_read as room account data {"event_id": ...} (delivered via /sync like any other entry).

Room tags (story room-tags)

GET /user/{userId}/rooms/{roomId}/tags, PUT/DELETE .../tags/{tag} are a thin layer over the room account data event m.tag ({"tags": {name: {"order": n, ...}}}); there is NO separate table. PUT/DELETE are an atomic read-modify-write: RoomAccountDataStore.update/4 reads the current event, applies a pure merge fun and writes the result in ONE transaction under the per-user advisory lock (same cap/size checks as put/4, fresh stream_pos, notify_sync), so concurrent tag writes of one user never lose each other (cluster-safe, ADR-020). A direct PUT .../account_data/m.tag stays allowed and replaces the whole event (last writer wins, never a corrupted event); a malformed stored m.tag (tags missing / not an object) is treated as {} for modification and unknown top-level keys are preserved. /sync needs no tag code: the single m.tag row per room is delivered like any room account data (exactly one m.tag event per room, per-device stream_pos, long-poll wake-up via notify_sync). A DELETE that changes nothing (unknown tag, room without row) writes nothing and creates no sync event; deleting the LAST tag leaves {"tags":{}} as the row (the store has no tombstone) so /sync delivers the change. The request logger treats the tag routes like account-data content (no body and no response capture, shared router-identical matcher RawBodyReader.room_tags_shape?/1); the tag NAME is user content and is redacted from the request log: RouteNormalizer replaces the segment after tags with :tag (in path and route_template), using the router-identical segment view (empty segments dropped, percent-decoded). Error behavior: 400 M_NOT_JSON for a tag PUT without / with a non-JSON Content-Type (stricter than the room account-data PUT, same as the global scope); 413 M_TOO_LARGE for a body or a merged m.tag event above 64 KiB; 403 M_RESOURCE_LIMIT_EXCEEDED for the row cap (NEW room rows only) and for the per-user total-bytes cap (4 MiB), which also refuses growth of an existing row. The 64 KiB per-event cap only applies when the event grows, so a tag can still be deleted after an operator lowered the cap.

Server-side key backup (story server-side-key-backup)

/_matrix/client/v3/room_keys/** (Matrix CS v1.18 "Server-side key backups") is an OPAQUE per-user store: auth_data and the session_data blobs are kept as opaque JSON text (the server re-encodes them canonically; it never interprets them), never decrypted and never parsed beyond well-formedness and size, and never logged (the RequestLogger captures no body of these routes; a failing write logs its error kind only). ADR-011 stays: this enables no E2EE room, m.room.encryption is still rejected. SQL is inline in the handler (ADR-018); the state lives only in room_key_backup_versions and room_key_backup_sessions (migration 000058, runtime-role grants, row level security enabled and forced), so count and etag are the same on every node (ADR-020).

  • Versions: a version is a decimal string, a per-user counter (the first version of every user is 1, strictly increasing; the session rows have a foreign key to their version without a cascade, so sessions are deleted before their version); the newest non-deleted one is the current one (after deleting it the newest remaining one, else 404 M_NOT_FOUND). DELETE /version/{version} deletes the info and all its keys at once and leaves a soft-delete marker (no auth_data), so a number is never reused and a repeated DELETE is idempotent (404 only for a number that never existed). At most 10 backup versions per user: the 11th is 403 M_FORBIDDEN (never M_LIMIT_EXCEEDED, which clients retry; nothing is deleted silently); deleted versions do not count.
  • Writes go to the current version only: any other version (older, deleted, unknown, another user's) is 403 M_WRONG_ROOM_KEYS_VERSION with the top-level current_version, or 404 M_NOT_FOUND when the user has no backup. Reads and deletes work on every own non-deleted version and answer 404 for anything else; another user's version looks unknown. The version query parameter is required (400 M_MISSING_PARAM). Missing members are M_MISSING_PARAM, mistyped ones M_INVALID_PARAM, malformed JSON M_NOT_JSON, a non-object body M_BAD_JSON; a request is stored whole or not at all.
  • Merge rule of a re-uploaded session (spec order): if is_verified differs the verified one is kept; else the lower first_message_index; if equal the lower forwarded_count; on a full tie the stored session is kept. A replace does not change count; etag changes whenever the stored set changes. The merge is decided in one SQL statement under the per-user lock, so concurrent uploads of one session never double count and the best one wins.
  • Limits: 100 000 sessions per version (403 M_FORBIDDEN for a batch that would pass it, as a whole; the seam :nebu, :room_keys_max_sessions_per_version), 8 KiB per session_data and 16 KiB per auth_data (canonical JSON bytes, 413 M_TOO_LARGE), 5 MiB per request body on these routes. The body is read only after the authentication: JsonBody leaves the bodies of these routes unfetched, the route authenticates first (an anonymous or junk-bearer request is 401 without its body being read, whatever its size), then charges the write rate limit (a 429 comes before the body is read), and only then parses the body with the 5 MiB limit; every other route keeps its 1 MB pre-router parse. Writes are rate limited per user (tier room_keys: 60 per minute, burst 5). The two bulk reads (GET /keys, GET /keys/{room}) have a bucket of their own (30 per minute, burst 3 per user, 429 M_LIMIT_EXCEEDED with retry_after_ms); GET /version and GET /version/{version} (a count(*) over up to 100 000 rows) have a light bucket of their own (120 per minute, burst 30 per user, shared by the two routes, separate from the bulk and the write buckets); the single-session GET is not limited (a divergence from the spec's "rate-limited": a restoring client reads one session per undecryptable event). All buckets and slots are node-local: in a cluster of N nodes the effective limit is N x the documented limit. The bulk response is spliced from the stored JSON text, not decoded and re-encoded per blob.
  • Storage quotas: a per-user aggregate byte budget over all live versions (for every stored session the octet_length of its room_id and its session_id plus a fixed 128 bytes per row plus the canonical bytes of its session_data, plus every live version's auth_data; default 64 MiB, seam :nebu, :room_keys_max_bytes_per_user; the check sums the user's rows on every write, O(scan), an O(1) counter column is a follow-up), decided under the advisory lock and net of replacements (a replace counts only the size delta), freed by deleting sessions or a version; exceeded is 403 M_FORBIDDEN and nothing is stored. The total version rows per user, soft-delete markers included, are capped at 1000 (seam :nebu, :room_keys_max_version_rows_per_user; the 1001st create is 403 M_FORBIDDEN): the markers are never pruned automatically (needed for never-reused numbers and the idempotent DELETE), their growth is bounded by that cap and accepted (a partial index on the live versions keeps the lookups cheap); an operator frees a locked-out user with the SQL of docs/compliance/gdpr-deletion-runbook.md ("Pruning old key backup version markers"), which keeps the highest-numbered marker.
  • Per-user lock (try-lock): every writer takes the sync_global lock with pg_try_advisory_xact_lock: a busy lock is 429 M_LIMIT_EXCEEDED with retry_after_ms at once (nothing stored), so no waiter ever holds one of the 10 pooled connections. Reads take no lock. SET LOCAL lock_timeout (seam :nebu, :room_keys_lock_timeout_ms, default 2000) stays only as the safety net of row locks (SQLSTATE 55P03 is 429 as well).
  • Bulk reads (memory): at most ONE bulk read in flight per user and at most TWO per node (seam :nebu, :room_keys_max_concurrent_bulk_reads_per_node, default 2): a monitor based slot (Nebu.Matrix.RoomKeysReadGate monitors the request process and frees the slot on DOWN, so a killed request frees it); the excess is 429 M_LIMIT_EXCEEDED with retry_after_ms, answered before any database work. The response is not streamed (follow-up); see 11-risks for the peak.
  • Logging: the route normalizer replaces the client-chosen room and session id segments of room_keys/keys/**.
  • Erasure: both tables are in UserDeletion.erased_tables/0 (sessions first, the soft-delete markers too); the creating writers (POST /version, PUT /keys) take the sync_global lock and refuse a user whose erasure is under way (403 M_USER_DEACTIVATED).

To-device messaging (story to-device-messaging)

PUT /_matrix/client/v3/sendToDevice/{eventType}/{txnId} and the to_device member of GET /sync (Matrix CS v1.18 "Send-to-Device messaging") are an OPAQUE per-device queue: the content of a message is never decrypted, interpreted or logged (the RequestLogger captures no body of the route and redacts the content of sync responses; the RouteNormalizer replaces the event type and the txn id of the path; a failing statement logs its error kind only). ADR-011 stays: this enables no E2EE room, m.room.encryption is still rejected. SQL is inline in the handler (ADR-018); the state lives only in to_device_messages (the queue; user_id is the recipient, sender, device_id, created_at) and to_device_txns (the idempotency records: user_id is the sender, device_id the sending device, event_type, txn_id) (migration 000059, runtime-role grants, row level security enabled and forced) in Nebu.Matrix.ToDeviceStore, so the queue, the idempotency and the acknowledgement position are the same on every node and survive a restart (ADR-020).

Errors. 400 M_NOT_JSON (malformed JSON), 400 M_BAD_JSON (messages missing or not an object, a user or device entry that is not an object, a body that is not an object), 400 M_INVALID_PARAM (an event type outside the allowlist of http_to_device.ex (story e2ee-compliance-channel-guard: exact match, checked first, before the rate limit; m.room.encrypted, m.room_key_request, m.room_key.withheld, m.secret.request, m.dummy, the eight m.key.verification.* types), a txn id of 0 or more than 255 bytes), 413 M_TOO_LARGE (a body above 64 KiB, more than 50 recipient entries; the request is refused whole, never truncated, and its txn id is not burnt), 429 M_LIMIT_EXCEEDED with retry_after_ms (the sender's rate limit, the byte-cost bucket, a transaction that is in flight, a statement over its 2 s timeout, a sender whose erasure holds its lock), 403 M_USER_DEACTIVATED (a sender whose erasure is under way), 500 M_UNKNOWN (generic text). Everything about WHO can be reached is a silent 200 {}.

Order of a request. Authentication, then the event-type allowlist (a type outside it is 400 M_INVALID_PARAM and costs no token, no bucket, no slot and no body read; story e2ee-compliance-channel-guard), then the per-user rate limit, and only THEN the body is read (the body is read only after the authentication, with a 64 KiB cap; JsonBody leaves the body of the route unfetched): an anonymous or junk-credential request is 401 without its body being read, an exhausted bucket is 429 before the body is parsed or validated (a request refused for its body or size still costs its token). The limit is 60 per minute, burst 30, per USER (own tier to_device, not per IP, a * request costs one token); the buckets are node-local, so in a cluster the effective limit is up to N x the documented limit. A SECOND bucket, the byte-cost bucket (4 MiB per minute, burst 8 MiB per sender, the same node-local N x rule), is charged AFTER the parse with the bytes the request ASKS to fan out (an explicit device: the content bytes plus 128; a *: what it could reach, at most the targets cap and never above the 1 MiB fan-out budget), never with the accepted bytes: an accepted-bytes cost would tell a delivered recipient from a skipped one by the 429, so the cost is state independent (no oracle). A refusal is 429 with retry_after_ms, the transaction is rolled back (its txn id is not burnt), a cost above the burst is charged as the burst (a request is never unsatisfiable).

Limits and budgets (every per-item cap has an aggregate cap). Per request: 64 KiB body, 50 recipient entries (* counts as one entry; an unknown or unreachable recipient counts too), at most 100 targets after the * expansion (silently truncated, deterministic). Per recipient device: 500 messages and 8 MiB. Per recipient user: 2000 messages and 32 MiB over all devices. Per sender and recipient device: 100 messages, per sender and recipient user: 200 messages and 4 MiB (one sender cannot fill another user's queue; beyond its share the sender's own oldest go first). Per sender and recipient device also 1 MiB (the count share of 100 stays). Per sender over ALL its recipients: a total budget of 4000 messages and 16 MiB (the sender's own oldest go first, whichever recipient they are queued for; another sender is unaffected; a soft cap: 16 MiB plus the in-flight fan-out, concurrent sends and rows locked by another transaction can leave the sender above it until its next send): without it one account could store its per-recipient shares times the number of users it shares a joined room with. The byte budgets count the canonical content bytes plus 128 per row. Per sending device: 1000 idempotency records (the oldest is forgotten). A sync response carries at most 100 messages and 1 MiB (the first message is always carried); the rest comes with the next sync without waiting for timeout. Messages are kept 7 days (the TTL is decided by the delivery itself, the purge job is housekeeping), idempotency records 24 hours. All are application settings :nebu, :to_device_<name> read at request time (ToDeviceStore.limits/0).

A full queue evicts the oldest. When a budget is exceeded the OLDEST messages of that device (or of the sender share) are evicted; at the DEVICE and at the USER level the eviction is fair: the oldest message of the sender with the largest share goes first, so flooding co-members (two colluding ones included) never evict the older messages of a sender below its shares from a victim device; the sender is never told (the same 200 {}, no 429: a 429 would reveal the recipient's state and let one sender's flood block the others), and the count is logged without content.

Recipients and no oracle. Recipients are the sender's own devices (no room needed) and the devices of users who share a room with the sender where BOTH sides are join (evaluated at send time, not at delivery); an invitation opens no send path (it needs no consent, and in-room verification does not use to-device messages). Every other recipient (no shared joined room, only invited, unknown user or device, a remote user, a malformed id, a deactivated user) is skipped silently: the answer is identical to a delivered one. * expands at send time to the devices that exist then (a device created later does not receive it); for the sender's own user the sending device is excluded; an explicit device entry of the same user wins over * (one copy per device). A deactivated user receives nothing new, its queue is kept (reversible).

Transaction ids. Scoped to (user, device, event type, txn id), persisted in to_device_txns and unique in the database, so a retry after a restart or on another node queues nothing again; the token is irrelevant (a new access token of the same device is the same transaction), another device, user, event type and the room send endpoint are independent transactions. A concurrent identical transaction is answered 429 at once (no waiting lock).

Acknowledgement on the NEXT token. A sync response only delivers: its messages are deleted when a LATER sync presents the since token of that response (never on delivery). The to-device position travels in the token (<core>.<position>[.<to-device position>], a device without traffic keeps its two-part token); a retry with an older token delivers the batch again, an initial sync, an unknown or garbage token and a token of another device (or user) acknowledge nothing and deliver everything pending. The acknowledgement is a position: two syncs of the same device in flight at once (a client does not do that) can acknowledge what the other response carried. An acknowledgement covers exactly the messages the responses stamped up to that position: a message that committed late (a lower queue position than a delivered one) is neither deleted by it nor missed. When the stamp of a response loses a race with an overlapping sync of the same since (it stamps nothing because the other one was faster), the response carries only what it stamped or what was stamped already and its position never covers a row it did not carry (it is the highest stamp of the rows it carried, else the since position), so the lost response of the other sync loses no message. to_device is always emitted, as {"events": []} when nothing is pending.

Wake-up. A new message wakes the long poll of exactly its target device (a {:to_device, device_id} message to the :pg group of the user, cluster wide; the other devices of the user keep waiting); a message already queued that the presented token does not cover makes the sync return at once, one it covers does not.

Concurrency bounds. A rate limit is not a concurrency limit: the rows of ONE request cost at most 1 MiB of fan-out (content + 128 per row; the targets beyond that are dropped silently like an unreachable one, the first target is always kept), so a 64 KiB content reaches about 16 devices (15 when the content fills the whole body); at most 4 sends per user and 6 per node are in flight (Nebu.Matrix.ToDeviceSendGate, settings to_device_max_concurrent_sends_per_user and to_device_max_concurrent_sends_per_node; monitor based slots freed on :DOWN, node-local like the rate buckets), a send that finds no slot is answered 429 before its body is read (6 per node: each running send holds a pooled connection for its statements, so the slots stay well below the pool). Every statement of a send and of a delivery runs under SET LOCAL statement_timeout of 2 s (over it: 429 for a send, nothing stored and the txn id not burnt (phase 1; a phase-2 timeout is only logged and the send answers 200); an empty to_device for a sync).

Concurrency. A request never waits for a lock while holding a pooled connection: the send takes the advisory lock to_device_erasure|<user> of the sender SHARED as a try-lock (the GDPR erasure takes that key exclusively; it is its OWN key, so a recipient's account data, cross-signing or key backup writer that holds sync_global|<user> never makes a message skip), the transaction has a short lock_timeout, a conflict is a 429; only the recipients that passed the shared-room join are locked (never one lock per requested id); the eviction, the acknowledgement and the stamp take FOR UPDATE SKIP LOCKED (a row another request holds is skipped, never waited for), and a delivery error (a lock, a statement timeout) never fails /sync: the answer is 200 with to_device: {"events": []}, the position is kept (nothing acknowledged or lost), the log names the kind only; a send takes FOR KEY SHARE on the target devices rows only around its INSERT (the soft eviction runs afterwards in a second, separate transaction without any lock on devices; a failed eviction never fails the send and the queues catch up with the next send) and a device delete locks the device row first, so a send racing a device delete never leaves rows for a recreated device of the same id; a send is a constant number of set-based statements however many recipients it names.

Lifecycle. A device removal (DELETE /devices, /delete_devices, logout, logout-all, the Admin UI revoke, the OAuth logout) deletes the queue and the idempotency records of the device (DeviceKeyStore.delete_device, one place); revoking only the sessions of a device (the device and its keys stay) does not purge its queue, an admin deactivation keeps it (reversible), the messages a removed device SENT stay in the recipients' queues. The GDPR erasure deletes the queue and records of the user and the messages the user sent. The TTL job Nebu.Workers.ToDevicePurgeWorker is an Oban cron job (the leader enqueues, the purge is batched atomic SQL with FOR UPDATE SKIP LOCKED that concurrent runs split between them), no node-local state.

Client compatibility. Verification and key requests work between your own devices and with any user you share a joined room with (an invitation does not count); a user you share no joined room with cannot be reached. Element and FluffyChat verify other users in the room, not through to-device messages, so the restriction costs them nothing.

Device-key directory (story device-key-directory)

POST /_matrix/client/v3/keys/claim, GET /_matrix/client/v3/keys/changes and the E2E members of GET /sync (device_one_time_keys_count, device_unused_fallback_key_types, device_lists; Matrix CS v1.18) complete the key directory next to /keys/upload and /keys/query. They serve public key material and device-list metadata only: ADR-011 stays (no E2EE room is enabled, m.room.encryption is still rejected). Modules: Nebu.Matrix.HttpKeyDirectory, Nebu.Matrix.KeyClaimStore, Nebu.Matrix.DeviceListStore (SQL inline per ADR-018, no domain model); state is PostgreSQL only (ADR-020, cluster safe): the table device_list_changes (migration 000060) and the existing device_one_time_keys / device_fallback_keys.

Claim. Exactly once: ONE set based statement takes the oldest unclaimed one-time key of every target with UPDATE ... WHERE ctid = ANY(...) over a FOR UPDATE SKIP LOCKED selection (a key another claim holds is skipped, never waited for, never handed out twice; a SELECT followed by an UPDATE would hand a key out twice); keys go out in upload order (created_at); the stored key object is returned verbatim. No one-time key left: the fallback key of that algorithm is returned, NOT consumed but marked used (its algorithm leaves device_unused_fallback_key_types of THAT device). The fallback key is stored with its key id (device_fallback_keys.key_id, <algorithm>:fallback for a row from before the migration). The answer is {"one_time_keys": ..., "failures": {}}: a target that yields nothing is LEFT OUT and failures is always empty (the spec keys it by server name, Nebu has no federation); timeout is ignored. Errors: 400 M_NOT_JSON, 400 M_BAD_JSON (one_time_keys missing or not an object, an entry that is not an object, an algorithm that is not a string), 400 M_INVALID_PARAM (more than 1000 targets, before any bucket is charged), 429 M_LIMIT_EXCEEDED with retry_after_ms.

Restriction and no oracle. The caller may claim its own devices and the devices of users who share a room with it where BOTH sides are join (the rule of sendToDevice), evaluated at request time inside the statement; an invitation opens no path; a deactivated or erased user is neither caller nor target. This is stricter than the spec (a claim consumes a key). Unknown users and devices, keyless devices, users without a shared room, invite-only shares, remote users and malformed ids are answered BYTE-IDENTICALLY and run the SAME number of statements (one claim statement, no early return, no statement per target), pinned by counting.

Rate limits. Token buckets through Nebu.Web.Plugs.RateLimit.take_all/1, checked all-or-nothing BEFORE the claim statement: per caller 30 requests per minute, burst 10 (requests, not targets), per caller 600 targets per minute, burst 1000 (the number of syntactically valid targets of the request, never their state; a cost above the burst is charged as the burst), and per (caller, target user, target device) 10 per hour, burst 5, for one-time and fallback claims alike and charged for EVERY valid target whatever its state (a limit that depended on the target would be an existence oracle). Any bucket over its limit: 429 M_LIMIT_EXCEEDED for the whole request, nothing consumed, nothing charged. The buckets are node-local: with N nodes up to N x the documented limit (07-deployment); the per-target buckets are keyed by a SHA-256 digest of the caller chosen ids (fixed size, never a sub-binary of the request body) and are charged in a second stage, only after the two caller buckets paid (a request refused in the first stage creates no per-target row); GET /keys/changes has its own per-user tier keys_changes (60 per minute, burst 20, charged before any database work) (11-risks).

Changes. GET /keys/changes?from=&to=: both are sync next_batch tokens; 400 M_MISSING_PARAM when one is missing or empty, 400 M_INVALID_PARAM when one is malformed; a well-formed token without a device-list position (an "expired" token) is answered conservatively: every user the caller shares a joined room with is changed. changed and left are always lists. A user is reported only through the caller's OWN shared rooms (both sides join) or when it is the caller itself: key changes (identity keys new or different, a device with keys removed or renamed, cross-signing keys), users who newly share a joined room, left = users who share no joined room any more after a leave in a room both were in.

The stream. device_list_changes (user_id, room_id, kind, pos) keeps the LATEST change per (user, room): kind keys (written by DeviceKeyStore and CrossSigningKeyStore.put_keys/2), join / leave (a trigger on room_members, so every writer is covered). pos comes from sync_global_pos_seq and is assigned AT COMMIT by a deferred constraint trigger under one global advisory lock: visible positions are commit ordered, so a reader that has seen position M never misses a change that was written earlier and commits later. The position is the LAST group of the sync token: <core>.<account data pos>[.<to-device pos>].d<device-list pos>; every response carries it, an incremental answer carries max(presented position, bound) where the bound is the highest visible stream position read BEFORE the two stream reads (so a quiet device's token advances and its next sync does not rescan old rows), and a token without the group is "unknown" (conservative answer). A rolling deploy is safe: an old node does not recognise the group and answers an initial sync, old tokens keep working. A forged position can only hide the caller's own future lists.

Sync members. device_one_time_keys_count is per device and ALWAYS contains signed_curve25519 (0 when none); device_unused_fallback_key_types is ALWAYS a list and PER DEVICE (it was a user aggregate: a second device's used fallback key was masked by the first device's unused one); a device-less request (an unbound raw IdP JWT) keeps the legacy per-user aggregates. device_lists (changed, left, both lists): an incremental sync lists the changes above, the caller itself for its own devices; an initial sync lists no peer; a fallback of a request that carried a since lists every shared user. A pending change ends a long poll at once; a change that arrives during a long poll is delivered with the next response (no wake-up).

Logging and erasure. Key material, tokens, claim bodies and the sync tokens of from / to are never logged (the request logger captures no body of keys/claim and keys/upload, only parameter names of keys/changes; a failing statement logs its kind only). device_list_changes is in UserDeletion.erased_tables/0 (the runbook sweep names it).

Key query hardening (story key-query-hardening)

POST /_matrix/client/v3/keys/query and POST /_matrix/client/v3/keys/signatures/upload (Matrix CS v1.18 "Cross-signing", REQ-2026-014) complete the key directory. They serve and store public key material and signatures only: ADR-011 stays (no E2EE room is enabled, m.room.encryption is still rejected). Modules: Nebu.Matrix.HttpKeyQuery (HTTP side), Nebu.Matrix.KeyQueryStore (the read), Nebu.Matrix.KeySignatureStore (who may sign what, verification, writes); SQL inline per ADR-018, state is PostgreSQL only (ADR-020): the new table cross_signing_user_signatures (migration 000061) next to cross_signing_keys and device_keys.

Visibility of a query (D1). The caller V sees the device keys, the master key and the self-signing key of a requested user X only when X is V, or V and X share a room in which BOTH are currently joined (room_members.left_at IS NULL on both sides: the predicate of keys/claim and of the device-list stream). A pending invitation, a left room, a room in which only one side is joined, an unknown user, a keyless user, a remote user and a malformed user id are all left out, and the answer is byte-identical for all of them: ONE statement (the visibility join is part of it) serves every request, there is no early return per user and no state-dependent status, and failures is always {} (it is keyed by server name, Nebu has no federation). The user-signing key is returned to its owner only, and only when the owner queried itself (D2). unsigned.device_display_name comes from the devices table, so a rename shows in the next query (D3). More than 1000 queried users are a 400 M_INVALID_PARAM; ids with a NUL byte name nobody and are dropped before the statement. Residual: Element's DM creation pre-check queries invitees (not yet joined), finds no keys and creates the DM unencrypted (harmless while rooms stay unencrypted); allowing a pending invitation is an owner option for later.

Signature visibility (D4). A cross-user signature (the signature user V makes with its user-signing key over the master key of another user X) is stored apart from the key object, one row per (signer, target user, target key id) in cross_signing_user_signatures, replaced idempotently, so the table is bounded by signers x the users they share a room with. The viewer V sees on the master key of X the signatures made by X's own keys (the self signatures, the signatures of X's devices; a signature by any other user stored inside the key object is not shown) and the signatures made by V, never a signature by a third user; X himself does not see the signatures made on his key by others. A stored row is shown only while its target key is a key of the CURRENT master key. When a user replaces the master key, the rows of the old key are deleted in the same transaction as the key write (CrossSigningKeyStore.put_keys/2, D7; re-uploading an unchanged master key keeps them).

Who may sign what (D5). Per entry of keys/signatures/upload: (a) the caller's own device key signed by the caller's self-signing key (merged into the stored device key object), (b) the caller's own master key signed by one of the caller's devices (merged into the stored master key object), (c) the master key of ANOTHER user (the object sits under the other user's id) signed by the caller's user-signing key, only when both are joined in a shared room. Everything else is M_FORBIDDEN for that entry: a foreign device, a foreign self-signing or user-signing key, the caller's own self-signing or user-signing key through this endpoint, a foreign master key without a shared joined room. The sharing check and the lookup of the target are ONE statement and run before anything of the target is examined, so an unknown user, a user who shares no room and a shared user whose key is not the named master key get the identical failure (no existence oracle).

Verification (D6). Every signature is verified with Ed25519 (:crypto.verify(:eddsa, ...)): the message is the canonical JSON (Nebu.Matrix.CanonicalJson.encode_strict/1: UTF-8, keys sorted by code point, no whitespace, integers only in [-(2^53)+1, 2^53-1], no floats) of the submitted object WITHOUT signatures AND WITHOUT unsigned; the signature is the unpadded base64 of 64 bytes; the verifying key (32 bytes, unpadded base64) is the one the caller stored under the key id named by the signature (signatures[caller]["ed25519:<id>"]): the self-signing key for (a), the ed25519 key of one of the caller's devices for (b), the user-signing key for (c). A valid signature by another key of the caller is not enough. The submitted object must also equal the stored key object apart from signatures and unsigned. A bad signature, an unknown signing key id or a changed object is M_INVALID_SIGNATURE; a non-object entry, a malformed signatures member (not a map of maps), no signature by the caller or more than 4 signatures by the caller on one object is M_INVALID_PARAM; a body that is not a JSON object is 400 M_NOT_JSON / M_BAD_JSON. Failures are always inside HTTP 200 {"failures": {user: {key_id: {errcode, error}}}} with fixed texts (nothing of the request is echoed); valid entries of the same request still apply (partial success), nothing is stored for a failed entry. Only the signatures of the caller are ever stored. The verification runs before any lock is taken; the write re-reads the target under its lock and compares the object again, so a concurrent key replacement is never overwritten.

Limits (D8). At most 200 signed objects per upload request (400 M_INVALID_PARAM before any verification or database work; the body-size cap of the raw body reader applies), a per-user rate tier keys_signatures (60 per minute, burst 20, node-local like every tier) in front of the handler (keyed by the authenticated user id, never by a term of the request body), at most 1000 queried users per keys/query.

Locks. Order of the device-key directory (devices -> device_keys -> stream row) and of the cross-signing writers. (a) takes the device_keys row FOR UPDATE, then the stream stamp; (b) is CrossSigningKeyStore.merge_signatures/4 (row, advisory lock, read); (c) takes the per-user advisory locks sync_global|<user> of the signer and of the target (sorted by user id, so two users who sign each other cannot cycle) and writes the row: they are the fence of the erasure (it holds the advisory lock of the erased user until it commits, so an insert either lands before its DELETE or waits and finds deletion_status set and stores nothing) and they serialise the insert with a master key replacement of the target, which holds the same lock. Consequence (Kassandra review, L3): a signer who signs the master key of a peer takes the PEER'S sync_global|<user> advisory lock too (not only its own), so a peer's key write or erasure can wait for a signer's short transaction and vice versa; the two locks are always taken in sorted order (the same order in every writer), so no deadlock cycle exists. Binding of the row (L1): a stored cross-user signature carries the public key value of the signed key next to its id, and both the drop in put_keys/2 and the visibility join compare the pair (id AND key), not the id alone.

Device-list stream (D10). A stored cross-user signature stamps the SIGNER's own key row in device_list_changes (the signer's other devices see the change in device_lists.changed and re-query), not the target (the signature is invisible to everybody else); an identical signature stored again changes nothing and stamps nothing.

Logging and erasure (D9, D11). The request logger captures no body of keys/query and keys/signatures/upload (and none of the answers), no key id, object or signature is part of an error text or a log line (a failing statement logs its kind only). cross_signing_user_signatures is in UserDeletion.erased_tables/0: user_id is the SIGNER (the signatures the erased user made are deleted by user_id), and one more statement deletes the signatures made ON the keys of the erased user (target_user_id); the runbook counts and sweeps both directions.

GDPR erasure inventory (story gdpr-erasure-global-data)

GDPR erasure (Nebu.Compliance.UserDeletion.delete_user_keys/3) physically deletes every row of personal data that is stored under the user in a per-user table, in the SAME transaction, in this order: the deletion_in_progress marker, the private keys set to NULL, the row locks and the advisory lock (below), the per-user deletes (then a check that no oauth_codes row is left), the profile anonymisation, and keys_deleted; one failing step rolls everything back and writes the user_keys_deletion_attempted audit row, whose error_detail names the failing step (step=<name>, a code atom, never data). Lock order: the transaction first locks the user's uia_sessions rows and cross_signing_keys row (FOR UPDATE) and only THEN takes the per-user sync_global advisory lock of AccountDataStore.put/3, CrossSigningKeyStore.put_keys/2 and the signature merge: the order of the cross-signing reset writer (uia row, cross-signing row, advisory lock), so a writer in flight is serialised with the erasure and the two do not deadlock (one narrow exception, below). Those three writers refuse a user whose deletion_status is set ({:error, :user_erased}, answered 403 M_USER_DEACTIVATED). The key backup writers RoomKeyBackupStore.create_version/3 and RoomKeyBackupStore.put_sessions/3 take the same per-user sync_global lock and refuse an erased user the same way (they run after the lock, so a write in flight is serialised with the erasure; the lock is a try-lock, a busy lock is a 429, see "Server-side key backup"). The cross-signing writers (both branches of device_signing/upload and the signature merge) decide on the stored keys only through CrossSigningKeyStore.lock_keys/1, in the same order: the cross_signing_keys row lock, then the sync_global advisory lock, and only THEN the read (row -> advisory -> read; story cross-signing-upload-races), so two concurrent first uploads store one key set and a signature merge never writes back a stale merge base. The exception is the absent-then-present window (accepted residual): when the row lock of a signature merge found no row and a first upload commits the row before the merge gets the advisory lock, an erasure that locks the new row and waits for the advisory lock can form a cycle with the merge's write, which only now waits for that row; PostgreSQL aborts one side (40P01: the erasure fails closed, or the merge answers 500). A first upload has the same window when the row committed in between has a NULL master (a partial upload): its read sees no master and its upsert takes the row lock only after the advisory lock (pre-existing; the erasure fails closed, or the upload answers 500). Three actors of one user within milliseconds; closing it would need the advisory lock first everywhere, which the story rejected. Events authored by the user are retained (room history preservation, recital 65; immutable signed event log, the sender is the pseudonym); the profile is anonymised (displayname = 'Deleted User', avatar_url = NULL, users.anonymized_at); admin deactivation keeps all of it (reversible). A second erasure answers :conflict and changes nothing.

The erasure is terminal for authentication. The last step also sets users.is_active = false and NULLs the users PII columns (display name, avatar and email ciphertexts: the display name and avatar are encrypted with the server-wide key, so the key deletion does not shred them; also last_seen_at and deactivation_reason, admin free text that may describe the person). Every authentication gate (TokenValidator, OAuthSessionStore.user_active/1, Nebu.Matrix.active?/1, the account pages, invitable_local_users) treats deletion_status IS NOT NULL as deactivated, so neither a fresh IdP token, an SSO login, an OAuth authorization nor an admin reactivation revives the account, and Profile.DB.upsert_profile/3 writes nothing for an erased user. A bearer (an unbound raw IdP JWT included) that the node-local provisioning cache of the OIDC plug (60 s TTL) already confirmed is still accepted on that node until the entry expires: new requests, not only one in flight, for up to 60 s, the same bound as for an admin deactivation. In that window it can still write the tables WITHOUT a guard (device keys, receipts, room account data, the lazy device row with its IP); the profile PUT, global account data, cross-signing, the signature merge and the sync persist are refused. The eviction of the cache entry on every node (a broadcast over :pg on erasure and deactivation) is the proper fix, recorded in follow-up gn. Admin UI access of an erased administrator follows the IdP admin group claim, not the erasure: remove the person from the IdP admin group. Nebu.Admin.DB.set_is_active/2 refuses to reactivate an erased user (:erased, answered as a failed precondition) and the admin user suggestions never offer one.

Erased by the transaction (UserDeletion.erased_tables/0, the list the deleting code and the guard test share): user_room_account_data, account_data, cross_signing_keys, device_keys, device_one_time_keys, device_fallback_keys, forgotten_rooms, auto_join_opt_outs, read_receipts, uia_sessions, oauth_account_sessions, oauth_consent_requests, oauth_consent_grants, oauth_codes, room_key_backup_sessions, room_key_backup_versions (the server-side key backup of story server-side-key-backup, migration 000058: every version row, the soft-delete markers included, and every session row). device_list_changes (the device-list change stream of story device-key-directory, migration 000060: the key changes and the membership changes of the user, deleted by user_id). cross_signing_user_signatures (the cross-user signatures of story key-query-hardening, migration 000061: the signatures the user MADE as signer are deleted by user_id, the signatures made ON the user's master key by other users by one more statement on target_user_id). to_device_messages, to_device_txns (the to-device queue and its idempotency records of story to-device-messaging, migration 000059: every message queued FOR the user, every record, and, by one more statement on sender, every message the user SENT that waits in another user's queue). ToDeviceStore.send takes its OWN fence key to_device_erasure|<user> (the erasure takes it exclusively inside the sync_global writers' lock set; it is not the sync_global key itself, so those writers do not make a message skip): it takes the user's lock SHARED as a try-lock (a busy lock is a 429 for the sender and a silent skip for a recipient) and refuses an erased sender with 403 M_USER_DEACTIVATED, so a send in flight never re-creates rows after the erasure. (oauth_codes needs the delete policy of migration 000055.)

Ended after the commit by the session teardown (best effort, outside the transaction): devices, matrix_sessions, jwt_device_bindings, sessions, oauth_idp_refresh_leases, sync_tokens, sync_room_cursors (the dispatcher also removes the devices rows of legacy devices).

Retained on purpose or anonymised, with the reason:

  • user_keys: public keys stay: they keep the signatures of retained events verifiable (private key NULL, deleted_at set).
  • users: the pseudonymous identity row referenced by events, membership and foreign keys; retained on purpose, but the PII columns (display name, avatar and email ciphertexts, last_seen_at, deactivation_reason) are set to NULL (the ciphertexts are not crypto-shredded: the display name and avatar are encrypted with the server-wide key), is_active = false, deletion_status / anonymized_at / deactivated_at set. Column residuals that stay: personal_room_id (a pointer to a retained room); the user's m.room.member events carry displayname / avatar_url in events.content (retained event log).
  • profiles: the row stays and is ANONYMISED (displayname = 'Deleted User', avatar_url = NULL), not deleted: the foreign key and /profile of an erased user.
  • room_members: mirror of retained membership events (room history preservation, recital 65); the user id is the pseudonym; retained on purpose.
  • room_invitations: mirror of retained invite events (inviter_id, invitee_id are pseudonymous user ids); retained on purpose.
  • rooms: creator_user_id of a room whose signed events are retained; retained on purpose.
  • events: the immutable signed event log: events authored by the user are retained, not redacted (sender is the pseudonym); room history preservation.
  • media_files: uploader_user_id of media referenced by retained events, lifecycle per ADR-030 (media erasure is a separate topic); retained on purpose. This reason does not fit an avatar that no event references: the former avatar object stays downloadable, open follow-up gm.
  • media_state_transitions: actor_id of the media lifecycle: accountability record (ADR-030); retained on purpose.
  • audit_log: accountability and legal obligation: append-only, retained 7 years; retained on purpose.
  • compliance_requests: requester / approver of compliance accesses: legal obligation; retained on purpose.
  • oauth_clients: approved_by / rejected_by / revoked_by of the admin decisions on a client registration: admin accountability record, not data of the erased user; retained on purpose.
  • war_room_sessions: started_by of admin war-room sessions: admin accountability record; retained on purpose.
  • admin_temporary_captures: started_by of admin captures: admin accountability record; retained on purpose.
  • role_overrides: dormant table (no writer in server/lib), user_id / granted_by of admin role overrides; retained, kept visible for the guard.
  • push_rules: dormant: FORCE RLS table without a writer in server/lib, no row can exist; retained / allow-listed.
  • pushers: dormant: FORCE RLS table without a writer in server/lib, no row can exist; retained / allow-listed.
  • notifications: dormant: FORCE RLS table without a writer in server/lib, no row can exist; retained / allow-listed.
  • room_account_data: dormant legacy table without a writer in server/lib (superseded by user_room_account_data); retained / allow-listed.
  • admin_sessions: dormant: no writer in server/lib, no row can exist; retained / allow-listed.
  • message_buffer: dormant: no writer in server/lib (sender column), no row can exist; retained / allow-listed.

Other users' rows that merely mention the erased user (a DM entry in someone else's m.direct, a signature inside another user's cross-signing key object) are those users' data and stay.

Recurrence guard. user_deletion_schema_guard_test.exs enumerates every table with a user_id, *_user_id, sender-like column or a foreign key to users and fails, naming the table, when it is neither in UserDeletion.erased_tables/0, nor ended by the session teardown, nor retained on purpose with a reason: a new migration with per-user data breaks the build until its author decides.

Users erased before this release keep the rows the old erasure did not delete (and their users PII ciphertexts, and stay active): a one-off, idempotent operator sweep (SQL in the release note of 07-deployment.md and in docs/compliance/gdpr-deletion-runbook.md, built from the same table list) deletes them, also deletes the session teardown tables, NULLs the PII columns and sets is_active = false. The sweep is also the retry path of an erasure whose best-effort post-commit teardown failed. Erased data persists in backups within their retention (runbook).

Erasure / deactivation, known limitations

GDPR erasure (Nebu.Compliance.UserDeletion.delete_user_keys/3) deletes the user's room account data in its transaction; admin deactivation does NOT (reversible); deleting a room cascades via FK. Known, not fixed here: (a) FIXED by story gdpr-erasure-global-data (follow-up ck): erasure deletes the global account_data rows too (see the inventory above); (b) FIXED by story sync-token-per-device: device-bound clients receive global changes by position, per device; only device-less requests (an unbound raw IdP JWT) still share the consume-once per-USER changed flag (follow-up cj (c)); (c) the catch-all answers 404 M_NOT_FOUND instead of M_UNRECOGNIZED; (d) a room dropped from an initial sync by the 10 s room-fetch timeout does not get its account data until the entry changes again; (e) rooms.leave.*.account_data and sync filters for account data are not implemented; (f) a request of the erased user that was already authenticated when the erasure committed can still write rows of tables the erasure guards do not cover (device keys, read receipts, room account data, ...): only the two account data / cross-signing stores, the signature merge, the profile upsert and the initial-sync persist refuse an erased user. The window is up to 60 s on a node whose provisioning cache already confirmed the user (an unbound raw IdP JWT is not ended by the teardown: new requests are accepted until the entry expires, the same bound as an admin deactivation); for a Nebu-minted token it is the one request in flight. The one-off sweep of the deployment note, which also covers the teardown tables, can be run again; the cache eviction broadcast is follow-up gn.

Device Identity vs Device Keys (Story s1-device-registry-model)

A device is a first-class entity, modelled separately from the cryptographic key blob a client may publish. Two tables back this split, both keyed on (user_id, device_id):

TableMigrationHoldsA row exists when
devices000010Device IDENTITY + management metadata (display_name, last_seen_ts, last_seen_ip, created_at, updated_at)The device is known to the server — even with no published keys (keyless device)
device_keys000009The signed device-key blob (device_keys_json) + OTK/fallback keys (in sibling tables)The client uploaded keys via POST /keys/upload

Design invariants:

  • Device existence is independent of keys. Nebu.Matrix.DeviceKeyStore.put_device_keys/4 upserts the devices row FIRST, then the device_keys row (the two writes are deliberately NOT in a transaction: writing devices first means the only intermediate state after a crash between them is a valid keyless device, which is permitted; the reverse order could leave a device_keys row with no devices row).
  • GET /devices / GET /devices/{deviceId} read from devices (list_devices/1, get_device/2, sole_device_id/1), so a keyless device is listed and manageable.
  • POST /keys/query reads the JOIN (Nebu.Matrix.KeyQueryStore's own visibility-filtered statement INNER-JOINs devices + device_keys; DeviceKeyStore.query_devices/1 is the same join without the visibility filter and not a client reader), so it returns ONLY key-bearing devices — Matrix /keys/query semantics are unchanged. The JOIN can never hide a current key-bearing device because every device_keys row is guaranteed a devices row (the 000010 backfill + the put-order invariant above).
  • Backfill of a shipped schema. Migration 000009 reached the operator's Stackit deployment and is immutable; 000010 is a NEW forward migration that backfills devices from existing device_keys rows (last_seen_ip has no source there → NULL) and is idempotent (IF NOT EXISTS / ON CONFLICT DO NOTHING).

This split is the foundation of the device-management epic (S2 session-driven device existence, S3, S4). It is orthogonal to E2EE (ADR-011): persisting device identity encrypts nothing. PostgreSQL-backed and cluster-correct (ADR-020); inline SQL with no schema/repository layer (ADR-018). No new ADR.

Login owns the device lifecycle; the acting device is server-authoritative (Story s2-device-lifecycle-at-login)

S2 makes login the single owner of device creation and makes the acting device server-authoritative on every authenticated request:

  • Login owns device creation. Nebu.Matrix.HttpPublic honours a client-supplied device_id (reuse — Matrix permits it) or generates one, then upserts the devices row (display_name taken from initial_device_display_name on first sight only; last_seen refreshed on every login). For a raw-JWT session it additionally persists the token→device binding via Nebu.Matrix.Session.DeviceBinding (opaque sessions already carry the device in matrix_sessions.device_id). The binding write is best-effort and never crashes login.
  • The acting device is read from the token, never the request body. OidcPlug assigns conn.assigns[:device_id] for ALL authenticated sessions from the token's server-side binding (opaque → matrix_sessions, raw-JWT → DeviceBinding). Handlers read the device from there.
  • POST /keys/upload rejects cross-device transfer. The device a key write is attributed to is taken SOLELY from conn.assigns[:device_id]. A body device_keys.device_id that contradicts the bound device is rejected with 403 M_FORBIDDEN before any write — a token bound to device A can never publish keys for device B (the prior sole_device_id/1 heuristic is removed). With no bound device and no body device_id the upload is a no-op (empty counts, no DB touch).
  • GET /sync performs a best-effort, throttled (60s) update of devices.last_seen_ts / last_seen_ip.

Security property: the device a request acts as is server-authoritative — derived from the token's server-side binding, never from attacker-controllable request input — which prevents cross-device session/token transfer. The binding stores the access token only as a SHA-256 hash (no cleartext bearer token at rest). Cluster-correct (PostgreSQL is the source of truth, per ADR-020); inline SQL (ADR-018). Still orthogonal to E2EE (ADR-011) — the device-key directory is a public-key directory, not encryption. No new ADR.

Default display name of OAuth devices (story oauth-device-default-display-name)

An OAuth login creates its device at the token endpoint, and most clients never name it (PUT /devices/{id} is optional), so users saw many unnamed devices. The token endpoint therefore gives a new device a default display name, "<base> (<suffix>)", for example FluffyChat (ABCD1234). Nebu.OAuth.DeviceName (OAuth domain) builds it and Nebu.Matrix.oauth_issue_session/1 carries it to OAuthSessionStore.insert_replacing_device/1 (ADR-020), which writes it in the issuance transaction with UPDATE devices SET display_name = $3 WHERE user_id = $1 AND device_id = $2 AND display_name IS NULL.

  • Never overwrites. The default fills an unset name only: a name the client chose (PUT /devices/{id}), a legacy login's initial_device_display_name, a re-login of the same device id and a refresh all keep what is stored. The client's own PUT after the login replaces the default. An empty string is a name the client chose and is kept; a device that exists with no name (NULL) is named at its next OAuth login. Existing devices are not renamed in bulk (no backfill). devices gets no schema change; the typed name (below) travels in oauth_codes.device_display_name (migration 000062).
  • Base. The client_name of the approved client record, sanitised: markup tags and angle brackets removed, control characters, format characters (bidi overrides, zero width), line and paragraph separators dropped, whitespace collapsed, bounded to 64 code points. A revoked, pending, rejected or unknown client, a missing or generic name (oauth client, client, app, application, unknown, test) or an unreadable database fall through to the next source of the chain below; the plain OAuth client remains only when nothing else is usable. The name is plain text: a client that renders it as HTML must escape it, like any display name.
  • Name chain (story oauth-pages-redesign/01-device-naming-chain, REQ-2026-016). Source order, first usable wins: (1) the approved client's sanitised client_name unless generic; (2) the https host of its client_uri (lower case, no port), named <host> (<suffix>); (3) the name the user TYPED on the consent page, stored exactly as typed after sanitising, WITHOUT suffix. The consent page asks for a name only when (1) and (2) yield nothing and the device carries no name; a refused Allow answers 422 and consumes nothing. A client's own PUT /devices/{id} still replaces the result later. The name is never logged, and it is HTML-escaped wherever rendered (Allow page, device lists, admin user page, account portal).
  • Suffix. The last 8 characters of the device id after dropping every character outside [A-Za-z0-9._-] (a device id may legally contain markup characters), so two logins of one client show two distinguishable names; with nothing left, 8 hex characters of the SHA-256 of the id.
  • Unchanged. Legacy and SSO logins (POST /login) keep initial_device_display_name (on first creation only) or no name. The new code does not log it (the debug request logger still captures the GET /devices response body, which shows device names; see docs/stories/oauth-session-and-consent/follow-ups.md), it carries nothing of the user and lives in devices.display_name, so the GDPR erasure needs no change.

Device management endpoints, UIA step-up, and the per-device JWT kill-switch (Story s3-matrix-device-endpoints)

S3 adds the WRITE side of Matrix device management — PUT /_matrix/client/v3/devices/{deviceId} (rename), DELETE /_matrix/client/v3/devices/{deviceId}, and POST /_matrix/client/v3/delete_devices (bulk, capped at ≤100 target devices) — all owner-scoped (a user can only touch their own devices). Delete and bulk-delete are gated by User-Interactive Authentication (UIA); rename is not.

UIA fresh-auth step-up via m.login.sso (Option B). Nebu is OIDC-only, so the single UIA stage is m.login.sso (CS API §"SSO during User-Interactive Authentication"). The gate is Option B: a recent fresh login satisfies UIA directly. Nebu.Matrix.Uia compares now - matrix_sessions.auth_time against a configurable window (NEBU_UIA_FRESH_AUTH_SECONDS, default 900s / 15 min); within the window the write proceeds, otherwise the caller is challenged (401 with a m.login.sso flow + a server-issued session id). The challenged client completes a fresh SSO login out-of-band via GET /_matrix/client/v3/auth/m.login.sso/fallback/web (unauthenticated by spec) and re-submits.

  • auth_time tracking. Set at login from the id_token's auth_time/iat (unix seconds, migration 000012). A token refresh copies it verbatim (MatrixSessionStore.refresh_auth_time/2 is only called on a fresh SSO completion, never on rotation), so refreshing a token can NOT reset freshness or defeat the gate. A session with no auth_time is always stale → challenge.
  • UIA session store (DB-backed, ADR-020). A uia_sessions row is server-issued (256-bit random session_id) and bound to (user_id, op, params) (params = the canonical-JSON sorted device-id list). It is single-use: consume/4 flips consumed = TRUE WHERE consumed = FALSE atomically, so a replay loses the race and is rejected. The store is in PostgreSQL — not node-local ETS — because mint (challenged request) → complete (SSO callback) → consume (re-submit) can each land on a different node.
  • Identity-bound completion (security, H-1). Uia.complete/2 marks the m.login.sso stage complete ONLY when the freshly-verified sub (the Matrix user derived from the verified id_token) equals the UIA session's bound user_id — enforced in SQL by AND user_id = $authenticated_user. Without this, a holder of a victim's open access token could complete the fallback with their OWN IdP account and satisfy the step-up for the victim's pending sensitive op. A mismatch matches 0 rows and returns :error (no stage completion, no auth_time refresh).

Per-device JWT kill-switch (F-1). Deleting a device (single or bulk) denylists every JWT bound to that (user_id, device_id) so an in-flight bearer token cannot outlive the device. The handler enumerates jwt_device_bindings rows for the pair (served by idx_jwt_device_bindings_user_device) and calls Nebu.Web.TokenDenylist.revoke_by_hash/2 for each, keyed by the binding's token_hash until that token's stored exp. The hash is the same SHA-256(token) lowercase-hex digest OidcPlug.revoked?/2 computes from an incoming token and that DeviceBinding stores — so the next request carrying the bound JWT resolves as revoked and is rejected 401 M_UNKNOWN_TOKEN. Bindings for the deleted device are then removed. The companion Nebu.Matrix.Session.Sweeper (hourly, supervised) evicts jwt_device_bindings rows whose exp has passed and expired uia_sessions rows.

device_id ingress validation (F-2). Nebu.Matrix.DeviceId.valid?/1 rejects a client-supplied device_id longer than 255 bytes or containing control characters (\x00–\x1f, \x7f) at the two ingress points that accept one — POST /login and POST /keys/upload — so a device_id can never inject log/header/path artifacts downstream.

All inline SQL (ADR-018), cluster-correct (ADR-020). Orthogonal to E2EE (ADR-011): none of these endpoints store key material or can enable encryption — they manage device identity and session lifecycle only. No new ADR.

Admin UI per-user device management (Story s4-admin-device-management)

S4 gives operators the same device-management capabilities over another user's devices, from the Admin UI (Phoenix dead-view / HTTP controller, not the Matrix CS API). A user-detail page (GET /admin/users/:user_id) lists every device (semantic table: device id, display name, last-seen, created), with a rename form and a revoke form (native confirm) per row. All three routes are :require_admin-gated, CSRF-protected, and follow the PRG (Post/Redirect/Get) pattern; revoke uses POST .../devices/:device_id with _method=DELETE (Plug.MethodOverride), mirroring the rooms member-removal destructive-action pattern.

  • The Admin domain consumes devices ONLY through the Nebu.Matrix facade (ADR-020) — list_user_devices/1, rename_user_device/3, delete_user_device/2 — never DeviceKeyStore or Session.* directly (see §05 building blocks for the facade contract).
  • Revoke reuses the S3 kill-switch. delete_user_device/2 runs the per-device JWT kill-switch FIRST (denylist every token bound to (user_id, device_id) until its own exp via TokenDenylist.revoke_by_hash/2, drop the bindings + the opaque session), THEN deletes the device identity row and all key material. Because an admin acts on another user's device there is no caller credential to preserve — EVERY bound token is revoked (unlike the Matrix self-delete path).
  • No IDOR. Rename and delete are user-scoped in SQL; a device_id that does not belong to the path :user_id maps to :error/no-op and a flash — never to another user's device.

Audit on success AND failure. Each successful rename/revoke writes a device_renamed / device_deleted audit entry (actor = admin, target user + device); a failed attempt is also audited. Both actions are registered in Nebu.Compliance.AuditWriter's @known_actions allowlist — an unregistered action is rejected with {:error, :audit_unknown_action} before any DB call, so a missing registration would silently drop the entry (see Audit Logging below). The do_log rescue is narrowed to RuntimeError (the documented missing-Repo degradation) so genuine logic bugs in the audit path still crash rather than being swallowed.

No new migration (S4 reuses the S1–S3 schema), inline SQL (ADR-018), cluster-correct (ADR-020), orthogonal to E2EE (ADR-011). No new ADR.

Error Handling

Elixir: Tagged tuples {:ok, result} / {:error, reason}. No raise/throw for business logic. Let-it-crash + OTP Supervisor Trees for unexpected failures. The HTTP layer maps domain results to Matrix error envelopes ({"errcode": "M_...", "error": "..."}). The in-process command layer (Nebu.Matrix.EventDispatcher.Server) raises Nebu.Matrix.Error with a Nebu.Matrix.Status atom as its error type (e.g. :not_found, :permission_denied, :internal); Nebu.Matrix.Http.call_core/1 translates the status atom to the HTTP/Matrix response. (Prior to the grpc-removal epic, these were GRPC.RPCError / GRPC.Status.<code>() — that dependency is fully removed, ADR-021 2026-06-22.)

Media Gateway - Storage Error Classification (Story 12.4):

The Storer interface returns sentinel errors that map to specific HTTP status codes:

SentinelCauseHTTPerrcode
storage.ErrNotFoundObject absent from backend (MinIO NoSuchKey, OS ErrNotExist)404M_NOT_FOUND
storage.ErrStorageUnavailableNetwork unreachable, MinIO degraded, other MinIO errors502M_UNKNOWN
otherCrypto failure, hex decode error500M_UNKNOWN

Classification logic: ClassifyMinIOError uses errors.As to unwrap minio.ErrorResponse (not minio.ToErrorResponse which does direct type assertion). LocalStorer.Get maps os.ErrNotExist → ErrNotFound. The handler logs the full error via slog.Error but returns only a generic message to the client (no credential/endpoint leak).

Media Gateway - Thumbnail Generation (Story 12.5):

On-demand thumbnail generation for GET /_matrix/media/v3/thumbnail/{serverName}/{mediaId}:

ConcernDecisionRationale
Librarygithub.com/disintegration/imaging v1.6.2 (MIT)Pure Go, no cgo, no shell exec - sandboxed by construction
MIME detectionnet/http.DetectContentType on first 512 bytesMagic bytes only; never trust stored Content-Type
Allowed typesimage/jpeg, image/png, image/gif, image/webpDeny-by-default: SVG/PDF/PS/EPS → 400 M_BAD_JSON
Scale methodimaging.FitAspect-ratio-preserved, fits within W×H
Crop methodimaging.Fill (center)Center-crop to exactly W×H
Animated GIFgif.DecodeAll + per-frame resize + Floyd-Steinberg re-palettizePreserves all frames + loop count
animated=falseStatic JPEG (first-frame decode)Spec MUST NOT return animated thumbnail
Output formatJPEG (85 quality) for static; GIF for animatedBalance size/quality
HeadersContent-Disposition: inline; filename=... (spec v1.12) + Cache-Control: max-age=86400Matrix spec MUST requirements
SecuritymediaId validated with ^[A-Za-z0-9_\-]+$ before DB/storage callPath traversal prevention (Matrix spec §5.6)
Dimension capconst maxThumbDim = 2048; width > 2048 or height > 2048 → 400 M_BAD_JSONMemory-amplification DoS prevention (Story 12.7 HIGH-1)
Source image budgetAfter imaging.Decode, check Dx * Dy > 100_000_000 → return errorDecompression-bomb defence - prevents ≥400 MB allocations from compressed source
GIF frame caplen(g.Image) > maxGIFFrames (200) → truncate to first 200 frames silentlyBounded resource usage for animated GIFs; no error returned
Response headersX-Content-Type-Options: nosniff set on all thumbnail responsesPrevent MIME sniffing in legacy browsers (Matrix spec v1.12)

Media Gateway - Upload/Download Security (Story 12.7 HIGH-3):

Content-Type enforcement to prevent stored XSS:

PhaseRuleMechanism
Upload - blocklisttext/html, application/xhtml+xml, text/javascript, application/javascript, image/svg+xml, application/x-shockwave-flash → 400 M_BAD_JSONblockedContentTypes map; normalized with mime.ParseMediaType (strips params)
Download - Content-TypeThe stored (upload) type is served unchanged as base type (no charset appended; put_resp_content_type(ct, nil)). Missing/unparseable types (no /) and active-content types neutralized by category (the internal neutralization predicate in Nebu.Media: text/html, application/xhtml+xml, text/xml, application/xml, image/svg+xml, application/x-shockwave-flash, ANY *+xml suffix, text/xsl, multipart/x-mixed-replace, the WHATWG JavaScript MIME family incl. application/ecmascript/text/jscript/…, plus application/wasm, text/x-component, text/cache-manifest) → application/octet-stream + attachment. The JS family is deliberately NOT in the upload blocklist (legitimate to share; neutralized on download, legacy rows included)Matrix spec v1.12+ ("exactly the same as the original upload, or reasonably close"); Nebu.Media.disposition_for/1
Download - inline listSpec inline list (26 types: text/css, text/plain, text/csv, application/json, application/ld+json, image/{jpeg,gif,png,apng,webp,avif}, video/{mp4,webm,ogg,quicktime}, audio/{mp4,webm,aac,mpeg,ogg,wave,wav,x-wav,x-pn-wav,flac,x-flac}) → Content-Disposition: inline; everything else (incl. application/pdf) → attachmentDisposition is computed from the served type; this replaces the former "octet-stream for everything not allowlisted" model (changed deliberately in media-download-content-type-and-filename; defense in depth now = attachment + CSP sandbox + nosniff + CORP + upload blocklist)
Download - filename{fileName} path segment > stored upload filename > no filename parameter (never the media_id). ASCII-safe filename="..." ("/\ escaped), plus filename*=UTF-8''<percent-encoded> for non-ASCII; control characters and bidi/invisible format characters (Unicode Cf, e.g. U+202E) are stripped (no header injection / filename spoofing); the stored filename and the output (incl. the path segment) are capped at 255 bytes on a codepoint boundary; an empty path segment falls back to the stored name; the admin media list shows the same sanitized name (Nebu.Media.display_filename/1)Nebu.Media.content_disposition/2; media_files.filename persisted on sync and async upload
ThumbnailOnly image/jpeg, image/png, image/apng, image/gif, image/webp are served (exact type, inline, thumbnail.<ext>); any other stored type → 400 M_UNKNOWN (membership 403 is checked first)Never relabels non-images as image/png
All download responsesX-Content-Type-Options: nosniff always setPrevent MIME sniffing
Watch-flag (security)This design relies on ALL media routes accepting only the Authorization: Bearer header (OidcPlug). Any change adding ?access_token=, cookie auth or unauthenticated v3 media MUST re-trigger the security review (JS-typed uploads would become same-origin scripts; Admin UI CSP script-src 'self').Kassandra 2026-10-08 F-4

Media Gateway - Upload JWT Validation + Fail-Closed OIDC Startup (Stories 12.7, 12.8):

The upload handler uses a TokenVerifier interface (HandlerConfig.OIDCVerifier). OIDCTokenVerifier wraps *oidc.IDTokenVerifier and implements VerifyToken(ctx, rawToken) (string, error) - returning the uploader's subject identity from the operator-configured OIDC claim (see Story 12.11 below). Uploads with a nil verifier receive 503 M_UNAVAILABLE (fail-closed, not fail-open).

Startup hardening (Story 12.8 + Story 12.12 F-3): NEBU_OIDC_ISSUER is mandatory. If empty, the media gateway exits immediately with FATAL: NEBU_OIDC_ISSUER is required. If Dex is unreachable at startup, initOIDCVerifier retries up to 5 times with 2s backoff, then exits (FATAL: media: OIDC provider unreachable after retries). The service never starts with a nil verifier. This eliminates the "OIDC fail-open at startup" pattern documented as a recurring vulnerability.

Story 12.12 (F-3) adds a 10-second per-attempt timeout to each oidc.NewProvider call via context.WithTimeout(ctx, 10s). A provider that accepts the TCP connection but never sends a response (hung Dex, firewall issue) can no longer block startup indefinitely - each attempt fails within 10s, and the 5-retry cycle completes within ≤60s total. Parent context cancellation (SIGTERM during startup) is checked at the top of every retry iteration for immediate exit.

Story 12.13 completes graceful shutdown during OIDC retries: main() creates a SIGTERM-aware context via signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM). This makes the parent ctx actually cancel on SIGTERM - previously context.Background() was used and the ctx.Err() guard was dead code for signal handling. Additionally, the inter-retry sleep (time.Sleep(retryDelay)) is replaced by a ctx-aware select { case <-time.After(retryDelay): case <-ctx.Done(): return error }. A SIGTERM received during the 2-second backoff sleep now interrupts immediately rather than waiting up to 2s, ensuring docker compose stop completes within Docker's 10-second grace window. Log messages distinguish signal abort (slog.Info) from retry exhaustion (slog.Error).

Story 12.14 - Full runtime graceful shutdown (HTTP + DB Pool + Rate Limiter): Completes the SIGTERM story. Three runtime gaps are closed:

  1. HTTP drain - srv.ListenAndServe() runs in a background goroutine. main() blocks on select { case err := <-serverErr; case <-ctx.Done() }. On SIGTERM, srv.Shutdown(shutdownCtx) is called with a 30-second timeout. In-flight HTTP requests complete before the server stops accepting new connections. After 30s, Shutdown returns context.DeadlineExceeded and shutdown proceeds regardless.

  2. DB pool close - pool.Close() is called explicitly after srv.Shutdown returns (not via defer). The defer pool.Close() pattern was removed because os.Exit(1) bypasses defers; explicit sequencing ensures DB connections are always released. The shutdown order is: srv.Shutdown → pool.Close() → slog.Info("media gateway stopped") → main() returns (exit code 0).

  3. Rate limiter goroutine - NewIPRateLimiter now accepts ctx context.Context (see Rate Limiting section above). The cleanup goroutine exits cleanly when SIGTERM cancels the context.

Shutdown helper: runShutdownSequence(ctx context.Context, srv *http.Server, poolClose func()) encapsulates the ordered shutdown steps and is unit-testable with a spy closure for poolClose (no real DB pool required in tests).

Media Gateway - Authenticated Media Endpoints (Story 12.16):

Matrix CS API v1.11+ introduced authenticated download/thumbnail paths (/_matrix/client/v1/media/*) alongside the deprecated unauthenticated /_matrix/media/v3/* paths. The Media Gateway now implements both path families.

Auth middleware package (media/internal/auth):

The auth.Middleware wraps any http.Handler with Bearer token verification. It defines a local TokenVerifier interface (same signature as upload.TokenVerifier - no import cycle):

Error mapping:

ConditionHTTPerrcode
Missing or non-Bearer Authorization header401M_MISSING_TOKEN
Empty Bearer token (Bearer with nothing after)401M_MISSING_TOKEN
Verifier returns error (invalid/expired token)401M_UNKNOWN_TOKEN
Nil verifier (guard)503M_UNAVAILABLE (fail-closed)
Valid token-handler called

*upload.OIDCTokenVerifier satisfies auth.TokenVerifier structurally; the same instance (uploadVerifier from initOIDCVerifier) is reused - no separate OIDC client or audience check.

Routing pattern in main.go:

// Deprecated unauthenticated (backward compat):
GET /_matrix/media/v3/config          → configHandler (no auth)
GET /_matrix/media/v3/download/...    → downloadHandler (no auth)
GET /_matrix/media/v3/thumbnail/...   → thumbnailHandler (no auth)

// v1.11+ authenticated:
GET /_matrix/client/v1/media/config                          → authMW.Wrap(configHandler)
GET /_matrix/client/v1/media/download/{serverName}/{mediaId} → authMW.Wrap(downloadHandler)
GET /_matrix/client/v1/media/download/{serverName}/{mediaId}/{fileName} → authMW.Wrap(downloadHandler)
GET /_matrix/client/v1/media/thumbnail/{serverName}/{mediaId} → authMW.Wrap(thumbnailHandler)

The {fileName} path segment maps to r.PathValue("fileName") inside the download handler for the Content-Disposition filename. When the route without {fileName} is matched, PathValue returns "" and the handler uses the stored upload filename and, if none exists, omits the filename parameter (never the media_id).

Security response headers on download and thumbnail (Matrix spec §Media Repository SHOULD, v1.4+):

All 200 responses from the download and thumbnail handlers - on both v3 and v1 paths - now include:

Content-Security-Policy: sandbox; default-src 'none'; script-src 'none'; plugin-types application/pdf; style-src 'unsafe-inline'; object-src 'self';
Cross-Origin-Resource-Policy: cross-origin

These headers prevent script execution from served media objects and allow cross-origin image loading by Element Web (required for inline image display in rooms).

Canonical Matrix user ID in audit trail (Story 12.9): After OIDC verification, the upload handler constructs a canonical Matrix user ID before storing uploader_user_id in media_files:

uploader_user_id = formatMatrixUserID(subject, serverName)
                 = "@" + sanitiseLocalpart(subject) + ":" + serverName

localpart sanitisation keeps only [a-z0-9._-] characters (spaces → _, all others dropped) — the same rule used for Matrix user IDs throughout the app. NEBU_SERVER_NAME is mandatory (the app raises at boot if unset). Historical rows (pre-12.9) contain raw OIDC claims and are grandfathered; see migration 000047 column comment. This enables compliance officers to correlate media_files.uploader_user_id with room event sender fields without manual claim-mapping.

Configurable OIDC claim for audit trail (Story 12.11 SEC Fix F-1): The claim used as the audit trail user identity is now operator-configurable via NEBU_OIDC_USER_ID_CLAIM (default: name, matching migration 000044 column comment and the gateway's DB default).

NEBU_OIDC_USER_ID_CLAIMBehavior
unset or name (default)Uses the name claim from the OIDC token
subUses the sub claim (UUID from IdP)
email, preferred_username, etc.Uses the specified claim
any (configured claim missing)Falls back to sub with a slog.Warn log entry

Implementation: extractClaimFromMap(rawClaims map[string]interface{}, claimName string) (string, error) - pure function in media/internal/upload/upload.go. The value goes through sanitiseLocalpart before storage. OIDCTokenVerifier is constructed with NewOIDCTokenVerifier(idTokenVerifier, claimName) in initOIDCVerifierWith.

server_config RLS UPDATE Policy (Story 12.7 MEDIUM-5, extended Story 14-2a):

Migration 000046 replaces the blanket config_update_all policy (USING true - introduced in migration 000045 for OIDC claim upserts) with a key-scoped config_update_mutable policy. Migration 000048 (Story 14-2a) extends the allowlist with two new OIDC directory keys. Migration 000049 (Story 14-3c) extends with three SCIM 2.0 keys. Only the following keys are updatable by nebu_app:

  • oidc_user_id_claim, oidc_displayname_claim, oidc_email_claim, admin_group_claim
  • oidc_issuer, oidc_client_id, oidc_client_secret
  • oidc_directory_enabled, oidc_directory_endpoint (added in migration 000048, Story 14-2a)
  • scim_enabled, scim_base_url, scim_bearer_token (added in migration 000049, Story 14-3c; bearer token is AES-256-GCM encrypted - raw value never stored in plaintext)

server_name, bootstrap_completed, and any future keys not in this allowlist are immutable at the DB level (defence-in-depth restoration from ADR G8).

Bool / Direct DB Upsert Pattern (Story 14-2a):

Fields stored as booleans in server_config (e.g. oidc_directory_enabled) must be updated with explicit nil-awareness. The pattern used: the Admin UI handler (in Nebu.Admin.*) upserts directly via inline SQL rather than going through the generic config update path. This avoids inadvertent resets of boolean flags when only a subset of fields is submitted.

OIDC Directory Service Pattern (Story 14-2b):

The OIDC directory service (in Nebu.Admin.*) provides a secure, cached client for fetching user lists from an admin-configured OIDC directory endpoint. Key design decisions:

  • secretString type: the bearer token is wrapped in type secretString string with String() "[REDACTED]" - prevents accidental log exposure via %+v or slog auto-formatting (CR-3).
  • HTTPS-only at call time: validateEndpoint is called inside FetchUsers on every invocation, not just at config load. This is a defensive check: if the stored endpoint is later mutated to HTTP, the service fails hard rather than silently falling back (CR-1).
  • No redirect following: the default http.Client uses CheckRedirect: ErrUseLastResponse. This prevents an HTTP→HTTPS redirect from bypassing the HTTPS check, and prevents SSRF via provider-issued redirects to internal metadata endpoints (CR-2).
  • Rate limit separation: Allow(sessionID string) bool and FetchUsers(ctx) ([]OIDCDirectoryUser, error) are separate methods. Callers MUST call Allow before FetchUsers to enforce the per-session 5 req/s limit. The separation allows callers to short-circuit before cache or network access. Session IDs MUST come from the validated JWT (admin middleware), not from request headers (CR-5).
  • singleflight + 30s cache: concurrent cache misses collapse into one outbound call (MR-4). Cache key = SHA-256(endpoint + "|" + token) so rotating the bearer token invalidates the stale cache (MR-1).
  • Graceful degradation: runtime errors (unreachable host, non-200, JSON parse failure) are logged as warnings and swallowed - FetchUsers returns empty list, not error. Configuration errors (non-HTTPS) are propagated as errors.

OIDC Directory User Search Merge Pattern (Story 14-2c):

Nebu.Admin.UsersLive (or the REST handler equivalent) merges Nebu DB users (from list_admin_users in-process) with OIDC directory users (from OIDCDirectoryService.FetchUsers) when oidc_directory_enabled=true. Key design decisions:

  • WithOIDCDirectory setter: UsersHandler accepts an *OIDCDirectoryService via a fluent setter (WithOIDCDirectory(svc, serverName)). The handler field is nil by default - zero-config backward compatibility.
  • Nebu DB wins on dedup: OIDC users whose sanitize(sub) matches a Nebu UserId are dropped from the OIDC-only list. The Nebu DB entry always takes precedence.
  • IsOIDCOnly flag: UserRowData.IsOIDCOnly=true + MatrixIDPreview mark users present only in the OIDC directory. The preview is computed as @{sanitize(sub)}:{serverName} and is not persisted.
  • Non-blocking OIDC failure: when the OIDC provider is unreachable, FetchUsers returns an empty list (logged internally). ListHandler detects this via IsEnabled() && len(oidcUsers)==0 and sets OIDCWarningBanner - the Nebu DB list is still rendered.
  • Rate-limit gate: Allow(sessionID) is called before FetchUsers. If rate-limited, OIDC fetch is silently skipped (no warning banner - rate-limiting is defensive, not an availability signal).

Bulk User Provisioning Pattern (Story 14-3a):

Nebu.Session.BulkImporter provisions a list of OIDC users via the bulk_import_users in-process call. The provisioning flow is identical to first-login provisioning in Nebu.Session.TokenValidator.Postgres.provision_new_user. Key design decisions:

  • Identical to first-login: BulkImporter calls the same UserStore.upsert_user/2 + UserProvisioner.provision_user/4 functions as first login. Ed25519 signing keypairs and X25519 encryption keypairs are generated; display_name (Tier 1) and email (Tier 2) are encrypted identically.
  • Duplicate detection: a user whose signing_key_id IS NOT NULL in the users table is counted as skipped - not an error. The UserProvisioner.Postgres update SQL already uses WHERE signing_key_id IS NULL (idempotent guard), but BulkImporter avoids unnecessary keypair generation by checking first.
  • Partial success: import_users/1 uses Enum.reduce across all users. Exceptions in import_one/2 are rescued with try/rescue - the user is counted as :failed and the batch continues. The aggregate {imported, skipped, failed} is always returned.
  • No bootstrap path: bulk import always uses the provided system_role (the admin import path always sets "user"). The BootstrapChecker.upsert_with_bootstrap step from first-login is skipped (bootstrap is a one-time event that only triggers on the very first login to an empty instance).
  • Trust model: the bulk-import command handler does not re-validate the admin role itself — the admin-only HTTP access is already enforced by the admin auth gate (Nebu.Admin.Auth) before the in-process call reaches it.
  • Module injection: all three dependencies (lookup_module, user_store_module, provisioner_module) are injectable via Application.put_env for test isolation. Default implementations use real PostgreSQL.

Bootstrap Wizard Step 4 - User Import UI Pattern (Story 14-3b):

BootstrapHandler extends the 3-step wizard with a Step 4 that lets admins pre-provision OIDC users immediately after claim selection. Key design decisions:

  • Redirect chain: ClaimSelectionHandler now redirects to /admin/bootstrap?step=4 instead of /admin/dashboard. Step 4 renders an "Import from OIDC" button. The admin can "Skip and finish" → /admin/dashboard at any time.
  • Interface abstraction: OIDCDirectoryFetcher (IsEnabled/FetchUsers) and BulkImportClient (BulkImportUsers) behaviours/callbacks allow test injection. WithImportServices(fetcher, core, serverName) wires them at startup.
  • Two sub-actions via form POST: action=preview fetches users (SCIM-preferred) and renders the preview table (display name, email, computed Matrix User ID); action=import re-fetches + calls bulk_import_users in-process + renders imported/skipped/failed counts.
  • AC4 disabled state: when neither OIDC nor SCIM is enabled, Step 4 renders with a disabled import button.
  • Matrix User ID computation: reuses sanitizeOIDCSub(u.Sub) from users.go (same package) to produce @{localpart}:{serverName} - identical to the preview shown in the Users page (Story 14-2c).

SCIM 2.0 User Fetch Pattern (Story 14-3c / ADR-015 Protocol B):

The SCIM client (in Nebu.Admin.*) fetches users from a SCIM 2.0 /Users endpoint using RFC 7644 pagination. Key design decisions:

  • Protocol priority: when both oidc_directory_enabled and scim_enabled are true, SCIM takes priority over the OIDC directory for the action=import (and action=preview) flows in Bootstrap Step 4. This is enforced in BootstrapHandler.StepHandler via the SCIM-preferred fetcher selection pattern.
  • SCIMFetcher interface: mirrors OIDCDirectoryFetcher (IsEnabled/FetchUsers) so BootstrapHandler uses a unified interface for both protocols. WithSCIMFetcher(f SCIMFetcher) wires the client at startup.
  • RFC 7644 pagination: startIndex is 1-based; count=100 per page; iteration terminates on empty Resources[]. totalResults is used for the early-abort cap check (HR-1).
  • secretString CR-1: bearer token stored as secretString - .String() returns [REDACTED]; .value() only called to set the Authorization header.
  • Bearer token encryption at rest: stored as AES-256-GCM (nonce || ciphertext, hex) in server_config.scim_bearer_token. Encrypted at config-save time with the internal secret; decrypted at startup before wiring into SCIMClient.
  • scim_base_url convention: the stored URL is the SCIM service root (e.g. https://idp.example.com/scim/v2). buildPageURL appends /Users if not already present, then adds startIndex/count query params.
  • userName as sub: scimSub() prefers userName over id - Azure AD and Okta export email-style userNames that align with OIDC sub semantics. The sub value goes through sanitizeOIDCSub for Matrix localpart derivation (same path as OIDC directory).
  • Import progress singleton: importInProgress atomic.Bool + importProgress *importProgressState are package-level singletons. CompareAndSwap(false, true) before each import → HTTP 409 on concurrent attempt (HR-3). Counters (imported, total, failed, done) are atomic.Int32/atomic.Bool - safe to read from the polling endpoint without a lock.
  • Progress endpoint: GET /api/v1/admin/bootstrap/import-status (behind sessionGuard) returns {imported, total, failed, done} JSON. The Bootstrap Wizard Step 4 HTML polls this endpoint via setInterval every 2s using fetch(credentials: 'same-origin').
  • Synchronous import: for S-size story scope, bulk_import_users/1 is called synchronously; counters advance from 0/N to imported/N/done in a single step after the call returns. The progress bar shows 0/N while the call is in flight, then reloads the page on done=true.

Media Event Content Pass-Through Contract (Story 12.6):

content.info in m.room.message (and all other event types) is an opaque JSON object - the send handler passes it verbatim to the event-dispatch layer, which stores it as JSONB without field inspection or modification. Client extension fields such as blurhash (used by Element Web for image loading placeholders) MUST be preserved through the full round-trip: send handler → event dispatch → DB → sync response.

  • The server never computes blurhash - it is always client-provided.
  • The send/sync handlers carry content as an opaque JSON value end-to-end - no field filtering.

This contract is validated by the blurhash pass-through tests in the Matrix domain test suite (AC1: send carries the field through; AC2: sync returns it unchanged).

Error surface rule (Story 9-27): the in-process command handlers (Nebu.Matrix.EventDispatcher.Server) raise Nebu.Matrix.Error with a Nebu.Matrix.Status atom as their error type. Bare :ok = pattern matches on Room.Server calls produce a MatchError, which maps to an ambiguous HTTP 500 with no structured error message. The correct form is a case expression that raises with Nebu.Matrix.Status.internal() on unexpected {:error, reason} tuples, so Nebu.Matrix.Http.call_core/1 can translate :internal to a proper server-error response.

Unrecognized endpoints (RU9-RU11)

Story oauth-room-summary-unrecognized (ADR-033). The spec answers an unimplemented endpoint with 404 M_UNRECOGNIZED and a known endpoint with the wrong method with 405 M_UNRECOGNIZED. The Matrix sub-routers (Router.Protected, Router.Media, Router.ClientV1Media) therefore run the pipeline :match -> :authenticate -> :dispatch: the catch-all match _ carries private: %{nebu_unrecognized: true}, and the public authenticate/2 skips the OidcPlug only when that marker is the boolean true (every real route authenticates as before; a halted conn never dispatches; a test sends an unauthenticated request to every route clause). The catch-all asks its own router which other methods route at the path (do_match/4 probe with the marker removed: a probe selects a clause and never dispatches) and answers 405 + Allow, or 404 when none does (shared responses in Nebu.Web.Unrecognized). A non-preflight OPTIONS answers 204 + Allow on a known path (no auth, no logic), 404 on an unknown one; HEAD is not mapped to GET (405). Handler-level errors (M_NOT_FOUND of a missing account-data entry, device, media, profile; M_BAD_JSON) are produced inside the route clauses and never rewritten; the unimplemented GET on /rooms/{id}/state[/...] keeps 404 M_NOT_FOUND (RU11a). HttpPublic keys its root requests on the forward prefix (conn.script_name, RU13).

Room GenServer Start on Demand (Story 11-11)

After a stack restart, Room GenServers are not automatically re-started - rooms exist in the DB but have no running process in the Horde registry. Handlers that need an active Room GenServer MUST use start_room/1 (idempotent) rather than lookup_room/1 (registry-only) to avoid returning 404 for rooms that genuinely exist.

Pattern:

Idempotency invariant: RoomSupervisor.start_room/1 handles {:error, {:already_started, pid}} internally and converts it to {:ok, pid}. Callers always receive either {:ok, pid} (GenServer running) or {:error, _} (room absent from DB).

{:error, _} wildcard trade-off: Horde supervisor errors (e.g. :max_children) also map to not_found via this pattern. This is an acceptable trade-off: such errors are rare, and the alternative of forwarding an opaque :internal error is less useful to clients. The same trade-off applies consistently across all handlers that use start_room/1.

Race guard - :noproc after start_room:

In rare cases, a GenServer may die between start_room returning {:ok, pid} and the subsequent get_state/1 call (e.g. during Horde CRDT reconciliation). Handlers that call get_state after start_room MUST wrap that call in a try/catch :exit, {:noproc, _} guard to surface the failure as not_found rather than a raw exit:

Handlers using start_room (not lookup_room):

Handler / functionCall site in server.ex
sync.ex - incremental syncline ~1053
unarchive_room/2line ~1959
upgrade_room/2 - new roomline ~2487
send_receipt/2line ~662 (added Story 11-11)

Handlers intentionally using lookup_room (started elsewhere in the same request lifecycle, or explicitly requiring the GenServer to be pre-running):

All other handlers in server.ex. Migration of remaining handlers (e.g. set_typing/2) is deferred to a separate story to keep diffs minimal and reviewable.

Logging

Elixir: Logger with keyword metadata, structured key-value pairs.

PII (email, display name, message content, credentials) must never appear in log output.

Request/Response Payload Logging with Two-Category Redaction (Story matrix-payload-debug-logging, epic enhanced-logging)

When DEBUG logging is enabled (via Logger.configure(level: :debug) at runtime), the request logger captures and logs HTTP request/response bodies with mandatory two-category redaction (Story AC4–AC7):

Category 1: Credentials and Secrets

  • Top-level fields: password, new_password, access_token, refresh_token, client_secret, id_access_token, token (OIDC token grant)
  • HTTP headers: Authorization (entire header value)
  • Nested recursively at any depth in the JSON tree
  • Deprecated query-string pattern: ?access_token=<secret> in URLs

All are replaced with the literal string "***REDACTED***".

Category 2: Chat Message Content

  • Any user-generated message text or metadata at any depth in the JSON tree — not just under a top-level content wrapper
  • Includes Matrix event content fields: body, formatted_body, topic, name, displayname, filename, reason (k/b/redaction free text)
  • Includes metadata inside m.relates_to (reaction emoji/key text), but NOT structural metadata fields like rel_type, event_id, algorithm, sender_key, device_id, session_id
  • For write-path requests (Matrix PUT .../send/{eventType}/{txnId} and PUT .../state/{eventType}[/{stateKey}]), the event content is the raw top-level request body with no content wrapper — this is treated as user-generated content by default

All are replaced with "***MESSAGE_CONTENT***".

Implementation (Nebu.Web.Plugs.RequestLogger + Nebu.Web.Plugs.RequestLogger.PayloadRedactor):

Nebu.Web.Plugs.RequestLogger.call/2 logs every HTTP request at INFO level with method, path, status, duration_ms, and request_id. When DEBUG logging is enabled (checked via Logger.level()), a secondary Logger.debug call issues a JSON-structured log line with:

  • All INFO-level fields
  • request_body — the decoded JSON request map, redacted
  • response_body — the decoded JSON response map, redacted
  • auth_header — the redacted Authorization header value (if present)
  • query_string — the redacted query string (if the request carried deprecated ?access_token= patterns)

The redaction is performed by Nebu.Web.Plugs.RequestLogger.PayloadRedactor.redact_payload/1 (pure, stateless, tested in isolation). The redactor walks the decoded JSON tree recursively, matches keys case-insensitively for credentials and at any depth for message-content fields (whether the field is under a content wrapper or at the root of a write-path body), and returns the redacted map. Redaction is idempotent.

Failure handling: If response-body JSON decoding fails (binary media, non-JSON Content-Type, or malformed JSON), the redacted response_body field is nil and the response is not logged verbatim.

No toggle in this story: The redaction layer itself is always enabled (it's a simple pass-through if DEBUG is off); no feature flag is required. Operators enable/disable DEBUG-level logging for the whole application via Logger.configure(level: :debug) at runtime without restarting the server.

Query Logging Without Bound Parameters (Story matrix-payload-debug-logging, cycle 2 — Nebu.Repo.QueryTelemetry)

Ecto's default query logger outputs :debug-level log lines including the raw SQL text and bound parameter values. When an operator enables DEBUG logging to diagnose application issues, every database query leaks any credentials bound as SQL parameters (e.g., SELECT * FROM matrix_sessions WHERE access_token = $1 prints the actual token value in $1).

Solution: Ecto's query logging is disabled globally (config :nebu, Nebu.Repo, log: false in server/config/runtime.exs, applied only when NEBU_DB_URL is set). Instead, Nebu hooks the underlying :telemetry event (which Ecto emits unconditionally regardless of the log: false setting) via Nebu.Repo.QueryTelemetry:

  • QueryTelemetry.attach/0 is called once from Nebu.Application.start/2, registering a handler on the [:nebu, :repo, :query] telemetry event.
  • The handler logs a sanitized summary at :debug level — only source (the table name, when schema metadata is available; "unknown" for ADR-018 inline-SQL queries — itself safe information) and duration_ms (query execution time).
  • Raw SQL text (query field) and bound parameter values (params field) are never logged.
  • Success/failure is classified as result=ok|error|unknown, never surfacing the raw exception message.

This restores equivalent query-level debug visibility ("a query ran, how long it took") for every query in the codebase (present and future) without leaking any bound credentials. The handler is disabled in :test environments (via config :nebu, Nebu.Repo.QueryTelemetry, enabled: false in server/config/test.exs) to avoid cross-test log interleaving with ExUnit.CaptureLog.

API Response Formats

Strict separation: Matrix endpoints return Matrix format; Admin API returns wrapper format. No mixing.

Admin UI Security

Phoenix HTTP Controller + LiveView Admin (admin-ui-migration + admin-http-refactor — supersedes Go template admin):

The Admin UI is now served by Nebu.Web.AdminEndpoint (Phoenix.Endpoint, server: false) forwarded from Nebu.Web.Router. Key security controls:

  • Admin auth gate — two mechanisms:
    • HTTP controllers use Nebu.Admin.Auth.require_admin/2 as a Plug in the :admin pipeline.
    • Remaining LiveViews (Dashboard, AuditLog, Bootstrap) use on_mount {Nebu.Admin.Auth, :require_admin}. Both check the signed Phoenix session for a valid admin claim derived from server_config.admin_group_claim (default instance_admin) via an inline SQL check (ADR-018). Unauthenticated access is redirected to /admin/login. Nested claim support (bugfix-nested-oidc-claim-support, 2026-07-01): admin_claim?/1 recursively searches the OIDC claims tree at arbitrary nesting depth for the configured marker value — not just flat top-level claims. This is required for Keycloak, whose role claims are nested (realm_access: {roles: [...]}, resource_access: {<client>: {roles: [...]}}), unlike Dex's flat groups: [...]. The Bootstrap Wizard's claim-picker (extract_discovered_claims/1) likewise flattens nested claims into dot-path-keyed options (e.g. realm_access.roles) so an operator can select them during setup. Backward compatible — flat claim configs are unaffected. Path-scoped gate (story scope-admin-claim-to-path, epic oidc-simplification, 2026-07-02 — RESOLVES the any-key-scan risk above): the Bootstrap Wizard now persists the selected claim's dot-PATH (server_config.admin_group_claim_path, e.g. "realm_access.roles" or a flat "groups") alongside its value (admin_group_claim), via a new Nebu.Web.OidcClaims.claim_at_path_matches?/3 (server/lib/nebu/web/oidc_claims.ex). When a path is configured, admin_claim?/1 resolves ONLY that path and matches the marker there — a hit anywhere else in the claims tree (e.g. a self-editable name/preferred_username claim) no longer counts. This is an allowlist of the ONE path the operator themselves selected during bootstrap, not a claim-key denylist (a denylist was rejected during bugfix-unify-admin-matrix-oidc-identity code review as unsafe — it could silently break a legitimately-configured admin_group_claim). The path is now MANDATORY on every bootstrap completion — bootstrap_confirm_claim/2 rejects a completion POST that omits a verified claim path (code-review MAJOR M1 fix during this story's own pipeline run) rather than silently persisting a path-less config, so a fresh instance can never end up on the fallback below. Fallback (not a regression, pre-existing deployments only): a deployment bootstrapped BEFORE this story shipped has no admin_group_claim_path in server_config and cannot acquire one retroactively (bootstrap runs exactly once) — for those, admin_claim?/1 falls back to the pre-story any-key scan unchanged, logging at INFO level (no PII) so operators can see that a fresh re-bootstrap would harden the gate. Migration 000016_config_admin_group_claim_path widens the config_update_mutable RLS policy so the key is runtime-mutable, mirroring 000013_config_oidc_scopes_mutable's pattern.
  • CRUD page rendering (admin-http-refactor): Users, Rooms, Spaces, Config, ClaimMapping, RoleMapping, and Compliance pages are now rendered by Phoenix HTTP controllers following the Post-Redirect-Get (PRG) pattern. Each controller module has a corresponding HTML view module and HEEx template. State changes are submitted as standard HTML form POSTs; the controller processes the request, writes to the DB via inline SQL (ADR-018), and redirects to the GET page. No persistent WebSocket is required for these pages.
  • Real-time LiveViews (unchanged): DashboardLive, AuditLogLive, and BootstrapLive remain as Phoenix LiveViews — they require server-push or multi-step stateful interactions that benefit from the persistent WebSocket connection.
  • CSRF: Phoenix protect_from_forgery with a signed session — applies to both HTTP controller form submissions (standard CSRF token in form) and LiveView event handlers (CSRF verified by Phoenix automatically).
  • Session cookie: http_only: true, secure: true (config-driven; enforced in prod TLS). The Phoenix session is signed/encrypted with secret_key_base, derived from NEBU_ADMIN_SECRET_KEY_BASE or SHA-512 of the internal secret file. A missing NEBU_ADMIN_SECRET_KEY_BASE raises at boot in prod when the internal secret file is also absent (fail-closed).
  • Content-Security-Policy (admin browser pipeline): default-src 'self'; connect-src 'self' ws: wss:; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'self' Applied as a Plug in the admin browser pipeline. ws:/wss: is required for the three remaining LiveViews (Dashboard, AuditLog, Bootstrap). style-src 'unsafe-inline' is required for Phoenix LiveView inline transition styles.
  • OIDC client secret at rest: BootstrapLive encrypts the oidc_client_secret with AES-256-GCM before persisting to server_config (reuses Nebu.Media.Crypto; key = SHA-256 of internal secret; fail-closed when no secret is available). Byte-compatible with the existing Go admin.EncryptAES256GCM scheme (shared internal secret, same key derivation).
  • Phoenix.PubSub: Added to Nebu.Application as a child process required by LiveView. Uses the Phoenix.PubSub.PG2 adapter (in-process, no external broker — consistent with ADR-002).

Admin login (go-removal-cleanup — complete): The Go admin handlers are deleted; server owns the full admin OIDC Authorization-Code + PKCE login in Elixir (Nebu.Admin.Auth). /admin/login/start mints a PKCE verifier/S256 challenge + CSRF state + OIDC nonce into the signed Phoenix session and redirects to the IdP; /admin/callback validates state (constant-time, fail-closed), exchanges the code at the provider token endpoint, verifies the ID token via the shared Nebu.Web.Plugs.OidcPlug.verify_token/2 (signature/alg/iss/aud/exp/sub) + nonce binding, enforces the admin-group claim, and mints the Phoenix admin session (renew: true, session_exp = min(idToken.exp, now+8h)). There is one session cookie (the signed Phoenix secret_key_base cookie — http_only, secure, SameSite=Lax). NEBU_ADMIN_OIDC_LOGIN_URL is removed; the admin OAuth client is configured via NEBU_ADMIN_OIDC_CLIENT_ID / ..._CLIENT_SECRET[_FILE] / ..._REDIRECT_URI. When NEBU_ADMIN_OIDC_REDIRECT_URI is not explicitly set, Nebu.Admin.Auth derives the default from NEBU_SERVER_NAME + NEBU_ADMIN_SESSION_SECURE: https://{server_name}/admin/callback for production (non-localhost) and http://localhost:8008/admin/callback for local dev. The value is always server-controlled and never derived from request headers (Host/X-Forwarded-*).

Admin silent OIDC token refresh (admin-oidc-session-refresh): Admin login now requests offline_access so the IdP returns a refresh_token. The RT is encrypted with Nebu.Web.SecretBox (AES-256-GCM) and stored inside the signed admin session cookie — no admin_sessions DB row, no plaintext RT at rest. On each authenticated HTTP request Nebu.Admin.Auth.fetch_current_user/2 silently refreshes within a 120 s pre-expiry window: it exchanges the stored RT at the token endpoint, re-verifies the new id_token via OidcPlug.verify_token, re-evaluates the admin-group gate on the refreshed claims (a revoked admin loses access on the next refresh), recomputes session_exp = min(new id_token.exp, now+8h), renews the session and rotates the stored RT. On refresh failure or lost group it clears the session and redirects to /admin/login. This replaces the previous behaviour where the admin session was capped at the short id_token.exp with no refresh, causing a forced re-login roughly every 5 minutes against Keycloak.

LiveView-idle session refresh (admin-liveview-session-refresh): the silent refresh above is an HTTP Plug, so it only re-runs on a genuine HTTP round-trip — an operator idling on an open LiveView (Dashboard, AuditLog) with zero new HTTP requests would otherwise be force-logged-out at id_token.exp regardless of admin_oidc_refresh_mode, since nothing on the persistent WebSocket connection ever re-triggered fetch_current_user/2. Fixed with a client-side heartbeat rather than a new server-side refresh path (Phoenix session cookies cannot be set over WebSocket anyway): a phx-hook (SessionKeepalive, server/priv/static//assets/app.js) mounted in the shared LiveView layout (Nebu.Admin.Layouts.live/1) polls a minimal GET /admin/keepalive endpoint every 60 s — well inside the 120 s pre-expiry window — while any admin LiveView tab is open. The endpoint (Nebu.Admin.KeepaliveController) sits behind the exact same :browser pipeline + require_admin gate as every other admin route, returns a bare 204 No Content, and introduces no business logic of its own: simply reaching it means fetch_current_user/2 already ran earlier in the pipeline and had the chance to perform its existing silent refresh. Separately, Nebu.Admin.Auth.on_mount/4 now treats a session with an elapsed session_exp as refreshable (mount proceeds instead of forcing logout) rather than calling the token endpoint itself — the socket handshake has no HTTP response to persist a rotated refresh token into, so the actual token-endpoint exchange stays exclusively on the keepalive HTTP path.

Security hardening (Kassandra HIGH finding, 2026-07-01): the initial version of this fix gated the grace purely on the stored refresh token still being decryptable — but decryptability has no time component (Nebu.Web.SecretBox/AES-256-GCM never expires a ciphertext), so a de-provisioned admin who deliberately severed their own heartbeat (disabling JS, blocking /admin/keepalive, backgrounding the tab) could keep an open Dashboard/AuditLog LiveView alive indefinitely across socket reconnects, bypassing the only other server-side bound (session_exp). Fixed with an independent, non-bypassable refresh_grace_exp timestamp (session_exp + 180s) written into the signed session cookie at login and on every successful HTTP-path refresh — never on the socket path itself, so a severed heartbeat cannot extend it. on_mount/4's refreshability check now requires BOTH the refresh token decrypts and now <= refresh_grace_exp; decryptability alone is necessary but no longer sufficient. A healthy 60 s heartbeat keeps re-extending the bound well before it lapses (idle-tab UX preserved, AC2), while a severed heartbeat now hard-expires the session within ~3 minutes of the last successful refresh instead of never. See docs/stories/oidc-simplification/security-reports/admin-liveview-session-refresh-security-review-2026-07-01.md for the full finding and independent re-verification.

Admin login provisions the Matrix identity (provision-matrix-identity-on-admin-login): a successful admin OIDC login (Nebu.Admin.Auth.normal_callback/2 OR bootstrap_confirm_claim/2) now provisions the SAME users-row the real Matrix POST /_matrix/client/v3/login path provisions, for the SAME matrix_user_id the admin session carries (Nebu.Matrix.UserId.from_claims/2 — single source of truth for both flows). Previously an operator who only ever logged into the Admin UI (never a Matrix client) had no users row at all and was invisible to GET /admin/users (Nebu.Admin.DB.list_users/3 reads exclusively from that table), even though their matrix_user_id could already act via room writes (creator/actor attribution) after the prior story fix-bootstrap-admin-session-identity.

The provisioning call is routed through a new Nebu.Matrix.provision_identity/2 facade function (ADR-020 — Nebu.Admin never calls Nebu.Matrix.Session.TokenValidator or Nebu.Matrix.Profile.DB directly). Internally it performs the same TokenValidator.validate/4 → Profile.DB.upsert_profile/3 pair the real Matrix /login path's provision_and_respond/6 uses — idempotent (a re-provisioned existing row is a no-op upsert, never an error) and nil-safe for the profile's COALESCE upsert (a later Matrix login carrying no displayname claim never clobbers a displayname the user set themselves).

Architecture decision (AC2) — no auto-join for admin-only provisioning: unlike the real Matrix login path, provision_identity/2 deliberately does not call Nebu.Matrix.EventDispatcher.Server.run_post_login_provisioning/2 (personal room + auto-join into is_auto_join rooms/spaces). Rationale: an admin who authenticates once in the Admin UI is not necessarily a communication participant, and unconditionally auto-joining them into every is_auto_join room — some of which may hold confidential content — would be an unwanted (and, per the story's security considerations, a potential information-disclosure) side effect of an administrative action. Only the users-row + profile provisioning happens; auto-join remains exclusive to the real Matrix client login flow. This is a recommendation, not a hard architectural constraint — a future story may revisit it with an explicit, documented justification (see docs/stories/oidc-simplification/provision-matrix-identity-on-admin-login.md, AC2).

Testing Architecture (Story 9-26)

Nebu uses a two-layer E2E test strategy, splitting tests by the level of observable behavior:

LayerRunnerScopeLocation
HTTP / Matrix APIExUnit :integration (Plug.Test / :httpc)REST endpoints, Matrix CS protocolserver/test/nebu/matrix/
Browser / UIPlaywright + playwright-bddElement Web flows, Admin UI, real OIDC redirectse2e/

No plain .spec.ts files without a .feature counterpart are accepted for new stories. Gherkin .feature files are the single source of truth for all E2E scenarios.

Browser-First E2E Layer

All browser-level tests use playwright-bdd as the execution engine. Feature files are defined first (failing), step definitions implement them in TypeScript. The playwright.config.ts registers three test projects:

  • chromium - legacy API-contract tests (no BDD change)
  • admin-ui - Admin UI BDD tests via playwright-bdd
  • element-web - Element Web browser-first E2E via playwright-bdd
e2e/
  features/
    element/            ← login, room/{create,join,leave}, messages/{send,receive}
    admin/              ← bootstrap, dashboard, auth-guard, users, rooms, audit-log
  step-definitions/
    common/             ← auth, navigation, stack-health, room-setup, assertions (shared)
    element/            ← login, room, messages steps
    admin/              ← bootstrap, dashboard, users, rooms steps
  fixtures/
    users.ts            ← NEBU_USERS const (4 pre-configured Dex test users)
    dex-auth.ts         ← loginViaOidcBrowser(), ensureStorageState(), getApiSession()
    element-app.ts      ← ElementAppPage (Playwright page object for Element Web)
    nebu-fixtures.ts    ← createBdd(test) - exports { Given, When, Then }
  global-setup.ts       ← warms token sidecars + bootstraps admin before tests

Token Sidecar Pattern for IndexedDB Sessions (Story 9-26b)

Element Web 1.11+ stores mx_access_token in IndexedDB, not localStorage. Playwright's storageState() captures only localStorage and cookies - not IndexedDB. The token sidecar pattern solves this:

  1. loginViaOidcBrowser() intercepts the POST /_matrix/client/v3/login response via Playwright route interception, captures the access_token + user_id from the JSON body.
  2. The token is written to e2e/auth-state/{user}.token.json (the "sidecar" file).
  3. getApiSession() reads the sidecar to obtain a valid token for Matrix API setup calls (createRoom, inviteUser) - without touching localStorage or IndexedDB.
  4. global-setup.ts warms sidecars for alex, marie, and kai before any test runs.
  5. Each test context performs a fresh OIDC browser login (no storageState restore) because IndexedDB sessions cannot be injected via browser.newContext({ storageState }).

The auth-state/ directory is gitignored. Sidecars expire after 12 hours (staleness check in ensureStorageState()).

API Seeding vs. UI Assertion Boundary

Matrix API calls via page.request are permitted in Given/When (setup) steps only:

Step typeAPI calls allowed?Rationale
Given - test pre-conditionYesSet up rooms, send invites, seed data
When - user actionNo (UI only)The feature under test
Then - assertionNo (UI only)Assertions must target the visible UI

Example for room/join.feature: kai creates the room and sends the invite via Matrix API (Given), the assertion is that alex sees the invite banner in Element Web and clicks "Accept" (When/Then).

OIDC Authorization Code + PKCE Requirement

All Gherkin E2E tests use Authorization Code + PKCE. grant_type=password (ROPC) is not supported by Dex v2.41+ and is forbidden in all tests. The consent screen is handled inside loginViaOidcBrowser() (first login only) via a role-based button locator (/grant access|allow|approve|confirm/i).

Makefile Targets

Thread Aggregations in Sync (Story 9-28)

Matrix spec §11.12 defines "bundled aggregations" - a mechanism for delivering relation metadata (reply counts, latest replies) inline in a parent event's unsigned.m.relations field during sync, avoiding extra HTTP round-trips from clients.

How Nebu implements bundled aggregations for m.thread:

  1. attach_thread_aggregations/3 is called for every list of timeline events returned from an initial sync or an incremental delta sync in Elixir Server.
  2. For each event, it calls count_thread_children(room_id, event_id) and (when count > 0) fetches the latest reply via fetch_events_by_relation/5 with limit = 1 and empty opts.
  3. The aggregation struct %{"m.thread" => %{count: N, latest_event: %{...}, current_user_participated: bool}} is JSON-encoded and stored in Core.Event.unsigned_relations (originally protobuf field 9; now a plain struct key).
  4. In the Matrix sync handler, the event's unsigned.m.relations field is populated from the unsigned_relations value only when it is non-empty; absent relations produce no extra JSON key (the key is omitted entirely).

Database invariant: The expression index on events((content->'m.relates_to'->>'event_id')) (migration 000042) is required; without it, attach_thread_aggregations/3 would cause N sequential scans per sync response (one per timeline event).

Scope: Only m.thread relation type is aggregated in the current implementation. The /relations endpoint (GetRelations RPC) supports any rel_type (including empty for all types) via fetch_events_by_relation/5's dynamic WHERE builder. Element Web typically calls it for m.thread. The function is parameterised on rel_type and event_type and can be extended to other relation types (reactions, edits) in a future story.

Relations API - Query Parameters (Story 9-29)

Story 9-29 extended the GET /relations handler with Matrix CS API-compliant query parameters and two additional route variants:

ParameterTypeDefaultValidation
dirstring"b"Must be "f" or "b"; 400 M_BAD_PARAM otherwise
limitinteger20Clamped to 100; 0 uses default
recursebooleanfalseMust be parseable; 400 M_BAD_PARAM otherwise
fromstring""Opaque pagination token; passed through to Server

dir semantics: "b" (backward) = newest-first DESC; "f" (forward) = oldest-first ASC. This mirrors the GET /messages semantics from the Matrix CS API spec.

recurse=true: Accepted without error (Matrix spec MUST requirement). MVP implementation treats it the same as dir=b; true recursive relation traversal is deferred to Phase 2.

prev_batch: When using dir=b pagination, the response includes a prev_batch token in GetRelationsResponse (field 3) allowing clients to page backwards. Currently always empty (no prev-page cursor implemented in MVP); the field is present in the Core.GetRelationsResponse struct for forward compatibility.

Configurable OIDC Claim Mapping (Story 11-10)

Nebu operators can configure which OIDC claims map to Matrix user identity and profile fields via the Admin UI settings page (GET/POST /admin/config/claim-mapping) or during the Bootstrap Wizard (Step 3). Three keys are stored in server_config:

KeyDefaultPurpose
oidc_user_id_claimsubClaim used as the source for Matrix localpart derivation
oidc_displayname_claimnameClaim used as the user's display name in Matrix profiles
oidc_email_claimemailClaim used to populate the user's email profile field

Migration 000044 seeds these defaults into server_config using INSERT … ON CONFLICT DO NOTHING. Existing deployments that already have these keys set manually are unaffected; new installs receive the Nebu defaults automatically.

FormatUserIDFromClaims refactored signature (AC6):

The function extracts claims[claimName] as a string, sanitises it via sanitiseLocalpart, and returns "@" + sanitised + ":" + serverName. If the result is empty (claim absent, non-string, or invalid characters), it falls back to FormatUserID(sub, serverName) - the SHA-256 opaque localpart path - using claims["sub"] as input.

Fallback chain (Elixir, Nebu.Matrix.UserId.localpart/1):

  1. server_config.oidc_user_id_claim (the operator-configured claim), served from Nebu.Matrix.OidcClaimCache (see below).
  2. If unset/empty: the preferred_username claim.
  3. then the name claim.
  4. then sub; and if the chosen value sanitises to empty, sub is used as the final source.

Claim cache (Nebu.Matrix.OidcClaimCache, story oidc-claim-boot-cache): the Go gateway's 60-second-TTL claimLoader closure was removed with the Go stack. In the Elixir stack the configured claim is cached in node-local persistent_term and read directly on the caller in configured_claim/0 — an O(1), lock-free read with no DB round-trip on the request hot path once populated. The cache is populated lazily on first read (three states: :unset / {:value, string} / {:value, nil}), so a confirmed value — including confirmed-absent — is never re-queried. Invalidation is event-driven, not TTL-based: all three write paths route through Nebu.Admin.DB.upsert_server_config/1, which calls the Nebu.Matrix public function invalidate_oidc_claim_cache/0 when the oidc_user_id_claim key changes; that re-reads the DB and broadcasts an invalidation to peer nodes over the nebu_matrix_oidc_claim_cache :pg group (ADR-020 — identical behaviour single-node and clustered). This eliminates the per-request DB read and the transient fail-open-to-weaker-claim window in steady state (residual bounded to the node-start-until-first-successful-read cold-start window; SEC-reviewed and accepted).

Claim name validation: oidcClaimNameRe (^[a-zA-Z0-9:_\-.]+$, 1–50 chars) is applied to all three claim fields on both the Admin UI POST handler and the Bootstrap Wizard Step 3. Invalid inputs return HTTP 422 with per-field error messages.

Identity stability warning: The oidc_user_id_claim determines the permanent Matrix user ID. Changing it post-bootstrap generates different Matrix IDs for existing OIDC principals, breaking all their room memberships. The Admin UI settings page and Bootstrap Wizard Step 3 both display a prominent warning banner about this consequence.

Audit logging: ClaimMappingHandler.UpdateHandler reads the previous values via LoadClaimMapping before persisting and includes previous_oidc_user_id_claim, previous_oidc_displayname_claim, and previous_oidc_email_claim in the audit_logs entry alongside the new values.

Build Metadata Injection (Story 11-9)

Nebu exposes build metadata (version, git_commit, build_time) via a GET /info endpoint on the application (port 4000). It requires no authentication and returns JSON:

Injection mechanism: ARG → ENV in the Dockerfile; Nebu.BuildInfo.get/0 reads System.get_env/2 at call time, defaulting to "unknown" when unset. Both the builder and runtime Dockerfile stages carry the three ENV vars so the values are available whether baked into the release BEAM files or read at process start.

Admin UI footer: The same build info is threaded into the shared admin layout so every authenticated admin page (both HTTP controller pages and LiveViews) renders a footer of the form nebu v<version> · <git_commit> · built <build_time>, suppressed on the login and error pages.

make redeploy integration: The redeploy Makefile target exports GIT_COMMIT (from git rev-parse --short HEAD) and BUILD_TIME (from date -u) before calling docker compose build. When these are unset (e.g. plain make redeploy without env exports), docker-compose.yml passes ${GIT_COMMIT:-unknown} - images build successfully and GET /info returns "unknown" values per AC3.

Relations API - JSONB Content Normalisation (Story 9-30)

Bug fix: Postgrex returns JSONB columns as a %Postgrex.JSONB{decoded: map} struct when the column value is a JSON object. Prior to Story 9-30, event_map_to_proto/1 in Nebu.Matrix.EventDispatcher.Server only handled plain Elixir maps and binary strings, causing a Protocol.UndefinedError (HTTP 500) whenever /relations was called on rooms that stored events with JSONB-typed content columns.

Fix: A shape-based guard was added as the first branch in the cond inside event_map_to_proto/1:

This extracts .decoded before Jason.encode!/1. The guard precedes the is_map/1 branch because %Postgrex.JSONB{} structs also satisfy is_map/1 (all structs are maps in Elixir).

Normalisation contract - all three Postgrex return forms are now handled:

Postgrex return formGuard matchedAction
%Postgrex.JSONB{decoded: map}is_struct and has :decodedExtract .decoded
Plain %{...} mapis_mapUse as-is
Binary "..." stringis_binaryJason.decode/1

Shape matching rationale: Postgrex is not a direct dependency of the event_dispatcher OTP application. Matching by module name (%Postgrex.JSONB{}) would introduce a compile-time dependency. The shape guard (is_struct/1 + Map.has_key?(:decoded) + is_map(.decoded)) is safe because the events.content column round-trips through PostgreSQL JSONB - no user-supplied term can produce an arbitrary Elixir struct with a :decoded key via this code path (Postgrex returns native Elixir terms, not ETF-deserialised arbitrary structs).

Scope: The fix applies to every call site of event_map_to_proto/1 - currently get_relations (Story 9-29) and get_messages. No new endpoints, migrations, or schema changes were introduced.

Source: _bmad-output/planning-artifacts/architecture.md, §Cross-Cutting Concerns, §Auth-Token-Flow, §Enforcement; _bmad-output/planning-artifacts/prd.md, §Cryptographic Identity Architecture; Story 9-22 (per-device sync token isolation, logout cleanup); Story 9-26 (Browser-First E2E layer, playwright-bdd, token sidecar pattern); Story 9-27 (gRPC error surface rule - GRPC.RPCError vs MatchError; failure audit trail pattern for multi-step operations); Story 9-28 (Thread aggregations in sync, bundled m.thread via unsigned_relations, GetRelations RPC, migration 000042 expression index); Story 9-29 (Relations API query params: dir, recurse, from; base and three-segment routes; prev_batch response field; fetch_events_by_relation/5 dynamic WHERE builder with rel_type + event_type + dir opts); Story 9-30 (JSONB content normalisation bug fix - %Postgrex.JSONB{decoded} struct guard in event_map_to_proto/1; resolves /relations HTTP 500 on JSONB-typed content columns); Story 11-7 (SSO nonce-based replay prevention; Cache-Control: no-store on SSO 302 redirect; denylist check in PostLogin with 403 M_FORBIDDEN; ssoStateStore capacity cap at 10,000; Safari re-login bugfix); Story 11-9 (build metadata injection - ldflags for Go, ARG→ENV for Elixir; NewInfoHandler static pre-marshalled response; Nebu.BuildInfo.get/0; Admin UI footer via SetBuildInfo/newPageData/ErrorMode; make redeploy GIT_COMMIT/BUILD_TIME exports); Story 11-10 (configurable OIDC claim mapping - FormatUserIDFromClaims refactored signature, TTL-cached claimLoader, oidcClaimNameRe validation, migration 000044 defaults seed, identity-stability warning, audit log with previous values, Bootstrap Wizard Step 3, ClaimMappingHandler admin settings page); Story 11-11 (Room GenServer start-on-demand concept - send_receipt/2 lookup_room → start_room fix; {:error,} wildcard trade-off; :noproc guard pattern; handler inventory for start_room vs lookup_room); Story 12-6 (blurhash pass-through contract - content.info is opaque JSONB, client extension fields preserved verbatim; animated=false MUST NOT return animated thumbnail regression test; explicit traceability tests for AC1+AC2); Story 12.16 (Authenticated media endpoints - auth.Middleware TokenVerifier interface, M_MISSING_TOKEN/M_UNKNOWN_TOKEN error codes, fail-closed nil-verifier guard; config.Handler media config endpoint; CSP + Cross-Origin-Resource-Policy headers on all download/thumbnail 200 responses; v3 unauthenticated + v1 authenticated routing pattern in main.go; {fileName} path value for Content-Disposition); Story 13-14 (squashed seed migration - FORCE RLS intentionally absent on audit_log in single-role model; append-only enforced at application layer; audit_log_purge SECURITY DEFINER bypasses RLS as table owner without BYPASSRLS); bugfix-sso-bootstrap-credentials (OIDC client credential source hierarchy - newServerConfigClientIDLoader / newServerConfigClientSecretLoader TTL-cached loaders; DB takes precedence over env var; AES-256-GCM decryption on secret load; slog.Warn on decrypt failure; startup observability check; LoginHandler ClientIDLoader/ClientSecretLoader API; resolveClientID/resolveClientSecret methods)_