Skip to content

SQL Injection in Modern ORMs & Raw Queries

Why SQL injection still occurs with Prisma, TypeORM, or SQLAlchemy: unsafe raw query fragments, dynamic ORDER BY clauses, and differential testing.

By BugSnaps Security Research · 2 min read · Updated

Add BugSnaps as a preferred source on Google(opens in a new tab)

Want more of our security guides? Select BugSnaps and confirm your choice on Google.

Many developers assume SQL Injection (SQLi) disappears automatically once a project adopts an Object-Relational Mapper (ORM) such as Prisma, TypeORM, Sequelize, Hibernate, or SQLAlchemy. While ORMs parameterize standard `find` and `create` calls by default, real-world penetration tests regularly uncover SQL injection in modern stacks whenever developers drop down to raw SQL helpers or build dynamic sorting and filtering queries.

Where modern ORM applications introduce SQL injection

An ORM only parameterizes values passed through its bound parameter APIs. When an endpoint needs a complex reporting join, a full-text search expression, or user-controlled sort columns, three coding patterns frequently bypass parameterization:

  • Dynamic identifiers in `ORDER BY` and `GROUP BY`: SQL prepared statements bind data values, not column names or sort directions (`ASC`/`DESC`). Concatenating `?sort=price` directly into an `ORDER BY` clause without a strict column allowlist opens an injection vector.
  • Unsafe raw query helpers: passing template strings with `${userInput}` into methods like Prisma's `$queryRawUnsafe()`, TypeORM's `.where("name = '" + input + "'")`, or SQLAlchemy's `text()` without bound parameters.
  • JSON path and operator injection: interpolating user keys directly into Postgres JSONB path expressions (`data->>'${key}'`) rather than passing the key as a bound parameter.
  • Second-order SQL injection: storing a value safely via an ORM insert, then concatenating that stored field into a raw SQL query inside an administrative export or nightly batch job.

How differential and time-based checks verify SQLi safely

Production APIs rarely print raw database syntax errors to the browser. Instead, security assessments verify suspected SQL injection using differential boolean pairs (comparing an expression that evaluates true against one that evaluates false) or controlled time-delay probes, always comparing against a baseline control request to avoid mistaking ordinary network jitter for a vulnerability.

To eliminate SQL injection across your codebase, forbid string interpolation inside raw SQL helpers via linter rules, map sort and filter column names against a hardcoded enum or allowlist, and use tagged template parameterization (such as Prisma's `$queryRaw` template tag) whenever custom SQL is required.

Never interpolate variables into raw ORM query strings, and always validate dynamic sort or column identifiers against a strict server-side allowlist.

Run a vulnerability scan on your website.

Verify your domain and use MyPentest to check for supported web and API weaknesses. Review findings and coverage; a clean scan does not rule out every CVE.