Skip to content

A complete AI agency at your fingertips - From frontend wizards to Reddit community ninjas, from whimsy injectors to reality checkers. Each agent is a specialized expert with personality, processes, and proven deliverables.

Shell

Scope tenant RLS context to each database transaction

Rudy Celeklica830a927386
diff --git a/engineering/engineering-identity-access-engineer.md b/engineering/engineering-identity-access-engineer.md
index f1ac2a23..a25d443a 100644
--- a/engineering/engineering-identity-access-engineer.md
+++ b/engineering/engineering-identity-access-engineer.md
@@ -132,14 +132,24 @@ challengeStore.put(user.id, options.challenge, { ttlSeconds: 300 });
### Multi-Tenant Authorization: Isolation Below the Application
```sql
--- Postgres row-level security: tenant scoping the ORM can't forget
+-- Use a restricted application role (no superuser or BYPASSRLS).
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
+ALTER TABLE documents FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON documents
- USING (tenant_id = current_setting('app.tenant_id')::uuid);
-
--- Set from the AUTHENTICATED session at connection checkout — never from request input:
--- SET app.tenant_id = '<tenant uuid from the verified session>';
+ USING (tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid)
+ WITH CHECK (tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid);
+
+-- EVERY request runs all its queries on this same connection and transaction.
+-- Bind the authenticated tenant UUID using the driver's parameter API.
+BEGIN;
+SELECT set_config('app.tenant_id', :authenticated_tenant_id, true);
+-- SELECT/INSERT/UPDATE/DELETE documents here, then COMMIT (or ROLLBACK on error).
+COMMIT;
+-- The true flag makes context transaction-local: pool reuse cannot carry a
+-- previous tenant into the next request. Missing context denies access.
+-- FORCE also subjects the table owner to RLS; privileged maintenance roles
+-- still bypass it and must never be used by request-serving connections.
```
## 🔄 Your Workflow Process