Guides

Supabase INSERT policies and WITH CHECK

WITH CHECK (true) and logged-in checks let a user create or rewrite rows under someone else's user_id. The fix is auth.uid() = user_id, on insert and on update.

Updated 27 October 2026 · LIM Agentic AI, Paris

A select policy decides which existing rows a role can see. It does not decide what a new row is allowed to look like. Insert uses WITH CHECK. Update uses both: USING picks the existing rows, WITH CHECK judges the row after the change. Generated Supabase apps often ship the dashboard template that only checks "this person is logged in."

USING and WITH CHECK

USING is added to the rows a SELECT, UPDATE, or DELETE can see. Rows that fail it are invisible to that command. WITH CHECK runs on the row that would be stored. INSERT policies only have WITH CHECK. There is no existing row to filter.

If you omit WITH CHECK on an UPDATE or on a FOR ALL policy, Postgres uses the USING expression for the new row as well. Omitting it is not itself the bug. The bug is a check that passes for every logged-in user.

These three policies are the usual shape. The names vary. The expressions are what matter.

Policies that accept someone else's user_id
-- Logged-in users may insert any user_id.
create policy "Enable insert for authenticated users only"
on public.notes
for insert
to authenticated
with check (true);

-- Same hole, written as "is logged in".
create policy "insert if authenticated"
on public.notes
for insert
to authenticated
with check (auth.uid() is not null);

-- Any signed-in user can update any visible row, and can change user_id.
create policy "update if logged in"
on public.notes
for update
to authenticated
using (auth.uid() is not null)
with check (auth.uid() is not null);

with check (true) accepts every column the grant lets the user set, including user_id. auth.uid() is not null and auth.role() = 'authenticated' are the same hole with extra words. The to authenticated clause already limits the policy to signed-in users. Repeating that test inside WITH CHECK does not compare the row to the session.

The matching client call is small. User B sends user A's id:

B inserts a row owned by A
const stolen = await b
  .from("notes")
  .insert({ body: "written by B", user_id: inserted.data.user_id })
  .select("id, user_id");

// Pass: stolen.error is set, and the message says the new row violates RLS.
// Fail: stolen.data contains a row whose user_id is A's id.

A failed check comes back as an error, often SQLSTATE 42501, with text like "new row violates row-level security policy." A success comes back as the inserted row. Empty data and a null error is not the signal here, because a successful insert with .select() returns the row.

The owner check

Drop the open policies first. Permissive policies (the default) are OR'd. If the strict policy and the with check (true) policy both exist, the open one still wins.

Owner policies for one user-owned table
alter table public.notes enable row level security;

drop policy if exists "Enable insert for authenticated users only" on public.notes;
drop policy if exists "insert if authenticated" on public.notes;
drop policy if exists "update if logged in" on public.notes;

create policy "notes select own"
on public.notes
for select
to authenticated
using (auth.uid() = user_id);

create policy "notes insert own"
on public.notes
for insert
to authenticated
with check (auth.uid() = user_id);

create policy "notes update own"
on public.notes
for update
to authenticated
using (auth.uid() = user_id)
with check (auth.uid() = user_id);

create policy "notes delete own"
on public.notes
for delete
to authenticated
using (auth.uid() = user_id);

The insert line is with check (auth.uid() = user_id). The update policy repeats it in both places. USING stops B from touching A's existing row. WITH CHECK stops A from changing user_id to B on a row A does own. Write both. A later edit that widens USING and forgets the check is how this regresses.

Supabase's docs also note that an UPDATE needs a SELECT policy, or the update does not see the row. The select policy above is that piece. Do not "fix" a failed update by adding using (true) on select.

WITH CHECK sees the row that will be stored, after column defaults and BEFORE triggers. A default of user_id uuid default auth.uid() satisfies auth.uid() = user_id when the client omits user_id. A client-supplied id of someone else does not. The default is convenient. The policy is the control.

A BEFORE trigger can also overwrite user_id so the client cannot pick it. Keep the policy anyway. A trigger is application code. The policy is the grant.

Optional trigger, still not a substitute for the policy
create or replace function public.set_note_owner()
returns trigger
language plpgsql
set search_path = public
as $$
begin
  if tg_op = 'INSERT' then
    new.user_id := auth.uid();
  elsif new.user_id is distinct from old.user_id then
    raise exception 'user_id is immutable';
  end if;
  return new;
end;
$$;

drop trigger if exists notes_set_owner on public.notes;
create trigger notes_set_owner
before insert or update on public.notes
for each row execute function public.set_note_owner();

That function is security invoker (the Postgres default) and sets search_path. It runs as the signed-in user. It does not need the service role.

Check it yourself

pg_policies lists every policy in the database. qual is the USING expression. with_check is the WITH CHECK expression. cmd is the command, or ALL.

Read every public policy
select tablename, policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname = 'public'
order by tablename, policyname;

Then run the insert from user B's session, aimed at a marker, not at a customer row. The error you want is the RLS violation. If the insert succeeds, quote qual and with_check for that table before you change anything else. Stacking another policy on top will not help while the open one remains.

Questions

Is WITH CHECK (auth.uid() = user_id) enough on its own for update?

No. Update needs USING to decide which existing rows can be touched, and WITH CHECK to decide what the stored row may become. Use auth.uid() = user_id in both. If you only set USING, Postgres copies it into the check, so a strict USING happens to cover the write. A USING of true, or of "is logged in", copied into the check, does not.

The dashboard created "Enable insert for authenticated users only." Is that safe?

Read the expression. That template is often WITH CHECK (true) for the authenticated role. It proves the caller is logged in, which the role already proved, and it lets them set user_id to anyone. Replace it with with check (auth.uid() = user_id) and drop the old policy.

I added the owner policy and user B can still insert. Why?

Permissive policies are OR'd. The old with check (true) policy is probably still there. Drop it. Then confirm the new policy's with_check column in pg_policies is (auth.uid() = user_id), not true.

Other guides