mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 16:39:05 +00:00
## Problem The Storage access control guide only mentions policies on `storage.objects`. A reader securing a private bucket can't tell whether those policies are enough or whether `storage.buckets` needs its own policies, and the private-bucket read pattern is hard to find among examples that look like public-bucket examples. Related to [FDBKIN-5913](https://linear.app/supabase/issue/FDBKIN-5913/clarify-documentation-for-securing-private-storage-buckets-with-rls) Closes [DOCS-1727](https://linear.app/supabase/issue/DOCS-1727/explain-which-storage-operations-need-policies-on-storagebuckets) ## Solution The "Access policies" section of `/guides/storage/security/access-control` now states that policies on `storage.objects` control every file operation in both private and public buckets, and names the two cases where Storage skips the check: public URLs of public buckets, and signed URLs. A short table lists the bucket operations that need policies on `storage.buckets`, with the permissions each needs on both tables. The section also points to the existing per-user `SELECT` example as the pattern for private-bucket reads. Existing text and the API reference link stay as they were. Checks: Prettier on the changed file, and `pnpm turbo run lint --filter='[origin/master]'` (0 errors). Facts were checked against the per-method RLS notes in `apps/docs/spec/supabase_py_v2.yml` and `supabase_kt_v1.yml`, and against `supabase/storage`, where object routes look up the bucket as superuser (`src/http/routes/object/getObject.ts`, `src/storage/object.ts`, `src/storage/uploader.ts`), and public and signed-URL downloads bypass RLS (`getPublicObject.ts`, `getSignedObject.ts`, `uploadSignedObject.ts`). The docs tests need Docker, so they did not run. How to verify on the preview, at `/docs/guides/storage/security/access-control`: 1. "Access policies" says `storage.objects` policies control every file operation in private and public buckets, and that file operations need no policy on `storage.buckets`. 2. A table lists create, retrieve, list, update, delete, and empty a bucket, with the permissions on `storage.buckets` and `storage.objects` for each. 3. The "Policy examples" link goes to the section with the "Individual user Access" policy, and the "public bucket" link goes to the Access model section of Buckets fundamentals. 4. The admonition link to the API reference still opens the createBucket reference. ## Second reader Verdict: ready, no blockers. - Fixed: the "Empty a bucket" row was incomplete without the sentence after the table, so the table now has a `storage.objects` column. - Fixed: the vague "manage buckets" now reads "call bucket methods". - Fixed: the exceptions are a separate paragraph, reworded so that Storage, not the policies, does the checking. - Fixed: the public-bucket exception now links to Buckets fundamentals. - Left: moving the API reference admonition next to the table. The brief keeps existing text in place. - Left: linking each table row to its reference page. The table stays short, and the existing admonition links to the reference. - Left as follow-ups outside this change: the frontmatter `description` and `sidebar_label` mention only uploads, the "here" link text, stray spaces before periods, "incredibly powerful", "easy", and missing periods in two admonitions. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Co-authored-by: Alexis Aguilar <98043211+alexisintech@users.noreply.github.com>
129 lines
5.8 KiB
Plaintext
129 lines
5.8 KiB
Plaintext
---
|
|
id: 'storage-access-control'
|
|
title: 'Storage Access Control'
|
|
description: 'Learn how to restrict Supabase file uploads.'
|
|
sidebar_label: 'Uploads'
|
|
tocVideo: '4ERX__Y908k'
|
|
---
|
|
|
|
Supabase Storage is designed to work perfectly with Postgres [Row Level Security](/docs/guides/database/postgres/row-level-security) (RLS).
|
|
|
|
You can use RLS to create [Security Access Policies](https://www.postgresql.org/docs/current/sql-createpolicy.html) that are incredibly powerful and flexible, allowing you to restrict access based on your business needs.
|
|
|
|
## Access policies
|
|
|
|
By default Storage does not allow any uploads to buckets without RLS policies. You selectively allow certain operations by creating RLS policies on the `storage.objects` table.
|
|
|
|
Policies on `storage.objects` control every file operation: upload, download, list, move, copy, update, delete, and creating signed URLs. This works the same way in private and public buckets. File operations don't need any policy on `storage.buckets`.
|
|
|
|
Storage skips the policy check in two cases: a download through the public URL of a [public bucket](/docs/guides/storage/buckets/fundamentals#access-model), and a download or upload through a signed URL, because Storage checked the policies when it created the URL.
|
|
|
|
To let users read their own files in a private bucket, add a `SELECT` policy on `storage.objects`, such as the "Individual user Access" policy in [Policy examples](#policy-examples).
|
|
|
|
You need policies on `storage.buckets` only when clients call bucket methods. Each bucket operation needs these permissions:
|
|
|
|
| Bucket operation | Permissions on `storage.buckets` | Permissions on `storage.objects` |
|
|
| ----------------- | -------------------------------- | -------------------------------- |
|
|
| Create a bucket | `INSERT` | None |
|
|
| Retrieve a bucket | `SELECT` | None |
|
|
| List all buckets | `SELECT` | None |
|
|
| Update a bucket | `SELECT` and `UPDATE` | None |
|
|
| Delete a bucket | `SELECT` and `DELETE` | None |
|
|
| Empty a bucket | `SELECT` | `SELECT` and `DELETE` |
|
|
|
|
You can find the documentation for the storage schema [here](/docs/guides/storage/schema/design) , and to simplify the process of crafting your policies, you can use these [helper functions](/docs/guides/storage/schema/helper-functions) .
|
|
|
|
If you need different `SELECT` policies for different Storage actions, such as listing objects versus reading authenticated objects, use the operation-aware helpers `storage.allow_only_operation()` and `storage.allow_any_operation()` documented in [Storage Helper Functions](/docs/guides/storage/schema/helper-functions).
|
|
|
|
<Admonition type="note">
|
|
|
|
The RLS policies required for different operations are documented [here](/docs/reference/javascript/file-buckets-createbucket)
|
|
|
|
</Admonition>
|
|
|
|
For example, the only RLS policy required for [uploading](/docs/reference/javascript/file-buckets-upload) objects is to grant the `INSERT` permission to the `storage.objects` table.
|
|
|
|
To allow overwriting files using the `upsert` functionality you will need to additionally grant `SELECT` and `UPDATE` permissions.
|
|
|
|
## Policy examples
|
|
|
|
An easy way to get started would be to create RLS policies for `SELECT`, `INSERT`, `UPDATE`, `DELETE` operations and restrict the policies to meet your security requirements. For example, one can start with the following `INSERT` policy:
|
|
|
|
```sql
|
|
create policy "policy_name"
|
|
ON storage.objects
|
|
for insert with check (
|
|
true
|
|
);
|
|
```
|
|
|
|
and modify it to only allow authenticated users to upload assets to a specific bucket by changing it to:
|
|
|
|
```sql
|
|
create policy "policy_name"
|
|
on storage.objects for insert to authenticated with check (
|
|
-- restrict bucket
|
|
bucket_id = 'my_bucket_id'
|
|
);
|
|
```
|
|
|
|
This example demonstrates how you would allow authenticated users to upload files to a folder called `private` inside `my_bucket_id`:
|
|
|
|
```sql
|
|
create policy "Allow authenticated uploads"
|
|
on storage.objects
|
|
for insert
|
|
to authenticated
|
|
with check (
|
|
bucket_id = 'my_bucket_id' and
|
|
(storage.foldername(name))[1] = 'private'
|
|
);
|
|
```
|
|
|
|
This example demonstrates how you would allow authenticated users to upload files to a folder called with their `users.id` inside `my_bucket_id`:
|
|
|
|
```sql
|
|
create policy "Allow authenticated uploads"
|
|
on storage.objects
|
|
for insert
|
|
to authenticated
|
|
with check (
|
|
bucket_id = 'my_bucket_id' and
|
|
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
|
|
);
|
|
```
|
|
|
|
Allow a user to access a file that was previously uploaded by the same user:
|
|
|
|
```sql
|
|
create policy "Individual user Access"
|
|
on storage.objects for select
|
|
to authenticated
|
|
using ( (select auth.jwt()->>'sub') = owner_id );
|
|
```
|
|
|
|
Allow anyone to access objects in the `avatars` bucket via publishable key. The `allow_any_operation()` filter is critical here as without it users would be able to list the bucket contents.
|
|
|
|
<Admonition type="note">
|
|
|
|
This is not needed for public buckets, as they are already publicly accessible
|
|
|
|
</Admonition>
|
|
|
|
```sql
|
|
create policy "Avatar images are publicly accessible." on storage.objects
|
|
for select using (bucket_id = 'avatars' and storage.allow_any_operation(array['object.get_authenticated_info', 'object.get_authenticated']));
|
|
```
|
|
|
|
---
|
|
|
|
{/* Finish with a video. This also appears in the Sidebar via the "tocVideo" metadata */}
|
|
|
|
<YouTube id="4ERX__Y908k" title="Storage access control policy examples" />
|
|
|
|
## Bypassing access controls
|
|
|
|
If you exclusively use Storage from trusted clients, such as your own servers, and need to bypass the RLS policies, you can use the `service key` in the `Authorization` header. Service keys entirely bypass RLS policies, granting you unrestricted access to all Storage APIs.
|
|
|
|
Remember you should not share the service key publicly.
|