casl icon indicating copy to clipboard operation
casl copied to clipboard

Condition using $exists doesn't behave as expected

Open egmacke opened this issue 1 year ago • 2 comments

Describe the bug

Given a rule that defines a nested field should not exist (i.e. $exists: false) it behaves as expected if all nesting up to the relevant field is present, but if the parent field also does not exist, then it returns incorrectly (for clarification see below)

To Reproduce This is a contrived example, but illustrates the problem.

import { defineAbility } from '@casl/ability';

const ability = defineAbility(can => {
  can('read', 'User', {
    'group.objectId': { $exists: false }
  });
});

console.log(
  ability.can(
    'read',
    subject('User', {
      group: {
        objectId: 'JgtM0H1IH5'
      }
    })
  )
); // > false - as expected

console.log(
  ability.can(
    'read',
    subject('User', {
      group: {
        
      }
    })
  )
); // > true - as expected

console.log(
  ability.can(
    'read',
    subject('User', {
    })
  )
); // > false - not expected. `group.objectId` does not exists so rule should return true.

Expected behavior Rules with the condition $exists: false should always resolve when field is not present regardless of if parent exists or not.

Interactive example (optional, but highly desirable) https://replit.com/@egmackenzie1/CaslJS-bug-report#index.js

CASL Version

@casl/ability - v6.5.0

Environment: NodeJS

egmacke avatar Oct 18 '23 17:10 egmacke

Getting the same behavior, on 6.3.3

marik22312 avatar Jan 14 '24 14:01 marik22312

fixed in https://github.com/stalniy/ucast/commit/ec1c7d6a82bee22dd5c26779f92ff2b5aaf2efb5 Will take some time to publish a new version, plan to do it till the end of the week

stalniy avatar Jan 15 '24 07:01 stalniy

@ucast/[email protected] has been published. The issue should be fixed. ensure to update your lock file.

Close the issue, in case of any issues, please do not hesitate to report it here

stalniy avatar Jan 31 '24 00:01 stalniy