Fixes › Database / Supabase RLS
Supabase view shows every user's data even with RLS on: fix Security Advisor 0010 security_definer_view
Published
Weakness type: CWE-863 (Incorrect Authorization)
The error
Security Advisor flags 0010 security_definer_view (View bypasses row-level security) or 0002 auth_users_exposed / my tables have RLS and owner policies but a view returns every user's rows / querying the view with the anon key returns data the table itself hides / an AI-generated dashboard or stats view leaks other users' data
Why it happens
In Postgres a view does not run as the person querying it by default. Supabase's Security Advisor explains that views default to running with the permissions of the view's creator, kept that way for backwards compatibility, and Supabase's RLS guide says views bypass RLS by default because they are usually created with the postgres user, which can bypass row level security. So you can have RLS switched on and a perfect owner policy on the orders table, and a view such as create view public.order_summary as select * from orders still returns every user's orders to anyone holding your publishable or anon key, because views in the public schema are exposed through the Data API like tables. AI coding tools create these views all the time for dashboards, joins and 'latest activity' lists, and the RLS check on the base table never runs. A view over auth.users is worse: Supabase lint 0002 auth_users_exposed warns it can hand every user's email and sign-in details to the whole internet. Materialized views can never respect RLS at all. The fix since Postgres 15 is the security_invoker option, which makes the view check the base tables' privileges and RLS policies as the user running the query.
How to fix it
- Open Advisors > Security Advisor in the Supabase Dashboard (or run supabase db advisors with the CLI) and note every view listed under 0010 security_definer_view, 0002 auth_users_exposed and 0016 materialized_view_in_api.
- Prove the leak before fixing it. In your app or with curl, query the view as a normal signed-in user (or with only the publishable key) and check whether rows belonging to other users come back. That tells you what was exposed while the view was live.
- Make each ordinary view respect RLS: alter view public.<view_name> set (security_invoker = true); or recreate it with create or replace view public.<view_name> with (security_invoker = true) as select ...; This needs Postgres 15 or later, which current Supabase projects use.
- Check the base tables now behave as expected through the view. With security_invoker on, the caller needs SELECT on the view and on every underlying table, and the tables' RLS policies apply. If the view now returns nothing, the base table is missing a SELECT policy for the authenticated role, for example create policy "owner can read" on public.orders for select to authenticated using ((select auth.uid()) = user_id);
- If a view reads auth.users, replace it. Supabase recommends a public.profiles table filled by a trigger on auth.users with its own RLS, or a security_invoker view plus RLS on the data, instead of exposing auth.users directly. Only include the columns the app really needs.
- For materialized views, which cannot respect RLS, remove API access: revoke select on public.<mat_view> from public, anon, authenticated; then serve the data through a function or a server route that checks the user first.
- If a view is genuinely meant to be public (for example a published-posts list), keep it but make sure it only selects public columns and rows, and document why. Do not leave private columns in a public view.
- Re-run Security Advisor until 0010 and 0002 are gone, then add a rule to your Lovable, Bolt, Cursor or Replit project instructions: 'Every view in the public schema must be created with (security_invoker = true); never create views on auth.users.'
- If other users' data was readable while the view was live, check Logs > API in the Dashboard for requests to the view and decide whether users need to be told under your privacy obligations.
Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)
Supabase Security Advisor reports 0010 security_definer_view (and possibly 0002 auth_users_exposed) for these views: <PASTE view names>. Views run with the creator's permissions by default and bypass Row Level Security. Do not disable RLS and do not use the service_role key. 1) List every view in the public schema from my migrations with the file and line. 2) Give me SQL to set security_invoker = true on each one (alter view ... set (security_invoker = true) or create or replace view ... with (security_invoker = true)). 3) For each underlying table, check there is an owner-scoped SELECT policy to authenticated using ((select auth.uid()) = user_id) and add it if missing. 4) Replace any view that selects from auth.users with a public.profiles table filled by a trigger, with RLS. 5) Revoke select from anon and authenticated on any materialized view. Explain each change in one line.
Error codes this fixes
Security Advisor 0010 security_definer_view(Supabase lint)Security Advisor 0002 auth_users_exposed(Supabase lint)Security Advisor 0016 materialized_view_in_api(Supabase lint)
FAQ
Why does my view ignore Row Level Security?
Because Postgres views run with the permissions of the view owner by default, not the person querying. In Supabase that owner is usually the postgres role, which bypasses RLS, so the view returns every row even though the table's policies are correct.
What does security_invoker = true do?
It tells Postgres to check the underlying tables using the privileges and RLS policies of the user running the query, as if they had queried the tables directly. It was added in Postgres 15.
After adding security_invoker my view returns no rows. Why?
The view is now subject to the base tables' RLS. If a table has RLS on but no SELECT policy for the signed-in role, no rows are visible. Add an owner-scoped SELECT policy to that table, and make sure the role has a GRANT on it.
Can I protect a materialized view with RLS?
No. Supabase notes materialized views cannot respect RLS and cannot be secured with RLS directly, so revoke select from anon and authenticated and expose the data through a function or server route that checks the user.
Is a view on auth.users ever safe?
Supabase recommends against exposing auth.users through a public view. Use a public.profiles table populated by a trigger, with its own RLS, so you only expose the columns you choose.
Sources
Based on the official documentation:
- Supabase: Database Advisors (0010 security_definer_view, 0002 auth_users_exposed, 0016)
- Supabase: Row Level Security (Views and RLS)
- PostgreSQL: CREATE VIEW (security_invoker)
- PostgreSQL: ALTER VIEW
Related fixes
- Supabase table has RLS disabled (public Data API access)
- Supabase RLS is on but every user can read every row: fix USING (true) and auth.uid() is not null policies
- AI "fixed" my Supabase error by disabling RLS or using the service_role key: how to undo it safely
- Supabase query returns an empty array (no error)
- Supabase "permission denied for table" (42501) on a new table
- 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.