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
- Drop the function: `drop function public.exec_sql(text);`.
- Write one function per operation with typed arguments, or query tables through RLS.
- 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 appReferences
Related checks (M4)
- Deep link URL loaded or opened without a host check (heuristic)
- Hybrid WebView may open any website inside the app
- Wildcard associated domain (universal-link hijack surface)
- Process execution via system()/exec/popen
- Unsafe C string function (buffer overflow risk)
- Hijackable custom URL-scheme deep link
- SQL built by joining request data into a string (text search)
- HTML rendered without sanitizing (dangerouslySetInnerHTML / innerHTML)