loopback icon indicating copy to clipboard operation
loopback copied to clipboard

loopback 3.x | ACL not working

Open x0cipher opened this issue 9 years ago • 16 comments

I have a derived 'Person' model from the in-built User model. But for some reason whenever I am accessing GET: /Person

It's saying 401 | Authentication required.

Things I've done:

  1. Added a custom role as per the documentation, "admin"
  2. My Person model's ACL config is:
{
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$admin",
      "permission": "ALLOW",
      "property": "find"
}
  1. In Debug mode, i tried to analyze the loopback acl resolver's output:
loopback:security:role isInRole(): $everyone +20s
loopback:security:access-context ---AccessContext--- +0ms
loopback:security:access-context principals: +0ms
loopback:security:access-context principal: {"type":"USER","id":"588c0a0cb4228040ec51ed20"} +0ms
loopback:security:access-context modelName Person +1ms
loopback:security:access-context modelId undefined +0ms
loopback:security:access-context property find +0ms
loopback:security:access-context method find +0ms
loopback:security:access-context accessType READ +0ms
loopback:security:access-context accessToken: +0ms
loopback:security:access-context   id "56qnbCovt2uGDwk3wXBObi9GfceZn27t254Yf07A4xllcqUbEQHFD4fYN0HSCfj0" +0ms
loopback:security:access-context   ttl 1209600 +0ms
loopback:security:access-context getUserId() 588c0a0cb4228040ec51ed20 +0ms
loopback:security:access-context isAuthenticated() true +0ms
loopback:security:role Custom resolver found for role $everyone +0ms
loopback:security:role isInRole(): $admin +0ms
loopback:security:access-context ---AccessContext--- +0ms
loopback:security:access-context principals: +0ms
loopback:security:access-context principal: {"type":"USER","id":"588c0a0cb4228040ec51ed20"} +0ms
loopback:security:access-context modelName Person +0ms
loopback:security:access-context modelId undefined +1ms
loopback:security:access-context property find +0ms
loopback:security:access-context method find +0ms
loopback:security:access-context accessType READ +0ms
loopback:security:access-context accessToken: +0ms
loopback:security:access-context   id "56qnbCovt2uGDwk3wXBObi9GfceZn27t254Yf07A4xllcqUbEQHFD4fYN0HSCfj0" +0ms
loopback:security:access-context   ttl 1209600 +0ms
loopback:security:access-context getUserId() 588c0a0cb4228040ec51ed20 +0ms
loopback:security:access-context isAuthenticated() true +0ms
loopback:security:role Role found: {"id":"588c0a0cb4228040ec51ed21","name":"$admin","created":{},"modified":{}} +4ms
loopback:security:role Role mapping found: null +5ms
loopback:security:role isInRole() returns: null +1ms
loopback:security:acl The following ACLs were searched:  +0ms
loopback:security:acl ---ACL--- +0ms
loopback:security:acl model Person +0ms
loopback:security:acl property * +0ms
loopback:security:acl principalType ROLE +0ms
loopback:security:acl principalId $everyone +0ms
loopback:security:acl accessType * +0ms
loopback:security:acl permission DENY +0ms
loopback:security:acl with score: 7495 +0ms
loopback:security:acl ---Resolved--- +0ms
loopback:security:access-context ---AccessRequest--- +0ms
loopback:security:access-context  model Person +0ms
loopback:security:access-context  property find +1ms
loopback:security:access-context  accessType READ +0ms
loopback:security:access-context  permission DENY +0ms
loopback:security:access-context  isWildcard() false +0ms
loopback:security:access-context  isAllowed() false +0ms

I was able to figure out that this has something to do with #2270

But even there, I couldn't find a solution to what should I do in order for loopback role resolver to give higher priority to my custom role.

x0cipher avatar Jan 28 '17 03:01 x0cipher

same here

davidchriqui avatar Feb 04 '17 13:02 davidchriqui

I think I figured this out last week I believe there was an equals some where that's comparing the role mapping principalType of 'Role' against the defined acl accessType of 'ROLE' and gets filtered out some where.

I also think there is an issue with https://github.com/strongloop/loopback/blob/master/common/models/acl.js#L114 as it's got an || ALL so for the acls that don't have a property set like https://github.com/strongloop/loopback/blob/master/common/models/user.json#L29 so both the ruleValue and requestedValue get set to ALL which adds to the score amount

BoLaMN avatar Feb 19 '17 00:02 BoLaMN

I got the same problem

tuanquynet avatar Mar 02 '17 15:03 tuanquynet

Any word on a fix for this or a work around?

jbrosan avatar Mar 14 '17 18:03 jbrosan

hi, could anyone provide a sandbox reproducing the issue, it would greatly ease the troubleshooting? (do not forget to update the dependancies to make sure it matches your setup)

🖐 Looking at the logs: i can see

loopback:security:role Role found: {"id":"588c0a0cb4228040ec51ed21","name":"$admin","created":{},"modified":{}} +4ms
loopback:security:role Role mapping found: null +5ms
loopback:security:role isInRole() returns: null +1ms

As the role $admin is found, i assume a static role mapping is used here, and not a dynamic role resolver. The log clearly states that no role mapping was found for the given Person instance and the role $admin : did you create this mapping?

The situation shown in the logs seems not related to ACL scoring as suggested in some comments, you can see the ACL is not even scored as the user is not considered to have the $admin role

ebarault avatar Mar 14 '17 21:03 ebarault

@ebarault Yes, I've used a static role mapping here. But as I'm writing this, I'm thinking I should try using a dynamic role resolver, although is the the ideal way to implement this?

Meanwhile, I'm creating a sandbox of this issue and will add it in the comments for everyone to go through.

Thanks.

x0cipher avatar Mar 16 '17 10:03 x0cipher

@akshayKrSingh Usually for an admin role a role mapping is the best solution. Let's wait for your sandbox.

ebarault avatar Mar 16 '17 18:03 ebarault

@ebarault Created a repo with the issue, Sandbox. Although, the issue is no longer reproducible after upgrading the packages, unless somebody is able to reproduce this issue I'm closing this.

FYI for future reference: as @ebarault mentioned in his comments -

do not forget to update the dependancies

That should solve the issue, in case anyone is facing the same problem.

x0cipher avatar Mar 18 '17 04:03 x0cipher

@ebarault @akshayKrSingh I'm also facing the same issue. Couldn't repro using your Sandbox. I verified that my dependencies are up to date. The only difference I can find is that I'm using MongoDB. I could track down the issue to https://github.com/strongloop/loopback/blob/master/common/models/role.js#L445 where roleMappingModel.findOne cannot find the RoleMapping instance even though I can see it in my database. Any thoughts? Thanks

UPDATE: Changed my datasource to In Memory and custom roles works fine. Looks like the issue exists using MongoDB

hyperd avatar Mar 22 '17 19:03 hyperd

@hyperd : are you able to reproduce the issue in a sandbox using mongodb as the db backend?

ebarault avatar Mar 23 '17 07:03 ebarault

@ebarault Looks like my application was created with an older version of lb which doesn't automatically set strictObjectIDCoercion for RoleMapping model. After adding it to model-config.json roles started working fine. Thanks for chiming in!

hyperd avatar Mar 24 '17 01:03 hyperd

@akshayKrSingh what dependencies did you update?

kodepareek avatar Apr 18 '17 13:04 kodepareek

@akshayKrSingh figured that out. Seems instantiating the project with the loopback cli installs with very outdated versions of all required lb related dependencies.

kodepareek avatar Apr 19 '17 04:04 kodepareek

@kodepareek yup.

x0cipher avatar Apr 21 '17 05:04 x0cipher

I have the same issue, did someone figured out the fix?

sisardor avatar May 20 '17 00:05 sisardor

@sisardor this workaround can help https://gist.github.com/spencermefford/bc73812f216e0e254ad1

tuanquynet avatar May 20 '17 10:05 tuanquynet