跳到正文
DEV Community· PrototypeVerdict·· 5 小時前AI 評分62

如何為 Lovable 和 Bolt 應用程式生成並檢查 Supabase RLS 政策

How to generate and check Supabase RLS policies for Lovable and Bolt apps

AI 導讀

PrototypeVerdict 介紹一套在瀏覽器中生成 Supabase RLS SQL 範本並檢查政策文字的方法,測試在本地 Postgres 17 環境進行,並非連接實際 Supabase 專案。

正文

PrototypeVerdict

A table can show "RLS enabled" in the Supabase dashboard and still leak rows, or break sign-in, if the policies are wrong.
This isn't a security audit. It's just a practical check. Full disclosure, we run PrototypeVerdict. The generator and checker on this page run in the browser. They write standard Postgres and Supabase Row Level Security templates and look for text patterns. They don't connect to your database, call Supabase or any AI service, or save what you type.
Bolt's built-in database was Supabase-backed in our tests. Lovable's docs say its built-in backend uses Supabase's open-source foundation, and you can connect your own project. In our first Bolt test build, on Oct 29, 2026, this one bit me. Sign-in got stuck on the login screen with a 500 error caused by recursive RLS policies on the profiles table. Bolt fixed it by moving the admin check into a SECURITY DEFINER is_admin() function. That one fix used about 84K tokens.
The generated SQL follows Supabase's Row Level Security docs (read 24 Nov 2026). It turns RLS on with alter table ... enable row level security. That's because a table in an exposed schema without RLS is readable and writable by any role with a grant on it. It revokes default grants and grants back only the operations you chose. On many Supabase projects a new public table starts with every privilege granted to anon and authenticated, and adding policies doesn't take those grants back. If they were never granted, the revoke changes nothing.
It writes one policy per operation (SELECT, INSERT, UPDATE, DELETE) rather than FOR ALL. USING decides which existing rows a user can read, update, or delete. WITH CHECK decides what a new or updated row may look like, so a user can't create a row for someone else or reassign it. Policies name their role with to authenticated (or to anon, authenticated for public reads), wrap auth.uid() as (select auth.uid()) so Postgres can evaluate it once per statement, and index the filter columns. Team and admin lookups go in a security definer function in a private schema, with set search_path = ''. That's the docs' pattern for avoiding recursive policies.
The generator writes five access patterns: private (owner only); public read with owner write; signed-in read with owner write; team or org rows via a membership table, where members can read, insert, and edit and only the current owner can delete; and admin only, via a security definer helper, for back-office tables.
Two things we confirmed when we ran that SQL. In the team pattern, any team member can edit the team's rows, including changing the owner column, but can't move a row to a team they aren't in, and only the current owner can delete. In the admin pattern, the roles table can be read only by the user it describes, so assign admins from the Supabase SQL editor or a trusted server, never from the browser.
We ran the checker in Node and its SQL on a local Postgres 17 database set up to behave like Supabase, not a live Supabase project. All 1,200 option-combination scripts ran without errors. What actually showed up:

  • RLS disabled, or a table created without enabling it: any role with a grant can read and write the table.- USING (true) or WITH CHECK (true) on INSERT, UPDATE, DELETE, or FOR ALL: everyone the policy applies to can change every row. In our test, one signed-in user could edit another user's row. USING (true) on SELECT for signed-out visitors makes every row public, which is fine only for data meant to be public.- A policy that queries its own table: Postgres raises 42P17, "infinite recursion detected in policy for relation". We reproduced it for both SELECT and UPDATE. A SELECT policy that calls a non-security-definer function which reads the same table failed with 54001, "stack depth limit exceeded". Two tables whose SELECT policies read each other also raise 42P17.- An INSERT policy without WITH CHECK blocked every insert in our test. An INSERT policy with USING is rejected by Postgres.- A policy with no TO clause applies to every role, including signed-out visitors. A policy that reads user_metadata cannot be trusted for authorization, because signed-in users can change their own user metadata.- A security definer function without set search_path, or in the public schema, is risky: a caller could hijack unqualified names, and a function in an exposed schema is callable over the API. A view without security_invoker = true can bypass the RLS of the tables it reads.Unwrapped auth.uid(), FOR ALL policies, and UPDATE without WITH CHECK are notes, not errors. A clean result only means those patterns weren't in the text you pasted. It can't see dashboard policies or earlier migrations you didn't paste. Rows owned by two users, roles in JWT claims, or multi-step permissions need hand-written policies. The real test is two accounts. Before launch, sign in as two separate non-admin test accounts and confirm neither can see or change the other's rows. Secret or service role keys bypass RLS when used without a user token, so keep them out of the browser and frontend code. The full finding table is on the same page: https://prototypeverdict.com/tools/supabase-rls-policy-generator/. Supabase's RLS docs and Lovable's Supabase docs read 24 Nov 2026. Tool tested on Postgres 17 on 24 Nov 2026. Bolt test build: Oct 29, 2026. If a leftover USING (true) or a recursive profiles policy has bitten you, how did you catch it?

來源:DEV Community · dev.to