SQL built from strings in raw queries (SQL injection)
Last updated November 12, 2026
Steps
- Search the code for $queryRawUnsafe, $executeRawUnsafe, Prisma.raw, sql.raw, .query(` with ${ inside, string concatenation around SELECT/UPDATE/DELETE, and in SQL migrations, EXECUTE inside plpgsql functions.
- Where you can, replace the raw query with normal ORM calls (findMany, where, orderBy). Prisma's docs say the ideal way to avoid SQL injection is to use the ORM's model queries.
- If you need raw SQL, pass values as parameters: Prisma's tagged-template $queryRaw`... ${value}` or Drizzle's sql`... ${value}`, which send prepared statements. Never wrap user input in Prisma.raw or sql.raw.
- Column names, sort direction and table names can't be parameters. Map them through an allow-list in code (for example, sort must be one of "created_at" or "price", direction must be "asc" or "desc").
- In Postgres functions you call through Supabase rpc(), use EXECUTE ... USING for values and format() with %I for identifiers, instead of building the string with ||.
- Run the database user with the least privilege the app needs, and test each search, filter and sort box with a quote character to see whether it errors.
Why it matters
Prisma's documentation says the methods with "Unsafe" in the name are at significant risk of SQL injection, and that even $queryRaw becomes vulnerable if you build the query text from strings or misuse Prisma.raw. Drizzle's docs describe sql.raw() as a way to include unescaped raw strings in a query.
OWASP's SQL Injection Prevention Cheat Sheet lists prepared statements with parameterized queries as the first defense, allow-list input validation for things like table names, and least privilege as an additional defense. Injection is A05 in the OWASP Top 10:2025, which says 100% of the applications it looked at were tested for some form of injection.
Sources
Prisma Docs: Raw queries (SQL injection prevention) — https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/raw-queries
Drizzle ORM Docs: Magic sql operator (sql.raw) — https://orm.drizzle.team/docs/sql
PostgreSQL Docs: PL/pgSQL basic statements (Executing Dynamic Commands) — https://www.postgresql.org/docs/current/plpgsql-statements.html
OWASP SQL Injection Prevention Cheat Sheet — https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
OWASP Top 10:2025, A05 Injection — https://owasp.org/Top10/2025/A05_2025-Injection/
Related patterns
- Mass assignment: users can set fields like role or is_admin
- Row Level Security disabled on Supabase tables
- Error messages that leak stack traces, queries or keys
- Running AI or user output with eval, exec or new Function (code injection)
- OS command injection from unsanitized shell calls
- Server-side template injection from unsanitized template strings
- NoSQL operator injection: login and search filters built from raw JSON
- CSV exports that run formulas when opened in a spreadsheet (CSV injection)
Found a problem you'd rather not fix yourself?
Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.
Get it fixed by StorenTechVibe Security Weekly
One email a week with new failure patterns and how to fix them.
By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.