Home / patterns

SQL built from strings in raw queries (SQL injection)

Quick answer: ORMs protect you until the AI reaches for a raw query. Search, sorting and report features in AI-built apps often glue user input into SQL with $queryRawUnsafe, Prisma.raw, Drizzle's sql.raw or a Postgres function that runs EXECUTE on a concatenated string. Any of those can let a visitor read or change data they shouldn't.

Last updated November 12, 2026

Steps

  1. 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.
  2. 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.
  3. 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.
  4. 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").
  5. 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 ||.
  6. 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

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 StorenTech

Vibe 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.