VibeSecCheck
Home/Security Guides/supabase-rls-policies
Critical RiskSupabase & DatabasesSupabasePostgreSQLSaaSMulti-Tenant

Production Supabase RLS Policies Cheat Sheet

Turning on RLS isn't enough if you attach a lazy USING (true) policy. Secure SaaS apps separate read-only billing rows, per-user CRUD tables, workspace memberships, and insert-only lead forms.

Why AI Builders Introduce This

When an AI hits a 'new row violates row-level security policy' error, its reflex is to generate CREATE POLICY 'allow all' USING (true) WITH CHECK (true) — which silently undoes all security.

Real-World Exploit Impact
  • USING (true) on UPDATE lets any authenticated user overwrite another customer's data
  • Missing WITH CHECK lets attackers insert rows stamped with someone else's user_id
  • Wrapping auth.uid() inside (SELECT auth.uid()) also boosts Postgres query performance

Vulnerable Pattern vs. Production Fix

Dangerous AI Reflex Policy (Allows Any Logged-In User To Edit Anyone)sql
-- BAD: Any signed-up user can update ANY subscription to 'pro'!
CREATE POLICY "Allow authenticated users"
  ON public.subscriptions
  FOR ALL
  TO authenticated
  USING (true)
  WITH CHECK (true);
Least-Privilege Billing Policy (Owner Read-Only; Server Writes Only)
ALTER TABLE public.subscriptions ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "Allow authenticated users" ON public.subscriptions;

CREATE POLICY "subscriptions_read_own"
  ON public.subscriptions
  FOR SELECT
  TO authenticated
  USING ((SELECT auth.uid()) = user_id);

-- Zero INSERT/UPDATE/DELETE policies for authenticated/anon.
-- Only verified server Stripe webhooks (service_role) mutate billing state.

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit all CREATE POLICY statements across supabase/migrations:
1. Flag and replace any USING (true) or WITH CHECK (true) policies on private, user, billing, or team tables.
2. Ensure every INSERT and UPDATE policy validates WITH CHECK ((SELECT auth.uid()) = user_id) (or equivalent workspace admin check).
3. Ensure billing/subscription tables allow ONLY SELECT for authenticated owners and zero client-side writes.
4. Add indexes on every column referenced inside RLS policies (user_id, team_id).

How to Verify Your Fix Worked

  1. 1Use VibeSecCheck's Tab 2 Supabase RLS SQL Generator to build exact policies for each table type.
  2. 2Log in as User A and attempt to update User B's row ID via browser console supabase.from(...).update() — verify 0 rows affected.
  3. 3Check pg_policies view in Supabase SQL Editor for any lingering qual = 'true' rules.

Frequently Asked Questions

Why should I write (SELECT auth.uid()) instead of auth.uid()?

Wrapping auth.uid() in a scalar subquery allows PostgreSQL's planner to evaluate it once per statement (initPlan) instead of calling the function on every scanned row.

How do I handle waitlist signups safely with RLS?

Create ONLY an INSERT policy for anon/authenticated and zero SELECT, UPDATE, or DELETE policies so visitors can submit their email without being able to read anyone else's.

Can RLS policies cause infinite recursion on team_members?

Yes, if a policy queries the same table without careful scoping or a SECURITY DEFINER helper function. Test multi-tenant policies with two test users before launching.

Related Security Playbooks