Fixes › Database / Supabase RLS
AI "fixed" my Supabase error by disabling RLS or using the service_role key: how to undo it safely
Published
Weakness type: CWE-862 (Missing Authorization)
The error
Security Advisor flags 0013 rls_disabled_in_public or 0007 policy_exists_rls_disabled right after an AI fix / the diff contains 'disable row level security', 'using (true)' or a new NEXT_PUBLIC_ or VITE_ variable holding the service_role or sb_secret_ key / the error went away but now every user can see every row
Why it happens
When a Supabase query fails with 'permission denied', 'new row violates row-level security policy', PGRST116 or an empty result, the fastest way for an AI coding agent to make the error disappear is to remove the check instead of fixing it. The three common shortcuts are: running alter table ... disable row level security; replacing a real policy with one that uses (true) for every role; or creating the client with the service_role key (or a new sb_secret_ secret key) so the query skips RLS entirely. Each one makes the app 'work' in testing, which is why it slips through. The cost is that your publishable or anon key, which ships in every browser and app bundle by design, now reaches every row in that table. Supabase's own docs say the publishable key is only safe because Row Level Security limits what it can reach, and that the service_role role has BYPASSRLS, so policies never apply to it. Legacy service_role keys are long JWTs starting with eyJ that work anywhere, including a browser; the newer sb_secret_ keys are rejected with HTTP 401 when sent from a browser, so an agent that hits that 401 may 'fix' it by moving to the legacy key or by proxying it through a public endpoint with no auth check. The original error still needs a real fix: usually a missing SELECT or INSERT policy, a query running before the session loads, or a missing table GRANT.
How to fix it
- Find what the agent changed. Search the git history and your migrations for 'disable row level security', 'using (true)', 'with check (true)', 'service_role', 'sb_secret_', 'SERVICE_ROLE' and 'bypassrls' (for example git log -p -S 'service_role'). In Lovable, Bolt or Replit, check the chat history or version history for the step where the error 'went away'.
- List tables with RLS off: select tablename, rowsecurity from pg_tables where schemaname = 'public'; Any row with rowsecurity = false is reachable by anyone with your publishable or anon key. Also open Advisors > Security Advisor in the Dashboard and look for 0013 rls_disabled_in_public, 0007 policy_exists_rls_disabled (policies exist but RLS was switched off, the typical sign of an AI shortcut) and 0024 permissive_rls_policy.
- Turn RLS back on for each affected table: alter table public.<table> enable row level security; Existing policies take effect again straight away. If the table has no policies, all access through the publishable key is blocked until you add them, so do the next step before you redeploy.
- Replace any using (true) or with check (true) policy with an owner check scoped to the right role, for example: create policy "owner can read" on public.<table> for select to authenticated using ((select auth.uid()) = user_id); Add matching INSERT (with check), UPDATE (using and with check) and DELETE policies only for the operations the app really needs. Keep anon read-only, and only on data that is meant to be public.
- Move every secret key out of client code. Search the built bundle and your env files for keys starting with eyJ that decode to role service_role, or for sb_secret_. Any variable with a NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_ prefix is shipped to the browser. The client must use the publishable (or legacy anon) key; the secret key belongs only in server routes, Edge Functions or jobs that check who the caller is before acting.
- If a secret or service_role key was ever deployed to the browser or committed, treat it as leaked: create a new secret key, switch your backend to it, then revoke the old key (or disable the legacy JWT keys) under Settings > API Keys. Do this after the code no longer needs the old key, so production does not break.
- Check the Postgres roles were not changed: select rolname, rolbypassrls from pg_roles where rolname in ('anon','authenticated'); Both should be false. Supabase warns against changing these roles' attributes.
- Now fix the original error properly. 'permission denied for table' is a missing GRANT, an empty result or PGRST116 is a missing SELECT policy or a query that runs before the user is signed in, and 'new row violates row-level security policy' is an INSERT or UPDATE policy whose with check fails. See the related fixes below.
- Assess exposure. If the table was open while the app was live, check API logs (Logs > API in the Dashboard) for unexpected reads or writes from the anon role, and decide whether users need to be told under your privacy obligations.
- Stop it happening again: add a line to your tool's project rules or knowledge file such as 'Never disable RLS, never use using (true), never put the service_role or secret key in client code; fix the policy instead', and re-run the Security Advisor after every AI fix that touches the database.
Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)
An earlier fix to my Supabase app disabled Row Level Security, added a using (true) policy, or switched to the service_role or secret key to make an error go away. The original error was: <PASTE error>. Do not disable RLS, do not use using (true) or with check (true), and do not put the service_role or sb_secret_ key in any client code or NEXT_PUBLIC_/VITE_/EXPO_PUBLIC_ variable. 1) Search the code, migrations and env files and list every place RLS is disabled, a permissive policy exists, or a secret key is used, with the file and line. 2) Give me SQL to re-enable RLS on each affected table and owner-scoped policies (using ((select auth.uid()) = user_id) to authenticated) for only the operations the app needs. 3) Move any secret-key usage into a server route or Edge Function that checks the signed-in user first, and switch the browser client to the publishable key. 4) Then fix the original error properly (GRANT, SELECT policy, or waiting for the session). 5) Tell me which keys I must rotate. Explain each change in one line.
Error codes this fixes
Security Advisor 0007 policy_exists_rls_disabled(Supabase lint)
FAQ
Why do AI coding tools disable RLS?
Because it is the quickest way to make a permission error disappear. The agent is rewarded for 'the error is gone', and turning off Row Level Security, adding a using (true) policy or switching to the service_role key all achieve that, while making the table readable and often writable by anyone.
Is it ever OK to use the service_role or secret key?
Yes, on the server only: in Edge Functions, API routes, cron jobs and admin tools that check who the caller is first. It bypasses every RLS policy, so it must never be in a browser, mobile app bundle or source control.
What is Security Advisor 0007 policy_exists_rls_disabled?
It means the table has RLS policies written, but RLS itself is turned off, so none of those policies are enforced. It is the classic sign that someone (or an AI agent) disabled RLS to get past an error. Re-enable RLS and the existing policies apply again.
Will turning RLS back on break my app?
It can, if the app was relying on open access. Tables with RLS on and no policies block all access through the publishable key. Add owner-scoped policies for the operations you need before redeploying, then test as a signed-in user and a signed-out visitor.
My sb_secret_ key returns 401 in the browser. Is that a bug?
No. Supabase blocks secret keys sent from a browser on purpose and returns HTTP 401. Use the publishable key in the browser and move the privileged call to a server route or Edge Function.
Sources
Based on the official documentation:
- Supabase: Understanding API keys
- Supabase: Row Level Security
- Supabase: Securing your API
- Supabase: Database Advisors
- Supabase lint 0007: policy_exists_rls_disabled
- Supabase lint 0013: rls_disabled_in_public
- Supabase: Postgres roles
Related fixes
- Supabase table has RLS disabled (public Data API access)
- Supabase service_role key exposed in frontend code
- Supabase RLS is on but every user can read every row: fix USING (true) and auth.uid() is not null policies
- Your API keys may have leaked: how to rotate every key without breaking your app
- Supabase query returns an empty array (no error)
- Changing the id in the URL shows other users' data (IDOR)
- Admin page is hidden but not protected (anyone can open /admin or call admin APIs)
- Is my Lovable app leaking data without login? A Supabase RLS audit checklist after the Crissy Field study
Get one new fix a week
Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.
We only use your email to send the newsletter. Unsubscribe any time. See privacy.