You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Revisit prisma.$queryRaw("...") vs. prisma.$queryRaw template literal #7142
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?
Reacted by Dick Fickling, Sachin Raja, pierre and Émile Fugulin
changed the title [-]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw`...`[/-][+]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template[/+]on May 18, 2021
changed the title [-]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template[/-][+]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template literal[/+]on May 18, 2021
changed the title [-]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template literal[/-][+]Revisit prisma.$queryRaw(\`...\`) vs. prisma.$queryRaw template literal[/+]on May 18, 2021
changed the title [-]Revisit prisma.$queryRaw(\`...\`) vs. prisma.$queryRaw template literal[/-][+]Revisit prisma.$queryRaw(``...``) vs. prisma.$queryRaw template literal[/+]on May 18, 2021
changed the title [-]Revisit prisma.$queryRaw(``...``) vs. prisma.$queryRaw template literal[/-][+]Revisit prisma.$queryRaw("...") vs. prisma.$queryRaw template literal[/+]on May 18, 2021
topic: raw$queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
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", "[email protected]"). This is mostly to make the upgrade path easier.
Discussed it with @millsp on slack, but basically I got into the code of a pretty large customer that uses prisma and I found a lot of sql injections due to improper use of prisma raw queries. So I support that 100%.
Basically I want the following to fail:
letquery=`SELECT * FROM table`if(id){query+=`WHERE id = ${id}`}prisma.$queryRaw(query)
But this should still work:
letquery=sql`SELECT * FROM table`if(id){query=sql`${query} WHERE id = ${id}`}prisma.$queryRaw(query)
Activity
[-]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw`...`[/-][+]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template[/+][-]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template[/-][+]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template literal[/+][-]Revisit prisma.$queryRaw(`...`) vs. prisma.$queryRaw template literal[/-][+]Revisit prisma.$queryRaw(\`...\`) vs. prisma.$queryRaw template literal[/+][-]Revisit prisma.$queryRaw(\`...\`) vs. prisma.$queryRaw template literal[/-][+]Revisit prisma.$queryRaw(``...``) vs. prisma.$queryRaw template literal[/+][-]Revisit prisma.$queryRaw(``...``) vs. prisma.$queryRaw template literal[/-][+]Revisit prisma.$queryRaw("...") vs. prisma.$queryRaw template literal[/+]pantharshit00 commentedon May 18, 2021
Related: #7095
matthewmueller commentedon Jul 6, 2021
Internal Note: we consider this breaking change before the next major release.
matthewmueller commentedon Aug 14, 2021
The design of this change is the following:
Narrowed
$queryRaw(breaking change)$queryRawnow only supports a template literal.Added
$queryRawUnsafe$queryRawUnsafeis a new function that can be a string or template string.prisma.$queryRawUnsafe(query, args...).prisma.$queryRawUnsafe("create table ${table}").prisma.$queryRawUnsafe("select * from users where email = $1", "[email protected]"). This is mostly to make the upgrade path easier.$queryRawto$queryRawUnsafecodemodWe plan to add a codemod to https://github.com/prisma/codemods that will turn all
$queryRawfunctions into$queryRawUnsafefunctions. It will ignore $queryRaw tagged templates, since those work as is.Sytten commentedon Aug 16, 2021
Discussed it with @millsp on slack, but basically I got into the code of a pretty large customer that uses prisma and I found a lot of sql injections due to improper use of prisma raw queries. So I support that 100%.
Basically I want the following to fail:
But this should still work:
millsp commentedon Aug 16, 2021
Yes, the last call will work while the first one will fail.
matthewmueller commentedon Aug 18, 2021
Closing since we've learned this will be possible.
Remaining tasks are tracked in: #8628