high · M3 · MASVS-AUTH-2 · PTKD-BACKEND-SUPABASE-STORAGE-OPEN-POLICY

    Supabase Storage policy lets anyone upload, overwrite or delete files

    A policy on storage.objects allows inserts, updates or deletes with a condition that does not tie the file to the caller (true, only the bucket name, or only "signed in"). Anyone with the app's anon key, or any user, can then upload files to your bucket, replace other users' files or delete them.

    How it is exploited

    avatars has `for insert to public with check (bucket_id = 'avatars')`. A script with the anon key uploads gigabytes of files, or malware with a user's file name, to your bucket; with the matching update policy it overwrites every user's avatar.

    Why it matters

    Storage abuse and bills, defacement, malware hosted under your domain, and users' files replaced or deleted.

    How to fix it

    1. Use one folder per user and check it: (storage.foldername(name))[1] = (select auth.uid())::text.
    2. Grant writes to the authenticated role only.
    3. Keep public read only for buckets that are meant to be public.

    migration

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

    Does your app have this?

    PTKD checks your build or your source project for PTKD-BACKEND-SUPABASE-STORAGE-OPEN-POLICY and every other check in this knowledge base.

    Scan your app

    References

    Related checks (M3)