SQL injection: are ORMs 100% safe?

asked 3 months ago358 viewsen

0

We use an ORM (Prisma/Sequelize/Entity Framework) everywhere, so our team assumes SQL injection is impossible. Is that true?

AI diagnosis

AI-generated. Not an answer — the community below confirms or corrects it. Always verify before relying on it.

asked 3 months ago

1 Answer

3
✓
✓ Accepted answer

Mostly safe, not 100%. ORMs parameterize values in their standard query builders, but injection comes back through the escape hatches:

1. Raw queries built with string concatenation:

// ❌ vulnerable
prisma.$queryRawUnsafe(`SELECT * FROM users WHERE name = '${name}'`);
// ✅ parameterized (tagged template)
prisma.$queryRaw`SELECT * FROM users WHERE name = ${name}`;

2. Dynamic identifiers: column names for sorting, table names. Values get parameterized, identifiers can't be. Allow-list them:

const SORTABLE = { newest: 'createdAt', score: 'score' } as const;
const column = SORTABLE[req.query.sort] ?? 'createdAt';

3. Operator injection in document or JSON-style filters: e.g. passing { "$ne": null } from request JSON straight into a MongoDB or Sequelize where. Validate input types with a schema (zod) so strings stay strings. 4. Raw fragments in orderBy, literal() and similar helpers.

Audit with a quick grep for Unsafe, raw(, literal(, FromSqlRaw and string templates near SQL keywords.

Sign in to tell others whether this worked.

answered 3 months ago

Sign in and verify your email to post an answer.