跳到正文
DEV Community· LeakPatrol·· 5 小時前AI 評分64

Supabase 公開儲存桶會暴露甚麼,以及如何修正

Is your Supabase storage bucket public? What anyone can see, and the fix

AI 導讀

LeakPatrol 説明 Supabase 公開儲存桶的讀取範圍及修正方法:任何人只要取得檔案 URL,便可無須登入下載檔案;若 storage.objects 的 select policy 只檢查 bucket_id,持有網站 anon key 的人亦可列出並下載整個儲存桶的檔案。

正文

LeakPatrol

AI builders love public storage buckets. They make image uploads "just work": no policies, no signed URLs, no errors. That's fine for logos and avatars. It's not fine when the same bucket holds ID photos, invoices, contracts or anything a user uploaded expecting it to stay private.

What "public" actually means

  • Anyone with the file URL can download it. Public files are served from /storage/v1/object/public/<bucket>/<path> with no login and no policy check.
  • Uploads, updates and deletes still need policies. "Public" only covers reading.
  • Listing is the bigger risk. A broad select policy on storage.objects that only checks bucket_id = 'uploads' lets anyone holding your publishable (anon) key, which is in your site's JavaScript, list every file in the bucket and download them all.

The 2-minute check

  1. In Supabase, open Storage. Buckets marked Public are readable by anyone with a link.
  2. For each public bucket ask: would a user be upset if a stranger saw these files? Avatars and product photos: fine. Passports, payslips, medical files, private chat attachments: not fine.
  3. Look at the select policies on storage.objects. A policy for anon/public that only checks the bucket name lets anyone list the bucket.
  4. Run the Security Advisor in the dashboard too.

The fix

Keep public buckets for public things only. Put private uploads in a bucket with Public off, stored under the user's id (<user-id>/invoice.pdf), with owner-only policies:

create policy "Users read own files"
on storage.objects for select to authenticated
using (
  bucket_id = 'private-uploads'
  and (storage.foldername(name))[1] = (select auth.uid())::text
);

create policy "Users upload to own folder"
on storage.objects for insert to authenticated
with check (
  bucket_id = 'private-uploads'
  and (storage.foldername(name))[1] = (select auth.uid())::text
);

Show files with short-lived signed URLs instead of permanent public links:

const { data } = await supabase.storage
  .from('private-uploads')
  .createSignedUrl(`${user.id}/invoice.pdf`, 60); // valid for 60 seconds

Then remove broad select policies from public buckets (public files load without one; the policy mostly adds listing), and move existing sensitive files: copy to the private bucket, update paths in your database, delete the public copies.

A prompt for Lovable, Bolt or Cursor

Audit my Supabase Storage setup:
1. List every bucket, whether it is public, and what the app stores in it.
2. Anything user-uploaded and private (documents, IDs, invoices,
   attachments) must go in a private bucket, under the user's id folder.
3. Add select and insert policies on storage.objects so users can only
   access their own folder. No select policy for anon on any bucket.
4. Replace getPublicUrl for private files with createSignedUrl.
5. Give me a migration plan for files already in public buckets.

The full guide, with FAQs, is on LeakPatrol. If you want an outside check of your live app (leaked keys, public files, tables that answer without a login), there's a free read-only scan there too.

來源:DEV Community · dev.to