Supabase Storage upload fails with "new row violates row-level security policy"
You call supabase.storage.from('avatars').upload(path, file) and get StorageApiError: new row violates row-level security policy, often with status 403. Every file in Supabase Storage is a row in the storage.objects table, and that table has Row Level Security on. Uploading inserts a row, so it needs an INSERT policy on storage.objects, separate from any policies on your own tables. Here are the causes, most common first.
1. There's no INSERT policy for this bucket
A new bucket has no policies, so nobody can upload, not even signed-in users. Making the bucket public doesn't change that: "public" only means anyone can download files through the public URL. Add a policy that lets each user upload into their own folder:
create policy "Users upload to their own folder"
on storage.objects for insert
to authenticated
with check (
bucket_id = 'avatars'
and (storage.foldername(name))[1] = (select auth.uid())::text
);
2. The file path doesn't match the policy
With the policy above, the path must start with the user's id: `${user.id}/avatar.png`. Common mistakes:
- Starting the path with the bucket name (
avatars/123/…). The bucket is already thefrom()argument. - A leading slash (
/123/avatar.png), which makes the first folder empty. - Using a profile id or an email instead of the auth user id.
- A different bucket id:
bucket_id = 'Avatars'won't matchavatars.
3. You upload with upsert: true
An upsert may overwrite an existing file, so Storage also checks SELECT and UPDATE policies. If only INSERT exists, the first upload works and the second fails. Either upload with upsert: false and a unique file name, or add the matching policies:
create policy "Users read their own files"
on storage.objects for select to authenticated
using (bucket_id = 'avatars'
and (storage.foldername(name))[1] = (select auth.uid())::text);
create policy "Users update their own files"
on storage.objects for update to authenticated
using (bucket_id = 'avatars'
and (storage.foldername(name))[1] = (select auth.uid())::text);
4. The user isn't signed in yet
If the upload runs before the session has loaded, it's sent as anon and a policy to authenticated rejects it. Check (await supabase.auth.getSession()).data.session right before uploading.
5. Downloading, listing or signed URLs fail too
download(), list() and createSignedUrl() on a private bucket need a SELECT policy. Removing files needs a DELETE policy. If those calls return empty results or errors, it's the same fix with a different command.
What not to do
- Don't add
with check (true)foranon. Anyone could fill your bucket, and pay for your storage, without signing in. - Don't upload with the service_role key from the browser. It bypasses every policy for everyone. If that already happened, see what to do when the service_role key is exposed.
- Don't make a bucket public for private files like ID photos, invoices or exports. Use a private bucket and signed URLs.
The same error on a normal table has other causes: see new row violates row-level security policy on tables.
A prompt to paste into Lovable, Bolt or Cursor
Uploading to the Supabase Storage bucket "avatars" fails with
"new row violates row-level security policy". Fix it safely:
1. List the current policies on storage.objects for this bucket.
2. Add INSERT, SELECT, UPDATE and DELETE policies for authenticated
users, limited to files in a folder named after their auth.uid().
3. Make the upload path `${user.id}/`, with no leading slash
and without the bucket name.
4. Only upload after the auth session has loaded.
Do not allow anon uploads, do not use with check (true), do not make
the bucket public, and do not use the service_role key in the frontend.
Want to check nothing else is exposed? LeakPatrol's free scan checks your live app for leaked keys, public files and other common mistakes in about a minute.
Check your app in a minute
Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.
Get the 10-point checklist
Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.
Plus the odd security tip. Unsubscribe any time.
Questions
Why does Supabase Storage upload say new row violates row-level security policy?
Every file is a row in storage.objects, which has RLS on. Uploading inserts a row, so you need an INSERT policy on storage.objects for that bucket. Policies on your own tables do not apply.
Does a public Supabase bucket allow uploads?
No. A public bucket only lets anyone download files through the public URL. Uploading, updating and deleting still need policies on storage.objects.
Why does the first upload work but the second fail?
Uploading with upsert: true can overwrite a file, so Storage also needs SELECT and UPDATE policies. Add them, or upload with upsert: false and a unique file name.
What path should I use with storage.foldername?
Start the path with the user id, like user-id/file.png, without a leading slash and without the bucket name. storage.foldername(name)[1] is then the user id the policy compares with auth.uid().
More guides
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase's October 30 change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase "Database error saving new user": find the cause and fix it safely
- Supabase "infinite recursion detected in policy for relation" fixed
- Supabase Edge Function "blocked by CORS policy" fixed
- Supabase "Forbidden use of secret API key in browser" (401) fixed
- Supabase "Invalid API key" and "Legacy API keys are disabled" fixed
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)