-
Notifications
You must be signed in to change notification settings - Fork 13
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
🐛 Bug Report - Catching all exceptions makes error handling and application debugging imposible #42
Comments
Hi @vokimon, thanks for the detailed description. I didn't clearly get your point in your proposal, but I am looking forward to your pull request on your proposal. |
- Unintended errors in code should be 500 - Non Http custom exceptions should be 500 - Handled exceptions should use handler (and be 418 in this case) But all of them are reported as 401 Unauthorized even when the entrypoint does not requires authorization.
I added a case of exception that should be handled: as auth errors: jwt validation errors Using same approach as expiration check, but maybe it should be a HttpException in order to be properly handled both by fastapi and users. Also unexpected errors are not transformed to 500 either on the test setup. Maybe because we are not using Fastapi TestClient.
- Removed the generic error handling in __call__() - Introduced specific error handling inside authenticate() for jwt decoding. - One use of jwt is in token generation in core, but in this case it won't be a authorization error but maybe a configuration one, we should see the details in logging or debugging platforms. - For sure, other authorization errors caught previously as Exception now run on the wild. Review required on that.
@ArtyomVancyan i wrote a draft PR, #44. I need more context from you to complete it. I tried to redo your commit aa8f4b3 without wrapping user code in the error handling. I am quite confident to have moved the Also i handled it in mirror of what the PR #43 did with expirated tokens. But the exception we are throwing is not an HttpException from FastApi and framework default handlers are missing it. |
The
I think you haven't noticed, but the |
You are right I was handling FastApi.HTTPException, a subclass of Starlette's. That's why my handling ignored them. Rest API cases with a PlainText response? weird. Take a look at: If we adhere to what Starlette does I would say that what this library should raise In summary, my proposal would be:
I am about to update de PR with those changes. I'll be away on vacation for some weeks so forgive me if i don't reply further messages until then. |
I looked at the links you provided and agreed with all the changes you have made. I made a few minor changes on the same branch that I won't merge before you return from vacation. I will update the related documentation as well. The main thing I have changed is using |
@vokimon, please let me know once you review my changes on your branch so I know that we have a consensus and I can merge and include them in the upcoming release. |
I am still on vacation and i cannot try them but all your changes makes perfect sense. Thanks for the fixes on coding convention and documentation. You can proceed with the integration. |
Bug description
OAuth2Middleware handles exceptions too widely by catching
Exception
and masking any error raised in user code.Because that middleware wraps the whole application and, more importantly, user code, any programming error in user code, like a missing import, or a misspelled variable will be reported as an exception related to Authentication and a 401 http error is emitted, which is confusing and hard to debug because the usual backtrace is not shown. Common fastapi behaviour is emiting a 500 error and an informative traceback on the console.
This makes development quite hard and mask other expected runtime exceptions you might want to handle in upper levels. My current workaround is to edit the library in my development environment by removing that general exception handler. But, on one hand, that is not an easily replicable environment, and secondly, i guess that we will miss some Auth related problem that the original code actually intended to catch.
I propose wrapping code more selectively, keeping user code outside the wrap, and expecting more specific exceptions than Exception.
fastapi-oauth2/src/fastapi_oauth2/middleware.py
Line 151 in d22bb4e
I use to disable other wide excepts, in paranoid mode, but a shallow look at them i would say they do not interfer with use code. Maybe i am wrong.
fastapi-oauth2/src/fastapi_oauth2/core.py
Line 129 in d22bb4e
fastapi-oauth2/src/fastapi_oauth2/core.py
Line 131 in d22bb4e
Reproduction URL
No response
Reproduction steps
Take the example, in any of the entry points in
router_ssr.py
append a line withboo
(an undefined identifier). You get the error message in the browser, if you are not doing rest, but even if you are authenticated the error is a 401. The normal behaviour, that you get if you remove the wide range except is to have a 500 error and a proper backtrace on the server.Screenshots
Logs
No response
Browsers
Firefox
OS
Linux
The text was updated successfully, but these errors were encountered: