loopback icon indicating copy to clipboard operation
loopback copied to clipboard

PersistedModel.destroyAll has very dangerous behaviour if the filter is not passed in correctly.

Open stringbeans opened this issue 9 years ago • 40 comments

  • [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.

stringbeans avatar Jan 11 '17 19:01 stringbeans

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 avatar Jan 11 '17 19:01 raymondfeng

@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

stringbeans avatar Jan 11 '17 20:01 stringbeans

@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 ?

vjtc0n avatar Feb 08 '17 17:02 vjtc0n

+1

shaheero avatar May 25 '17 07:05 shaheero

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.

ZenStudioLab avatar Jul 15 '17 08:07 ZenStudioLab

Another solution is creating a new deleting API with new behavior, IMO.

ZenStudioLab avatar Jul 15 '17 08:07 ZenStudioLab

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.

stale[bot] avatar Sep 13 '17 08:09 stale[bot]

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.

stale[bot] avatar Sep 27 '17 08:09 stale[bot]

I hope this gets look at..

shaheero avatar Sep 28 '17 02:09 shaheero

this issue just bit us in the butt again :(

stringbeans avatar Dec 21 '17 23:12 stringbeans

Damn, i just got burned by this too... sigh -_-'

Can we please get this re-opened @nitro404 ?

TheBoroer avatar Mar 30 '18 08:03 TheBoroer

This really needs to be addressed :(

coco-napky avatar Apr 25 '18 21:04 coco-napky

Sorry for the inconvenience everyone; re-opening because this issue is still relevant and important. I will take a look at it next week.

b-admike avatar Apr 26 '18 02:04 b-admike

+1

averypfeiffer avatar May 13 '18 22:05 averypfeiffer

++

mrNamelesss avatar May 18 '18 12:05 mrNamelesss

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?

b-admike avatar Jun 21 '18 13:06 b-admike

@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?

b-admike avatar Jun 29 '18 13:06 b-admike

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 Location model has a where property of type GeoPoint - the invocation above is a valid usage.
  • A Location model has two properties lat and lng - 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.

bajtos avatar Jun 29 '18 14:06 bajtos

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:

  1. deleteAll(undefined, ...);
  2. deleteAll({}, ...);
  3. deleteAll({userId: undefined}, ...);
  4. 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.

raymondfeng avatar Jun 29 '18 14:06 raymondfeng

@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

shimks avatar Jul 04 '18 17:07 shimks