loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Ability to return 401 and 403 response codes

Open BradleyFord opened this issue 12 years ago • 16 comments

For our AngularJS app we have a number of different roles (User, Engineer, Administrator, etc).

Currently when the user fails to authenticate Loopback is returning a 401; this is the correct response. So when we receive the result we look at the response code and redirect to the login page if it is a 401.

But when a user logs in correctly, and tries to access an API that he does not have permission to access we are still getting a 401 response. I understand that this should be a 403 code. http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

You can see an example of an ACL below { "accessType": "*", "permission": "ALLOW", "principalType": "ROLE", "principalId": "Administrator", "property": "APIname" },

Our goal is to redirect to login when we get a 401; if they get a 403 then we need to give the user another message that their access is denied for that function.

BradleyFord avatar Jun 04 '14 16:06 BradleyFord

The specification says this for 403: "Authorization will not help and the request SHOULD NOT be repeated". IMO that means 403 is not suitable for the situation when the user does not have permissions to access the resource.

What if the information whether the user is not authenticated or whether it is not authorized to access the requested resource was sent in the JSON response body, instead of 401/403 status code?

bajtos avatar Jun 04 '14 17:06 bajtos

I tend to disagree @bajtos,

401 if they are not authorized, as authorization in that instance will make a difference. 403 if the user is authorized, as they don't have permission with the authorization already provided.

This is how Microsoft have interpreted the specs for years.

403 http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/21079107-1740-470e-a933-23a45494b8ba.mspx?mfr=true

401 http://technet.microsoft.com/en-us/library/cc780140(v=ws.10).aspx

karlmikko avatar Jun 04 '14 21:06 karlmikko

Further, Ben Nadel make a good point about hiding the fact the resource exists for security purposes. http://www.bennadel.com/blog/2400-handling-forbidden-restful-requests-401-vs-403-vs-404.htm

Would it be worth having a config option to send a specific status for this kind of event? So the API creator can decide?

karlmikko avatar Jun 04 '14 21:06 karlmikko

@karlmikko Good argument. We can potentially expose it as an option to strong-remoting via app.json.

raymondfeng avatar Jun 04 '14 22:06 raymondfeng

The other think i think of would be to create an error handling middleware that can intercept the 401 and alter it if the auth is valid.

karlmikko avatar Jun 04 '14 22:06 karlmikko

You could also allow afterRemote to be called on error. This would allow one to intercept the error there also.

karlmikko avatar Jun 04 '14 23:06 karlmikko

@karlmikko IMO 403 frequently misinterpreted. It should be used when the access to the resource is forbidden by server configuration (e.g. you are not allowed to view /etc/passwd).

If you look at the IIS sub-codes for 401 and 403, you'll see that 403 is used for situations where 403 is returned regardless of which user is logged in. On the other hand, 401 has a sub-code that perfectly matches the scenario described in this issue:

401.3 Access is denied due to an ACL set on the requested resource.

Would it be worth having a config option to send a specific status for this kind of event? So the API creator can decide?

That's a good idea. While LoopBack can have its own opinion (e.g. return an equivalent of 401.3 by default), it should provide hooks for customising the error responses.

I like your idea of providing some kind of afterRemote hook called when the operation fails. I would rather not change the existing afterRemote hook, but add a new one - e.g. onRemoteError.

bajtos avatar Jun 05 '14 06:06 bajtos

@bajtos I think you may have won me over with the "as spec" use cases for 403 /401.

I do however believe that there is a need for a distinguishing of no user / no permission. This is evident by people choosing to use 403 not as per spec and in Microsoft creating sub-codes.

There is also the need to hide the permission fact for security.

I think there should be a global approach (app setting) as an onRemoteError would need to be manually set for all models.

I do also really like the idea of onRemoteError as that could be very helpful on specific models. Like if you had a Model controller method to do a remote action that only a admin could do. Then the app could prompt for further credentials for that function.

karlmikko avatar Jun 05 '14 08:06 karlmikko

@bajtos I agree with @karlmikko.

It would be good to either have a global setting or possibly even in the models.json for each ACL. In some complex cases there may also be a need for an onRemoteError.

Overall I think the simpler the better; and a global setting seems the simplest for now. Then if a strong need appears it could be extended for more advanced cases.

BradleyFord avatar Jun 05 '14 08:06 BradleyFord

The other place the setting could be placed is in models.json in the options. onRemoteErrorDefault = 401 / 403 / 404?

karlmikko avatar Jun 05 '14 08:06 karlmikko

I like the proposal of having some mechanism for the API creator to decide what error codes to send in specific situations since this offers the greatest flexibility.

Nonetheless, I would like to say that I agree with the original comment on the meaning of 401/403: 401 for when the client fails to authenticate (e.g. no credentials or invalid credentials) and 403 for when the client does not have sufficient permission privileges to access a protected resource.

We've had long discussions on the meaning of these codes at my current job and, since we are using OAuth 2.0, what finally settled it for us was this section on the OAuth 2.0 Authorization Framework: Bearer Token Usage spec: http://tools.ietf.org/html/rfc6750#section-3.1

gcirne avatar Jun 05 '14 14:06 gcirne

We've had long discussions on the meaning of these codes at my current job and, since we are using OAuth 2.0, what finally settled it for us was this section on the OAuth 2.0 Authorization Framework: Bearer Token Usage spec.

Fair enough, keeping our error responses compatible with OAuth 2.0 is a good idea.

Is anybody willing to submit a patch for this? Here is the line to change:

https://github.com/strongloop/loopback/blob/d8c6f9d96228169aa8af7f30e21dfe841573971a/lib/application.js#L323

The ACL unit-tests are using loopback-testing helpers, it will be necessary to fix that module first.

bajtos avatar Jun 05 '14 15:06 bajtos

@BradleyFord From looking at the current code the message of the error will be 'Access Denied' at the moment. So you could look at the message to determine why you get the 401.

Alternatively you could always do a subsequent call to something that every user has access to, like /api/users/{self id}/exists. So if 401 then check if can read myself, if not then go to login screen. This will add latency but will unequivocal as desired.

karlmikko avatar Jun 05 '14 16:06 karlmikko

To be clear, is this the agreed plan?

  1. Provide a global setting (app.json) to set the ACL failure status. 404 / 403 / 401 (default 401)
  2. Provide an onRemoteError hook for the model.
  3. Update/Add unit-tests.

I am willing to have a look at this and provide a patch for point 1.

Looking further at the code onRemoteError should be implemented in strong-remoting. I believe a separate issue should be raised on strong-remoting for this. Maybe as add error. to before. and after. remotes. Looks like it would implemented be in RemoteObjects.prototype.invokeMethodInContext. I don't know how confident I am in making point 2 chage though.

karlmikko avatar Jun 05 '14 16:06 karlmikko

IMO it's not necessary to implement everything at once.

The most important part is to change 401 to 403 and update unit-tests.

The next step is adding a global app.json setting to set the ACL failure status code.

I am willing to have a look at this and provide a patch for point 1.

Unit tests have to be updated as part of the patch, how else will we now the change works correctly?

I suppose the most critical question is this one: should the default code be 401 or 403? If it remains 401, then the existing tests are not affected and you only need to add new tests for the new code. However, if you change the default to 403, many existing tests will start to fail.

Looking further at the code onRemoteError should be implemented in strong-remoting. I believe a separate issue should be raised on strong-remoting for this.

Agreed. Don't feel obliged to implement this feature, it's a "nice to have" at this point.

bajtos avatar Jun 05 '14 17:06 bajtos

https://github.com/strongloop/loopback-testing/pull/12

karlmikko avatar Jun 06 '14 01:06 karlmikko