Skip to content

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

docs: add Wrappers API access security suggestion (#27514)

Bo Lu11315e987a2f
diff --git a/apps/docs/content/guides/database/extensions/wrappers/overview.mdx b/apps/docs/content/guides/database/extensions/wrappers/overview.mdx
index cf78407db38..87e5bc4bd8f 100644
--- a/apps/docs/content/guides/database/extensions/wrappers/overview.mdx
+++ b/apps/docs/content/guides/database/extensions/wrappers/overview.mdx
@@ -35,7 +35,7 @@ Examples:
- A `subscriptions` table which maps to your Stripe subscriptions.
- A `collections` table which maps to a Firebase collection.
-Although a Foreign Table behaves like any other table, the data is not stored inside your database. The data remains inside the Remote Server.
+Although a foreign table behaves like any other table, the data is not stored inside your database. The data remains inside the Remote Server.
### ETL with Wrappers
@@ -111,9 +111,89 @@ This process can be taxing on your database if you are moving large amounts of d
## Security
-Foreign Data Wrappers do not provide Row Level Security. We do not advise exposing them via your API. Wrappers should _always_ be stored in a private schema. For example, if you are connecting to your Stripe account, you should create a `stripe` schema to store all of your Foreign Tables inside. This schema should _not_ be added to the “Additional Schemas” setting in the API section.
-
-If you want to expose any of the columns to your public API, create a [View](https://supabase.com/docs/guides/database/tables#views) in the `public` schema. You can also interact with your Foreign Tables inside a [Database Function](https://supabase.com/docs/guides/database/functions).
+Foreign Data Wrappers do not provide Row Level Security, thus it is not advised to expose them via your API. Wrappers should _always_ be stored in a private schema. For example, if you are connecting to your Stripe account, you should create a `stripe` schema to store all of your foreign tables inside. This schema should _not_ be added to the “Additional Schemas” setting in the API section.
+
+If you want to expose any of the foreign table columns to your public API, you can create a [Database Function with security definer](https://supabase.com/docs/guides/database/functions#security-definer-vs-invoker) in the `public` schema, and then you can interact with your foreign table through API. For better access control, the function should have appropriate filters on the foreign table to apply security rules based on your business needs.
+
+As an example, go to [SQL Editor](https://supabase.com/dashboard/project/_/sql/new) and then follow below steps,
+
+1. Create a Stripe Products foreign table:
+
+ ```sql
+ create foreign table stripe.stripe_products (
+ id text,
+ name text,
+ active bool,
+ default_price text,
+ description text,
+ created timestamp,
+ updated timestamp,
+ attrs jsonb
+ )
+ server stripe_fdw_server
+ options (
+ object 'products',
+ rowid_column 'id'
+ );
+ ```
+
+2. Create a security definer function that queries the foreign table and filters on the name prefix parameter:
+
+ ```sql
+ create function public.get_stripe_products(name_prefix text)
+ returns table (
+ id text,
+ name text,
+ active boolean,
+ default_price text,
+ description text
+ )
+ language plpgsql
+ security definer set search_path = ''
+ as $$
+ begin
+ return query
+ select
+ t.id,
+ t.name,
+ t.active,
+ t.default_price,
+ t.description
+ from
+ stripe.stripe_products t
+ where
+ t.name like name_prefix || '%'
+ ;
+ end;
+ $$;
+ ```
+
+3. Restrict the function execution to a specific role only, for example, the authenticated users:
+
+ <Admonition type="danger">
+ By default, the function created can be executed by any roles like `anon`, that means the
+ foreign table is public accessible. Always limit the function execution permission to
+ appropriate roles.
+ </Admonition>
+
+ ```sql
+ -- revoke public execute permission
+ revoke execute on function public.get_stripe_products from public;
+ revoke execute on function public.get_stripe_products from anon;
+
+ -- grant execute permission to a specific role only
+ grant execute on function public.get_stripe_products to authenticated;
+ ```
+
+Once the preceding steps are finished, the function can be invoked from Supabase client to query the foreign table:
+
+```js
+const { data, error } = await supabase
+ .rpc('get_stripe_products', { name_prefix: 'Test' })
+ .select('*')
+if (error) console.error(error)
+else console.log(data)
+```
## Resources