Skip to content

feat(core): make Scope the argument of every repository and Tx method #35

Closed

Description

@aRustyDev

Context

The permission seam in crates/thrust-core/src/lib.rs mints a Scope only through authorize (lines 157-175), and the type's doc comment (lines 136-137) says that repository calls taking a Scope "cannot be reached unchecked". No repository call takes one. The Database port (crates/thrust-ports/src/lib.rs:92-138) and Tx (lines 142-157) take bare WorkspaceId and ProjectId, and every use-case unwraps the scope through its public getters (crates/thrust-core/src/lib.rs:145-155) right before the call (lines 252, 312, 360, 374, 488). Two adapters implement the port: MemoryDatabase (crates/thrust-adapters-memory/src/lib.rs) and PgDatabase (crates/thrust-adapters-postgres/src/lib.rs).

Problem

Tenant scoping is enforced by convention, not by types.

  • Any caller that has the ids can call the port directly. Scope has private fields but pub fn workspace_id(), project_id() and user_id() hand them back (crates/thrust-core/src/lib.rs:145-155); they are also used from outside the crate (crates/thrust-core/tests/authorize.rs:46).
  • Tx::allocate ignores _ws in both adapters (crates/thrust-adapters-postgres/src/lib.rs:399, crates/thrust-adapters-memory/src/lib.rs:205), and project_sequences has no workspace_id column (crates/thrust-adapters-postgres/migrations/0001_init.sql:20-24).
  • The membership query joins project_members without m.workspace_id (crates/thrust-adapters-postgres/src/lib.rs:189-191).
  • undispatched_outbox, mark_outbox_dispatched, outbox_depth and seed take no tenant, and insert_activity carries one only inside the record (crates/thrust-ports/src/lib.rs:119-137). docs/src/architecture/app.md:80 already records this.
  • The plan says otherwise: "repositories accept only a Scope" (ADR-0051 round 24, line 102; ADR-0041 round 25, line 72), and the ADR-0045 sketch has begin(&self, scope: Scope) (ADR-0045:84).
  • The ignore-tenant mutant (scripts/mutants.sh:30, crates/thrust-adapters-memory/src/lib.rs:66-70) is killed today only because the one use-case passes the right ids. ADR-0141 names tenant-isolation bugs the highest-impact class (ADR-0141:16). Every later repository copies this port shape, so it is cheapest to change while one aggregate exists.

Proposal

Implements ADR-0041, ADR-0045 and ADR-0051 (round-29 notes).

  • Every Database and Tx method takes &Scope, never bare ids, and begin takes the scope as in the ADR-0045 sketch. The adapter derives workspace_id and project_id from the scope.
  • ADR-0045's round-29 note (line 116) leaves one decision to this issue: where the proof type is declared. thrust-ports cannot name a type defined in thrust-core (core depends on ports), and the adapters have to read the ids. So Scope moves down to thrust-ports with private fields and read accessors. Once the port takes &Scope, ids read back out of a scope can no longer reach it. What has to be enforced is that only authorize mints a Scope: give it one constructor, and forbid calls to it anywhere else with clippy disallowed-methods (run by the clippy job from chore(ci): enforce the clippy deny list, cargo fmt --check and cargo-deny; forbid sqlx, sea-orm, diesel in check-deps.sh #40) or a grep rule in scripts/check-deps.sh.
  • Untenanted operations take a separate &SystemScope, minted only by the two composition roots and the queue consumer (ADR-0045:110): the relay and the sweeper (undispatched_outbox, mark_outbox_dispatched, outbox_depth), the consumer's insert_activity, and seed. The consumer authorises by the tenant id the envelope carries (ADR-0141:40).
  • Add workspace_id to project_sequences (key (workspace_id, project_id)) and AND m.workspace_id = p.workspace_id to the membership join, in both adapters. 0001_init.sql is the spike schema, not the product schema (ADR-0042), so either edit it in place or add 0002.
  • Update docs/src/architecture/app.md:80. In docs/src/contributing/patterns.md, the "Repository calls take a Scope, never ids" paragraph (round 29) currently says "not yet the code"; change it to describe the code once this lands, and update the "Authorize in the core" and "Scope every tenant query" paragraphs to match.

Acceptance criteria

  • grep -nE 'ws: WorkspaceId|project: ProjectId' crates/thrust-ports/src/lib.rs matches only membership's arguments; every other Database and Tx method takes &Scope or &SystemScope. (Amended 2026-10-22, owner decision: membership runs before a Scope exists, only authorize calls it, and feat(core): user-only Principal, database-authoritative sessions, delete KvSessionStore #36 reshapes it around the workspace slug; ADR-0045 round 29 already names it the exception.)
  • Both adapters use the workspace in allocate, project_sequences has workspace_id, and the membership join tests m.workspace_id.
  • A compile_fail doctest in thrust-core, run by cargo test --doc --workspace, shows that a use-case can neither build a Scope nor call a repository method with bare ids. A plain doctest of the same call through authorize compiles; it is the control.
  • Adding a call to the Scope constructor in a use-case fails the lint or the grep rule. Show the failing run once.
  • model_work_items_match_the_reference_model (two tenants, crates/thrust-core/tests/model.rs:144) and cross_tenant_access_is_rejected_and_has_no_effect (crates/thrust-contract-tests/tests/contract.rs:136) stay green on all four targets, and scripts/mutants.sh still reports ignore-tenant KILLED.
  • cargo nextest run --workspace and scripts/contract-all.sh pass, and gate is green.

References

  • Review: .agents/plans/contrail/reviews/2026-10-19-fable-architecture-review.md (FR-02)
  • crates/thrust-ports/src/lib.rs:92-157; crates/thrust-core/src/lib.rs:136-175; crates/thrust-adapters-postgres/src/lib.rs:189-191,399; crates/thrust-adapters-memory/src/lib.rs:66-70,205; crates/thrust-adapters-postgres/migrations/0001_init.sql:20-24
  • ADR-0041 (.agents/plans/contrail/adrs/data/base/0041-tenant-isolation-strategy.md:42,72), ADR-0045 (adrs/data/interfaces/0045-repository-and-unit-of-work-ports.md:19,84,107-116), ADR-0051 (adrs/auth/0051-authorization-model-rbac-rebac.md:102), ADR-0141 (adrs/security/0141-tenancy-and-threat-model.md:16,40,51)
  • SPEC R-S1; docs/src/architecture/app.md:80; docs/src/contributing/patterns.md; scripts/mutants.sh:30

Finding: FR-02

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