SKY-D417 · Security in code
PostgreSQL SECURITY DEFINER routine inherits its search path
A function or procedure declared SECURITY DEFINER runs with its owner's privileges. Without a fixed search_path on the routine, unqualified object names can resolve according to the caller's session settings.
- Languages
- PostgreSQL
- Quality gate
- listed, not counted by the gate (full and diff scans). This PostgreSQL check is report-only.
- Finding type
- review
- Catalog name
- PostgreSQL privileged routine inherits search_path
Why it matters
An unqualified name can resolve to the wrong object when an untrusted user can create objects in a schema searched first. The routine then accesses that object with the owner's privileges.
What Skylos flags
CREATE FUNCTION order_count() RETURNS bigint SECURITY DEFINERLANGUAGE sql AS $$ SELECT count(*) FROM orders $$;[tool.skylos.sql]dialect = "postgresql"What Skylos leaves alone
CREATE FUNCTION order_count() RETURNS bigint SECURITY DEFINERSET search_path = pg_catalog, pg_tempLANGUAGE sql AS $$ SELECT count(*) FROM app.orders $$;Why Skylos leaves it alone
The function pins its path and uses the qualified name of its application table.
How Skylos decides
- Install the postgres extra and run
skylos . --danger. Setdialect = "postgresql"under[tool.skylos.sql]in pyproject.toml for the folder containing the SQL file. - Skylos checks CREATE and CREATE OR REPLACE on functions and procedures. On ALTER FUNCTION, PROCEDURE or ROUTINE, it reports only when the same statement selects SECURITY DEFINER and leaves the local path explicitly cleared. A later SET in that statement can fix the path again.
- SET search_path FROM CURRENT captures the current value. The rule checks whether the path is fixed. Review schema grants and the captured value in the database to judge whether the schemas are trusted.
How to fix it
Qualify application objects and pin the path to trusted schemas, with pg_temp last. Confirm that only trusted roles can write to those schemas.
CREATE FUNCTION order_count() RETURNS bigint SECURITY DEFINERRemoved: LANGUAGE sql AS $$ SELECT count(*) FROM orders $$;Added: SET search_path = pg_catalog, pg_tempAdded: LANGUAGE sql AS $$ SELECT count(*) FROM app.orders $$;When it can be wrong
- A later migration can set the routine's path.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Inspect the effective function settings and grants before accepting this finding.