prisma: `where: { shop: undefined }`: The entire table data was accidentally deleted
Bug description
let shop = undefined; // shop is variable, and in some cases, it may be undefined
await prisma.theme.deleteMany({
where: {
shop,
}
})
I left out the shop parameter check, causing the shop to be undefined in some cases, triggering the deletion of the entire table. This operation of Prisma is very risky, I hope to improve it.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 15
- Comments: 18 (6 by maintainers)
Commits related to this issue
- feat(client): implement `strictUndefinedChecks` If preview feature is enabled, client-side validator will disallow explicitly setting fields to `undefined`. Newly introduced `Prisma.skip` could be us... — committed to prisma/prisma by SevInf 2 years ago
- feat(client): implement `strictUndefinedChecks` If preview feature is enabled, client-side validator will disallow explicitly setting fields to `undefined`. Newly introduced `Prisma.skip` could be us... — committed to prisma/prisma by SevInf 2 years ago
- feat(client): implement `strictUndefinedChecks` (#25224) * feat(client): implement `strictUndefinedChecks` If preview feature is enabled, client-side validator will disallow explicitly setting fi... — committed to prisma/prisma by deleted user 2 years ago
Hello everyone, in the past weeks we’ve came up with a different proposal for handling that issue, addressing some of the feedback.
TL;DR: we’ll introduce a preview feature flag for making explicitly
undefinedvalues invalid in Prisma 5, make it a default in Prisma 6, as well as introduce a safer shortcut for explicitly skipping part of the query.Full proposal text below, we’d like to hear you comments on it:
Problem
Currently, we treat
undefinedvalues as absent. Meaning, that any field set toundefinedexplicitly is treated as if the query was not supplied at all.This makes some usecases, listed in the doc pretty convenient, however, it also causes problems: JS language itself is very inconsistent on when it treats
nullvsundefinedas the same values or as two different ones and our behaviour is not the default most people expect.More importantly, for write queries, the feature can accidentally cause a data loss: https://github.com/prisma/prisma/issues/20169
For read queries, similar situation might cause a data leak.
Overall, there is strong opinion that that feature was a bad decision on our part for a long time. This proposal suggests we change that behaviour.
Proposal
In Prisma 5
Introduce new preview feature
strictUndefinedChecks.When preview feature is on, any value that is explicitly set to
undefinedwill cause a runtime error. Omitting a field must still be allowed - only explicit values are disallowed.We adjust our doc to recommend setting
exactOptionalPropertyTypessetting ([docs](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes)) to get equivalent type error.To simplify the migration, we also introduce
Prisma.skipsymbol which would act as “ignore this part of the query as if the field were not set” instead ofundefined.So these two calls are equivalent:
In that case, migration from
undefinedtoPrisma.skipis relatively simple:It is a safer option, because users are forced to be explicit about their intent.
In Prisma 6
We remove
strictUndefinedCheckspreview feature and make this behavior a default. Depending on feedback, we might introduce a generator option to revert back to old, unsafe, behaviour.This is extremely dangerous, I do not think it’s being treated as such. You are offering an abstraction that does the opposite of what the SQL standard does (and the majority of your users understand SQL before they understand Prisma). When filtering by emptiness (
where id in [], or undefined), no database will return records, but Prisma does. This is even worse in the case of deletes.I fear this is one famous case of mass-deletion away from a PR disaster for Prisma. I am a happy user of the library, mind you, but I’d recommend more urgency to this issue. I spent 2 minutes googling, and I already found a case of someone who had to suffer from this issue: https://www.farmin.dev/posts/35ad668d-29ae-4d86-8b4a-126b6d561aaa.
From our point of view this is not surprising and a “bug we have to fix”, but how Prisma is currently designed to work: https://www.prisma.io/docs/concepts/components/prisma-client/null-and-undefined We know this can be dangerous, but it has been like this for quite some while and switching would also have other deficits.
So as soon as we find the proper issue that requests us to change this, we will close this issue here as a duplicate.
We unfortunately ran into this issue (luckily had snapshots we could restore), and I had to write some middleware because this was super dangerous. I hope this can help somebody!
and then
Best guard here is strict type safety and attribute validation ahead of db writes. Also, always always always backup your production database!
I don’t care if breaking change were to be introduced, this evil shall be corrected.
undefinedandnullaren’t the same though. While moving in this direction is theoretically safer, it’s not as safe as it should be. Honestly, I believe the correct answer here is that Prisma should just throw an error if an undefined value is passed.Let’s say we have this method in Typescript.
In this case, if for some unchecked reason
userIdends up beingundefined. There query would just end up beingwhich ultimately has the same issue when you deal with DBs with significant scale. This could potentially query extreme amounts of data just in the same way the current behavior does.
To follow up: This feature is now live as the
strictUndefinedChecksPreview feature in Prisma ORM 5.20.0. Please let us know what you think.If you have any feedback, please leave it on the GitHub Discussion below
Docs: https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/null-and-undefined Feedback: https://github.com/prisma/prisma/discussions/25271
Hey, thanks for the report, we are aware of this issue and I fully agree with you that it can be dangerous. We should improve it.
The release notes say that this will be the default in the next major version. While not a blocker, that will require many changes with how my team currently uses prisma.
It’s quite common for us to use prisma to implement REST GET endpoints to list entities that can have many optional query params. For that sort of use case it’ll require us to make many changes to fallback to
Prisma.skip.Is it possible to make this behavior configurable in the future?