loopback icon indicating copy to clipboard operation
loopback copied to clipboard

ACL for models extending User Model is not working

Open jannyHou opened this issue 10 years ago • 33 comments

  1. slc loopback to create a new lb app
  2. slc loopback:model to create a model extends User model
  3. node . and open explorer
  4. Try endpoint Post/ myusers/ :

Result:

{
  "username": "string",
  "email": "[email protected]",
  "id": 1
}

Try endpoint Get/ myusers/ :
Result:

[
  {
    "username": "string",
    "email": "[email protected]",
    "id": 1
  }
]

By default, people should not be able to Get /myusers without login.

jannyHou avatar Apr 29 '16 15:04 jannyHou

I think ACLs are not inherited when extending a model. See #157 for more details.

bajtos avatar May 02 '16 06:05 bajtos

I think there is one catch: ACL is represented as a list of access-control entries and AFAIK we don't have a declarative way for clearing this list from model JSON file. This means that when a child model inherits ACL from the parent, it is not possible to drop parent's ACL and build a new ACL from scratch.

We should find a solution for that while working on the fix for this issue.

Since this was discovered when extending a built-in model, I think it relates to #397 A better way for customizing built-in models and models from components

bajtos avatar May 02 '16 07:05 bajtos

@bajtos Are you saying this is a bug everywhere, or that it wasn't intended to work in the first place?

To your second point - what if the logic was this: Given Child extends Parent - if my child.json file has no acls property, then it inherits. If it has "acls":[], that means "I'm not inheriting Parent, I'm setting acls to an empty array."

That may not be backwards compat though.

cfjedimaster avatar May 02 '16 12:05 cfjedimaster

I am in middle of something and I need it. please fix it ASAP. Also please suggest any workaround for the time being, if possible. Thanks in advance.

sahilanguralla avatar May 03 '16 06:05 sahilanguralla

Are you saying this is a bug everywhere, or that it wasn't intended to work in the first place?

I believe that we have never implemented inheritance of ACL. I don't think it was an intentional decision, just the result of the way how we implemented the feature.

To your second point - what if the logic was this: Given Child extends Parent - if my child.json file has no acls property, then it inherits. If it has "acls":[], that means "I'm not inheriting Parent, I'm setting acls to an empty array."

I think the typical scenario would be to built a custom ACL in the JSON file, which would not be supported if you had to specify "acls":[].

Here are few other ideas of what can be done:

  1. When "acls" is not defined, or set to a special value like "inherit" (notice it's a string, not an array as usually), the model will inherit "acls" from the parent. If "acls" is set, parent ACL will be discarded and the ACL provided in the child model will be used instead.
// inherits from parent
{
  "base": "User",
  "acls": "inherit" 
}

// defines custom ACL
{
  "base": "User",
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW",
    }
  ]
}
  1. Another approach is to create a special ACL entry to clear the inherited list:
// defines custom ACL
{
  "base": "User",
  "acls": [
    "reset",
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW",
    }
  ]
}
  1. I think the cleanest option is to add a model setting specifying whether parent ACL should be inherited:
{
  "base": "User",
  "inheritAcls": false, // true by default
  "acls": []
}

The last option looks best to me.

@raymondfeng @ritch @superkhau thoughts?

bajtos avatar May 03 '16 07:05 bajtos

Also if we add a setting inheritAcls, then we can implement it in 2.x and modify model generator to set the flag to true. That way we get backwards compatibility for existing codebase and still have the benefit of doing things the right way for newly created models.

bajtos avatar May 03 '16 07:05 bajtos

@sahilanguralla

I am in middle of something and I need it. please fix it ASAP.

If you can contribute a pull request fixing the issue then I am happy to help you along the way.

Also please suggest any workaround for the time being, if possible. Thanks in advance.

Copy the acls entry from LoopBack's common/models/user.json into your newly created model.

bajtos avatar May 03 '16 07:05 bajtos

Personally, it feels wrong to me that some things inherit automatically and some would not (ACLs).

"I think the typical scenario would be to built a custom ACL in the JSON file, which would not be supported if you had to specify "acls":[]."

I'm not quite sure what you mean here. If I'm building Child(extends Parent) and I want to specify custom acls, then I'd type them at the property. If I wanted to specify custom acls and specifically no ACLs, then [] implies an empty array.

If I leave the property off, my assumption would be that it would inherit it from Parent.

I don't like the "reset" idea because it wouldn't be consistent. Like, as a user, I may see that and assume I could apply it to properties too.

On Tue, May 3, 2016 at 2:31 AM, Miroslav Bajtoš [email protected] wrote:

@sahilanguralla https://github.com/sahilanguralla

I am in middle of something and I need it. please fix it ASAP.

If you can contribute a pull request fixing the issue then I am happy to help you along the way.

Also please suggest any workaround for the time being, if possible. Thanks in advance.

Copy the acls entry from LoopBack's common/models/user.json https://github.com/strongloop/loopback/blob/6b40c69bb3bebbf1b7651b881f6f8b139922112a/common/models/user.json#L36-L91 into your newly created model.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/2270#issuecomment-216458231

Raymond Camden, Developer Advocate for StrongLoop at IBM

Email : [email protected] Blog : www.raymondcamden.com Twitter: raymondcamden

cfjedimaster avatar May 03 '16 13:05 cfjedimaster

What do you think about giving most general rules from child ACLs higher weight than most specific rules from parent? It will allow developer to deny access to all inherited methods with one rule and allow access with more specific ones. On the other hand, if developer applies deny all to all rule in the child model, he will need to copy rules from parent model. It is not DRY. But, at last, with general denial rule in child model developer will be sure, that no new methods will be accessible if parent model extended.

arma-gast avatar Jun 11 '16 20:06 arma-gast

I'm honestly having a hard time wrapping my head around it - can you create a demo? I don't mean a working demo, just the JSON files.

cfjedimaster avatar Jun 13 '16 12:06 cfjedimaster

Say, I define a model Person, which inherits User:

{
  "name": "Person",
  "plural": "people",
  "base": "User",
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    }
  ],
  "methods": {}
}

I get the following log when try POST /people/login:

  loopback:security:acl The following ACLs were searched:  +2ms
  loopback:security:acl ---ACL--- +0ms
  loopback:security:acl model Person +1ms
  loopback:security:acl property login +0ms
  loopback:security:acl principalType ROLE +0ms
  loopback:security:acl principalId $everyone +0ms
  loopback:security:acl accessType * +0ms
  loopback:security:acl permission ALLOW +0ms
  loopback:security:acl with score: +0ms 8004
  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: +0ms 7495
  loopback:security:acl ---ACL--- +0ms
  loopback:security:acl model Person +0ms
  loopback:security:acl property * +1ms
  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: +0ms 7495
  loopback:security:acl ---Resolved--- +0ms
  loopback:security:access-context ---AccessRequest--- +0ms
  loopback:security:access-context  model Person +0ms
  loopback:security:access-context  property login +0ms
  loopback:security:access-context  accessType EXECUTE +0ms
  loopback:security:access-context  permission ALLOW +0ms
  loopback:security:access-context  isWildcard() false +0ms
  loopback:security:access-context  isAllowed() true +0ms

Correct me if I am wrong.

We see 3 rules: one from ACL of model Person (general denial) and two inherited from User (general denial and allowing method /login). The allowing rule has higher weight, because it is more specific. Even it is inherited. I think, rules from model itself must have higher weight than inherited ones.

For now, there is no way I can deny access to methods that will be added to model User in the future.

arma-gast avatar Jun 13 '16 13:06 arma-gast

So given that, yes, I would have expected the ACL in Person to block everything.

cfjedimaster avatar Jun 14 '16 15:06 cfjedimaster

Hi everyone! Right now I'm facing a problem similar to yours, but not the same thing. I'm creating a Model extending from the built-in User model of loopback, and the ACL i actually being inherited completely. But in all the forums I've read, the problem seems to be the other way around.

I don't know what is going on...

mora260 avatar Jun 28 '16 23:06 mora260

@mora260 and anyone runs into the same issue, we fixed acl problem in pr https://github.com/strongloop/loopback-workspace/commit/bd6c93c03eadc168e5c55308aee2883848d18e6a

And it's released in [email protected] Please update loobpack-workspace in your genrator-loopback, or a more quick way, reinstall your strongloop to get it work.

I am glad to help if you still have problem.

jannyHou avatar Jun 29 '16 04:06 jannyHou

Hi @jannyHou, I'm experiencing the same problem as @mora260. My model extending the user model is inheriting all of the acls.

My strongloop is the latest version

Sloff avatar Jul 13 '16 09:07 Sloff

@Sloff @mora260 Could you explain more on what problem you are having? Let's see now we have a custom user MyUser that based on User model, what problems do you have? Thanks!

jannyHou avatar Aug 22 '16 19:08 jannyHou

I am closing this issue since I believe the original bug is fixed. Open a new one to keep track of new bug:https://github.com/strongloop/loopback/issues/2664

Let's keep discussion under the new one.

jannyHou avatar Aug 23 '16 14:08 jannyHou

The original problem appeared to be that models inheriting from User did not inherit ACL's so they had to be copied over.

Now the problem seems to be the ACL's on User are not respecting overrides from the child. If I put a blanket deny all rule on the child model, the specific ACL's from the parent are still overriding.

In my case, the easiest fix is to copy out the source ACL's from the parent (which was a solution to the original problem), paste them into the child, remove any I don't need to change (inheritance seems to work now) and change the ones I need to override with my child models specs.

So, is the intended behavior that parent ACL's are now inherited and that because of the granularity of the rule, may overrule general rules in child models? Is there any way to clear the parent rules and use only the child rules?

Thanks!

dleute avatar Oct 22 '16 00:10 dleute

I am experiencing this problem even in version 3.6.0. Is it really not fixed yet?

maziarz avatar Apr 03 '17 18:04 maziarz

hi, i believe this is will be fixed in https://github.com/strongloop/loopback-datasource-juggler/pull/1289

ebarault avatar Apr 04 '17 13:04 ebarault