Probing Supabase RLS leaks with HEAD Prefer: count=exact (0 rows downloaded)

2026-12-01来源 dev.toEnglish

When you ship a Supabase app generated with Bolt, Lovable, or Cursor, the dashboard can show a green RLS badge on every table while your public anon key can still read every row.

That usually happens for one of four reasons:

  1. The AI ran ENABLE ROW LEVEL SECURITY and then added CREATE POLICY ... FOR SELECT USING (true) to make an early prototype error go away.
  2. A service_role JWT or third-party API key landed in the compiled Vite/Next.js client bundle.
  3. A Postgres view queries an RLS-protected table without WITH (security_invoker = true), so the view executes with the owner's privileges and skips RLS.
  4. A SECURITY DEFINER function is missing a fixed SET search_path = ''.

Checking row exposure without downloading user data

If you want to verify what an unauthenticated visitor can read from your own deployed project without pulling a single byte of user PII into your terminal or logs, PostgREST supports HEAD requests combined with Prefer: count=exact.

First, list the exposed tables from the OpenAPI spec:

curl -s "https://YOUR_REF.supabase.co/rest/v1/" \ -H "apikey: YOUR_ANON_KEY" | jq '.paths | keys' 

Then run a HEAD probe against each table:

curl -I -s "https://YOUR_REF.supabase.co/rest/v1/profiles?select=*" \ -H "apikey: YOUR_ANON_KEY" \ -H "Authorization: Bearer YOUR_ANON_KEY" \ -H "Prefer: count=exact" 

Look at the response status and Content-Range header. If it returns 200 OK with Content-Range: */1284, any visitor holding the public anon key from your frontend bundle can read all 1,284 rows. Because the method is HEAD, the response body is empty and zero rows are transferred.

Automating the check before launch

Running those curls by hand gets tedious once a project has 10+ tables and several client JS chunks. I wrapped the exact HEAD count probe, client bundle secret scan, and static schema.sql parser into a free read-only web checker so you can sanity-check your own app in ~10 seconds without signing up:

https://rlsguard-rho.vercel.app

It supports two modes:

  • Live URL scan (with an ownership checkbox): reads public JS chunks for leaked keys and runs read-only HEAD count probes against discovered PostgREST tables.
  • Paste schema.sql: checks locally for missing ENABLE ROW LEVEL SECURITY, USING (true) policies, views without security_invoker = true, and SECURITY DEFINER functions without search_path.

Quick SQL fixes for the two quietest bugs

For views that silently bypass RLS:

ALTER VIEW public.your_view SET (security_invoker = true); 

For USING (true) policies on user-owned tables:

DROP POLICY IF EXISTS "allow_read_all" ON public.profiles; CREATE POLICY "profiles_select_own" ON public.profiles FOR SELECT TO authenticated USING ((select auth.uid()) = id); 

If you run the scanner on your own schema and hit a false positive or weird SQL syntax it misses, drop a comment and I will patch the parser.

原文:https://www.a8q9.cn/en/article/probing-supabase-rls-leaks-with-head-prefer-count-exact-0-rows-downloaded-d002fa.html

相关文章