<!-- Source: @site/projects/sqlens-for-laravel/docs/rules/sec-priv-routine-definer-mutable-path.md -->
<!-- Canonical: https://docs.pushery.com/sqlens-for-laravel/rules/sec-priv-routine-definer-mutable-path/ -->

# SEC.PRIV.ROUTINE_DEFINER_MUTABLE_PATH — EXECUTE means "run code as the owner"

> A SECURITY DEFINER routine that does not pin its search_path lets its caller substitute the functions it calls.

- **Category:** security
- **Severity:** critical
- **Level:** 0
- **Confidence:** deterministic
- **Downtime class:** none — the finding is about a routine's privileges, not about a statement
- **Stability:** stable
- **Suites:** audit
- **Applies to:** PostgreSQL 18

## The routine calls somebody else's function, as you

A `SECURITY DEFINER` routine runs with the privileges of its **owner** rather than its caller. That is
deliberate and useful — it is how a low-privilege application gets one narrow, audited path into
something it otherwise could not touch.

Inside the routine, an unqualified name — `now()`, `users`, `crypt()` — is resolved through whichever
`search_path` is in effect. Unless the routine pins its own, that is the **caller's**.

So a caller who can create a schema does this:

```sql
CREATE SCHEMA evil;
CREATE FUNCTION evil.now() RETURNS timestamptz AS $$
    -- runs as the routine's owner
    GRANT ALL ON ALL TABLES IN SCHEMA public TO attacker;
    SELECT clock_timestamp();
$$ LANGUAGE sql;

SET search_path = evil, public;
SELECT app.grant_access();     -- calls evil.now(), as app_owner
```

`EXECUTE` on such a routine is therefore not "may run this function". It is **"may run arbitrary code
as the owner"** — and the owner is usually the role that owns the schema, which on an ordinary Laravel
deployment owns everything.

## Bad

```sql
CREATE FUNCTION app.grant_access(uid bigint) RETURNS void
    LANGUAGE sql
    SECURITY DEFINER
AS $$
    INSERT INTO memberships (user_id, granted_at) VALUES (uid, now());
$$;
```

## Good

```sql
CREATE FUNCTION app.grant_access(uid bigint) RETURNS void
    LANGUAGE sql
    SECURITY DEFINER
    -- one clause, and the whole path is closed
    SET search_path = pg_catalog, pg_temp
AS $$
    INSERT INTO public.memberships (user_id, granted_at) VALUES (uid, pg_catalog.now());
$$;
```

Any schema list closes what this rule reports, and it has to end with `pg_temp`. A path that leaves
`pg_temp` out, the empty string included, still searches the caller's temporary schema first for
tables, which [`SEC.PRIV.ROUTINE_DEFINER_UNSAFE_PATH`](https://docs.pushery.com/sqlens-for-laravel/rules/sec-priv-routine-definer-unsafe-path.md)
reports.

```sql
ALTER FUNCTION app.grant_access(bigint) SET search_path = pg_catalog, pg_temp;
```

Check what your database currently has:

```sql
SELECT n.nspname, p.proname, p.proconfig
FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE p.prosecdef AND p.proconfig IS NULL
  AND n.nspname NOT IN ('pg_catalog', 'information_schema');
```

## Related

- [`SEC.PRIV.ROUTINE_DEFINER`](https://docs.pushery.com/sqlens-for-laravel/rules/sec-priv-routine-definer.md) — the same routine with its path pinned. It
  is still reported, at `low`, because `EXECUTE` on it means more than `EXECUTE` usually does.
