Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/installcheck
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ else
fi

# Execute the test fixtures
psql -v ON_ERROR_STOP= -f test/fixtures.sql -f lints/0001*.sql -f lints/0002*.sql -f lints/0003*.sql -f lints/0004*.sql -f lints/0005*.sql -f lints/0006*.sql -f lints/0007*.sql -f lints/0008*.sql -f lints/0009*.sql -f lints/0010*.sql -f lints/0011*.sql -f lints/0013*.sql -f lints/0014*.sql -f lints/0015*.sql -f lints/0016*.sql -f lints/0017*.sql -f lints/0018*.sql -f lints/0019*.sql -f lints/0020*.sql -f lints/0021*.sql -f lints/0022*.sql -f lints/0023*.sql -f lints/0024*.sql -f lints/0025*.sql -f lints/0026*.sql -f lints/0027*.sql -f lints/0028*.sql -f lints/0029*.sql -f lints/0030*.sql -f lints/0031*.sql -f lints/0032*.sql -d contrib_regression
psql -v ON_ERROR_STOP= -f test/fixtures.sql -f lints/0001*.sql -f lints/0002*.sql -f lints/0003*.sql -f lints/0004*.sql -f lints/0005*.sql -f lints/0006*.sql -f lints/0007*.sql -f lints/0008*.sql -f lints/0009*.sql -f lints/0010*.sql -f lints/0011*.sql -f lints/0013*.sql -f lints/0014*.sql -f lints/0015*.sql -f lints/0016*.sql -f lints/0017*.sql -f lints/0018*.sql -f lints/0019*.sql -f lints/0020*.sql -f lints/0021*.sql -f lints/0022*.sql -f lints/0023*.sql -f lints/0024*.sql -f lints/0025*.sql -f lints/0026*.sql -f lints/0027*.sql -f lints/0028*.sql -f lints/0029*.sql -f lints/0030*.sql -f lints/0031*.sql -f lints/0032*.sql -f lints/0035*.sql -d contrib_regression

# Run tests
${REGRESS} --use-existing --dbname=contrib_regression --inputdir=${TESTDIR} ${TESTS}
Expand Down
91 changes: 91 additions & 0 deletions docs/0035_policy_on_anon_role.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
**Level:** WARN

**Summary:** An RLS policy applies to the `anon` role, directly or through `public`.

**Ramification:** `anon` is the role used for signed-out requests and for any JWT that has no `role` claim, such as an OIDC ID token issued to an OAuth client. A policy that applies to `anon` and relies on `auth.uid()` or `auth.jwt()` can therefore grant access to a token that was never meant to reach your data API or Storage.

---

### Rationale

PostgREST and Storage run a request as the role named in the JWT `role` claim and fall back to `anon` when the claim is missing. Tokens that carry a `sub` but no `role` therefore run as `anon`, and `auth.uid()` still returns the `sub`.

A policy with no `TO` clause applies to `public`, which includes `anon`. Writing `for all using (owner_id = auth.uid())` without `to authenticated` looks like a signed-in-only rule, but it also applies to `anon`. Identity-based policies should never rely on `anon`.

### What is flagged

A permissive policy on a table is flagged when it applies to `anon`:

- the policy names `anon` explicitly (`TO anon`),
- the policy has no `TO` clause or uses `TO public`, or
- the policy names a role that `anon` is a member of.

Policies on `storage.objects` and `realtime.messages` are included.

Two kinds of policy are not flagged:

- restrictive policies (`AS RESTRICTIVE`), which can only narrow access, and
- `SELECT` policies whose expression is a literal `true`, which is the usual deliberate public-read pattern.

Every other `SELECT` policy that applies to `anon` is flagged, whether or not it uses `auth.` functions. That includes plain column checks such as `using (published)`, comparisons with constants, and `current_setting(...)` expressions. Policies for `INSERT`, `UPDATE`, `DELETE` and `ALL` are always flagged when they apply to `anon`.

### How to Resolve

**Option 1: Scope the policy to signed-in users**

```sql
drop policy "own_rows" on public.docs;

create policy "own_rows" on public.docs
for all to authenticated
using (owner_id = (select auth.uid()))
with check (owner_id = (select auth.uid()));
```

**Option 2: Keep the policy for several roles and check the role explicitly**

```sql
using (
(select auth.role()) = 'authenticated'
and owner_id = (select auth.uid())
)
```

### Example

Given this problematic configuration:

```sql
create policy "folder_is_uid" on storage.objects
for all
using (
bucket_id = 'files'
and (storage.foldername(name))[1] = auth.uid()::text
);
```

Fix:

```sql
drop policy "folder_is_uid" on storage.objects;

create policy "folder_is_uid" on storage.objects
for all to authenticated
using (
bucket_id = 'files'
and (storage.foldername(name))[1] = (select auth.uid())::text
);
```

### False Positives

- Public-read policies that are more than a literal `true`, for example `using (published)`. Add `TO anon` only if you intend public access, and dismiss the finding for that object.
- Policies that are deliberately open to signed-out users and also check identity, for example one that lets anyone read published rows and owners read their own drafts. Dismiss the finding for that object, or split it into a `TO anon` policy without the identity check and a `TO authenticated` policy with it.
- Policies that already check the role with `auth.role()` or `auth.jwt() ->> 'role'` but have no `TO` clause are still flagged. Adding `TO authenticated` makes the intent explicit and clears the finding.

### Limitations

- The lint does not look inside expressions, so any expression other than a literal `true` is flagged, including ones that already restrict access to signed-in users.
- The lint does not check table grants. A policy that applies to `anon` is reported even if `anon` has no privilege on the table, because a later `GRANT` activates it.

See also lint [0024_permissive_rls_policy](0024_permissive_rls_policy.md) for policies that are always true.
112 changes: 112 additions & 0 deletions lints/0035_policy_on_anon_role.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
create view lint."0035_policy_on_anon_role" as

-- A policy applies to `anon` when its role list is `public` (no TO clause) or
-- names a role that `anon` is a member of. SELECT policies are skipped only
-- when the expression is a literal `true` (deliberate public read).
with anon_role as (
select
r.oid
from
pg_catalog.pg_roles r
where
r.rolname = 'anon'
),

policies as (
select
nsp.nspname as schema_name,
pc.relname as table_name,
pa.polname as policy_name,
case pa.polcmd
when 'r' then 'SELECT'
when 'a' then 'INSERT'
when 'w' then 'UPDATE'
when 'd' then 'DELETE'
when '*' then 'ALL'
end as command,
(
select
array_agg(x.role_name order by x.role_name)
from
(
select
case when u.role_oid = 0 then 'public' else u.role_oid::pg_catalog.regrole::text end as role_name
from
unnest(pa.polroles) as u(role_oid)
where
u.role_oid = 0
or pg_catalog.pg_has_role(anon_role.oid, u.role_oid, 'usage')
) x
) as anon_roles,
coalesce(pg_catalog.pg_get_expr(pa.polqual, pa.polrelid), '') as qual,
coalesce(pg_catalog.pg_get_expr(pa.polwithcheck, pa.polrelid), '') as with_check
from
pg_catalog.pg_policy pa
join pg_catalog.pg_class pc
on pa.polrelid = pc.oid
join pg_catalog.pg_namespace nsp
on pc.relnamespace = nsp.oid
cross join anon_role
left join pg_catalog.pg_depend dep
on pc.oid = dep.objid
and dep.deptype = 'e'
and dep.classid = 'pg_catalog.pg_class'::pg_catalog.regclass
where
pc.relkind in ('r', 'p')
and pa.polpermissive
and dep.objid is null -- exclude tables owned by extensions
-- NOTE: does not exclude realtime or storage in support of monitoring user-authored
-- policies on realtime.messages and storage.objects
and nsp.nspname not in (
'_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'repack', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'
)
),

flagged as (
select
p.*
from
policies p
where
p.anon_roles is not null
)

select
'policy_on_anon_role' as name,
'RLS Policy Applies to Anon Role' as title,
'WARN' as level,
'EXTERNAL' as facing,
array['SECURITY'] as categories,
'Detects permissive RLS policies that apply to the `anon` role, directly or through `public`. `anon` is also the role a JWT without a `role` claim (such as an OIDC ID token) resolves to, so restrict identity-based policies with `TO authenticated`.' as description,
format(
'Table `%s.%s` has a permissive RLS policy `%s` for `%s` that applies to the `anon` role (via `%s`). Add `TO authenticated` or an explicit role check if it is not meant for signed-out or role-less tokens.',
schema_name,
table_name,
policy_name,
command,
array_to_string(anon_roles, ', ')
) as detail,
'https://supabase.com/docs/guides/database/database-linter?lint=0035_policy_on_anon_role' as remediation,
jsonb_build_object(
'schema', schema_name,
'name', table_name,
'type', 'table',
'policy_name', policy_name,
'command', command,
'roles', anon_roles
) as metadata,
format(
'policy_on_anon_role_%s_%s_%s',
schema_name,
table_name,
policy_name
) as cache_key
from
flagged
where
command <> 'SELECT'
or qual !~ '^\(*(true)?\)*$'
order by
schema_name,
table_name,
policy_name;
1 change: 1 addition & 0 deletions mkdocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ nav:
- Autovacuum Disabled: '0030_autovacuum_disabled.md'
- Public Can Execute Dynamic SQL Function: '0031_anon_dynamic_sql_function_executable.md'
- Signed-In Users Can Execute Dynamic SQL Function: '0032_authenticated_dynamic_sql_function_executable.md'
- RLS Policy Applies to Anon Role: '0035_policy_on_anon_role.md'

theme:
name: 'material'
Expand Down
114 changes: 113 additions & 1 deletion splinter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2336,4 +2336,116 @@ from
order by
schema_name,
function_name,
function_args)
function_args)
union all
(
-- A policy applies to `anon` when its role list is `public` (no TO clause) or
-- names a role that `anon` is a member of. SELECT policies are skipped only
-- when the expression is a literal `true` (deliberate public read).
with anon_role as (
select
r.oid
from
pg_catalog.pg_roles r
where
r.rolname = 'anon'
),

policies as (
select
nsp.nspname as schema_name,
pc.relname as table_name,
pa.polname as policy_name,
case pa.polcmd
when 'r' then 'SELECT'
when 'a' then 'INSERT'
when 'w' then 'UPDATE'
when 'd' then 'DELETE'
when '*' then 'ALL'
end as command,
(
select
array_agg(x.role_name order by x.role_name)
from
(
select
case when u.role_oid = 0 then 'public' else u.role_oid::pg_catalog.regrole::text end as role_name
from
unnest(pa.polroles) as u(role_oid)
where
u.role_oid = 0
or pg_catalog.pg_has_role(anon_role.oid, u.role_oid, 'usage')
) x
) as anon_roles,
coalesce(pg_catalog.pg_get_expr(pa.polqual, pa.polrelid), '') as qual,
coalesce(pg_catalog.pg_get_expr(pa.polwithcheck, pa.polrelid), '') as with_check
from
pg_catalog.pg_policy pa
join pg_catalog.pg_class pc
on pa.polrelid = pc.oid
join pg_catalog.pg_namespace nsp
on pc.relnamespace = nsp.oid
cross join anon_role
left join pg_catalog.pg_depend dep
on pc.oid = dep.objid
and dep.deptype = 'e'
and dep.classid = 'pg_catalog.pg_class'::pg_catalog.regclass
where
pc.relkind in ('r', 'p')
and pa.polpermissive
and dep.objid is null -- exclude tables owned by extensions
-- NOTE: does not exclude realtime or storage in support of monitoring user-authored
-- policies on realtime.messages and storage.objects
and nsp.nspname not in (
'_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_config', '_timescaledb_internal', 'auth', 'cron', 'extensions', 'graphql', 'graphql_public', 'information_schema', 'net', 'pgmq', 'pgroonga', 'pgsodium', 'pgsodium_masks', 'pgtle', 'pgbouncer', 'pg_catalog', 'repack', 'supabase_functions', 'supabase_migrations', 'tiger', 'topology', 'vault'
)
),

flagged as (
select
p.*
from
policies p
where
p.anon_roles is not null
)

select
'policy_on_anon_role' as name,
'RLS Policy Applies to Anon Role' as title,
'WARN' as level,
'EXTERNAL' as facing,
array['SECURITY'] as categories,
'Detects permissive RLS policies that apply to the `anon` role, directly or through `public`. `anon` is also the role a JWT without a `role` claim (such as an OIDC ID token) resolves to, so restrict identity-based policies with `TO authenticated`.' as description,
format(
'Table `%s.%s` has a permissive RLS policy `%s` for `%s` that applies to the `anon` role (via `%s`). Add `TO authenticated` or an explicit role check if it is not meant for signed-out or role-less tokens.',
schema_name,
table_name,
policy_name,
command,
array_to_string(anon_roles, ', ')
) as detail,
'https://supabase.com/docs/guides/database/database-linter?lint=0035_policy_on_anon_role' as remediation,
jsonb_build_object(
'schema', schema_name,
'name', table_name,
'type', 'table',
'policy_name', policy_name,
'command', command,
'roles', anon_roles
) as metadata,
format(
'policy_on_anon_role_%s_%s_%s',
schema_name,
table_name,
policy_name
) as cache_key
from
flagged
where
command <> 'SELECT'
or qual !~ '^\(*(true)?\)*$'
order by
schema_name,
table_name,
policy_name)
Loading
Loading