-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
🐛 Fix exception status codes in security http schemes #2120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Fix exception status codes in security http schemes #2120
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
I was just about to open a similar PR myself, and noticed yours here 😄 In general, the status code for invalid authorization seems to depend on the application. The use case I am building would require 401 for a missing (or malformed, e.g. not But I have seen other status codes being used. 400: If header was malformed. In my opinion, this is bad form. But I've seen it, to make client-side error handling easier; any request parameter - be it header, body, query, whatever - must match this format, and if not, 400 is sent. 400 is then handled client side as something like 403: For example if an auth header was provided, but malformed, you may wish to respond as if it was valid auth, but insufficient scope. This also matches the RFC, in my opinion:
404: E.g. if you wish to obfuscate the endpoint. This was a long winded comment, in order to justify the following: I think the constructor should accept a parameter |
|
I'd like to add my support for this PR. In response to @aksel:
It is true that the quoted definition of 403 fits the scenario you describe. However, the definition of 401 in the same RFC fits it better:
|
I agree. |
|
@gostekk Thanks for putting this change together. Are there plans to merge / release this soon? |
|
Bumping this. Would be great to get this merged. |
|
Also stopping by to support this change! I had conversations about this status code thing this week with colleagues, it would be good to see this get merged. =) |
|
Would love to see this fixed. Any update on this? |
|
Hi @tiangolo! Sorry to tag you but just doing it in case you missed this one, since this PR is supported by a few people already. |
|
Would be nice to have this fixed |
|
Recently, I found this problem. I think it's a good point to separate error codes. When it will be merged to a master branch? |
|
Hello! Pinging in to this PR. We've just had this discussion with colleagues and we agree that the 403 when the auth is missing is not a correct default. We agree all applications have peculiar standards and needs and it's okay to return different codes, but this is about default values, not custom behaviour. We all agree 401 is a better fit in the unauth case instead of a 403. |
arthurio
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it should be 401 instead of 403 as well.
Side question, @tiangolo is there a reason why we are asserting against 401, 403, etc. in the tests instead of HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN?
|
Hey there! Yep, this makes sense and I've been wanting to fix this, actually, since the first moment I was writing it for the first time while reading all the standards and specs. 😅 But here's the thing:
I haven't been able to put the time to figure out what are the correct challenges for each security scheme, with the right spec references, etc. ...only for some of them. That's why I haven't updated the rest, because 403 is not as precise as 401, but it's not wrong. While a 401 without If you (or anyone else) can find the right references to the related specs that define the specific challenges that should be used for each one of those security schemes, I would love to have this in. It could also be independent PRs, assuming you could find only references for one of the schemes but not the others, I could take that in a single PR. |
|
This is now a discussion but I think shoud remain an open issue. |
creyD
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
|
I think RFC 6750 "The OAuth 2.0 Authorization Framework: Bearer Token Usage", specifically section 3 "The WWW-Authenticate Response Header Field" enumerates the required In summary:
An example would be |
…-status-codes' into fix-security-exception-status-codes
|
This pull request has a merge conflict that needs to be resolved. |
|
🫡 Haven't used FastAPI in three years. I can't believe the nostalgia I am feeling, seeing a little quirk from five years ago being addressed haha |
Hi!
TL;DR:
This PR changes exception response status codes in security http schemes from 403_FORBIDDEN to 401_UNAUTHORIZED.
Long
HTTPBearer and HTTPDigest security schemes used as a dependency are returning a 403_FORBIDDEN status on scheme Exception. This should be 401_UNAUTHORIZED status because we can't identify user without token or with invalid credentials.
There's issue mentioning this: #2026