buzz icon indicating copy to clipboard operation
buzz copied to clipboard

A-RLS-1..5 conformance gate is specified in multi-tenant-relay.md but not implemented; reference deployment discharges none of it

Open Brad9474 opened this issue 4 months ago • 1 comments

docs/multi-tenant-relay.md proves tenant isolation relative to stated axioms, including A-RLS-1..5 covering Postgres row-level security. §Conformance states how each axiom is discharged (docs/multi-tenant-relay.md:655):

A-RLS-1..5 are admitted by a startup/CI assertion suite: enumerate every tenant-bearing table and assert RLS enabled + restrictive policy present; assert the request role is NOBYPASSRLS and non-owner-or-FORCE; assert no SECURITY DEFINER function in the request path reads tenant tables without re-establishing context; assert every unique/FK constraint includes community_id. A failing assertion rejects the deployment.

I can't find that assertion suite, or any RLS implementation, in the tree.

Searched at 19d57b0d46baa55814ac737041a36d0b405c9f64 (current origin/main at time of writing): migrations/, crates/, deploy/, schema/, scripts/, .github/workflows/, Justfile, docker-compose.yml, Dockerfile.

Patterns: ENABLE ROW LEVEL SECURITY, CREATE POLICY, app.community_id, pg_policies, relrowsecurity, rolbypassrls, NOBYPASSRLS.

Result: zero hits outside docs/multi-tenant-relay.md itself.

Against the repo's reference docker-compose.yml, the axioms evaluate as:

Axiom Status
A-RLS-1 — RLS enabled + restrictive community_id policy on tenant tables Not met — no ENABLE ROW LEVEL SECURITY or CREATE POLICY in migrations/
A-RLS-2 — request role non-superuser, NOBYPASSRLS, non-owner Not met — the single role in the reference compose owns the tenant tables
A-RLS-3 — app.community_id set via SET LOCAL per transaction Not met — no SET LOCAL of that setting anywhere in crates/
A-RLS-4 — SECURITY DEFINER/leakproof audit Not applicable — no such functions found
A-RLS-5 — unique/FK constraints include community_id Appears met in migrations/0001_initial_schema.sql

To be clear about what this is not: the document is marked draft and its abstract is explicitly forward-looking about elevating community to the tenant boundary, so this isn't "you documented a control you never built." The row-level half has shipped — communities exists, community_id is on every scoped table, and the fail-closed host→community bind is live in crates/buzz-relay/src/router.rs. The gap is specifically that §Conformance describes a discharge mechanism for A-RLS-1..5 that doesn't exist.

Why this matters rather than being pedantry

The document states the RLS layer is load-bearing, in its own words (I4, docs/multi-tenant-relay.md:477):

A dropped application predicate yields ∅ under A-RLS, and NI still holds; removing the RLS guard makes the dropped predicate produce a cross-label row — proving RLS load-bearing, not decorative.

So under the model as specified, the application-level fence in router.rs is one of two layers, and I4 is the argument for what happens when the first one has a bug. Deployments matching the reference compose have one layer, not two — and §Conformance reads as though a gate would have caught that.

Precedent for the fix

§Conformance already handles exactly this situation for a different axiom, naming the non-conforming shipped artifacts by file and line (docs/multi-tenant-relay.md:684):

the shipped HA examples (replicaCount: 3 in deploy/charts/buzz/examples/argocd-app.yaml:27 and deploy/charts/buzz/examples/flux-helmrelease.yaml:35) are P3-non-conforming as shipped unless the operator adds one of: …

Flagging shipped artifacts that fail to discharge an axiom is therefore the document's established convention. A-RLS-1..5 has no equivalent note, which is what makes the current text misleading rather than merely incomplete — a reader reasonably concludes the gate exists.

Relationship to #2621

#2621 proposes CI for the formal artifacts themselves — the Tamarin/TLC/NIP-PL models that are load-bearing per the docs but run by hand, and therefore "silently decay" between manual runs. This issue is the complementary half: those models are proven relative to the axioms in §Axioms, and A-RLS-1..5 has no discharge mechanism either. Both are needed for VISION.md's "proven, not asserted" to hold continuously — #2621 keeps the proof green, this keeps the premises true. If maintainers prefer, resolution 2 below could fold into #2621's proposed formal.yml as an additional job rather than landing separately.

Suggested resolutions, cheapest first

  1. Docs only — add an A-RLS non-conformance note in §Conformance mirroring the P3 one, stating that the reference docker-compose.yml does not discharge A-RLS-1/2/3 and what an operator must add. Removes the misleading reading immediately.
  2. Implement the gate the doc already specifies — the startup/CI assertion suite. The doc specifies its checks precisely enough to implement directly, and it fails closed by design.
  3. Ship the layer — an RLS migration plus a dedicated NOBYPASSRLS non-owner request role and SET LOCAL app.community_id in the request path.

Happy to be told I've missed an implementation that lives somewhere I didn't search.

Brad9474 avatar Aug 01 '26 05:08 Brad9474

Independent confirmation, with some implementation groundwork to add. We host multi-tenant Buzz in production (GKE + Cloud SQL, stock images, zero relay forks) and run the conformance_multitenant suite against a two-tenant topology as part of our own gate. We had drafted essentially this issue before finding this one, so: second operator, same searches, same result.

Re-checked at 38bf642f (main, 2026-08-05): still zero CREATE POLICY / ENABLE ROW LEVEL SECURITY anywhere in migrations/0001..0028.

A few things from our draft that may save whoever picks this up some time.

Refinements to the axiom table

A-RLS-5 is stronger than "appears met": it is lint-enforced in CI. crates/buzz-db/src/migration.rs asserts, for every table not in _operator_global_tables, NOT NULL community_id and that every PK / UNIQUE / FK, including standalone CREATE UNIQUE INDEX (migration.rs:465), leads with community_id. That is the expensive, easy-to-regress half of the RLS story, and it is already done and gated. The pattern is also holding under change: since we counted, 0025 added relay_invites (tenant-bearing, composite keys leading with community_id) and 0026 added replica_heartbeat (registered in _operator_global_tables), both conforming.

The tenant-table census has a counting trap the assertion harness must avoid. At 49a3da01: 37 base tables, 10 declared operator-global, and the remaining 27 all carry NOT NULL community_id. A naive "has a community_id column" scan finds 29: product_feedback and rate_limit_violations carry the column as provenance only and are declared operator-global. So the harness has to enumerate from _operator_global_tables, not from column presence, or it will constrain two tables the schema deliberately leaves global.

A-RLS-4: agreed, currently trivially satisfied. No SECURITY DEFINER in any migration; the 14 trigger functions all run as invoker.

Why resolution 3 is a seam problem, not a migration problem

SET LOCAL takes effect only inside an explicit transaction block, and the data layer today has nothing to hang it on: at 49a3da01, buzz-db has 186 production entry points taking pool: &PgPool (225 raw matches including test helpers; 240 at today's tip, so the surface is growing), against 4 that take a transaction. Each call acquires its own pooled connection, so a GUC set anywhere else lands on the wrong connection.

Two shapes for closing A-RLS-3:

  • (a) A tenant-scoped connection guard: acquire, BEGIN, SET LOCAL app.community_id, hand out the held connection. The signature change from pool: &PgPool is mechanical and can land table by table, so no single giant PR.
  • (b) Session-level SET on checkout, RESET on release: fewer changes, but it leaves tenant context on a pooled connection between statements, which A-RLS-3 rules out by name.

(a) looks like the only shape that admits the axiom as written. Row zero is already solved: crates/buzz-relay/src/tenant.rs::bind_community is the single source of tenancy and already returns a TenantContext, so there is exactly one place to install the context.

Roles: with BUZZ_AUTO_MIGRATE the runtime role is normally the table owner, and owners bypass policies. A-RLS-2 therefore needs either FORCE ROW LEVEL SECURITY on every scoped table or a migrator/runtime role split. FORCE keeps single-URL self-hosted deployments working unchanged and looks like the smaller ask.

Paths that are cross-tenant by design need naming and an explicit exemption or a separate role: the operator provisioning API (crates/buzz-relay/src/api/operator.rs), the usage rollups, the partition manager, retention/TTL sweeps, and the migration runner. The read-replica pool (config.read_database_url) needs the same context installation.

A wording problem in A-RLS-1 worth settling first

A-RLS-1 as written asks for "a restrictive policy". In Postgres, a table with only RESTRICTIVE policies returns nothing at all: restrictive policies AND with the permissive set, and an empty permissive set denies. The implementable shapes are a single PERMISSIVE ... FOR ALL USING (...) WITH CHECK (...) carrying the equality, or permissive-true plus a RESTRICTIVE equality policy. The spec text should pin one, because the assertion harness has to check the shape that actually holds.

Sequencing, mapped to the resolutions above

Resolution 2 (the gate) is shippable alone with no behavior change, and A-RLS-5 is already covered by the existing lint, so the harness only needs the RLS-enabled / policy-shape / role checks. Resolution 3 then splits into three independently useful steps:

  1. The seam (the guard above), converted incrementally.
  2. Turn RLS on in CI first, as a coverage oracle. With policies enabled and the GUC unset, current_setting('app.community_id', true) is NULL and every unconverted path returns zero rows instead of another tenant's rows. The integration suite then localizes every uncovered path loudly. The fail-closed property is itself the completeness proof for the migration.
  3. Policies per table plus FORCE, in small migrations, highest-value tables first (events, channels, channel_members, users, subscriptions).

If regression risk for existing deployments is the blocker, the whole thing can sit behind BUZZ_RLS=off | assert | enforce, defaulting to off: existing deployments see no change, assert is exactly resolution 2's report, enforce is the layer.

Three implementation traps worth recording now:

  • The monthly partition manager (crates/buzz-db/src/partition.rs:125) creates partitions at runtime, so it must emit ENABLE ROW LEVEL SECURITY plus the policy alongside each new CREATE TABLE ... PARTITION OF, or the first new month lands an unprotected partition after everything else is green.
  • Write the predicate as community_id = (SELECT current_setting('app.community_id', true))::uuid. The scalar subquery becomes an InitPlan constant instead of a per-row call, and the predicate is then an equality on the leading column of every existing composite index, so plans keep using them.
  • INSERT ... ON CONFLICT stays safe under RLS precisely because A-RLS-5 holds: no cross-tenant conflict can exist, so a hidden row cannot surface as a unique violation and become an existence oracle.

One stale spec item on the way through: the I1/I4 bullet in §Implementation Correspondence flags get_accessible_channel_ids() as unscoped, but at 49a3da01 both union branches filter on community_id (crates/buzz-db/src/channel.rs:628-648).

We are happy to contribute the assertion harness (resolution 2) and to prototype the connection guard, behind the BUZZ_RLS knob if that is the shape maintainers prefer. We would run assert mode in production and report back.

vsima avatar Aug 06 '26 19:08 vsima