Skip to content

Error "401 Authorization required" when querying methods involving relations #426

Closed

Description

@sam-artuso

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?

Activity

  1. daankets commented on Aug 12, 2014

    @daankets

    I have the same issue. I also think the 401 Unauthorized is a wrong response. I am authenticated, so it should be 403 forbidden. I'm opening a new issue for that.

  2. gcirne commented on Aug 12, 2014

    @gcirne
    Contributor

    I think this has already been implemented: #301

  3. daankets commented on Aug 12, 2014

    @daankets

    This issue (#426) itself is not yet resolved. Defining an ACL on a relation property does not seem to work for me.
    Replicate by defining a model, adding a relationship to it (for example many user to many group). Then DENY ALL on * for ROLE $everyone, and finally ALLOW READ/EXECUTE on for ROLE $authenticated. I consequently get access denied for these relationships.

  4. dagumak commented on Aug 19, 2014

    @dagumak

    I am having the exact same issue! I spent a few hours digging through anything I can find, but I still couldnt get it to work!

  5. fabien commented on Aug 19, 2014

    @fabien
    Contributor

    @dagumak - can you post the ACL's you defined?

  6. dagumak commented on Aug 19, 2014

    @dagumak

    @fabien I posted it here (#459 (comment)), but here it is:

    {
      "name": "TestUser",
      "base": "User",
      "strict": true,
      "properties": {
        "username": {
          "type": "string",
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "message": {
          "type": "hasMany",
          "model": "Message",
          "foreignKey": "userId"
        }  
      },
      "acls": [
    
      ],
      "methods": []
    }
    
    
    {
      "name": "Message",
      "base": "PersistedModel",
      "properties": {
        "type": {
          "type": "string",
          "required": true
        },
        "text": {
          "type": "string",
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "testUser": {
          "type": "belongsTo",
          "model": "TestUser",
          "foreignKey": "userId"
        }
      },
      "acls": [
        {
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "ALLOW"
        }
      ],
      "methods": []
    }
    
    
  7. fabien commented on Aug 19, 2014

    @fabien
    Contributor

    I think the core User ACL's are interfering with the ACL's you defined. I recall the following from one of my projects, which overrides all the ACL's:

    TestUser.settings.acls = [ 
          { principalType: 'ROLE',
            principalId: '$everyone',
            permission: 'DENY' },
          { principalType: 'ROLE', // this is the important bit
            principalId: '$owner',
            permission: 'ALLOW' },
          { principalType: 'ROLE',
            principalId: '$everyone',
            permission: 'ALLOW',
            property: 'create' },
          { principalType: 'ROLE',
            principalId: '$owner',
            permission: 'ALLOW',
            property: 'deleteById' },
          { principalType: 'ROLE',
            principalId: '$everyone',
            permission: 'ALLOW',
            property: 'login' },
          { principalType: 'ROLE',
            principalId: '$everyone',
            permission: 'ALLOW',
            property: 'logout' },
          { principalType: 'ROLE',
            principalId: '$owner',
            permission: 'ALLOW',
            property: 'findById' },
          { principalType: 'ROLE',
            principalId: '$owner',
            permission: 'ALLOW',
            property: 'updateAttributes' },
          { principalType: 'ROLE',
            principalId: '$everyone',
            permission: 'ALLOW',
            property: 'confirm' }
        ]
  8. fabien commented on Aug 19, 2014

    @fabien
    Contributor

    @dagumak the very least you could do is trace your current TestUser.settings.acls to compare them.

  9. dagumak commented on Aug 19, 2014

    @dagumak

    @fabien I feel silly for asking, but other than console log, how can I do that?

    On Tue, Aug 19, 2014 at 11:32 PM, Fabien Franzen notifications@github.com
    wrote:

    @dagumak the very least you could do is trace your current TestUser.settings.acls to compare them.

    Reply to this email directly or view it on GitHub:
    #426 (comment)

  10. fabien commented on Aug 19, 2014

    @fabien
    Contributor

    @dagumak console.log is fine

  11. dagumak commented on Aug 19, 2014

    @dagumak

    @fabien I am not very familiar with the framework yet, but where are the models loaded and a good place for me to console.log it?

  12. fabien commented on Aug 19, 2014

    @fabien
    Contributor

    You can put the following into a file at /server/boot/debug.js:

    module.exports = function(app) {
      var TestUser = app.loopback.getModel('TestUser');
      console.log(TestUser.settings.acls);
    };
  13. raymondfeng commented on Aug 19, 2014

    @raymondfeng
    Member

    You can run the application with DEBUG enabled:

    $ DEBUG=loopback:security:* node .

    Thanks,


    Raymond Feng
    Co-Founder and Architect @ StrongLoop, Inc.

    StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

    On Aug 19, 2014, at 8:29 AM, Fabien Franzen notifications@github.com wrote:

    You can put the following into a file at /server/boot/debug.js:

    module.exports = function(app) {
    var TestUser = app.loopback.getModel('TestUser');
    console.log(TestUser.settings.acls);
    };
    —
    Reply to this email directly or view it on GitHub.

  14. dagumak commented on Aug 19, 2014

    @dagumak

    @fabien Thanks a bunch! Here it is:

    [ { principalType: 'ROLE',
        principalId: '$everyone',
        permission: 'DENY' },
      { principalType: 'ROLE',
        principalId: '$everyone',
        permission: 'ALLOW',
        property: 'create' },
      { principalType: 'ROLE',
        principalId: '$owner',
        permission: 'ALLOW',
        property: 'deleteById' },
      { principalType: 'ROLE',
        principalId: '$everyone',
        permission: 'ALLOW',
        property: 'login' },
      { principalType: 'ROLE',
        principalId: '$everyone',
        permission: 'ALLOW',
        property: 'logout' },
      { principalType: 'ROLE',
        principalId: '$owner',
        permission: 'ALLOW',
        property: 'findById' },
      { principalType: 'ROLE',
        principalId: '$owner',
        permission: 'ALLOW',
        property: 'updateAttributes' },
      { principalType: 'ROLE',
        principalId: '$everyone',
        permission: 'ALLOW',
        property: 'confirm' } ]
    
  15. SwadhyayStudios commented on Jan 31, 2015

    @SwadhyayStudios

    Is this resolved ? I am still having the same issue
    The last thread doesn't help

  16. 14 remaining items

  17. mike-aungsan commented on Oct 28, 2015

    @mike-aungsan

    @crandmck Many Thanks

  18. superkhau commented on Feb 4, 2016

    @superkhau
    Contributor

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

  19. jdhiro commented on Feb 4, 2016

    @jdhiro

    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.

  20. superkhau commented on Feb 4, 2016

    @superkhau
    Contributor

    @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.

  21. gekko68 commented on Feb 4, 2016

    @gekko68

    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,

  22. superkhau commented on Feb 4, 2016

    @superkhau
    Contributor

    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.

  23. gekko68 commented on Feb 5, 2016

    @gekko68

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

  24. emazzu commented on Oct 26, 2017

    @emazzu

    somebody can help me ? with the same themes ?

  25. emazzu commented on Oct 26, 2017

    @emazzu

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

  26. crandmck commented on Oct 26, 2017

    @crandmck
    Contributor
  27. emazzu commented on Oct 26, 2017

    @emazzu

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

  28. princecharmx commented on Nov 3, 2017

    @princecharmx

    @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 #3518

  29. princecharmx commented on Nov 23, 2017

    @princecharmx

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions