Supabase Vault: how to store secrets, and the function that leaks them all
#supabase#vault#secrets#postgres#security-definer
Supabase Vault stores secrets encrypted in your database and decrypts them on the fly through the vault.decrypted_secrets view. Out of the box only postgres and service_role can read that view, so your publishable key can't reach it. The leaks happen when someone builds a way in: a SECURITY DEFINER function like get_secret(name) in public, which any visitor can call through the API, a grant on the vault schema to authenticated, or a secret written straight into a migration file. Use secrets inside the database function that needs them, and keep anything that reads Vault off the API.
What is Supabase Vault?
A Postgres extension for secrets you need in SQL: an API key a trigger sends to a webhook, a token a pg_cron job uses, a password for an outside service called with pg_net. Per Supabase's Vault guide, secrets are stored with authenticated encryption in vault.secrets, and the vault.decrypted_secrets view decrypts them when you query it. Backups and replicas keep the encrypted form.
The encryption key isn't stored in your database. Supabase keeps a root key per project in its own systems, so a dump of your database contains only ciphertext. The guide's one warning is the one that matters here: "anyone that has access to the view has access to decrypted secrets."
How do I create, read, update and delete a Vault secret?
-- Create (the name and description are optional; the name must be unique)
select vault.create_secret('the-secret-value', 'stripe_secret_key', 'Stripe live key');
-- Read
select decrypted_secret
from vault.decrypted_secrets
where name = 'stripe_secret_key';
-- Update (by id: the new value, and optionally a new name and description)
select vault.update_secret('<secret-id>', 'the-new-value');
-- Delete
delete from vault.secrets where id = '<secret-id>' returning id, name;
Run create_secret in the SQL Editor or from a script that reads the value from somewhere safe, not in a migration file. More on that below.
Who can read Vault secrets by default?
Only postgres and service_role. In Supabase's own setup migration, those two get access to the vault schema, the vault.secrets table, the decrypted_secrets view and the create and update functions. anon and authenticated get nothing, and the Vault extension revokes its functions from PUBLIC. The vault schema also isn't exposed through the Data API unless you add it.
So a fresh project is closed: nobody with your publishable key can read a secret. Every leak below is something a project adds.
1. A get_secret() function anyone can call
The most common way to "read a Vault secret from the app" is a helper like this:
-- Read a Vault secret from the app by name.
create or replace function public.get_secret(secret_name text)
returns text
language sql
security definer
set search_path = ''
as $$
select decrypted_secret from vault.decrypted_secrets where name = secret_name;
$$;
- Criticalsupabase/migrations/20261130120000_get_secret.sql:2
SECURITY DEFINER function anyone can call, with no check on the caller
If only your server or a cron job calls it: `REVOKE EXECUTE ON FUNCTION <fn>(<args>) FROM PUBLIC, anon, authenticated; GRANT EXECUTE ON FUNCTION <fn>(<args>) TO service_role;` (REVOKE … FROM PUBLIC alone is not enough on Supabase: anon and authenticated have their own grant). If signed-in users call it, check the caller inside: use auth.uid() instead of a user-id parameter, or `IF p_user_id IS DISTINCT FROM auth.uid() THEN RAISE EXCEPTION 'forbidden'; END IF;`. To stop new functions repeating this, run both: `ALTER DEFAULT PRIVILEGES FOR ROLE postgres REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;` (global, no IN SCHEMA: Postgres's PUBLIC grant is a global default) and `ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE EXECUTE ON FUNCTIONS FROM anon, authenticated;`
SECURITY DEFINER makes it run as its owner, postgres, which can read the view. It sits in public, so the Data API exposes it at /rest/v1/rpc/get_secret. And Postgres grants EXECUTE on every new function to PUBLIC, which includes anon. One request with your publishable key returns any secret by name:
const { data } = await supabase.rpc("get_secret", { secret_name: "stripe_secret_key" });
Granting it only to authenticated doesn't help much. With open sign-ups, that's anyone with an email address, and the function still never checks who's asking or which secret.
The fix depends on why the function exists:
- The secret is used inside the database (a trigger calling a webhook, a cron job): read it inside that function, and never return it. The secret stays in Postgres.
- Your server needs it: don't keep it in Vault. Put it in your host's environment variables or in Edge Function secrets (next section).
- You really need an RPC that returns it: make it server-only, and remember that revoking from
anonalone isn't enough:
revoke execute on function public.get_secret(text) from public, anon, authenticated;
grant execute on function public.get_secret(text) to service_role;
Better still, move it to a schema the Data API doesn't expose, so it isn't reachable at /rest/v1/rpc/ at all.
2. Granting the vault schema to signed-in users
The other shortcut, usually after a permission denied for schema vault error:
grant usage on schema vault to authenticated;
grant select on vault.decrypted_secrets to authenticated;
Now every signed-in user can read every secret, through any function that runs as the caller, or directly if vault is ever added to the exposed schemas. The error was Postgres doing its job. Revoke both grants and use one of the patterns above.
3. A secret written into a migration
-- supabase/migrations/20261130_secrets.sql
select vault.create_secret('sk_live_...', 'stripe_secret_key');
The value is now in your git history, in every clone and every CI log that prints the migration, and encrypting it in the database afterwards doesn't take it back out of git. Create secrets in the SQL Editor or in the dashboard's Vault page instead. If one has already been committed, rotate it at the provider first, because removing it from git history afterwards doesn't undo the copies already made.
Vault or Edge Function secrets?
They solve different problems:
- Edge Function secrets are environment variables for your Edge Functions. Set them in the dashboard or with
supabase secrets set STRIPE_SECRET_KEY=…, and read them withDeno.env.get("STRIPE_SECRET_KEY"). Names can't start withSUPABASE_, which Supabase reserves. - Vault is for secrets your SQL needs: triggers,
pg_cronjobs,pg_netcalls.
If code outside the database needs a secret, Edge Function secrets or your host's environment variables are the simpler and safer place. A Vault secret your app reads back through an RPC is the pattern that leaks. Database webhooks are a good example of the right use: the trigger function reads the token from Vault and sends it, and it never leaves Postgres.
Moving to a new project
Each project has its own root key. Pausing, restoring, branching and "restore to a new project" keep or copy it, so your secrets stay readable. A manual pg_dump into a new project doesn't: the new project has a fresh key and can't decrypt what you copied. Supabase's guide shows how to copy the old project's root key with the Management API's pgsodium endpoint before you rely on the migrated data.
A quick self-check
-- Functions outside vault that read decrypted secrets, and whether the API roles can call them
select p.oid::regprocedure as function,
has_function_privilege('anon', p.oid, 'execute') as anon_can_run,
has_function_privilege('authenticated', p.oid, 'execute') as authenticated_can_run
from pg_proc p
where p.prosrc ilike '%decrypted_secrets%'
and p.pronamespace <> 'vault'::regnamespace;
-- Grants on the vault schema's tables to roles that shouldn't have them
select grantee, table_name, privilege_type
from information_schema.role_table_grants
where table_schema = 'vault'
and grantee in ('anon', 'authenticated', 'PUBLIC');
Every true in the first query is a function anyone with your publishable key, or any signed-in user, can use to read secrets. The second query should return nothing. Then search your migrations for vault.create_secret( with a value in quotes.
Does GuardLayer catch this?
Yes, all three. The code scan flags a SECURITY DEFINER function in public that the API roles can still execute and that reads vault.decrypted_secrets without checking the caller (supabase/security-definer-no-caller-check, critical); a grant on the vault schema, its secrets view or its decrypt functions to anon, authenticated or PUBLIC (supabase/vault-granted-to-api-role, critical); and a literal value passed to vault.create_secret() or vault.update_secret() in a SQL file (supabase/vault-secret-in-migration). A value in a known key format, such as a Stripe live key, is also flagged as a hardcoded secret. Placeholders like '<your-api-key>' and values read from a setting aren't flagged.
FAQ
How do I use a Vault secret in an Edge Function?
Usually you don't. Store the value as an Edge Function secret and read it with Deno.env.get(). Vault is for secrets that SQL code uses. Reading Vault from an Edge Function means exposing a function that returns secrets, which is the leak this post is about.
Can I read Vault secrets from the browser?
Not by default, and you shouldn't make it possible. anon and authenticated have no access to the vault schema. Any way around that, a public function or a grant, gives the secret to everyone who can call it.
Where is the Vault encryption key stored? Outside your database, in Supabase's systems, with one root key per project. The Management API can return it, so you can decrypt your data elsewhere or copy it to a new project.
Is Supabase Vault a secrets manager? For secrets your database uses, yes. For your app's environment variables, use Edge Function secrets or your host's settings instead.
See which of your Supabase tables anyone can read, free
Connect your Supabase project read-only. GuardLayer checks RLS, policies, grants and functions as they are right now, and gives you the SQL to fix each problem. It never reads your rows and can't change anything.
Check my Supabase projectPrefer to check code instead? Scan your repo, no signup.