Deutsch
Fix libraryHighSupabase · Access control

Missing ownership check on update and delete: any user can change anyone’s data

If your update and delete rules only check that someone is signed in, every user can edit or delete other users’ rows. Find these policies and add the owner check.

By Tim GeithnerPublished 3 min read

ShipRely's Security audit checks for this today.

Short answer

An ownership check asks ‘is this row yours?’ before a change. Many AI-built apps only ask ‘are you signed in?’ for updates and deletes, and filter by user_id in the frontend. Then any user can edit or delete any row whose ID they can see. Bind every update and delete policy to the row’s owner with (select auth.uid()) = user_id, in both USING and WITH CHECK, and test it as a second user.

Ask an AI about this page:ChatGPTClaude

On this page

A missing ownership check means the app asks “are you signed in?” before an update or delete, but never “is this row yours?”. Every signed-in user can then edit or delete any row whose ID they know, and in a typical app IDs are visible in lists and URLs. This is the classic insecure direct object reference. It is rated high: it needs an account, which is usually free to create, and it lets one user deface or wipe everyone else’s content.

Why it matters

The pattern is common in apps where content is public to read but private to change, such as comments, reviews or community posts. Reading by everyone is fine; the write rules are the problem:

VulnerableThe vulnerable policies
create table public.comments (
  id uuid primary key default gen_random_uuid(),
  post_id uuid not null,
  user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
  body text not null,
  created_at timestamptz not null default now()
);

alter table public.comments enable row level security;

create policy "Anyone can read comments"
  on public.comments for select
  to anon, authenticated
  using (true);

create policy "Users can add comments"
  on public.comments for insert
  to authenticated
  with check ((select auth.uid()) = user_id);

create policy "Signed-in users can edit comments"
  on public.comments for update
  to authenticated
  using (auth.uid() is not null);

create policy "Signed-in users can delete comments"
  on public.comments for delete
  to authenticated
  using (auth.uid() is not null);

The frontend looks careful, because it only shows the edit button on your own comments and sends .delete().eq("id", commentId).eq("user_id", user.id). But that second filter is your code’s choice. Anyone can call the same API without it and delete a comment that isn’t theirs. The update policy has a second hole: with no with check, PostgreSQL reuses the using condition for the new row, so a user can also rewrite user_id and pin their words on someone else.

The same mistake appears outside policies: an edge function or database function that runs with admin rights and deletes “the row with this ID” without comparing its owner to the caller. The fix idea is identical; see edge functions that trust the user ID from the request.

How to find it in your app

List every update, delete and “all” policy and whether its condition compares something with auth.uid():

Update and delete policies without an owner comparison
select tablename, policyname, cmd, qual,
       coalesce(qual, '') ~* '(auth\.uid\(\)( as uid\))? = )|(= (\( select )?auth\.uid\(\))' as compares_owner
from pg_policies
where schemaname = 'public'
  and cmd in ('UPDATE', 'DELETE', 'ALL')
order by compares_owner, tablename, policyname;

Rows with compares_owner = false come first. Some are fine, for example a membership helper such as private.is_member(workspace_id). Every other one needs a closer look. A true row still deserves a glance: is the column it compares really the owner?

In your frontend, search for .update( and .delete(. If the only owner condition sits in .eq("user_id", …), the database must enforce the same rule.

How to fix it

Replace the two write policies in a new migration. Use the owner column in both using (which rows may be touched) and with check (what the row may look like afterwards):

SafeFix: authors only
drop policy "Signed-in users can edit comments" on public.comments;
drop policy "Signed-in users can delete comments" on public.comments;

create policy "Authors can edit their comments"
  on public.comments for update
  to authenticated
  using ((select auth.uid()) = user_id)
  with check ((select auth.uid()) = user_id);

create policy "Authors can delete their comments"
  on public.comments for delete
  to authenticated
  using ((select auth.uid()) = user_id);

Keep the .eq("user_id", …) in the frontend if you like; it is harmless. It just isn’t the protection any more. If moderators need to edit comments, add an or with a role check that users cannot set themselves, as described in the admin check fix.

Add ownership checks to update and delete

In the Supabase migrations, find every update, delete or all policy on public tables whose condition only checks that the user is signed in (auth.uid() is not null, auth.role() = ‘authenticated’ or true). Start with public.comments. Add a new migration that drops each of these policies and creates a replacement for the authenticated role only that allows the action only when (select auth.uid()) = user_id (use the table’s real owner column). Update policies need the same condition in USING and WITH CHECK.

Do not change the select policies and do not edit existing migrations. Also list every edge function or database function that updates or deletes rows by an ID from the request, and say whether it compares the row’s owner with the caller.

Check that the fix works

Act as a second user and try to change and delete the first user’s rows:

Check as another user
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
update public.comments set body = 'edited by someone else'
where user_id = '11111111-1111-1111-1111-111111111111'
returning id;
delete from public.comments
where user_id = '11111111-1111-1111-1111-111111111111'
returning id;
rollback;

Put a real ID of a second user in the claims and the first user’s ID in both where clauses. Both statements must return zero rows. There will be no error: PostgreSQL silently skips rows that fail the policy, so always count what changed. Then repeat with the first user’s own ID in the claims: their update and delete must still work. The RLS testing guide shows how to keep checks like this in your test suite, and the always-true policy fix covers the read side of the same problem.

Frequently asked questions

My delete call already filters by user_id. Isn’t that an ownership check?

Only in your own code. Someone calling the API with your public key sends the same request without that filter. The database has to refuse the change, which means the condition belongs in the policy.

Why does an update by another user return no error after the fix?

Postgres skips rows that fail an update or delete policy’s USING condition without reporting an error. The request succeeds and changes zero rows. Check the number of changed rows, not just the absence of an error.

Are UUIDs as IDs enough protection?

No. Random IDs are hard to guess, but they show up wherever rows are visible: in public lists, URLs, API responses and shared links. Authorization must not depend on an ID staying secret.

How do I let moderators or admins edit other people’s rows?

Add a second condition to the policy that checks a role users cannot set themselves, such as a user_roles table read through a security definer helper. Never check a role stored in user_metadata.

Sources

  1. 1.Row Level Security — Supabase Docs
  2. 2.CREATE POLICY — PostgreSQL Documentation
  3. 3.CWE-639: Authorization Bypass Through User-Controlled Key — MITRE
  4. 4.Authorization Cheat Sheet — OWASP Cheat Sheet Series
  5. 5.A01 Broken Access Control — OWASP Top 10

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt