0006 Multiple Permissive Policies: Fix Comparator And OR/AND Logic In Documentation Examples
The Supabase documentation example for lint 0006_multiple_permissive_policies contains incorrect comparator descriptions and uses OR instead of AND in the consolidated policy, which would lead users to implement overly permissive row-level security. This issue proposes corrections including using <= consistently and changing OR to AND, plus adding a restrictive policies alternative.
Documentation example for combining permissive RLS policies incorrectly used OR in the consolidated policy, maintaining the permissive OR semantics instead of the intended AND semantics. It also mismatched the textual description of Policy B ("at or above") with the actual comparator <=, and contained typos ("departemnt").
1. Follow the Supabase docs for lint 0006_multiple_permissive_policies.
2. Create the employee_data table with department and grade_level columns.
3. Create the two permissive policies as shown in the original documentation (department_access and grade_level_access).
4. Notice that a user can see rows from other departments if their grade_level is <= the user's grade level, or see higher-grade employees in their own department, because the policies are permissive and combined with OR.
5. Apply the suggested consolidated policy from the original docs using OR. The same broad access remains, demonstrating the bug in the documentation.
Fixing Code Block
-- Drop existing permissive policies
drop policy if exists department_access on employee_data;
drop policy if exists grade_level_access on employee_data;
-- Option 1: Single consolidated policy with correct AND semantics
create policy consolidated_access on employee_data
for select
using (
department = current_user_department()
and grade_level <= current_user_grade_level()
);
-- Option 2: Multiple restrictive policies (alternative for maintainability)
-- Note: Ensure no other permissive policies exist, or understand that permissive policies will still be OR'ed
-- drop policy if exists department_access on employee_data;
-- drop policy if exists grade_level_access on employee_data;
-- create policy department_access on employee_data
-- as restrictive
-- for select
-- using (department = current_user_department());
-- create policy grade_level_access on employee_data
-- as restrictive
-- for select
-- using (grade_level <= current_user_grade_level());
The corrected documentation uses <= consistently to reflect 'at or below' semantics, and changes the consolidated policy from OR to AND so that both conditions must be true, matching the intended access. Additionally, an alternative using RESTRICTIVE policies is provided to maintain multiple policies while enforcing AND semantics.
Edge Case Audit
Applying these policy changes can lock out users if current_user_department() or current_user_grade_level() are not stable or if policies are not carefully audited. Dropping existing policies without a backup can temporarily remove all access. Restrictive policies can interact with other permissive policies on the same table; if any permissive policy exists, rows may still be visible through that permissive path even if restrictive policies fail. Test in a transaction and rollback if unexpected results occur. Rollback suggestion: Save original policy definitions before dropping and recreate them if necessary.