Error "401 Authorization required" when querying methods involving relations
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 error401 Authorization required. The same model used to work in Loopback 1.0.
Any clues?
@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.
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?
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
I spent a few hours on this too. Doc is not clear. It should clearly mention that default is denied
@crandmck ^
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
@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 Many Thanks
Are you guys still running into issues? Can I close this?
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 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.
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,
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.
Hi, got it working. Thx. U just run the application DEBUG=loopback:security:* node . on commandline .
somebody can help me ? with the same themes ?
if somebody can help me, I send the problem !!!
https://loopback.io/doc/en/contrib/Reporting-issues.html#dont-use-an-issue-to-ask-a-question
Execuse me, but on gitter nobody responds, and I cant resolve the problem, I dont know what I can do !!!
@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
@emazzu , @crandmck @raymondfeng , this problem is only with custom access token. I reverted to loopback AccessToken model than it started working as intended.