SQL injection: are ORMs 100% safe?

poze 3 months ago355 gadeen

0

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

Dyagnostik IA

Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.

poze 3 months ago

1 Repons

3
✓
✓ Repons yo aksepte

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.

Konekte pou di lòt moun si li te mache.

answered 3 months ago

Sign in and verify your email to post an answer.