vibesupport
Security Tutorial

The Complete Guide to Securing Supabase Row-Level Security (RLS) in Vibe-Coded Apps

When you build an app using Cursor, Bolt.new, v0, or Lovable, AI developers often recommend Supabase because it takes under two minutes to get authentication, database tables, and client libraries wired together.

However, there is a dangerous misconception that has led to dozens of public database dumps in recent months: the Supabase `anon` key is not an authorization barrier.

If Row-Level Security (RLS) is disabled or improperly configured, anyone with a web browser can dump your entire database in under 10 seconds. In this tutorial, we will break down why AI models make this mistake, how to find vulnerabilities in your app right now, and the exact SQL policies required to lock your data down.


The "Anon Key" Illusion

When Supabase generates your project credentials, it provides two keys:

  • anon (public): Designed to be placed in browser-facing code.
  • service_role (secret): Grants complete, unrestricted superuser access.

Because the anon key is named "anonymous", many founders and vibe coders assume that Supabase automatically restricts anonymous requests to read-only or harmless operations.

That is completely false.

By default in PostgREST (the HTTP API layer that powers Supabase), having the anon key gives a visitor direct REST API access to every table in your public schema. If Row-Level Security is OFF, any visitor can run:

bash
curl 'https://YOUR_PROJECT_ID.supabase.co/rest/v1/users?select=*' \
  -H "apikey: YOUR_PUBLIC_ANON_KEY" \
  -H "Authorization: Bearer YOUR_PUBLIC_ANON_KEY"

If RLS is not active, that command will return every single row in your users table—including customer email addresses, created dates, billing IDs, and whatever private fields the table contains.


How Attackers Dump Vibe-Coded Databases

Attackers don't even need special hacker software. The attack workflow looks like this:

  • They visit your AI-built landing page or web app.
  • They open Chrome DevTools (Cmd + Option + I), go to the Application or Sources tab, or inspect network traffic.
  • They copy your NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY.
  • They run automated scrapers against standard Supabase REST endpoints:
  • /rest/v1/users?select=*
  • /rest/v1/profiles?select=*
  • /rest/v1/organizations?select=*
  • /rest/v1/documents?select=*
  • /rest/v1/subscriptions?select=*
  • If any table has RLS disabled, or a policy configured as USING (true), the data is exfiltrated instantly.

Step 1: Enable RLS on Every Table

The first rule of Supabase security is that every single public table must have RLS explicitly enabled.

Run this SQL query in your Supabase SQL Editor or migration files:

sql
-- Enable Row Level Security on your tables
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.projects ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.invoices ENABLE ROW LEVEL SECURITY;
Important: Simply enabling RLS without adding any policies will block all operations via the anon key (default deny). This is good! Your data is safe until you explicitly grant access.

To ensure no new table is ever created without RLS, you can also run:

sql
-- Verify which tables currently lack RLS
SELECT
  schemaname,
  tablename,
  rowsecurity
FROM pg_tables
WHERE schemaname = 'public'
  AND rowsecurity = false;

If this query returns any rows, those tables are currently exposed to anyone who has your public URL and anon key.


Step 2: Write Explicit Tenant Policies

AI models often generate broad policies like CREATE POLICY "Allow public read" ON profiles FOR SELECT USING (true);. While that might be appropriate for a public blog post, it is catastrophic for customer profiles or private documents.

Here are the standard, production-hardened RLS policy templates you should use:

1. User Profile: Only the owner can view and update their profile

sql
-- 1. Users can only read their own profile
CREATE POLICY "Users can view own profile"
ON public.profiles
FOR SELECT
TO authenticated
USING (auth.uid() = id);

-- 2. Users can only update their own profile
CREATE POLICY "Users can update own profile"
ON public.profiles
FOR UPDATE
TO authenticated
USING (auth.uid() = id)
WITH CHECK (auth.uid() = id);

2. Multi-tenant Projects: Users can only access projects they created

sql
-- Enable RLS
ALTER TABLE public.projects ENABLE ROW LEVEL SECURITY;

-- Select policy
CREATE POLICY "Users can select own projects"
ON public.projects
FOR SELECT
TO authenticated
USING (auth.uid() = user_id);

-- Insert policy (ensuring user_id matches logged-in user)
CREATE POLICY "Users can insert own projects"
ON public.projects
FOR INSERT
TO authenticated
WITH CHECK (auth.uid() = user_id);

-- Delete policy
CREATE POLICY "Users can delete own projects"
ON public.projects
FOR DELETE
TO authenticated
USING (auth.uid() = user_id);

Step 3: Test with curl and Browser DevTools

Never assume your RLS policy works just because your app displays data correctly in development. The frontend UI might work while the database remains vulnerable.

Test 1: Anonymous request should return an empty array [] or 401 error

bash
curl -i 'https://YOUR_PROJECT.supabase.co/rest/v1/projects?select=*' \
  -H "apikey: YOUR_PUBLIC_ANON_KEY" \
  -H "Authorization: Bearer YOUR_PUBLIC_ANON_KEY"

Expected Result: [] (empty list) or HTTP 401/403. If you see customer records, your RLS is failing.

Test 2: User A trying to access User B's record

Log in as User A in Chrome, and as User B in an Incognito window. Copy User B's project ID. From User A's browser console, run:

javascript
const { data, error } = await supabase
  .from('projects')
  .select('*')
  .eq('id', 'USER_B_PROJECT_ID');
console.log(data);

Expected Result: data must be [] or null. If User A can see User B's project data, you have an Insecure Direct Object Reference (IDOR) bug.


Common RLS Gotchas in AI Code

  • Using `service_role` in Frontend Code:

If you ever see process.env.SUPABASE_SERVICE_ROLE_KEY inside a file with "use client" or prefixed as NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY, your entire database is compromised. The service role key bypasses all RLS policies. It must only ever live in secure server-side Node.js code or Next.js Route Handlers.

  • Missing `WITH CHECK` on INSERT and UPDATE:

A policy with only USING (auth.uid() = user_id) protects reads, but without WITH CHECK (auth.uid() = user_id), an authenticated user might update a record and reassign its user_id to another customer.

  • Database Views Bypassing RLS:

In PostgreSQL, creating a standard view over an RLS-enabled table bypasses RLS unless the view is created with WITH (security_invoker = true). If you have analytics or reporting views, make sure security invoker mode is active.


Pre-Launch Supabase RLS Checklist

  • ☐Every table in the public schema has rowsecurity = true.
  • ☐No service_role keys are present in frontend files or client bundles.
  • ☐No policies contain indiscriminate USING (true) on sensitive or personal data tables.
  • ☐Multi-tenant tables verify auth.uid() = user_id on both USING and WITH CHECK.
  • ☐Tested unauthenticated curl requests against all primary endpoints to confirm zero data leaks.

How vibesupport Helps

Worried that your Cursor or Bolt-generated codebase has hidden Supabase RLS loopholes? vibesupport runs deep automated security scans against your schema and API endpoints, and our senior engineers write tested pull requests to patch database policies before launch.

BUILT WITH AI? A SECOND LOOK BEFORE YOU LAUNCH

Don't let a hidden bug derail your launch.

vibesupport scans your AI-built codebase or live website for exposed secrets, missing database RLS, and broken access controls—then pairs you with senior engineers to fix them directly in GitHub.

Related Tutorials & Guides