diff --git a/bin/installcheck b/bin/installcheck index 3a74aa7..c2a68b2 100755 --- a/bin/installcheck +++ b/bin/installcheck @@ -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} diff --git a/docs/0035_policy_on_anon_role.md b/docs/0035_policy_on_anon_role.md new file mode 100644 index 0000000..98cc46f --- /dev/null +++ b/docs/0035_policy_on_anon_role.md @@ -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. diff --git a/lints/0035_policy_on_anon_role.sql b/lints/0035_policy_on_anon_role.sql new file mode 100644 index 0000000..c51b090 --- /dev/null +++ b/lints/0035_policy_on_anon_role.sql @@ -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; diff --git a/mkdocs.yaml b/mkdocs.yaml index bf45eb5..2361dc9 100644 --- a/mkdocs.yaml +++ b/mkdocs.yaml @@ -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' diff --git a/splinter.sql b/splinter.sql index dd36e57..54c5930 100644 --- a/splinter.sql +++ b/splinter.sql @@ -2336,4 +2336,116 @@ from order by schema_name, function_name, - function_args) \ No newline at end of file + 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) \ No newline at end of file diff --git a/test/expected/0035_policy_on_anon_role.out b/test/expected/0035_policy_on_anon_role.out new file mode 100644 index 0000000..f7cc0b0 --- /dev/null +++ b/test/expected/0035_policy_on_anon_role.out @@ -0,0 +1,165 @@ +begin; + set local search_path = ''; + -- BASELINE: 0 issues on empty schema + select * from lint."0035_policy_on_anon_role"; + name | title | level | facing | categories | description | detail | remediation | metadata | cache_key +------+-------+-------+--------+------------+-------------+--------+-------------+----------+----------- +(0 rows) + + create table public.docs( + id int primary key, + owner_id uuid, + published boolean + ); + alter table public.docs enable row level security; + savepoint a; + -- NEGATIVE: TO authenticated is not reachable by anon + create policy "own_rows" on public.docs + for all to authenticated + using (owner_id = auth.uid()) + with check (owner_id = auth.uid()); + select * from lint."0035_policy_on_anon_role"; + name | title | level | facing | categories | description | detail | remediation | metadata | cache_key +------+-------+-------+--------+------------+-------------+--------+-------------+----------+----------- +(0 rows) + + rollback to savepoint a; + -- NEGATIVE: SELECT policies with a literal true are deliberate public read + create policy "explicit_anon_read" on public.docs + for select to anon + using (true); + create policy "public_read" on public.docs + for select + using (true); + select * from lint."0035_policy_on_anon_role"; + name | title | level | facing | categories | description | detail | remediation | metadata | cache_key +------+-------+-------+--------+------------+-------------+--------+-------------+----------+----------- +(0 rows) + + rollback to savepoint a; + -- NEGATIVE: restrictive policies only narrow access + create policy "restrict" on public.docs + as restrictive + for all to anon + using (owner_id = auth.uid()); + select * from lint."0035_policy_on_anon_role"; + name | title | level | facing | categories | description | detail | remediation | metadata | cache_key +------+-------+-------+--------+------------+-------------+--------+-------------+----------+----------- +(0 rows) + + rollback to savepoint a; + -- POSITIVE: explicit TO anon with an identity check + create policy "anon_own_rows" on public.docs + for all to anon + using (owner_id = auth.uid()); + select name, detail, cache_key from lint."0035_policy_on_anon_role"; + name | detail | cache_key +---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------- + policy_on_anon_role | Table `public.docs` has a permissive RLS policy `anon_own_rows` for `ALL` that applies to the `anon` role (via `anon`). Add `TO authenticated` or an explicit role check if it is not meant for signed-out or role-less tokens. | policy_on_anon_role_public_docs_anon_own_rows +(1 row) + + rollback to savepoint a; + -- POSITIVE: no TO clause defaults to public, which includes anon + create policy "no_to_clause" on public.docs + for all + using (owner_id = auth.uid()); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + policy_name | roles +--------------+------------ + no_to_clause | ["public"] +(1 row) + + rollback to savepoint a; + -- POSITIVE: SELECT policies with any expression beyond a literal true are flagged + create policy "select_column" on public.docs + for select + using (published); + create policy "select_setting" on public.docs + for select to anon + using (current_setting('app.tenant', true) = owner_id::text); + create policy "select_uid" on public.docs + for select + using (auth.uid() = owner_id); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + policy_name | roles +----------------+------------ + select_column | ["public"] + select_setting | ["anon"] + select_uid | ["public"] +(3 rows) + + rollback to savepoint a; + -- POSITIVE: explicit TO public with initplan-wrapped identity on a SELECT + create policy "public_select_identity" on public.docs + for select to public + using (owner_id = (select auth.uid())); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + policy_name | roles +------------------------+------------ + public_select_identity | ["public"] +(1 row) + + rollback to savepoint a; + -- POSITIVE: write policies are flagged without any identity reference + create policy "anon_insert" on public.docs + for insert to anon + with check (true); + select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0035_policy_on_anon_role"; + policy_name | command +-------------+--------- + anon_insert | INSERT +(1 row) + + rollback to savepoint a; + -- POSITIVE: a role that anon is a member of also applies to anon + create role api_base; + grant api_base to anon; + create policy "via_member_role" on public.docs + for update to api_base + using (owner_id = auth.uid()); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + policy_name | roles +-----------------+-------------- + via_member_role | ["api_base"] +(1 row) + + rollback to savepoint a; + -- POSITIVE: only the anon-matching roles are reported + create policy "anon_and_authenticated" on public.docs + for all to anon, authenticated + using (owner_id = auth.uid()); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + policy_name | roles +------------------------+---------- + anon_and_authenticated | ["anon"] +(1 row) + + rollback to savepoint a; + -- POSITIVE: user-authored policies on storage.objects (the role-less JWT case) + create schema storage; + create table storage.objects( + id int primary key, + bucket_id text, + name text + ); + alter table storage.objects enable row level security; + create policy "folder_is_uid" on storage.objects + for all + using (bucket_id = 'files' and (string_to_array(name, '/'))[1] = auth.uid()::text); + select name, detail, cache_key from lint."0035_policy_on_anon_role"; + name | detail | cache_key +---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------- + policy_on_anon_role | Table `storage.objects` has a permissive RLS policy `folder_is_uid` for `ALL` that applies to the `anon` role (via `public`). Add `TO authenticated` or an explicit role check if it is not meant for signed-out or role-less tokens. | policy_on_anon_role_storage_objects_folder_is_uid +(1 row) + + -- RESOLUTION: scope the policy to authenticated + 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 (string_to_array(name, '/'))[1] = auth.uid()::text); + select * from lint."0035_policy_on_anon_role"; + name | title | level | facing | categories | description | detail | remediation | metadata | cache_key +------+-------+-------+--------+------------+-------------+--------+-------------+----------+----------- +(0 rows) + +rollback; diff --git a/test/expected/queries_are_unionable.out b/test/expected/queries_are_unionable.out index e7738bd..fbca60c 100644 --- a/test/expected/queries_are_unionable.out +++ b/test/expected/queries_are_unionable.out @@ -60,7 +60,9 @@ begin; union all select * from lint."0031_anon_dynamic_sql_function_executable" union all - select * from lint."0032_authenticated_dynamic_sql_function_executable"; + select * from lint."0032_authenticated_dynamic_sql_function_executable" + union all + select * from lint."0035_policy_on_anon_role"; name | title | level | facing | categories | description | detail | remediation | metadata | cache_key ------+-------+-------+--------+------------+-------------+--------+-------------+----------+----------- (0 rows) diff --git a/test/sql/0035_policy_on_anon_role.sql b/test/sql/0035_policy_on_anon_role.sql new file mode 100644 index 0000000..b1214b2 --- /dev/null +++ b/test/sql/0035_policy_on_anon_role.sql @@ -0,0 +1,129 @@ +begin; + set local search_path = ''; + + -- BASELINE: 0 issues on empty schema + select * from lint."0035_policy_on_anon_role"; + + create table public.docs( + id int primary key, + owner_id uuid, + published boolean + ); + alter table public.docs enable row level security; + + savepoint a; + + -- NEGATIVE: TO authenticated is not reachable by anon + create policy "own_rows" on public.docs + for all to authenticated + using (owner_id = auth.uid()) + with check (owner_id = auth.uid()); + select * from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- NEGATIVE: SELECT policies with a literal true are deliberate public read + create policy "explicit_anon_read" on public.docs + for select to anon + using (true); + create policy "public_read" on public.docs + for select + using (true); + select * from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- NEGATIVE: restrictive policies only narrow access + create policy "restrict" on public.docs + as restrictive + for all to anon + using (owner_id = auth.uid()); + select * from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: explicit TO anon with an identity check + create policy "anon_own_rows" on public.docs + for all to anon + using (owner_id = auth.uid()); + select name, detail, cache_key from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: no TO clause defaults to public, which includes anon + create policy "no_to_clause" on public.docs + for all + using (owner_id = auth.uid()); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: SELECT policies with any expression beyond a literal true are flagged + create policy "select_column" on public.docs + for select + using (published); + create policy "select_setting" on public.docs + for select to anon + using (current_setting('app.tenant', true) = owner_id::text); + create policy "select_uid" on public.docs + for select + using (auth.uid() = owner_id); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: explicit TO public with initplan-wrapped identity on a SELECT + create policy "public_select_identity" on public.docs + for select to public + using (owner_id = (select auth.uid())); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: write policies are flagged without any identity reference + create policy "anon_insert" on public.docs + for insert to anon + with check (true); + select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: a role that anon is a member of also applies to anon + create role api_base; + grant api_base to anon; + create policy "via_member_role" on public.docs + for update to api_base + using (owner_id = auth.uid()); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: only the anon-matching roles are reported + create policy "anon_and_authenticated" on public.docs + for all to anon, authenticated + using (owner_id = auth.uid()); + select metadata->>'policy_name' as policy_name, metadata->>'roles' as roles from lint."0035_policy_on_anon_role"; + + rollback to savepoint a; + + -- POSITIVE: user-authored policies on storage.objects (the role-less JWT case) + create schema storage; + create table storage.objects( + id int primary key, + bucket_id text, + name text + ); + alter table storage.objects enable row level security; + create policy "folder_is_uid" on storage.objects + for all + using (bucket_id = 'files' and (string_to_array(name, '/'))[1] = auth.uid()::text); + select name, detail, cache_key from lint."0035_policy_on_anon_role"; + + -- RESOLUTION: scope the policy to authenticated + 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 (string_to_array(name, '/'))[1] = auth.uid()::text); + select * from lint."0035_policy_on_anon_role"; + +rollback; diff --git a/test/sql/queries_are_unionable.sql b/test/sql/queries_are_unionable.sql index 6169d02..564061b 100644 --- a/test/sql/queries_are_unionable.sql +++ b/test/sql/queries_are_unionable.sql @@ -62,6 +62,8 @@ begin; union all select * from lint."0031_anon_dynamic_sql_function_executable" union all - select * from lint."0032_authenticated_dynamic_sql_function_executable"; + select * from lint."0032_authenticated_dynamic_sql_function_executable" + union all + select * from lint."0035_policy_on_anon_role"; rollback;