loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Error "401 Authorization required" when querying methods involving relations

Open sam-artuso opened this issue 12 years ago • 40 comments

I've recently migrated an application to LoopBack 2.0.

All my models require user authentication and have these ACLs:

    "acls": [
      {
        "accessType": "*",
        "permission": "DENY",
        "principalType": "ROLE",
        "principalId": "$everyone"
      },
      {
        "accessType": "READ",
        "permission": "ALLOW",
        "principalType": "ROLE",
        "principalId": "$authenticated"
      }
    ]

I have a model called issues. After logging in through the POST method /Users/login:

  • I don't have any problems when calling the GET method /issues. I get a list of issues as expected;
  • When I query a method involving a relation between two models (e.g. GET /issues/{id}/categories), I get the error 401 Authorization required. The same model used to work in Loopback 1.0.

Any clues?

sam-artuso avatar Jul 31 '14 20:07 sam-artuso

@rubentorresbonet Holy cow, that worked! I had to use the property __get__customer for a relationship named customer. Thanks!

DEBUG=loopback:security:* slc run really helped me understand what was happening with the ACLs.

This was on a subclassed user model. The full thing looks like this now:

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {
    "customer": {
      "type": "hasOne",
      "model": "Customer"
    }
  },
  "acls": [
    {
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "__get__customer"
    }
  ],
  "methods": []
}

This can't possibly be the intended design.

jdhiro avatar Aug 02 '15 21:08 jdhiro

Agree with @jdhiro that from a design perspective, this is very uncomfortable. Would it not be better to introduce a single declaration in order to to set R/W/X privileges on a single relation, just as we can do for other built-in methods?

csvan avatar Sep 10 '15 07:09 csvan

By the way, the link to the documentation for accessing related models provided above is broken, the correct one is here:

https://docs.strongloop.com/display/public/LB/Accessing+related+models

csvan avatar Sep 10 '15 07:09 csvan

I spent a few hours on this too. Doc is not clear. It should clearly mention that default is denied

mike-aungsan avatar Oct 23 '15 01:10 mike-aungsan

@crandmck ^

superkhau avatar Oct 23 '15 02:10 superkhau

While trying CoffeeShop example given at https://docs.strongloop.com/display/LB/Getting+started+part+II I'm getting 401 only while editing the review. I've the ACLs defined as given @ https://docs.strongloop.com/display/public/LB/Define+access+controls

So the following ACL entry is failing:

{ "accessType": "WRITE", "principalType": "ROLE", "principalId": "$owner", "permission": "ALLOW" }

If I change $owner to $authenticated, then I don't get 401. So there seems to be some problem in isOwner(). I've tried using DEBUG=loopback:security:* and it gives following output

loopback:security:access-context ---AccessContext--- +1ms loopback:security:access-context principals: +3ms loopback:security:access-context principal: {"type":"USER","id":"562a105037cfea381ac14fbb"} +2ms loopback:security:access-context modelName Review +3ms loopback:security:access-context modelId 562a105037cfea381ac14fbe +1ms loopback:security:access-context property upsert +1ms loopback:security:access-context method upsert +2ms loopback:security:access-context accessType WRITE +1ms loopback:security:access-context accessToken: +2ms loopback:security:access-context id "EoEwOGWIWLNfw4DLgxf9ga2zCoFht974DgCccMFkQLmLrmnQJ64eHJidDIf4e8dh" +1ms loopback:security:access-context ttl 1209600 +3ms loopback:security:access-context getUserId() 562a105037cfea381ac14fbb +2ms loopback:security:access-context isAuthenticated() true +3ms loopback:security:role Custom resolver found for role $everyone +0ms loopback:security:role isInRole(): $everyone +2ms loopback:security:access-context ---AccessContext--- +1ms loopback:security:access-context principals: +1ms loopback:security:access-context principal: {"type":"USER","id":"562a105037cfea381ac14fbb"} +1ms loopback:security:access-context modelName Review +2ms loopback:security:access-context modelId 562a105037cfea381ac14fbe +1ms loopback:security:access-context property upsert +1ms loopback:security:access-context method upsert +3ms loopback:security:access-context accessType WRITE +1ms loopback:security:access-context accessToken: +1ms loopback:security:access-context id "EoEwOGWIWLNfw4DLgxf9ga2zCoFht974DgCccMFkQLmLrmnQJ64eHJidDIf4e8dh" +2ms loopback:security:access-context ttl 1209600 +2ms loopback:security:access-context getUserId() 562a105037cfea381ac14fbb +2ms loopback:security:access-context isAuthenticated() true +1ms loopback:security:role Custom resolver found for role $everyone +1ms loopback:security:role isInRole(): $owner +2ms loopback:security:access-context ---AccessContext--- +1ms loopback:security:access-context principals: +2ms loopback:security:access-context principal: {"type":"USER","id":"562a105037cfea381ac14fbb"} +2ms loopback:security:access-context modelName Review +2ms loopback:security:access-context modelId 562a105037cfea381ac14fbe +2ms loopback:security:access-context property upsert +1ms loopback:security:access-context method upsert +1ms loopback:security:access-context accessType WRITE +2ms loopback:security:access-context accessToken: +1ms loopback:security:access-context id "EoEwOGWIWLNfw4DLgxf9ga2zCoFht974DgCccMFkQLmLrmnQJ64eHJidDIf4e8dh" +2ms loopback:security:access-context ttl 1209600 +2ms loopback:security:access-context getUserId() 562a105037cfea381ac14fbb +1ms loopback:security:access-context isAuthenticated() true +2ms loopback:security:role Custom resolver found for role $owner +2ms loopback:security:role isOwner(): Review 562a105037cfea381ac14fbe userId: 562a105037cfea381ac14fbb +2ms loopback:security:role Model found: {"date":"2015-10-19T10:47:44.791Z","rating":5,"comments":"A very good coffee shop.","id":"562a105037cfea381ac14fbe","coffeeShopId":"562a105037cfea381ac14fb8","publisherId":"562a105037cfea381ac14fbb"} +5ms loopback:security:role Checking relation reviewer to Reviewer: {"name":"reviewer","type":"belongsTo","modelFrom":"Review","keyFrom":"pulisherId","modelTo":"Reviewer","keyTo":"id","multiple":false} +4ms loopback:security:acl The following ACLs were searched: +4ms loopback:security:acl ---ACL--- +1ms loopback:security:acl model Review +2ms loopback:security:acl property * +1ms loopback:security:acl principalType ROLE +1ms loopback:security:acl principalId $everyone +1ms loopback:security:acl accessType * +1ms loopback:security:acl permission DENY +2ms loopback:security:acl with score: +1ms 7495 loopback:security:acl ---ACL--- +1ms loopback:security:acl model Review +1ms loopback:security:acl property * +1ms loopback:security:acl principalType ROLE +1ms loopback:security:acl principalId $everyone +2ms loopback:security:acl accessType READ +1ms loopback:security:acl permission ALLOW +1ms loopback:security:acl with score: +1ms -1 loopback:security:acl ---Resolved--- +1ms loopback:security:access-context ---AccessRequest--- +1ms loopback:security:access-context model Review +2ms loopback:security:access-context property upsert +1ms loopback:security:access-context accessType WRITE +1ms loopback:security:access-context permission DENY +1ms loopback:security:access-context isWildcard() false +2ms loopback:security:access-context isAllowed() false +2ms

vnathalye avatar Oct 23 '15 11:10 vnathalye

@mike-aungsan I added a note to https://docs.strongloop.com/display/LB/Accessing+related+models#Accessingrelatedmodels-note. Please let me know if there's any issue with it.

crandmck avatar Oct 28 '15 23:10 crandmck

@crandmck Many Thanks

mike-aungsan avatar Oct 28 '15 23:10 mike-aungsan

Are you guys still running into issues? Can I close this?

superkhau avatar Feb 04 '16 19:02 superkhau

The design seems undesirable, but as long as the design is documented (and it looks like it is from @crandmck post), I don't see a problem closing it. I worked around it in my project -- no longer an immediate concern.

jdhiro avatar Feb 04 '16 19:02 jdhiro

@jdhiro Sounds good. Please open a new issue if you guys are still running into issues. Better yet, submit a patch and we can go from there.

superkhau avatar Feb 04 '16 20:02 superkhau

Yes - i do

sorry for this question but how do I enable the debug view ?

"You can run the application with DEBUG enabled:

$ DEBUG=loopback:security:* node . "

Thanks,

gekko68 avatar Feb 04 '16 20:02 gekko68

sorry for this question but how do I enable the debug view ?

What do you mean debug view? Run DEBUG=loopback:security:* node . when starting your loopback app.

superkhau avatar Feb 04 '16 20:02 superkhau

Hi, got it working. Thx. U just run the application DEBUG=loopback:security:* node . on commandline .

gekko68 avatar Feb 05 '16 08:02 gekko68

somebody can help me ? with the same themes ?

emazzu avatar Oct 26 '17 00:10 emazzu

if somebody can help me, I send the problem !!!

emazzu avatar Oct 26 '17 00:10 emazzu

https://loopback.io/doc/en/contrib/Reporting-issues.html#dont-use-an-issue-to-ask-a-question

crandmck avatar Oct 26 '17 17:10 crandmck

Execuse me, but on gitter nobody responds, and I cant resolve the problem, I dont know what I can do !!!

emazzu avatar Oct 26 '17 18:10 emazzu

@emazzu @crandmck , i think this problem has come again in the latest repo. @raymondfeng , please look into it. This exposes the security loophole for the app in production. Refer this thread https://github.com/strongloop/loopback/issues/3518

princecharmx avatar Nov 03 '17 14:11 princecharmx

@emazzu , @crandmck @raymondfeng , this problem is only with custom access token. I reverted to loopback AccessToken model than it started working as intended.

princecharmx avatar Nov 23 '17 09:11 princecharmx