Deutsch
Security wikiDatabase

What is SQL injection?

Also known as: SQLi, SQL injection attack, CWE-89

SQL injection happens when user input becomes part of an SQL command. Where it still shows up in Supabase apps, how to find it and how to fix it, with tested SQL.

By Tim GeithnerPublished 2 min read

Short answer

SQL injection is when an app builds an SQL command by gluing user input into the query text, so the input can change the command itself. supabase-js and query builders send values separately and are not affected. In AI-built apps the risk sits in Postgres functions that use EXECUTE with concatenated strings, especially SECURITY DEFINER functions, and in raw SQL in server code. Use static SQL or bind values with USING.

Ask an AI about this page:ChatGPTClaude

On this page

SQL injection is a weakness where an app builds an SQL command from text that includes user input, so that the input is read as part of the command instead of as plain data. MITRE files it as CWE-89 (opens in a new tab), and it belongs to A05 Injection in the OWASP Top 10 2025.

SQL injection in an AI-built app

When your React app calls supabase.from("orders").select().ilike("item", term), the value travels separately from the query, so it can’t change the SQL. The risk appears when code builds SQL text itself. In Supabase apps that usually means a Postgres function called through supabase.rpc() that uses EXECUTE with a concatenated string:

VulnerableSearch built by gluing text together
create or replace function public.search_my_orders(term text)
returns setof public.orders
language plpgsql
security definer
set search_path = ''
as $$
begin
  return query execute
    'select * from public.orders where user_id = ''' || auth.uid()
    || ''' and item ilike ''%' || term || '%''';
end;
$$;

With a normal search term it returns only the caller’s orders. But the term is pasted into the SQL text, and the function is security definer, so RLS does not apply inside it. In our test, a search term that closes the quote and adds or true returned every user’s orders. Server code that uses raw SQL with string concatenation, such as sql.unsafe(...) or $queryRawUnsafe, has the same problem.

How to check your app

List the functions in your database that build SQL at runtime. Run this in the SQL Editor:

Find functions that build SQL at runtime
select p.proname as function_name
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
  and p.prosrc ilike '%execute%';

Read each result: is any part of the command built with || from a function argument? Then search your server code and migrations for unsafe, raw and string-built SQL.

How to fix or prevent it

The best fix is no dynamic SQL at all. A plain SQL function treats the argument as a value:

SafeFix: no string building
create or replace function public.search_my_orders(term text)
returns setof public.orders
language sql
stable
security definer
set search_path = ''
as $$
  select * from public.orders
  where user_id = (select auth.uid())
    and item ilike '%' || term || '%';
$$;

If you really need EXECUTE, pass values with USING. The PostgreSQL docs call this “much less prone to SQL-injection attacks”:

SafeIf you really need dynamic SQL
create or replace function public.search_my_orders(term text)
returns setof public.orders
language plpgsql
security definer
set search_path = ''
as $$
begin
  return query execute
    'select * from public.orders where user_id = $1 and item ilike $2'
    using (select auth.uid()), '%' || term || '%';
end;
$$;

Table and column names can’t be parameters; use format() with %I or map choices to a fixed list. Also ask whether the function needs security definer at all; see SECURITY DEFINER functions done right.

Frequently asked questions

Can supabase.from().select() be SQL-injected?

Not in the classic way: the query builder sends values as data, not as SQL text. Be careful with filter strings you build yourself from user input, such as the argument of .or(...), and with any raw SQL in your server code or database functions.

Does RLS protect me from SQL injection?

Partly. An injected query that runs as the signed-in user is still limited by RLS. A SECURITY DEFINER function or a server using the secret key skips RLS, so an injection there can reach every row.

Is escaping quotes enough?

OWASP discourages manual escaping. Use parameterized queries; in PL/pgSQL that means static SQL or EXECUTE ... USING. For table or column names, which can’t be parameters, use format() with %I or a fixed allow-list.

Sources

  1. 1.SQL Injection Prevention Cheat Sheet — OWASP Cheat Sheet Series
  2. 2.CWE-89: Improper Neutralization of Special Elements used in an SQL Command — MITRE
  3. 3.PL/pgSQL: Basic Statements (Executing Dynamic Commands) — PostgreSQL Documentation

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt