Supabase storage bucket public by mistake: close it and switch to signed URLs

By AI App Medic · Updated · Supabase · Storage · Security · Lovable

A lot of AI-generated apps put avatars, ID scans, or customer uploads in Supabase Storage and leave the bucket public so getPublicUrl() "just works". That setting means anyone who can guess or discover the URL can read the file, and in many cases list objects, without signing in. Row Level Security on your tables does not protect those files. This is the storage version of RLS enabled but the data is still public.

How public buckets actually behave

Each bucket has a flag, public, in storage.buckets. When it is true, Supabase serves object reads as public assets. Access policies on storage.objects do not save you for those public reads. When the bucket is private, every read, list, upload, and delete has to pass a policy, and the browser uses a signed URL or a request with the user's JWT.

You can confirm it in the dashboard: Storage > the bucket > look at the Public badge or toggle. Or in the SQL editor:

select id, name, public, created_at
from storage.buckets;

Step 1: close the bucket

If the files were not meant for the whole internet, turn Public off in the dashboard, or run:

update storage.buckets
set public = false
where id = 'your-bucket-name';

Do this as soon as you know it is wrong. Then list policies:

select policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname = 'storage' and tablename = 'objects';

If you see policies with true for SELECT or INSERT on that bucket, they are as open as a public bucket once someone has the anon key (which is in your JavaScript). Tighten them. A usual pattern is one folder per user id:

-- repeat similar policies for update and delete if users may replace files
create policy "Users can upload to their folder"
on storage.objects for insert to authenticated
with check (
  bucket_id = 'your-bucket-name'
  and (storage.foldername(name))[1] = auth.uid()::text
);

create policy "Users can read their folder"
on storage.objects for select to authenticated
using (
  bucket_id = 'your-bucket-name'
  and (storage.foldername(name))[1] = auth.uid()::text
);

Uploads from the app should then use a path like ${user.id}/filename.pdf, not a flat filename.pdf in the bucket root.

For files that really are public (a marketing image, a public product shot), keep a separate public bucket that only holds those assets. Do not mix passports and hero images in one public bucket.

Step 2: change how the app builds URLs

Search the project for getPublicUrl, /object/public/, and hard-coded storage hosts. Replace display and download paths with a signed URL, created on a server or in a signed-in client that is allowed to read the object:

const { data, error } = await supabase
  .storage
  .from("your-bucket-name")
  .createSignedUrl(`${user.id}/${fileName}`, 60 * 10); // 10 minutes

if (error) throw error;
// use data.signedUrl as the href or <img src>

Short expiries are better for private documents. If you need to show many files on a page, sign them in one batch with createSignedUrls. Do not mint a week-long URL and log it in analytics or a public repo.

If the upload ran from the browser with a loose policy, also check that the client is not using the service role key. That key bypasses storage policies the same way it bypasses table RLS. If it was shipped in the frontend, follow service role key exposed first: rotate the key, then come back to the bucket.

Step 3: treat what was public as exposed

Anyone who had the link, or who listed the bucket while it was public, may still have a copy. A private toggle does not retract a file that was already downloaded.

Step 4: test as two users and as a stranger

  1. Sign in as user A, upload a file, confirm it appears only under A's folder, and that a signed URL works for A.
  2. Sign in as user B and confirm B cannot list or download A's file, including by pasting A's path into the API.
  3. Sign out and open the old public URL. It should fail. If it still works, the bucket is still public, a CDN cached the object, or you are looking at a different project.
  4. Repeat the checks on the production project, not only a preview.

The official Storage access-control docs are worth a pass if you need policies for a team, a shared album, or an admin role. The idea stays the same: private bucket, policies that name the bucket and the path, signed URLs in the app, and no service role in the browser.

FAQ

I already added storage policies. Why can strangers still download the file?

On a public bucket, reads are not enforced the way people expect. The object URL works without a user session. Policies start protecting reads after the bucket is private.

Is the Supabase anon key the problem?

The anon key is public by design. Combined with a public bucket, or with a loose policy on a private bucket, it is enough for anyone to fetch files. Fix the bucket and the policies; do not try to hide the anon key in frontend code.

Will turning the bucket private break my images overnight?

Yes, if the app uses getPublicUrl or hard-coded public object URLs. Switch those calls to createSignedUrl or an authenticated download before or immediately after you flip the toggle, and test every upload and display path.

Related guides