Skip to content

feat(core): user-only Principal, database-authoritative sessions, delete KvSessionStore #36

Closed

Description

@aRustyDev

Context

Principal { user_id, workspace_id } (crates/thrust-contracts/src/lib.rs:81-86) is what the SessionStore port returns (crates/thrust-ports/src/lib.rs:187-191). authenticate maps a bearer token to it (crates/thrust-core/src/lib.rs:177-183), and authorize checks membership against who.workspace_id (line 165). On the Cloudflare set, sessions live in KV: KvSessionStore (crates/thrust-adapters-cloudflare/src/lib.rs:65-94) on the SESSIONS binding (crates/thrust-worker/wrangler.toml:27-29, wrangler.stage2.toml:33-35), wired at crates/thrust-worker/src/lib.rs:92. The native server keeps sessions in process memory in both modes (docs/src/architecture/infra.md:91-92). A session exists only if /internal/seed created it (crates/thrust-api/src/lib.rs:150-156). The slice's routes carry no workspace segment (crates/thrust-api/src/lib.rs:278-291), and the spike schema has no workspaces or workspace-membership table (crates/thrust-adapters-postgres/migrations/0001_init.sql).

Problem

  • A session bound to one workspace cannot serve the stock React app. Plane resolves the workspace from kwargs["slug"] on every request (apps/api/plane/app/permissions/base.py:28,47,55 at oracle/v1-pin-20261018). Its Session model holds session_key, user_id and device_info, with no workspace (apps/api/plane/db/models/session.py:17). A user can belong to several workspaces and switches between them by URL.
  • KV is eventually consistent, and the adapter forces a TTL of at least 60 s (crates/thrust-adapters-cloudflare/src/lib.rs:81-82), so revocation and sign-out-everywhere depend on KV propagation. The accepted design keeps sessions in the database with no cache at P0: ADR-0052 (lines 37, 41), D-003 (DEVIATIONS.md:11). ADR-0082 round 26 already says the spike's KV store "is not the accepted design" (ADR-0082:93).
  • A native postgres server loses every session on restart (docs/src/architecture/infra.md:94).
  • Every use-case takes &Principal, so changing the shape is cheap now and expensive after the second feature.

Proposal

Implements ADR-0052, ADR-0082 and ADR-0051 (round-29 notes) and D-003.

  • Principal { user_id } only. authorize takes the workspace slug from the route as well as the project, resolves slug to workspace and checks membership in one query, and returns the Scope from feat(core): make Scope the argument of every repository and Tx method #35. This needs a workspaces (id, slug) table and workspace membership in the slice schema, and a workspace segment in the slice routes. Agree the path with chore(api): move the slice's routes out of the /api/v1 prefix; reserve it for wire-compatible routes #47.
  • Sessions become rows behind the Database port in a sessions table with directory scope (ADR-0041's scope: tenant|directory, line 42). Store a hash of the token (ADR-0141:26: sessions are "revocable and stored hashed") with user_id, created_at, expires_at (30 days, ADR-0052), revoked_at, and last_seen_at, which is updated lazily. The lookup runs before authorization, so it carries no tenant key.
  • Delete KvSessionStore, the SESSIONS binding in both wrangler*.toml files, and the sessions field of Ports (crates/thrust-core/src/lib.rs:20). Keep the memory SessionStore only if the tests still want it, and drop CacheError if nothing else uses it.
  • SeedSession (crates/thrust-contracts/src/lib.rs:201-203) seeds rows through the same repository, and the contract fixture (crates/thrust-contract-tests/src/lib.rs:67-68) drops the per-session workspace.
  • Update docs/src/flows/app.md:31,42, docs/src/architecture/app.md:82 and docs/src/architecture/infra.md:91-99.

Acceptance criteria

  • Principal has no workspace_id, and grep -rn "KvSessionStore\|SESSIONS" crates/ returns nothing.
  • Contract test a_session_is_not_bound_to_a_workspace: one token works in both workspaces a user belongs to. Control: the same token gets 404 on a project in a workspace the user does not belong to.
  • Contract test: after the session row is revoked, the next request is 401 on every target. The revocation goes through a test route under THRUST_TEST_ROUTES until sign-out from spike(auth): the two-day Rust auth slice (argon2id, sessions, CSRF, API keys, one OIDC login) #38 exists. Control: an unrevoked token on the same request gets 200.
  • No plaintext token column in the migration (ADR-0052 Tests, line 53).
  • cross_tenant_access_is_rejected_and_has_no_effect (crates/thrust-contract-tests/tests/contract.rs:136) and authenticate_resolves_a_live_session_and_nothing_else (crates/thrust-core/tests/authorize.rs:52) stay green, with the 404/403 split written down for both URL forms, and scripts/mutants.sh reports every mutant KILLED.
  • scripts/contract-all.sh passes on all four targets, and gate is green.

References

  • Review: .agents/plans/contrail/reviews/2026-10-19-fable-architecture-review.md (FR-03)
  • crates/thrust-contracts/src/lib.rs:81-86,201-203; crates/thrust-ports/src/lib.rs:187-191; crates/thrust-core/src/lib.rs:165,177-183; crates/thrust-adapters-cloudflare/src/lib.rs:65-94; crates/thrust-worker/src/lib.rs:92; crates/thrust-worker/wrangler.toml:27-29
  • ADR-0052 (.agents/plans/contrail/adrs/auth/0052-sessions-and-api-keys.md:37,41,53), ADR-0082 (adrs/storage/0082-cache-and-session-port-kv.md:93), ADR-0051, ADR-0041:42, ADR-0141:26; D-003 (.agents/plans/contrail/DEVIATIONS.md:11); spike 0003 step 2
  • Plane oracle (oracle/v1-pin-20261018 = ba85d4d3d3): apps/api/plane/app/permissions/base.py:28,47,55, apps/api/plane/db/models/session.py:17
  • docs/src/flows/app.md:31,42; docs/src/architecture/infra.md:91-99

Blocked by: #35

Finding: FR-03

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:appprio:p0review:fable-2026-10-19type:security

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions