Avoiding Row-Level Security (RLS) pitfalls in Supabase AI apps
AI-generated frontend and Supabase backend applications often overlook Row-Level Security (RLS), leading to data leaks and unauthorized access. This guide breaks down 6 common RLS security failure scenarios, pre-launch audit steps, and CI persistence solutions.
DEV.to #vibecoding
SupabasePostgresLovableBoltCursorClaude Code
Added 2026-11-07 · originally published Fri, 06 Nov 2026 02:00:44 +0000
Key points
- Check for tables without RLS enabled: AI-generated SQL migration scripts do not enable row-level security by default, so you must manually run alter table ... enable row level security.
- Avoid using USING (true) or WITH CHECK (true): This exposes all rows to logged-in or anonymous users or allows spoofing other users' user_id.
- Bind ownership and optimize auth.uid(): Use (select auth.uid()) = user_id wrapped in a subquery in policies, and store permission data in app_metadata rather than user_metadata, which users can edit.
- Watch out for views, functions, and storage vulnerabilities: PostgreSQL 15+ views need security_invoker = true set, and never use the service role key which bypasses RLS in client-side code.
- Perform pre-launch audits: Inventory tables without RLS enabled, review policies from an attacker's perspective, and run cross-read/write tests with two real users.
Pitfalls
- SQL migration scripts missing RLS enablement, causing tables to be publicly exposed
- Using USING (true) or WITH CHECK (true), leading to full-table data leaks or allowing forged user IDs
- Storing sensitive authorization data, such as user roles, in user_metadata which users can edit themselves
- Views and security definer functions unintentionally bypassing RLS restrictions
- Directly using the high-privilege service_role key in client-side code (browser-side)
.cursorrules
- Ensure all tables created via migration scripts have `enable row level security` enabled - Do not use overly permissive security policies like `using (true)` or `with check (true)` - All INSERT/SELECT policies must bind `auth.uid()` to `user_id` - Permission and role checks must use `app_metadata` instead of `user_metadata` - Do not use the `service_role` key in client-side code
Copy-ready prompt
When using [AI tool name] to write Supabase databases and migration scripts for [project name], you must strictly follow these RLS (Row-Level Security) rules: 1. All newly created tables must explicitly execute enable row level security; 2. Never write permissive policies using (true) or WITH CHECK (true); 3. INSERT and SELECT policies must explicitly bind ownership using (select auth.uid()) = user_id; 4. Authorization data must be stored in app_metadata, and never use user_metadata for permission checks; 5. Never use the service_role key in client-side code.
#Supabase#RLS Security#Backend Security#AI Coding Pitfalls
Related techniques
Summarized by AI from the original source - refer to the original post for details. Copyright belongs to the original author.