critical · M4 · MASVS-CODE-4 · PTKD-BACKEND-SUPABASE-DYNAMIC-SQL-RPC

    Supabase function runs any SQL the caller sends

    A function in the public schema takes a text argument (sql, query, statement...) and runs it with EXECUTE. Public functions are callable through supabase.rpc() with the app's anon key unless EXECUTE is revoked, so the caller can run any SQL the function's role may run: read every table, drop tables, or change users.

    How it is exploited

    An assistant adds `create function public.exec_sql(query text) returns json language plpgsql security definer as $$ begin execute query; ... $$;` so the app can run reports. Anyone calls `supabase.rpc('exec_sql', { query: 'select * from auth.users' })` with the anon key from the app bundle.

    Why it matters

    Full database takeover: every row read or changed, tables dropped, users created.

    How to fix it

    1. Drop the function: `drop function public.exec_sql(text);`.
    2. Write one function per operation with typed arguments, or query tables through RLS.
    3. Revoke what stays: `revoke execute on function public.f(text) from anon, authenticated, public;`.

    Does your app have this?

    PTKD checks your build or your source project for PTKD-BACKEND-SUPABASE-DYNAMIC-SQL-RPC and every other check in this knowledge base.

    Scan your app

    References

    Related checks (M4)