Full answer
Setup for the example:
CREATE SCHEMA vault;
CREATE TABLE vault.secrets (id int, value text);
CREATE TABLE secrets (id int, value text);
CREATE FUNCTION count_secrets_unsafe() RETURNS bigint LANGUAGE sql SECURITY DEFINER AS $$ SELECT count(*) FROM secrets $$;
CREATE FUNCTION count_secrets_safe() RETURNS bigint LANGUAGE sql SECURITY DEFINER
SET search_path = pg_catalog, pg_temp AS $$ SELECT count(*) FROM vault.secrets $$;
REVOKE EXECUTE ON FUNCTION count_secrets_safe() FROM PUBLIC;SELECT proname, prosecdef, coalesce(proconfig::text, 'no search_path set') AS config FROM pg_proc WHERE proname LIKE 'count_secrets%' ORDER BY proname;PostgreSQL returns:
proname | prosecdef | config
----------------------+-----------+-------------------------------------
count_secrets_safe | t | {"search_path=pg_catalog, pg_temp"}
count_secrets_unsafe | t | no search_path set
(2 rows)SELECT p.proname FROM pg_proc p WHERE p.prosecdef AND p.pronamespace = current_schema()::regnamespace AND (p.proconfig IS NULL OR NOT EXISTS (SELECT 1 FROM unnest(p.proconfig) c WHERE c LIKE 'search_path=%'));Result on PostgreSQL 17:
proname
----------------------
count_secrets_unsafe
(1 row)DROP SCHEMA vault CASCADE;Result:
NOTICE: drop cascades to table vault.secrets
DROP SCHEMAThe catalog shows that both functions are security definers, but only the safe one carries its own search_path setting, with pg_temp listed last so temporary objects cannot shadow anything. The second query is an audit that lists security-definer functions without a fixed search_path, here the unsafe one. The actual attack needs a second, less privileged role and was not run here: that role creates a table or function named like the referenced object in a schema it controls and calls the function with that schema first on its path. Besides the fixed path, schema-qualify names in the body and grant EXECUTE only to the roles that need it. The last run removes the demo's vault schema.
CREATE SCHEMA vault;
CREATE TABLE vault.secrets (id int, value text);
CREATE TABLE secrets (id int, value text);
CREATE FUNCTION count_secrets_unsafe() RETURNS bigint LANGUAGE sql SECURITY DEFINER AS $$ SELECT count(*) FROM secrets $$;
CREATE FUNCTION count_secrets_safe() RETURNS bigint LANGUAGE sql SECURITY DEFINER
SET search_path = pg_catalog, pg_temp AS $$ SELECT count(*) FROM vault.secrets $$;
REVOKE EXECUTE ON FUNCTION count_secrets_safe() FROM PUBLIC;
SELECT proname, prosecdef, coalesce(proconfig::text, 'no search_path set') AS config FROM pg_proc WHERE proname LIKE 'count_secrets%' ORDER BY proname;
SELECT p.proname FROM pg_proc p WHERE p.prosecdef AND p.pronamespace = current_schema()::regnamespace AND (p.proconfig IS NULL OR NOT EXISTS (SELECT 1 FROM unnest(p.proconfig) c WHERE c LIKE 'search_path=%'));
DROP SCHEMA vault CASCADE;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- SECURITY DEFINER runs with owner rights
- Unqualified names follow the caller's search_path
- Set search_path on the function
- Put pg_temp last
- Revoke EXECUTE from PUBLIC
Common mistakes
- Creating security definer functions without SET search_path
- Leaving EXECUTE granted to PUBLIC
Follow-ups
- How did PostgreSQL 15 change the public schema to reduce this risk?
- When is SECURITY DEFINER the right tool?
Tips
- Audit with the catalog