Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: update docs to reflect new permission models for data api (#45288)

Chris Chinchilla0e736457c850
diff --git a/apps/docs/content/_partials/quickstart_db_setup.mdx b/apps/docs/content/_partials/quickstart_db_setup.mdx
index ab4f09b4da3..eedf4167677 100644
--- a/apps/docs/content/_partials/quickstart_db_setup.mdx
+++ b/apps/docs/content/_partials/quickstart_db_setup.mdx
@@ -34,7 +34,9 @@ curl -X POST https://api.supabase.com/v1/projects \
When your project is up and running, go to the [**Table Editor**](/dashboard/project/_/editor) section of the Dashboard, create a new table and insert some data. Then in the [**Integrations > Data API**](/dashboard/project/_/integrations/data_api/settings) section of the Dashboard, expose the specific tables or functions you want to access. To automatically grant access for new tables and functions in `public`, enable **Default privileges for new entities**.
-Alternatively, you can run the following snippet in your project's [SQL Editor](/dashboard/project/_/sql/new). This will create an `instruments` table with some sample data.
+Alternatively, you can run the following snippet in your project's [SQL Editor](/dashboard/project/_/sql/new).
+
+This creates an `instruments` table with some sample data, sets a secure baseline by setting only the privileges each Postgres role needs, and adds [Row Level Security (RLS)](/docs/guides/database/postgres/row-level-security) for enhanced security for database data by default.
</StepHikeCompact.Details>
@@ -46,30 +48,33 @@ Alternatively, you can run the following snippet in your project's [SQL Editor](
id bigint primary key generated always as identity,
name text not null
);
- -- Insert some sample data into the table
+
+ -- Insert sample data into the table
insert into instruments (name)
values
('violin'),
('viola'),
('cello');
- alter table instruments enable row level security;
-
- -- Enable read access for the Data API
+ -- Grant the privileges the role needs, which is read access
grant select on public.instruments to anon;
+
+ -- Enable row level security for the table
+ alter table instruments enable row level security;
```
</StepHikeCompact.Code>
<StepHikeCompact.Details>
-Make the data in your table publicly readable by adding an RLS policy:
+Create an RLS policy to make the data in your table publicly readable:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```sql SQL_EDITOR
+ -- Create a policy to allow the anon role to read from the instruments table
create policy "public can read instruments"
on public.instruments
for select to anon
diff --git a/apps/docs/content/_partials/user_management_quickstart_sql_template.mdx b/apps/docs/content/_partials/user_management_quickstart_sql_template.mdx
index 8006c362da9..28f790a6d37 100644
--- a/apps/docs/content/_partials/user_management_quickstart_sql_template.mdx
+++ b/apps/docs/content/_partials/user_management_quickstart_sql_template.mdx
@@ -10,6 +10,11 @@ create table profiles (
constraint username_length check (char_length(username) >= 3)
);
+
+-- Grant the privileges roles need
+GRANT SELECT ON public.profiles TO anon;
+GRANT SELECT, INSERT, UPDATE ON public.profiles TO authenticated;
+
-- Set up Row Level Security (RLS)
-- See https://supabase.com/docs/guides/database/postgres/row-level-security for more details.
alter table profiles
diff --git a/apps/docs/content/guides/ai/examples/nextjs-vector-search.mdx b/apps/docs/content/guides/ai/examples/nextjs-vector-search.mdx
index c908ab87638..c427bbbc248 100644
--- a/apps/docs/content/guides/ai/examples/nextjs-vector-search.mdx
+++ b/apps/docs/content/guides/ai/examples/nextjs-vector-search.mdx
@@ -121,9 +121,19 @@ Let's prepare the database schema. We can use the "OpenAI Vector Search" quickst
type text,
source text
);
+
+ -- Grant the privileges the roles need
+ GRANT SELECT ON public.nods_page TO anon;
+
alter table "public"."nods_page"
enable row level security;
+ create policy "Allow public read access to nods_page"
+ on public.nods_page
+ for select
+ to anon
+ using (true);
+
-- Stores the actual embeddings with some metadata
create table "public"."nods_page_section" (
id bigserial primary key,
@@ -134,8 +144,18 @@ Let's prepare the database schema. We can use the "OpenAI Vector Search" quickst
slug text,
heading text
);
+
+ -- Grant the privileges the roles need
+ GRANT SELECT ON public.nods_page_section TO anon;
+
alter table "public"."nods_page_section"
enable row level security;
+
+ create policy "Allow public read access to nods_page_section"
+ on public.nods_page_section
+ for select
+ to anon
+ using (true);
```
</StepHikeCompact.Code>
diff --git a/apps/docs/content/guides/ai/rag-with-permissions.mdx b/apps/docs/content/guides/ai/rag-with-permissions.mdx
index 763952e2974..08c8ca7b021 100644
--- a/apps/docs/content/guides/ai/rag-with-permissions.mdx
+++ b/apps/docs/content/guides/ai/rag-with-permissions.mdx
@@ -36,6 +36,9 @@ create table document_sections (
Notice how we record the `owner_id` on each document. Let's create an RLS policy that restricts access to `document_sections` based on whether or not they own the linked document:
```sql
+-- Grant the privileges the roles need
+GRANT SELECT ON public.document_sections TO authenticated;
+
-- enable row level security
alter table document_sections enable row level security;
diff --git a/apps/docs/content/guides/api/securing-your-api.mdx b/apps/docs/content/guides/api/securing-your-api.mdx
index 422ef68cc0a..ba064520884 100644
--- a/apps/docs/content/guides/api/securing-your-api.mdx
+++ b/apps/docs/content/guides/api/securing-your-api.mdx
@@ -50,8 +50,8 @@ Bundle grants with your RLS setup in the same migration. They belong together: `
By default on existing projects, tables and functions you create in `public` are automatically granted `SELECT`, `INSERT`, `UPDATE`, `DELETE` (or `EXECUTE` for functions) to `anon`, `authenticated`, and `service_role`. That means a new table is reachable through the Data API the moment it lands, even if you forgot to enable RLS or did not intend to expose it.
-Supabase is moving the platform default to **revoke** these automatic grants, so that exposure becomes opt-in:
-{/* TODO: link to changelog */}
+Supabase is moving the platform default to **revoke** these automatic grants, so that exposure becomes opt-in, read more about the change in [this changelog entry](https://github.com/orgs/supabase/discussions/45329).
+
To opt an existing project in today, open the [SQL Editor](/dashboard/project/_/sql/new) and run:
```sql
diff --git a/apps/docs/content/guides/auth/managing-user-data.mdx b/apps/docs/content/guides/auth/managing-user-data.mdx
index 625f6434d5d..8e1dffb4ca5 100644
--- a/apps/docs/content/guides/auth/managing-user-data.mdx
+++ b/apps/docs/content/guides/auth/managing-user-data.mdx
@@ -10,7 +10,7 @@ You can view your users on the [Users page](/dashboard/project/_/auth/users) of
For security, the Auth schema is not exposed in the auto-generated API. If you want to access users data via the API, you can create your own user tables in the `public` schema.
-Make sure to protect the table by enabling [Row Level Security](/docs/guides/database/postgres/row-level-security). Reference the `auth.users` table to ensure data integrity. Specify `on delete cascade` in the reference. For example, a `public.profiles` table might look like this:
+Make sure to protect the table by enabling [Row Level Security](/docs/guides/database/postgres/row-level-security) and only granting the necessary privileges for each role. Reference the `auth.users` table to ensure data integrity. Specify `on delete cascade` in the reference. For example, a `public.profiles` table might look like this:
```sql
create table public.profiles (
@@ -21,6 +21,12 @@ create table public.profiles (
primary key (id)
);
+-- Grant the privileges the roles need
+GRANT SELECT ON public.profiles TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.profiles TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.profiles TO service_role;
+
+-- Enable row level security for the table
alter table public.profiles enable row level security;
```
diff --git a/apps/docs/content/guides/database/postgres/row-level-security.mdx b/apps/docs/content/guides/database/postgres/row-level-security.mdx
index 3c48dd8ee4b..40fed201f49 100644
--- a/apps/docs/content/guides/database/postgres/row-level-security.mdx
+++ b/apps/docs/content/guides/database/postgres/row-level-security.mdx
@@ -15,9 +15,13 @@ Supabase allows convenient and secure data access from the browser, as long as y
RLS _must_ always be enabled on any tables stored in an exposed schema. By default, this is the `public` schema.
-RLS is enabled by default on tables created with the Table Editor in the dashboard. If you create one in raw SQL or with the SQL editor, remember to enable RLS yourself:
+RLS is enabled by default on tables created with the Table Editor in the dashboard. If you create one in raw SQL or with the SQL editor, remember to enable RLS yourself and grant only the permissions each Postgres role needs.
```sql
+GRANT SELECT ON <schema_name>.<table_name> TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON <schema_name>.<table_name> TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON <schema_name>.<table_name> TO service_role;
+
alter table <schema_name>.<table_name>
enable row level security;
```
diff --git a/apps/docs/content/guides/integrations/supabase-for-platforms.mdx b/apps/docs/content/guides/integrations/supabase-for-platforms.mdx
index fdf84ffcd06..f2950894cf0 100644
--- a/apps/docs/content/guides/integrations/supabase-for-platforms.mdx
+++ b/apps/docs/content/guides/integrations/supabase-for-platforms.mdx
@@ -204,7 +204,7 @@ curl https://api.supabase.com/v1/projects/{ref}/database/migrations \
--header 'Authorization: Bearer YOUR_SECRET_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
- "query": "create table public.todos (id serial primary key, task text not null); alter table public.todos enable row level security;",
+ "query": "create table public.todos (id serial primary key, task text not null); grant select on public.todos to anon; grant select, insert, update, delete on public.todos to authenticated; grant select, insert, update, delete on public.todos to service_role; alter table public.todos enable row level security;",
"name": "Create a todos table"
}'
```
diff --git a/apps/docs/content/guides/local-development/testing/pgtap-extended.mdx b/apps/docs/content/guides/local-development/testing/pgtap-extended.mdx
index ac6e42168de..b7bbefecbea 100644
--- a/apps/docs/content/guides/local-development/testing/pgtap-extended.mdx
+++ b/apps/docs/content/guides/local-development/testing/pgtap-extended.mdx
@@ -336,7 +336,34 @@ create table public.comments (
);
```
-#### 2. RLS policies declaration
+#### 2. Grant role privileges
+
+Grant the necessary privileges for each role, ensuring the principle of least privilege is followed:
+
+```sql
+-- Grant the privileges that roles need
+GRANT SELECT ON public.profiles TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.profiles TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.profiles TO service_role;
+
+GRANT SELECT ON public.organizations TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.organizations TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.organizations TO service_role;
+
+GRANT SELECT ON public.org_members TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.org_members TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.org_members TO service_role;
+
+GRANT SELECT ON public.posts TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.posts TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.posts TO service_role;
+
+GRANT SELECT ON public.comments TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.comments TO authenticated;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.comments TO service_role;
+```
+
+#### 3. RLS policies declaration
Now to setup the RLS policies for each tables:
@@ -465,7 +492,7 @@ create policy "Users can update their own comments"
on public.comments for update using (author_id = (select auth.uid()));
```
-#### 3. Test cases:
+#### 4. Test cases:
Now everything is setup, let's write RLS test cases, note that each section could be in its own test:
diff --git a/apps/docs/content/guides/realtime/authorization.mdx b/apps/docs/content/guides/realtime/authorization.mdx
index 62065ac1ceb..5a4a68ef205 100644
--- a/apps/docs/content/guides/realtime/authorization.mdx
+++ b/apps/docs/content/guides/realtime/authorization.mdx
@@ -84,6 +84,8 @@ create table public.rooms (
topic text not null unique
);
+GRANT SELECT ON public.rooms TO anon;
+
alter table public.rooms enable row level security;
create table public.profiles (
@@ -93,6 +95,9 @@ create table public.profiles (
primary key (id)
);
+GRANT SELECT ON public.profiles TO anon;
+GRANT SELECT, INSERT, UPDATE, DELETE ON public.profiles TO authenticated;
+
alter table public.profiles enable row level security;
create table public.rooms_users (
@@ -101,7 +106,15 @@ create table public.rooms_users (
created_at timestamptz default current_timestamp
);
+GRANT SELECT ON public.rooms_users TO authenticated;
+
alter table public.rooms_users enable row level security;
+
+create policy "authenticated can read own room memberships"
+on public.rooms_users
+for select
+to authenticated
+using ((select auth.uid()) = user_id);
```
### Broadcast
diff --git a/apps/docs/content/guides/realtime/postgres-changes.mdx b/apps/docs/content/guides/realtime/postgres-changes.mdx
index 0f36c5112b9..9c5ac6caaeb 100644
--- a/apps/docs/content/guides/realtime/postgres-changes.mdx
+++ b/apps/docs/content/guides/realtime/postgres-changes.mdx
@@ -69,6 +69,9 @@ In this example we'll set up a database table, secure it with Row Level Security
<StepHikeCompact.Code>
```sql
+ -- Grant the privileges roles need
+ GRANT SELECT ON public.todos TO anon;
+
-- Turn on security
alter table "todos"
enable row level security;