diff --git a/docs/0024_permissive_rls_policy.md b/docs/0024_permissive_rls_policy.md index 1422538..3799053 100644 --- a/docs/0024_permissive_rls_policy.md +++ b/docs/0024_permissive_rls_policy.md @@ -1,5 +1,5 @@ -**Level:** WARN +**Level:** ERROR for INSERT, UPDATE, DELETE and ALL policies; WARN for SELECT policies **Summary:** Security policy allows unrestricted access @@ -24,12 +24,13 @@ The lint identifies policies with these always-true patterns: - `USING (true)` - explicitly allows reading all rows - `USING (1=1)` - tautology that always evaluates to true - `USING ('a'='a')` - string comparison tautology -- Missing USING clause on permissive SELECT policies **WITH CHECK Clause (controls which rows can be written):** - `WITH CHECK (true)` - allows writing any row - `WITH CHECK (1=1)` - tautology that always evaluates to true -- Missing WITH CHECK clause on permissive INSERT/UPDATE policies +- Missing WITH CHECK clause on UPDATE/ALL policies whose USING clause is always true (Postgres reuses USING) + +Policies with no `USING` or `WITH CHECK` expression at all are not flagged: Postgres skips them, so they grant no access. ### Security Impact @@ -45,7 +46,7 @@ This is particularly dangerous when the policy applies to `anon` or `authenticat **Option 1: Add proper row-level conditions** -Replace the permissive policy with one that properly restricts access: +Replace the permissive policy with one that properly restricts access. The lint checks each permissive policy on its own, so layering a restrictive policy on top of an always-true one does not clear it: ```sql -- Instead of: USING (true) @@ -58,28 +59,7 @@ for select using (auth.uid() = user_id); ``` -**Option 2: Use restrictive policies in combination** - -If you need a base permissive policy, combine it with restrictive policies: - -```sql --- Base permissive policy -create policy "authenticated_access" -on public.posts -for select -to authenticated -using (true); - --- Restrictive policy to limit access -create policy "only_published" -on public.posts -as restrictive -for select -to authenticated -using (status = 'published' or auth.uid() = user_id); -``` - -**Option 3: Remove the policy if RLS is not needed** +**Option 2: Remove the policy if RLS is not needed** If you don't need row-level restrictions, consider whether RLS should be disabled: @@ -127,8 +107,8 @@ using (auth.uid() = user_id); ### False Positives -In some cases, `USING (true)` may be intentional: +In some cases, `USING (true)` on a `SELECT` policy may be intentional: - Public read-only tables (e.g., blog posts, product catalogs) - Tables where access is controlled by other means (e.g., API layer) -If the policy is intentional, you can document why in a comment or consider suppressing this lint for specific tables. +If a table is meant to be publicly readable, make sure it holds no sensitive data, since anyone with the anon key can read every row. If the policy is intentional, document why in a comment or suppress this lint for that table. diff --git a/lints/0024_rls_policy_always_true.sql b/lints/0024_rls_policy_always_true.sql index 7e05efa..0f5a8fe 100644 --- a/lints/0024_rls_policy_always_true.sql +++ b/lints/0024_rls_policy_always_true.sql @@ -43,20 +43,14 @@ permissive_patterns as ( select p.*, -- Check for always-true USING clause patterns - -- Note: SELECT with (true) is often intentional and documented, so we only flag UPDATE/DELETE case when ( - command in ('UPDATE', 'DELETE', 'ALL') - and ( - normalized_qual in ('true', '(true)', '1=1', '(1=1)') - -- Empty or null qual on permissive policy means allow all - or (qual is null and is_permissive) - ) + command in ('SELECT', 'UPDATE', 'DELETE', 'ALL') + -- A NULL qual is not allow-all: Postgres skips the policy, so it grants no rows + and normalized_qual in ('true', '(true)', '1=1', '(1=1)') ) then true else false end as has_permissive_using, -- Check for always-true WITH CHECK clause patterns case when ( normalized_with_check in ('true', '(true)', '1=1', '(1=1)') - -- Empty with_check on INSERT means allow all (INSERT has no USING to fall back on) - or (with_check is null and is_permissive and command = 'INSERT') -- Empty with_check on UPDATE/ALL with permissive USING means allow all writes or (with_check is null and is_permissive and command in ('UPDATE', 'ALL') and normalized_qual in ('true', '(true)', '1=1', '(1=1)')) @@ -81,10 +75,11 @@ permissive_patterns as ( select 'rls_policy_always_true' as name, 'RLS Policy Always True' as title, - 'WARN' as level, + -- Write access (INSERT/UPDATE/DELETE/ALL) is an ERROR; unrestricted reads are a WARN + case when command in ('INSERT', 'UPDATE', 'DELETE', 'ALL') then 'ERROR' else 'WARN' end as level, 'EXTERNAL' as facing, array['SECURITY'] as categories, - 'Detects RLS policies that use overly permissive expressions like \`USING (true)\` or \`WITH CHECK (true)\` for UPDATE, DELETE, or INSERT operations. SELECT policies with \`USING (true)\` are intentionally excluded as this pattern is often used deliberately for public read access.' as description, + 'Detects RLS policies that use overly permissive expressions like \`USING (true)\` or \`WITH CHECK (true)\` for SELECT, INSERT, UPDATE, or DELETE operations.' as description, format( 'Table `%s.%s` has an RLS policy `%s` for `%s` that allows unrestricted access%s. This effectively bypasses row-level security for %s.', schema_name, diff --git a/splinter.sql b/splinter.sql index 46bb331..5af37af 100644 --- a/splinter.sql +++ b/splinter.sql @@ -1337,20 +1337,14 @@ permissive_patterns as ( select p.*, -- Check for always-true USING clause patterns - -- Note: SELECT with (true) is often intentional and documented, so we only flag UPDATE/DELETE case when ( - command in ('UPDATE', 'DELETE', 'ALL') - and ( - normalized_qual in ('true', '(true)', '1=1', '(1=1)') - -- Empty or null qual on permissive policy means allow all - or (qual is null and is_permissive) - ) + command in ('SELECT', 'UPDATE', 'DELETE', 'ALL') + -- A NULL qual is not allow-all: Postgres skips the policy, so it grants no rows + and normalized_qual in ('true', '(true)', '1=1', '(1=1)') ) then true else false end as has_permissive_using, -- Check for always-true WITH CHECK clause patterns case when ( normalized_with_check in ('true', '(true)', '1=1', '(1=1)') - -- Empty with_check on INSERT means allow all (INSERT has no USING to fall back on) - or (with_check is null and is_permissive and command = 'INSERT') -- Empty with_check on UPDATE/ALL with permissive USING means allow all writes or (with_check is null and is_permissive and command in ('UPDATE', 'ALL') and normalized_qual in ('true', '(true)', '1=1', '(1=1)')) @@ -1375,10 +1369,11 @@ permissive_patterns as ( select 'rls_policy_always_true' as name, 'RLS Policy Always True' as title, - 'WARN' as level, + -- Write access (INSERT/UPDATE/DELETE/ALL) is an ERROR unrestricted reads are a WARN + case when command in ('INSERT', 'UPDATE', 'DELETE', 'ALL') then 'ERROR' else 'WARN' end as level, 'EXTERNAL' as facing, array['SECURITY'] as categories, - 'Detects RLS policies that use overly permissive expressions like \`USING (true)\` or \`WITH CHECK (true)\` for UPDATE, DELETE, or INSERT operations. SELECT policies with \`USING (true)\` are intentionally excluded as this pattern is often used deliberately for public read access.' as description, + 'Detects RLS policies that use overly permissive expressions like \`USING (true)\` or \`WITH CHECK (true)\` for SELECT, INSERT, UPDATE, or DELETE operations.' as description, format( 'Table `%s.%s` has an RLS policy `%s` for `%s` that allows unrestricted access%s. This effectively bypasses row-level security for %s.', schema_name, diff --git a/test/expected/0024_rls_policy_always_true.out b/test/expected/0024_rls_policy_always_true.out index 35fc690..7a0e4b2 100644 --- a/test/expected/0024_rls_policy_always_true.out +++ b/test/expected/0024_rls_policy_always_true.out @@ -15,21 +15,50 @@ begin; ); alter table public.posts enable row level security; ---------------------------------------- - -- Test: SELECT with USING (true) should NOT be flagged - -- SELECT with (true) is often intentional for public read access + -- Test: SELECT with USING (true) SHOULD be flagged ---------------------------------------- create policy "select_using_true" on public.posts for select to authenticated using (true); + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +-------------------+---------+------- + select_using_true | SELECT | WARN +(1 row) + + drop policy "select_using_true" on public.posts; + ---------------------------------------- + -- Test: SELECT with USING (true) for anon SHOULD be flagged + ---------------------------------------- + create policy "anon_select_using_true" + on public.posts + for select + to anon + using (true); + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +------------------------+---------+------- + anon_select_using_true | SELECT | WARN +(1 row) + + drop policy "anon_select_using_true" on public.posts; + ---------------------------------------- + -- Test: SELECT with a real condition should NOT be flagged + ---------------------------------------- + create policy "select_scoped" + on public.posts + for select + to authenticated + using (user_id = auth.uid()); select count(*) from lint."0024_rls_policy_always_true"; count ------- 0 (1 row) - drop policy "select_using_true" on public.posts; + drop policy "select_scoped" on public.posts; ---------------------------------------- -- Test: UPDATE with USING (true) SHOULD be flagged ---------------------------------------- @@ -38,10 +67,10 @@ begin; for update to authenticated using (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; - policy_name | command --------------------+--------- - update_using_true | UPDATE + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +-------------------+---------+------- + update_using_true | UPDATE | ERROR (1 row) drop policy "update_using_true" on public.posts; @@ -53,10 +82,10 @@ begin; for delete to authenticated using (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; - policy_name | command --------------------+--------- - delete_using_true | DELETE + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +-------------------+---------+------- + delete_using_true | DELETE | ERROR (1 row) drop policy "delete_using_true" on public.posts; @@ -68,10 +97,10 @@ begin; for all to authenticated using (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; - policy_name | command -----------------+--------- - all_using_true | ALL + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +----------------+---------+------- + all_using_true | ALL | ERROR (1 row) drop policy "all_using_true" on public.posts; @@ -83,10 +112,10 @@ begin; for insert to authenticated with check (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; - policy_name | command -------------------------+--------- - insert_with_check_true | INSERT + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +------------------------+---------+------- + insert_with_check_true | INSERT | ERROR (1 row) drop policy "insert_with_check_true" on public.posts; @@ -98,10 +127,10 @@ begin; for insert to authenticated with check (1=1); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; - policy_name | command -------------------------+--------- - insert_with_check_1eq1 | INSERT + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +------------------------+---------+------- + insert_with_check_1eq1 | INSERT | ERROR (1 row) drop policy "insert_with_check_1eq1" on public.posts; @@ -114,10 +143,10 @@ begin; to authenticated using (user_id = auth.uid()) with check (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; - policy_name | command -------------------------+--------- - update_with_check_true | UPDATE + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + policy_name | command | level +------------------------+---------+------- + update_with_check_true | UPDATE | ERROR (1 row) drop policy "update_with_check_true" on public.posts; @@ -292,20 +321,23 @@ begin; drop policy "all_true_using_no_with_check" on public.posts; ---------------------------------------- - -- Test: INSERT with no WITH CHECK (null) SHOULD be flagged + -- Test: Policies with no USING / WITH CHECK should NOT be flagged + -- Postgres skips a permissive policy with a NULL expression, so it grants no rows ---------------------------------------- - create policy "insert_no_with_check" - on public.posts - for insert - to authenticated; - select - metadata->>'policy_name' as policy_name, - metadata->>'permissive_with_check' as permissive_with_check - from lint."0024_rls_policy_always_true"; - policy_name | permissive_with_check -----------------------+----------------------- - insert_no_with_check | true + create policy "select_no_using" on public.posts for select to anon; + create policy "update_no_using" on public.posts for update to anon; + create policy "delete_no_using" on public.posts for delete to anon; + create policy "all_no_using" on public.posts for all to anon; + create policy "insert_no_with_check" on public.posts for insert to anon; + select count(*) from lint."0024_rls_policy_always_true"; + count +------- + 0 (1 row) + drop policy "select_no_using" on public.posts; + drop policy "update_no_using" on public.posts; + drop policy "delete_no_using" on public.posts; + drop policy "all_no_using" on public.posts; drop policy "insert_no_with_check" on public.posts; rollback; diff --git a/test/sql/0024_rls_policy_always_true.sql b/test/sql/0024_rls_policy_always_true.sql index f175e4c..1c8c206 100644 --- a/test/sql/0024_rls_policy_always_true.sql +++ b/test/sql/0024_rls_policy_always_true.sql @@ -14,8 +14,7 @@ begin; alter table public.posts enable row level security; ---------------------------------------- - -- Test: SELECT with USING (true) should NOT be flagged - -- SELECT with (true) is often intentional for public read access + -- Test: SELECT with USING (true) SHOULD be flagged ---------------------------------------- create policy "select_using_true" on public.posts @@ -23,10 +22,36 @@ begin; to authenticated using (true); - select count(*) from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "select_using_true" on public.posts; + ---------------------------------------- + -- Test: SELECT with USING (true) for anon SHOULD be flagged + ---------------------------------------- + create policy "anon_select_using_true" + on public.posts + for select + to anon + using (true); + + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; + + drop policy "anon_select_using_true" on public.posts; + + ---------------------------------------- + -- Test: SELECT with a real condition should NOT be flagged + ---------------------------------------- + create policy "select_scoped" + on public.posts + for select + to authenticated + using (user_id = auth.uid()); + + select count(*) from lint."0024_rls_policy_always_true"; + + drop policy "select_scoped" on public.posts; + ---------------------------------------- -- Test: UPDATE with USING (true) SHOULD be flagged ---------------------------------------- @@ -36,7 +61,7 @@ begin; to authenticated using (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "update_using_true" on public.posts; @@ -49,7 +74,7 @@ begin; to authenticated using (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "delete_using_true" on public.posts; @@ -62,7 +87,7 @@ begin; to authenticated using (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "all_using_true" on public.posts; @@ -75,7 +100,7 @@ begin; to authenticated with check (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "insert_with_check_true" on public.posts; @@ -88,7 +113,7 @@ begin; to authenticated with check (1=1); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "insert_with_check_1eq1" on public.posts; @@ -102,7 +127,7 @@ begin; using (user_id = auth.uid()) with check (true); - select metadata->>'policy_name' as policy_name, metadata->>'command' as command from lint."0024_rls_policy_always_true"; + select metadata->>'policy_name' as policy_name, metadata->>'command' as command, level from lint."0024_rls_policy_always_true"; drop policy "update_with_check_true" on public.posts; @@ -257,18 +282,21 @@ begin; drop policy "all_true_using_no_with_check" on public.posts; ---------------------------------------- - -- Test: INSERT with no WITH CHECK (null) SHOULD be flagged + -- Test: Policies with no USING / WITH CHECK should NOT be flagged + -- Postgres skips a permissive policy with a NULL expression, so it grants no rows ---------------------------------------- - create policy "insert_no_with_check" - on public.posts - for insert - to authenticated; + create policy "select_no_using" on public.posts for select to anon; + create policy "update_no_using" on public.posts for update to anon; + create policy "delete_no_using" on public.posts for delete to anon; + create policy "all_no_using" on public.posts for all to anon; + create policy "insert_no_with_check" on public.posts for insert to anon; - select - metadata->>'policy_name' as policy_name, - metadata->>'permissive_with_check' as permissive_with_check - from lint."0024_rls_policy_always_true"; + select count(*) from lint."0024_rls_policy_always_true"; + drop policy "select_no_using" on public.posts; + drop policy "update_no_using" on public.posts; + drop policy "delete_no_using" on public.posts; + drop policy "all_no_using" on public.posts; drop policy "insert_no_with_check" on public.posts; rollback;