Supabase database

Contact form fails with “new row violates row-level security policy”: fix it without exposing every submission

A contact, lead or quote form is the simplest table in an app: visitors write to it and only you read it. It is also where we see AI builders open the most personal data, because the error that shows up on submit points at the wrong problem. We ran Postgres 17 with PostgREST 12.2.12, the server supabase-js talks to, on 24 November 2026, gave it Supabase’s default grants, reproduced the error, and tried each fix as a logged-out visitor.

TL;DR

  • If anon has an insert-only policy and the form still fails with “new row violates row-level security policy”, the code is almost certainly calling .insert(row).select(). Asking for the row back needs read access, and the error doesn’t say so.
  • The usual AI fixes (a select policy using (true), turning RLS off, or one policy “for all” using (true)) all made the form work. All three let a logged-out visitor read every submission; the last two also let them delete every row.
  • The fix: drop .select() from the insert, keep the insert-only policy, limit anon to the form’s columns, and let only admins read.

This guide is for founders and agencies whose Lovable app (or any Supabase app built with Bolt, Cursor or Claude Code) has a contact, lead, quote, waitlist or booking form that fails on submit. The results come from our own run on 24 November 2026: Postgres 17.11 and PostgREST 12.2.12, with the anon and authenticated roles and the table grants a new Supabase project gives them. The data was fake: two earlier enquiries from “Bob” and “Dana” with phone numbers and messages.

Why the form fails when anon is allowed to insert

We started where a careful builder would: row level security on, and one policy that lets logged-out visitors insert and nothing else.

the correct starting point
alter table public.contact_submissions enable row level security;

create policy "anon can submit" on public.contact_submissions
  for insert to anon with check (true);

Then we sent the same form twice, the two ways supabase-js sends an insert:

lab output, 24 Nov 2026
.insert(row)            (Prefer: return=minimal)         201
.insert(row).select()   (Prefer: return=representation)  401
  {"code":"42501","message":"new row violates row-level
   security policy for table \"contact_submissions\""}
anon reads the table: []

The second call asks the database to send the new row back. Reading it back counts as a select, and anon has no select policy, so the whole insert is refused and rolled back: only the first of our two submissions was stored. The message says the new row violated a policy, which sends people, and AI builders, off to loosen the insert rules. The insert rules were fine.

AI builders add .select() out of habit, often to get the new id for a “thanks” screen. A form rarely needs it.

The three quick fixes that expose every submission

These are the three answers AI builders and forum threads most often give for this error. We applied each one to a fresh table, then tried it as a logged-out visitor with nothing but the public anon key, which ships in every Supabase app’s JavaScript.

Quick fix A: “Enable read access for all users”

do not ship
create policy "Enable read access for all users"
  on public.contact_submissions for select using (true);
lab output, 24 Nov 2026
form posts with .select(): 201                     form works
anon GET /contact_submissions:
  [{"name":"Bob Client","email":"bob@example.com","phone":"+447700900002"},
   {"name":"Dana Client","email":"dana@example.com","phone":"+447700900004"},
   ...]

The form works, and every enquiry anyone has ever sent is one request away from anyone. Contact forms collect exactly what you would not want public: names, emails, phone numbers, and messages people wrote assuming only you would read them. The policy name is Supabase’s own template name, which is why it looks safe. This is finding PL-005.

Quick fix B: turn row level security off

lab output, 24 Nov 2026
anon reads: every submission (same as above)
anon edits Bob's message (PATCH):      204
anon deletes all rows (DELETE):        204
rows left: 0

With RLS off, Supabase’s default grants are all that is left, and they give anon every right on the table. A visitor can read your leads, rewrite them, or wipe them. This is finding PL-001.

Quick fix C: one policy “for all” that is always true

do not ship
create policy "Allow all" on public.contact_submissions
  for all using (true) with check (true);
lab output, 24 Nov 2026
anon reads: every submission
anon deletes all rows: 204
rows left: 0

Same result as turning RLS off, but the dashboard still shows RLS as enabled, so it passes a glance.

The quiet problem with insert-only: anon can set any column

Even the correct policy has a gap. with check (true) checks rows, not columns, and the default grants let anon write every column. We posted a submission that set its own status and created_at:

lab output, 24 Nov 2026
anon posts status=closed, created_at=2020-01-01: 201
stored: status=closed created_at=2020-01-01

On a lead table that can mean spam that files itself as handled, or a booking that marks itself paid. Granting insert on only the form’s columns closes it:

lab output, 24 Nov 2026
revoke insert on public.contact_submissions from anon;
grant insert (name, email, phone, message) on public.contact_submissions to anon;

anon posts status=closed again:  401 permission denied for table contact_submissions
anon posts only the form fields: 201

The fix we proved

In the app, send the insert without asking for the row back. If you need an id for the thank-you screen, make it in the browser with crypto.randomUUID() and send it as a column you allow.

form submit (supabase-js v2)
const { error } = await supabase
  .from('contact_submissions')
  .insert({ name, email, phone, message });   // no .select()
if (error) showError(error.message);

In the database, one migration:

contact-form-fix.sql
alter table public.contact_submissions enable row level security;

-- visitors (and signed-in users) can submit, nothing else
create policy "anon can submit" on public.contact_submissions
  for insert to anon, authenticated with check (true);

-- only admins read; role set in app_metadata (users can't edit it)
create policy "admins read" on public.contact_submissions
  for select to authenticated
  using ((auth.jwt() -> 'app_metadata' ->> 'role') = 'admin');

-- replace the default grants with exactly what the form needs
revoke all on public.contact_submissions from anon, authenticated;
grant insert (name, email, phone, message)
  on public.contact_submissions to anon, authenticated;
grant select on public.contact_submissions to authenticated;

-- cheap limits against junk
alter table public.contact_submissions
  add constraint msg_len check (char_length(message) between 1 and 5000),
  add constraint email_shape check (email ~* '^[^@\s]+@[^@\s]+\.[^@\s]+$');

What our run showed after it:

lab output, 24 Nov 2026
anon submits (.insert(row)):            201     form works
anon submits (.insert(row).select()):   401     still refused, by design
anon reads:                             permission denied
signed-in customer reads:               []
admin (app_metadata.role=admin) reads:  all 3 submissions
anon deletes:                           permission denied
anon posts a 6,000-character message:   400 violates check constraint "msg_len"

Use app_metadata for the admin role, never user_metadata, which users can change themselves (why). If your team reads leads in the Supabase dashboard or from a server with the service key, you can leave out the admin policy entirely: those bypass RLS.

The constraints stop oversized junk, not a bot that sends thousands of valid-looking rows. If that starts, move the submit to an Edge Function that checks a CAPTCHA token and inserts with the service key, and revoke anon’s insert.

Check which state your app is in

Run this in the Supabase SQL editor. It only reads. It lists public tables that a logged-out visitor can read: RLS off, or a read, update, delete or “all” policy for anon or everyone whose condition is just true.

anon-readable-tables.sql
select c.relname as table_name,
       case when not c.relrowsecurity
            then 'RLS OFF: anon can read/write everything granted'
            else 'policy ' || p.polname || ' using '
                 || coalesce(pg_get_expr(p.polqual, p.polrelid), '-')
       end as finding
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
left join pg_policy p on p.polrelid = c.oid
  and p.polcmd in ('r', '*', 'd', 'w')
  and (p.polroles = '{0}' or 'anon'::regrole::oid = any(p.polroles))
  and coalesce(pg_get_expr(p.polqual, p.polrelid), 'true') = 'true'
where n.nspname = 'public' and c.relkind = 'r'
  and has_table_privilege('anon', c.oid, 'select')
  and (not c.relrowsecurity or p.polname is not null);

In our run it flagged the form table with quick fix A and a table with RLS off, and correctly left out an insert-only newsletter table:

lab output, 24 Nov 2026
quote_requests      | RLS OFF: anon can read/write everything granted
contact_submissions | policy Enable read access for all users using true

It only catches the plainest cases. A policy like using (status = 'new') is just as open and won’t show up; our every-table check covers more.

Tell your AI builder the rule

prompt for Lovable or your AI builder
The contact form fails with "new row violates row-level
security policy". Do not add a select policy using (true),
do not disable RLS, and do not add a FOR ALL policy.
Fix it like this:
- Remove .select() from the insert call; the form doesn't
  need the row back.
- Keep one INSERT policy TO anon, authenticated.
- Revoke all from anon, authenticated and grant INSERT only
  on (name, email, phone, message).
- Only users with app_metadata.role = 'admin' may SELECT.
Show me the migration before applying it.

Sources

The checks behind this guide, each with example code, a self-check and the fix.

Free check

Check your app for this, free.

Paste your public repo. PatchLabs rebuilds your database from its migrations, tests it as a visitor, a stranger and the owner, reads the code for keys and risky patterns, and hands you the fix.

First app free, fixes includedNever touches your database

Written by Manraj Singh Dhillon. Last checked 24 Nov 2026. Code samples are synthetic and run against your own project; nothing here names a real app. Spotted a mistake? Tell us.