Supabase RLS security for AI-generated apps
Review who can read and change your data. Build an owner-only RLS example, then verify it with two users and a signed-out request.
Plainrelay · Updated
Start with a cross-account request
A prototype can look private because its screen only lists the current user’s records. Test the underlying request with another account. If changing the record ID exposes someone else’s content, the missing ownership check is already a release blocker.
AI-generated scaffolding deserves this review when the prompt specified screens and a happy path but left ownership rules undefined. A demo account, permissive development policy, or privileged server call can mask the gap. Record the actual behavior before choosing a repair.
Separate public configuration from privileged access
A Supabase publishable key can appear in a browser client. Secret keys and legacy service-role keys belong on trusted servers because they can bypass RLS. If a privileged key reached a client bundle or repository, rotate it and review its use. A server function using that key must authorize its caller before acting on a supplied record ID.
Write the access rule before the policy
For this example, a signed-in user may create, read, edit, and delete their own notes. A user may not transfer ownership through an update. Signed-out visitors get no access. Use a separate development project with two test users and fictional content.
Create a small, isolated example
-- Run once in a disposable Supabase development project.
-- This example models private notes owned by individual users.
begin;
create table public.audit_notes (
id uuid primary key default gen_random_uuid(),
owner_id uuid not null references auth.users(id),
body text not null
);
create index audit_notes_owner_idx on public.audit_notes (owner_id);
alter table public.audit_notes enable row level security;
revoke all on public.audit_notes from anon, authenticated;
grant select, insert, update, delete on public.audit_notes to authenticated;
create policy "Read own notes" on public.audit_notes
for select to authenticated
using ((select auth.uid()) = owner_id);
create policy "Create own notes" on public.audit_notes
for insert to authenticated
with check ((select auth.uid()) = owner_id);
create policy "Update own notes" on public.audit_notes
for update to authenticated
using ((select auth.uid()) = owner_id)
with check ((select auth.uid()) = owner_id);
create policy "Delete own notes" on public.audit_notes
for delete to authenticated
using ((select auth.uid()) = owner_id);
commit;The update rule checks both the existing row and its replacement. The select policy also permits the owner to read rows during updates. Table grants and row policies both participate in access control.
Test the real client boundary
Create users A and B through the normal sign-up flow. Use their separate authenticated browser sessions or API clients with the publishable key. Run the following checks against the same saved note and inspect both the response and the stored result.
| Attempt | Expected result |
|---|---|
| A creates a note with A as owner | One saved note, readable by A |
| B reads A’s note by its ID | No private row returned |
| B updates or deletes A’s note | A’s saved row remains unchanged |
| B inserts a note naming A as owner | Write rejected |
| A updates the owner to B | Write rejected, original owner retained |
| A updates the body, then deletes the note | Both intended operations succeed |
| A signed-out visitor requests the table | No private data or permitted writes |
Check returned rows and affected-row counts. A request can complete successfully while a policy filters out every row. Repeat the same assertions against custom endpoints and Edge Functions that expose this data.
Review the policy set on an existing app
-- Inspect the current policy set before changing an existing table.
select policyname, roles, cmd, qual, with_check
from pg_policies
where schemaname = 'public' and tablename = 'audit_notes';Permissive policies combine with OR. An older broad allow policy can keep granting access after you add a narrower one. Review all policies together and replace conflicting rules through a tested migration. Table owners and roles with bypass privileges need separate checks.
PostgreSQL row security policy behavior
Map each data boundary
For a team application, write down the membership and role rules for invitations, removed members, and shared records. Extend the test matrix to those cases. Review file buckets, views, RPC functions, and privileged background jobs individually so each has an explicit access decision.
Release the change with evidence
Keep the migration and access tests in the repository. Capture the allowed and denied cases before deploying, rerun them in staging, and verify the intended account flows after release. Record which tables and endpoints were covered so the next developer can repeat the check.
Apply this to your app
Work through the production readiness checklist to identify the remaining gaps. For a written review of your code and a prioritized repair plan, explore the AI app production audit.
Discuss my audit