Skip to content

tenant-policy

Default severity: warn

What it does

Reports, in a PostgreSQL schema file, a tenant table with row-level security that the database does not scope to one tenant: its owner skips the policies, or a policy does not compare the tenant key with the session for some command. sqlty check reports it on the statement to change: the ALTER TABLE that enabled row-level security, or the CREATE POLICY.

A tenant table is one with a tenant key, from tenancy or from a policy that compares a column with a session value, as in tenant-scope.

``sql schema CREATE TABLE projects (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, name TEXT NOT NULL); -- error: [tenant-policy] "projectshas row-level security withoutFORCE ROW LEVEL SECURITY`, so its owner sees every tenant" ALTER TABLE projects ENABLE ROW LEVEL SECURITY; CREATE POLICY tenant ON projects USING (org_id = current_setting('app.org_id')::int);

CREATE TABLE invoices (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, total NUMERIC NOT NULL); ALTER TABLE invoices ENABLE ROW LEVEL SECURITY, FORCE ROW LEVEL SECURITY; -- error: [tenant-policy] "policy tenant on invoices does not check org_id on INSERT, UPDATE; compare it to the session in WITH CHECK" CREATE POLICY tenant ON invoices USING (org_id = current_setting('app.org_id')::int) WITH CHECK (true);

Both tables are scoped once `projects` forces row-level security and the policy of
`invoices` checks the rows it lets in:

```sql schema
-- error:
CREATE TABLE projects (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, name TEXT NOT NULL);
ALTER TABLE projects ENABLE ROW LEVEL SECURITY, FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant ON projects USING (org_id = current_setting('app.org_id')::int);

CREATE TABLE invoices (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, total NUMERIC NOT NULL);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY, FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant ON invoices USING (org_id = current_setting('app.org_id')::int);

A policy for one command is judged for that command alone, and a restrictive policy that pins the tenant key covers the permissive ones:

``sql schema CREATE TABLE notes (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, body TEXT); ALTER TABLE notes ENABLE ROW LEVEL SECURITY, FORCE ROW LEVEL SECURITY; CREATE POLICY mine ON notes FOR SELECT USING (org_id = current_setting('app.org_id')::int); -- error: [tenant-policy] "policycleanuponnotesdoes not filter DELETE byorg_id`; compare it to the session in USING" CREATE POLICY cleanup ON notes FOR DELETE USING (true); CREATE POLICY writes ON notes FOR INSERT WITH CHECK (org_id = current_setting('app.org_id')::int); CREATE POLICY edits ON notes FOR UPDATE USING (org_id = current_setting('app.org_id')::int);

CREATE TABLE tags (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, name TEXT); ALTER TABLE tags ENABLE ROW LEVEL SECURITY, FORCE ROW LEVEL SECURITY; CREATE POLICY open ON tags USING (true); CREATE POLICY tenant ON tags AS RESTRICTIVE USING (org_id = current_setting('app.org_id')::int);

Row-level security is set on each table: a partition does not take it from its parent,
and `USING (true)` lets every row through for each command it covers:

```sql schema
CREATE TABLE events (id INT, org_id INT NOT NULL) PARTITION BY RANGE (id);
CREATE TABLE events_1 PARTITION OF events FOR VALUES FROM (0) TO (1000);
ALTER TABLE events ENABLE ROW LEVEL SECURITY;
ALTER TABLE events FORCE ROW LEVEL SECURITY;
-- error: [tenant-policy] "policy `open` on `events` does not filter SELECT, UPDATE, DELETE or check INSERT by `org_id`; compare it to the session in USING and WITH CHECK"
CREATE POLICY open ON events USING (true) WITH CHECK (true);
CREATE POLICY tenant ON events FOR SELECT USING (org_id = current_setting('app.org_id')::int);

Until then, tenant-scope judges the table's queries. Open this example in the playground to see both, the policy in schema.sql:

[tool.sqlty]
schema = "schema.sql"

schema.sql:

CREATE TABLE notes (id SERIAL PRIMARY KEY, org_id INTEGER NOT NULL, body TEXT);
ALTER TABLE notes ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant ON notes USING (org_id = current_setting('app.org_id')::int);
from sqlty import SQL

# error: [tenant-scope] "`notes` is read without `notes.org_id = <parameter>`"
notes = SQL[tuple[str | None]]("--sql\nSELECT body FROM notes")
Open in playground

Why is this bad?

tenant-scope leaves the queries of a table to the database only when its policies hold for everyone. While they don't, it reports each query that does not filter by the tenant key, and this rule says what to change in the schema so the database enforces it:

  • The owner of a table skips its policies unless the table has FORCE ROW LEVEL SECURITY, and an application often connects as the owner.
  • PostgreSQL combines permissive policies with OR, so one that does not compare the tenant key with the session, such as USING (true), lets every tenant's rows through.
  • WITH CHECK (true) lets an INSERT or UPDATE write a row of another tenant. Without WITH CHECK, a policy checks new rows with USING.

Superusers and BYPASSRLS roles skip every policy, FORCE or not: connect as a role that has neither. See Row Security Policies.

How to fix

Add ALTER TABLE ... FORCE ROW LEVEL SECURITY, and compare the tenant key with the session in each permissive policy: in USING for the rows it reads, updates or deletes, and in WITH CHECK for the rows it writes. Or add a restrictive policy that does.

Configuration

A warning by default: the queries are still judged by tenant-scope meanwhile. Turn it off:

[tool.sqlty.rules]
tenant-policy = "ignore"

-- sqlty: ignore[tenant-policy] on the first line of a statement, or alone on its line above the statement, drops it.

Limits

Only the PostgreSQL dialect reads policies. Policies for some roles only (TO app) count as permissive policies for every role when they let rows through, and as nothing when they are restrictive, since sqlty does not know which role the application uses. sqlty check reads the schema files from disk: an editor shows a schema file's diagnostics once it is saved. VS Code for the Web and Restricted Mode, which run the bundled engine instead of sqlty server, don't check schema files.