Sub topic 13
SECURITY DEFINER done safely
Assume you know basic PostgreSQL roles, schemas, and privileges. This chapter focuses on functions that run with the privileges of their owner.
What SECURITY DEFINER changes
A normal function uses the privileges of the role that calls it. This behavior is called SECURITY INVOKER, and it is the default.
A function marked SECURITY DEFINER runs with the privileges of its owner instead. This lets you expose a controlled operation without granting callers direct access to the underlying table.
For example, a reporting role might be allowed to read an account balance through a function but not allowed to query the accounts table directly.
CREATE FUNCTION bank.get_balance(p_account_id integer)
RETURNS numeric
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = bank, pg_catalog, pg_temp
AS $$
SELECT a.balance
FROM bank.accounts AS a
WHERE a.id = p_account_id;
$$;
REVOKE EXECUTE ON FUNCTION bank.get_balance(integer) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION bank.get_balance(integer) TO reporting;The REVOKE matters because PostgreSQL grants EXECUTE on newly created functions to PUBLIC by default. Without it, every database role could call the function.
Pin the search path
The search_path controls where PostgreSQL looks for unqualified tables, functions, operators, and types. A caller can often influence the session search path. If a security-definer function relies on an unqualified object, a caller might cause PostgreSQL to resolve that name to an object the caller controls.
Set a safe search path inside the function. Put trusted schemas first, put pg_catalog in the path, and keep pg_temp last. Temporary objects can otherwise take precedence over permanent objects.
Schema-qualifying objects is an additional defense:
SELECT bank.accounts.balance
FROM bank.accounts
WHERE bank.accounts.id = p_account_id;The schema used by the function should be controlled by a trusted owner. Do not include a schema where untrusted roles have CREATE privileges.
Ownership and privileges
The function owner needs the privileges required by the body. Callers do not need those table privileges, but they do need EXECUTE on the function.
A common setup is:
- Create a private schema owned by an administrative role.
- Create the function in that schema.
- Revoke
EXECUTEfromPUBLIC. - Grant
EXECUTEonly to the intended application role. - Prevent untrusted roles from creating objects in the function's schema.
The owner should also be a role that cannot be casually modified by application users. If callers can change the function owner, replace the function, or alter referenced objects, the privilege boundary is lost.
Review checklist
| Risk | Safer practice |
|---|---|
| Everyone can call the function | Revoke `EXECUTE` from `PUBLIC` |
| Name resolution is caller-controlled | Set a trusted `search_path` |
| Temporary objects shadow trusted objects | Put `pg_temp` last |
| Untrusted users can add objects | Restrict `CREATE` on trusted schemas |
| Input reaches dynamic SQL | Use parameters and careful quoting |
Exercise
What two changes prevent every database role from calling bank.get_balance and reduce search-path hijacking?
Show answer
Revoke EXECUTE on the function from PUBLIC, then grant it only to the intended role. Also set a trusted searchpath inside the function, keep pgtemp last, and qualify protected objects where practical.