PersistedModel.destroyAll has very dangerous behaviour if the filter is not passed in correctly.
- [x] Bug
- [ ] Feature request
Description of feature (or steps to reproduce if bug)
The .destroyAll method is very dangerous if the filter you pass into it is incorrect. For example...
User.destroyAll({
where: {
userId: 5
}
})
Will actually destroy ALL User records because the filter passed into the function is incorrect. This seems like very dangerous behaviour and our developers have been hurt by this multiple times.
(for reference, the CORRECT filter would look like this:)
User.destroyAll({
userId: 5
})
Expected result
.destroyAll should only be deleting MATCHING records to the filter. So in the case where the filter is erroneous and doesn't match anything, nothing should be deleted.
Actual result (if bug)
It looks like by default, if .destroyAll does not match any records it deletes everything.
Maybe some validation can be done to see if the caller accidentally pass in filter instead of where directly. Most DBs will delete all records if no criteria is provided. We need to have a way to delete all records too.
@raymondfeng i would think there would be different behaviour between PersistedModel.destroyAll() vs PersistedModel.destroyAll({})
in the case where there's ever an object passed in you destroy all records that match that object filter. otherwise if there's an undefined object then you destroy all records
@raymondfeng So maybe we have to check if the record exists or not, then destroyAll() ?
I think this is inconvenient somehow.
Don't you think we could separate into 2 situations like @stringbeans said ?
+1
Request to change API, too.
The behavior is too dangerous. One of my colleagues accidentally deleted all of DB records because of his mistyping in where filter.
Another dangerous case in that when the DB property somehow changed, without the necessary changing of source codes which uses destroyAll API.
Another solution is creating a new deleting API with new behavior, IMO.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
I hope this gets look at..
this issue just bit us in the butt again :(
Damn, i just got burned by this too... sigh -_-'
Can we please get this re-opened @nitro404 ?
This really needs to be addressed :(
Sorry for the inconvenience everyone; re-opening because this issue is still relevant and important. I will take a look at it next week.
+1
++
I am not able to reproduce this bug at https://github.com/strongloop/loopback-datasource-juggler/pull/1597. Can anyone provide a test application, steps you took to reproduce this issue, or suggestions for the test case I wrote?
@stringbeans @fluteguitar @vjtc0n @shaheero @TheBoroer what was the filter you were passing when you hit the issue? Can you check out #1597 to see if it covers your use case?
@raymondfeng i would think there would be different behaviour between PersistedModel.destroyAll() vs PersistedModel.destroyAll({})
in the case where there's ever an object passed in you destroy all records that match that object filter. otherwise if there's an undefined object then you destroy all records
Hm I think the usage is consistent in LoopBack for most, if not all, CRUD APIs AFAIK (for e.g. User.find({}) and User.find() do the same thing), but I understand that doesn't mean it shouldn't change if it's not reasonable. @raymondfeng @bajtos do we want to treat it differently for destroyAll?
I am personally fine to change destroyAll to reject an empty where condition {}, asking users to use undefined as the where value instead.
.destroyAll should only be deleting MATCHING records to the filter. So in the case where the filter is erroneous and doesn't match anything, nothing should be deleted.
I agree this would be a great user experience. I am not sure how to implement such detection though!
Let's consider the example from the issue description:
User.destroyAll({
where: {
userId: 5
}
})
How should we tell that the developer is incorrectly sending a filter-like object instead of a where-like object? I am concerned that checking for where property may back-fire on us, consider the following use cases:
Command:
Locations.destroyAll({
where: {
// Bikini Atoll, Marshall Islands
lat: 11.583331,
lng: 165.333332
}
});
Now consider two different model definitions:
- A
Locationmodel has awhereproperty of typeGeoPoint- the invocation above is a valid usage. - A
Locationmodel has two propertieslatandlng- the invocation above is a mistake.
I suppose we could check the condition provided to destroyAll to see if it matches any Model properties, but this gets complicated when operators like and and or get involved (docs):
User.destroyAll({
or: [
{userId: 5},
{email: '[email protected]'},
]
});
What's worse, LoopBack models are not strict by default, it's possible to attach arbitrary properties to model instances. In that case it's not possible to say whether the filter/where object is trying to match a valid property not defined in the model, or whether the developer made a mistake.
The method name deleteAll already hints it's a destructive operation. What we need to explore is to see if there are some patterns that are typical mistakes from user input. To give a few examples:
- deleteAll(undefined, ...);
- deleteAll({}, ...);
- deleteAll({userId: undefined}, ...);
- deleteAll({where: {userId: 5}}, ...);
Which one should we reject?
Personally, I think we could reject 1, 3, and 4.
For case 4, it's a common mistake that developers pass in where and filter incorrectly. Maybe we should always check both patterns and report errors if the received object seems to be wrong.
@bajtos This does seem tricky to get right. I personally think that for now we shouldn't worry about the where object having properties named where or some other operator and implement logic that will only delete by what the where object has explicitly specified.
That said, I can't reproduce the issue either. For reference, I've created a model instance, called destroyAll(where: {id: 1}}), and called find to get the created model instance back