question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Revisit prisma.$queryRaw("...") vs. prisma.$queryRaw template literal

See original GitHub issue

Problem

Right now it’s really easy to write a raw query that’s susceptible to SQL injections

// Raw query. Susceptible to SQL injections! 
prisma.$queryRaw(`SELECT \* FROM User WHERE email = ${email}`);

// Prepared query. Not susceptible to SQL injections.
prisma.$queryRaw`SELECT \* FROM User WHERE email = ${email}`;

The difference is in the ( and ). This is far too subtle and there’s no warning about which is which.

Suggested solution

  • Investigate: Can we differentiate between prisma.$queryRaw("...") and the prisma.$queryRaw template literal?

If so,

  • Rename prisma.$queryRaw("...") to something else. Perhaps $queryDangerouslyRaw.

    I think there are since there are places where you can’t use a prepared statement (e.g. CREATE TABLE “${table}”). If so, I don’t think we want to disallow the capability entirely.

  • Consider writing a codemod for npx @prisma/codemods to rename function calls (not template literals) to the new method

If not,

  • Consider other alternatives for clearly separating these two behaviors

Other Ideas

  • Maybe it’s possible for our VS Code extension to detect it and return a warning.
  • Or maybe it’s possible at runtime to detect that it’s used the wrong way and throw an error?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
matthewmuellercommented, Aug 16, 2021

The design of this change is the following:

Narrowed $queryRaw (breaking change)

  • $queryRaw now only supports a template literal.
  • This is always a parameterized query and therefore not susceptible to SQL injections.
  • The signature is prisma.$queryRaw`query`.
  • For example, prisma.$queryRaw`select * from users where email = ${email}`

Added $queryRawUnsafe

  • $queryRawUnsafe is a new function that can be a string or template string.
  • The signature is prisma.$queryRawUnsafe(query, args...).
  • This allows you to pass a raw template string in. For example, prisma.$queryRawUnsafe("create table ${table}").
  • You could still have a parameterized query. For example, prisma.$queryRawUnsafe("select * from users where email = $1", "alice@prisma.io"). This is mostly to make the upgrade path easier.

$queryRaw to $queryRawUnsafe codemod

We plan to add a codemod to https://github.com/prisma/codemods that will turn all $queryRaw functions into $queryRawUnsafe functions. It will ignore $queryRaw tagged templates, since those work as is.

2reactions
millspcommented, Aug 16, 2021

Yes, the last call will work while the first one will fail.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Raw database access (Reference) - Prisma
For relational databases, Prisma Client exposes four methods that allow you to send raw queries. You can use: $queryRaw to return actual records...
Read more >
Building a SQL Query from Variables with prisma.$queryRaw
Using prisma.$queryRaw, I was able to write a basic query that returned the data I needed. However, I also needed the query to...
Read more >
Prisma queryRaw throws error when using template string ...
The $queryRawUnsafe method allows you to pass a raw string (or template string) to the database. But! By using this method with user...
Read more >
Prisma 2.30.0 Release - GitClear
Today, we are excited to share the 2.30.0 stable release. Help us spread the word about Prisma by starring the repo or tweeting...
Read more >
Prisma $queryRaw with variable length parameter list
The sql-template-tag library which I think is used by prisma, has a way of supporting this so after installing and importing it, I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found