Supabase: Row Level Security policies explained simply

posée a month ago435 vuesen

5

Supabase keeps warning me that RLS is disabled on my tables. What is RLS, and what's a minimal safe policy for a notes table where users only see their own notes?

Diagnostic IA

Généré par IA. Ce n'est pas une réponse — la communauté ci-dessous le confirme ou le corrige. Vérifiez toujours avant de vous y fier.

posée a month ago

1 réponse

9
✓
✓ Réponse acceptée

With Supabase, your frontend talks to the database directly using the public anon key. Without Row Level Security, anyone holding that key (i.e. anyone who opens your site) can read or modify every row.

RLS adds a filter to every query based on who is asking:

ALTER TABLE notes ENABLE ROW LEVEL SECURITY;

CREATE POLICY "read own notes" ON notes
  FOR SELECT USING ((select auth.uid()) = user_id);

CREATE POLICY "insert own notes" ON notes
  FOR INSERT WITH CHECK ((select auth.uid()) = user_id);

CREATE POLICY "update own notes" ON notes
  FOR UPDATE USING ((select auth.uid()) = user_id)
  WITH CHECK ((select auth.uid()) = user_id);

CREATE POLICY "delete own notes" ON notes
  FOR DELETE USING ((select auth.uid()) = user_id);

Notes: - With RLS enabled and no policies, nothing is accessible. That's a safe default. - The service role / secret key bypasses RLS. Only use it on the server, never in the browser. - Add an index on user_id. Policies run on every query.

Connectez-vous pour dire si ça a marché.

answered a month ago

Sign in and verify your email to post an answer.