CCyberwiki

Security definer functions

Security definer functions
ClassAccess control
Applies toPostgres functions created with SECURITY DEFINER, and views without security_invoker
CWECWE-426 (search path), CWE-732 (who may call it)
OWASP Top 10A01:2025 Broken Access Control
Typical severityHigh
AffectsSupabase projects and other Postgres databases that serve functions or views through an API

Every PostgreSQL function runs under one of two identities. By default it runs with the privileges of the role that calls it; a function declared SECURITY DEFINER runs with the privileges of the role that owns it instead.1 The definer form lets a caller do one narrow thing it has no right to do directly, such as read a table it cannot see. It also means the function's own code is the only check on what the caller gets, because the owner is usually a role that row level security does not restrict.

Superusers and roles with the BYPASSRLS attribute skip row security entirely, and table owners normally skip it as well.2 In Supabase, a function created in the dashboard or by a migration is owned by postgres, which bypasses row level security.3

A function that leaks rows

A developer adds a function so the app can load a profile by its ID:

create function public.get_profile(uid uuid)
returns setof public.profiles
language sql
security definer
as $$
  select * from public.profiles where id = uid;
$$;

The profiles table has a policy that shows each user only their own row. The function ignores it: it runs as postgres, and nothing in its body compares uid with the person asking. Postgres grants the right to run a new function to PUBLIC, which means every role.1 Supabase serves functions in its exposed schemas (the named groups of tables and functions its API offers) at POST /rest/v1/rpc/<name>, so anyone holding the public anon key can call this one and read any profile whose ID they know or guess.4 The app calls it with supabase.rpc('get_profile', { uid }); an attacker calls it the same way with someone else's ID. This is Insecure direct object reference moved from application code into the database.

Revoking the right from anon alone does not close the hole. In a default Supabase project the anon and authenticated roles start with the same grants, so the function stays open to every account that can sign up.4 An execute grant decides which roles may call a function; it does not limit which rows a permitted caller receives, so a definer function that reads per-user data has to check ownership in its body.3

Views

A view, a saved query that can be read like a table, has the opposite default. Unless told otherwise, Postgres checks access to the tables behind a view using the permissions of the view's owner, and applies the owner's row security policies, not the querying user's.5 A view over a protected table, created by postgres, hands every row to whoever can query the view.6 The default is kept for backwards compatibility with older Postgres versions.4 A public view over auth.users, the table behind Supabase Auth, exposes every user's record to every API caller.4

Postgres 15 added the security_invoker option. With it set, the view checks the tables against the permissions and policies of the user running the query.54 Without the option, a view still differs from a definer function in two ways: functions called inside it keep their own invoker or definer setting, and CURRENT_USER in a view returns the caller.5

The search path

When a function names a table or function without a schema, Postgres finds it through the search path. If that list includes a schema the caller can write to, the caller can create an object with the same name and have the definer function use it, with the owner's rights. The temporary schema is the usual route: by default it is searched first, and it is normally writable by any role.1

This was a Postgres vulnerability in its own right. CVE-2007-2138 let unprivileged users gain the SQL privileges of the owner of any definer function they could call, and fixing it required both a patched release (8.2.4, 8.1.9, 8.0.13, 7.4.17 or 7.3.19) and a change to each function's definition.7 The same pattern appeared in aiven-extras, a Postgres extension, in an advisory published on 10 May 2023 (CVE-2023-32305): privileged functions called other functions without a schema name, so a low-privileged user could create objects with colliding names and reach superuser. Version 1.1.9 fixed it.8

Writing one safely

The invoker default is the recommended form.3 One accepted use for a definer function is a helper called from a policy, for example a check that reads a roles table the caller cannot query; such helpers belong in a schema the API does not expose, such as private, because a definer function in an exposed schema can be called over the API with its creator's privileges.6 When a definer function is needed:

  1. Set search_path = '' on the function and write every name with its schema, as in public.profiles.3
  2. Compare the rows it returns with the caller, for example with auth.uid() in Supabase.3
  3. Revoke execute from PUBLIC and from each role with a direct grant, then grant it to the roles that call it, following Principle of least privilege. Doing all of this in one transaction (a group of statements applied together) leaves no moment when the function is open to everyone.1
begin;
create function public.get_my_profile()
returns setof public.profiles
language sql
security definer
set search_path = ''
as $$
  select * from public.profiles where id = (select auth.uid());
$$;
revoke execute on function public.get_my_profile() from public;
revoke execute on function public.get_my_profile() from anon;
grant execute on function public.get_my_profile() to authenticated;
commit;

Postgres grants execute on new functions to PUBLIC through a global default, and Supabase adds default grants for anon and authenticated.4 Two statements remove both for every function the same role creates later; the first has no in schema, because a revoke limited to one schema does not change a global default.3

alter default privileges revoke execute on functions from public;
alter default privileges in schema public revoke execute on functions from anon, authenticated;

For views, create them with with (security_invoker = true); on Postgres versions before 15, revoke access from anon and authenticated or keep the view in an unexposed schema.6

Prompt for a coding agent

Search the SQL migrations and schema files in this project for every function declared security definer and every view. For each security definer function: add set search_path = '' if it is missing and qualify every table and function name inside it with its schema; check that the body limits returned or changed rows to the calling user (auth.uid() in Supabase) where the data is per user; revoke execute from public and anon, and grant it only to the roles that call it. Move definer functions that only serve row level security policies into a schema that is not exposed through the API. For each view in an exposed schema, add with (security_invoker = true) unless it is meant to show all rows to everyone. Write the changes as a new migration and list each object you changed and why.

In SecFlash

On the Cloud page, SecFlash reports a view that runs with its creator's rights as SF-CLD-506, a definer function visitors can run as SF-CLD-509, one every signed-in user can run as SF-CLD-510, and a function with no fixed search path as SF-CLD-511; a view over Supabase Auth users is SF-CLD-505. In the project's SQL files, it reports a privileged function without a fixed search path as SF-SEC-750 and one in the public schema that anyone can call as SF-SEC-751, both on the Security page.

See also

References

  1. a b c d "CREATE FUNCTION". PostgreSQL Documentation. PostgreSQL Global Development Group.
  2. back PostgreSQL Global Development Group. "5.10. Row Security Policies". PostgreSQL 19 Documentation.
  3. a b c d e f Supabase (18 November 2026). "Database Functions". Supabase Docs.
  4. a b c d e f Supabase (2 October 2026). "Advisors". Supabase Docs.
  5. a b c PostgreSQL Global Development Group. "CREATE VIEW". PostgreSQL 19 Documentation.
  6. a b c Supabase (19 November 2026). "Row Level Security". Supabase Docs.
  7. back PostgreSQL Global Development Group. "CVE-2007-2138". PostgreSQL security information.
  8. back Aiven (10 May 2023). "PostgreSQL Privilege Escalation Through Missing Schema Qualifiers" (GHSA-7r4w-fw4h-67gp). GitHub Security Advisories.