Supabase Storage: public buckets vs signed URLs (keep user uploads private)
Your tables have RLS, your keys are server-side, and then a user uploads a passport photo to a bucket that anyone on the internet can read. In Supabase Storage, a public bucket skips access checks for reading files entirely. If the file has a URL, anyone holding that URL can open it.
storage.objects (Storage Buckets docs). Use public buckets only for things meant for everyone. Put user uploads in a private bucket, scope policies to the owner's folder, and give the browser short-lived signed URLs.Here's the setup AI tools write all the time, usually to make an image preview "just work":
// bucket "uploads" created with Public = ON
const path = `${user.id}/${file.name}`
await supabase.storage.from('uploads').upload(path, file)
const { data } = supabase.storage.from('uploads').getPublicUrl(path)
// saved to the database and shown in the app:
// https://YOUR-PROJECT.supabase.co/storage/v1/object/public/uploads/<user-id>/drivers-license.jpg
That URL never expires and needs no login. It ends up in your database, in page HTML, in browser history, in analytics and in shared links, and user ids are often visible in your API responses, so paths like <user-id>/avatar.png are easy to guess. Turning RLS on for your tables doesn't change any of this, because reads from a public bucket don't go through RLS.
Step 1: Find your public buckets
Run this in the Supabase SQL Editor:
select id, public, file_size_limit, allowed_mime_types
from storage.buckets
order by public desc, id;
Every row with public = true serves its files to anyone with the URL. Empty file_size_limit and allowed_mime_types mean the bucket itself doesn't limit upload size or type (step 6).
Step 2: Decide what can really be public
- Fine in a public bucket: avatars you show to everyone, blog and marketing images, public product photos. Supabase lists these as the public use cases, and public buckets are faster because they're cached differently.
- Must be private: ID documents, invoices and receipts, private photos, voice notes, generated reports and exports, contracts, anything a user would be upset to see on someone else's screen.
If one bucket mixes both, split it: keep avatars public and move everything else to a new private bucket such as user-files.
Step 3: Make the bucket private
In the dashboard, open Storage, edit the bucket and switch off Public. Or do it from server code with the service key (never from the browser):
// server-side only (API route / Edge Function)
await supabaseAdmin.storage.updateBucket('user-files', { public: false })
Old /object/public/... links stop working once the bucket is private, so replace every getPublicUrl() for private files with a signed URL (step 5), and stop storing full public URLs in your tables. Store the file path instead.
Step 4: Scope storage.objects policies to the owner
Put each user's files under a folder named after their user id (<user-id>/file.pdf) and let users touch only their own folder. These follow the patterns in Supabase's Storage Access Control and helper functions docs:
create policy "Users read own files"
on storage.objects for select to authenticated
using ( bucket_id = 'user-files'
and (storage.foldername(name))[1] = (select auth.jwt()->>'sub') );
create policy "Users upload to own folder"
on storage.objects for insert to authenticated
with check ( bucket_id = 'user-files'
and (storage.foldername(name))[1] = (select auth.jwt()->>'sub') );
create policy "Users delete own files"
on storage.objects for delete to authenticated
using ( bucket_id = 'user-files'
and (storage.foldername(name))[1] = (select auth.jwt()->>'sub') );
Add an update policy with the same check only if users need to overwrite files (upsert needs SELECT and UPDATE as well as INSERT). Then look for policies that are too wide:
select policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname = 'storage' and tablename = 'objects';
A select policy that only checks bucket_id = '...' (or true) lets everyone it covers read and list every file in that bucket, because listing uses the same SELECT permission. Same for an insert policy of with check (true): anyone can upload anything.
Step 5: Serve files with short-lived signed URLs
// after confirming the signed-in user may see this file
const { data, error } = await supabase.storage
.from('user-files')
.createSignedUrl(`${user.id}/invoice-1042.pdf`, 60) // seconds
// use data.signedUrl right away; don't save it in the database
- Keep expiries short: about a minute for showing an image or starting a download.
- Store the file path in your tables, and sign a fresh URL when the user needs the file.
- If a server route signs URLs with the service key, it skips your policies, so it must check that the requester owns the file first.
- A signed URL can't be revoked on its own. To cut off access early, delete, move or rename the file, or rotate your project's URL signing key, which revokes every signed URL (Serving assets docs).
Step 6: Set upload limits and test logged out
In the bucket settings, set a file size limit and the allowed MIME types (for example image/png, image/jpeg, application/pdf) so nobody can use your storage to host huge files or HTML pages. Then test with only your public key, the same thing a stranger has:
# a private file through the public URL: must NOT return the file (any error status, never 200)
curl -s -o /dev/null -w "%{http_code}\n" \
"https://YOUR-PROJECT.supabase.co/storage/v1/object/public/user-files/SOME-USER-ID/SOME-FILE.pdf"
# list a bucket while logged out: must return [] or an error, never file names
curl -s -X POST "https://YOUR-PROJECT.supabase.co/storage/v1/object/list/user-files" \
-H "apikey: YOUR_PUBLIC_ANON_KEY" -H "Authorization: Bearer YOUR_PUBLIC_ANON_KEY" \
-H "Content-Type: application/json" -d '{"prefix":"","limit":100}'
Then sign in as two test users and check that user B can't list, read or sign URLs for user A's folder. Re-run these after every AI edit that touches uploads. How to check Row Level Security covers the table side, and RLS filters rows, not columns covers private fields in tables.
Copy-paste prompt for your AI coding tool
Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:
Audit how my app uses Supabase Storage. 1) List every bucket, whether it is public, and what kind of files the app puts in it. Anything per-user or private (IDs, invoices, private photos, exports) must be in a private bucket. 2) Write a migration or dashboard steps to move private files to a private bucket and store file paths, not public URLs, in my tables. 3) Store each user's files under a folder named after their user id, and write storage.objects policies for select, insert, update (only if needed) and delete that check bucket_id and that (storage.foldername(name))[1] equals the signed-in user's id. Remove any policy that only checks bucket_id or uses true. 4) Replace getPublicUrl() for private files with createSignedUrl(path, 60) and make sure any server route that signs URLs with the service key checks ownership first. 5) Set a file size limit and allowed MIME types on each bucket. 6) Give me curl commands using only the public anon key that prove a logged-out user can't download or list private files. Never put the service_role key in browser code and don't print any secret keys.
Check your live site for exposed keys in about a minute
Enter your app's URL on KeySnare for a free check of the page and its same-site JavaScript for a leaked service_role key and other secret keys, or paste a public GitHub repo for a free grade and your top 3 issues, each with a copy-paste AI fix prompt.
Run a free scan →FAQ
Is a public Supabase bucket readable without logging in?
Yes. Supabase's docs say a public bucket bypasses access control for retrieving and serving files, so anyone who has a file's URL can download it. RLS policies still apply to uploads, deletes, moves and copies in a public bucket, but not to reading files.
Are Supabase buckets public by default?
No. Buckets are private by default. A bucket only becomes public if someone ticks Public when creating it or changes it later, which AI tools and tutorials often do so that images just work.
Is a public bucket OK if the file names are random?
Only for files that would be fine to leak. A random name makes a URL harder to guess, but anyone who gets the link (shared, logged, cached or scraped from your pages) can open it forever, and you can't take that access back without moving or deleting the file. For anything private, use a private bucket and signed URLs.
How long should a signed URL last?
As short as the use allows: often 60 seconds to a few minutes for showing an image or starting a download, longer only for things like emailed links. Supabase can't revoke a single signed URL. To kill one early you delete, move or rename the file, or rotate the project's URL signing key, which revokes all of them.
Can people list every file in my bucket?
They can if a select policy on storage.objects matches the bucket for their role, because listing uses the same SELECT permission as reading. A policy like bucket_id = 'uploads' with no owner check lets anyone covered by it list and read every file. Scope select policies to the owner, and use storage.allow_only_operation() or storage.allow_any_operation() when listing and reading need different rules.
Should I upload files with the service_role key?
Only from server code. The service key bypasses every Storage policy, so it must never reach the browser. If your frontend needs to upload, let the user upload with their own session under a policy that limits them to their folder, or create a signed upload URL on the server.
Does KeySnare check my Supabase Storage buckets?
Not yet. KeySnare doesn't connect to your Supabase project, so it can't see which buckets are public or read your storage.objects policies. The free live-website check does find a Supabase service_role key (which bypasses Storage policies) and other secret keys in your site's JavaScript, and the GitHub repo scan flags tables created without RLS and the service_role key in browser code. Run the tests in this guide for Storage itself.