Preventing AI from exposing Supabase service keys in frontend code
When AI tools encounter frontend queries blocked by RLS, they often incorrectly use the service-role key to bypass security restrictions. This exposes the key directly to browser visitors and risks database tampering.
DEV.to #vibecoding
SupabaseLovableBoltv0ReplitCursor
Added 2026-11-07 · originally published Wed, 04 Nov 2026 09:40:53 +0000
Key points
- Never use the Supabase service-role key in browser-side code; always use the anon key
- Strictly limit the service-role key to server-side code (such as API routes, Server Actions, or Edge Functions)
- When queries are blocked by RLS policies, fix the RLS policy rather than switching to the service-role key to disable security restrictions
- If a key has already been deployed with frontend code, you must immediately rotate it in Supabase under Settings → API
Pitfalls
- AI tools automatically use SUPABASE_SERVICE_ROLE_KEY in frontend code to resolve RLS errors
- Bundling the service-role key into browser code, allowing visitors to read, modify, or delete the entire database
.cursorrules
- Never use SUPABASE_SERVICE_ROLE_KEY or service-role keys in browser-side code - Use anon key for all browser Supabase clients - Keep service-role keys exclusively in server-side code (API routes, server actions, edge functions) - Fix RLS policies instead of bypassing them with service-role keys when queries fail
Copy-ready prompt
The Supabase service-role key is used in code that runs in the browser. Move every use of it to server-side code (an API route or edge function), use the anon key in the browser, and make the RLS policies allow what the app needs. Then tell me to rotate the key in Supabase.
#AI Coding#Security Pitfalls#Supabase#RLS
Related techniques
Summarized by AI from the original source - refer to the original post for details. Copyright belongs to the original author.