-
Notifications
You must be signed in to change notification settings - Fork 31
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
Include reason in JSON response on CSRF failure #608
base: master
Are you sure you want to change the base?
Conversation
The current response suggests the error unconditionally lies with the CSRF token but there are other reasons that might fail the CSRF validation checks including absent or malformed Referer header when connecting to HTTPS.
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.
Agree that this is a big improvement for debugging failures. Should the error message now be reduced to just "CSRF Error: {reason}"
, since the extra information currently given about including the token and where it needs to be included might not be the cause of the error? Or only include it if reason
is blank (if that can even happen)?
Thanks @knabar, I should add here that ome/openmicroscopy#6419 expands the scope of the CSRF integration tests to cover several of these conditions (missing cookie, missing token, invalid/incorrect token) and check the error message. On the content of the error message, I tried to limit the changes to the message in case someone is depending on its content but I agree it can still be misleading. An option would also be to review and link to the content of https://omero.readthedocs.io/en/stable/developers/Web/CSRF.html. |
Testing as suggested using merge-ci server from home, I always see:
printing the
This is the same whether I include the I'll try setting up local server... |
Testing locally (without https)... Without any CRSF token...
With
With an old invalid token
Missing |
Re #608 (comment), I was able to reproduce the issue while poiting to the OME CI Web deployment. Looking at the actually status code, it is not 403 but 405 i.e. |
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.
Tested again with last commit. Eg. {'message': 'CSRF Failed: CSRF token from POST has incorrect length.'}
looks good.
Tested with HTTPS. Used a valid
Without
With
With
With
With
With matching |
If a POST/PUT/DELETE request is rejected by
CsrfViewMiddleware
, the current body of the HTTP response unconditionally suggests the rejection is due to an invalid CSRF token. However there are other reasons that might cause the rejection of the request such as a missing or malformed Referer header when connecting to HTTPS - see https://docs.djangoproject.com/en/4.2/ref/csrf/ for more details.One way to test these condition is to do a simple connection test using the JSON API e.g. in Python using https://github.com/ome/openmicroscopy/blob/develop/examples/Training/python/Json_Api/Login.py and test various failures conditions including
X-CSRFToken
in the headerX-CSRFToken
in the headerReferer
in the header (note OMERO.web must be deployed on HTTPS)Referer
value in the header (note OMERO.web must be deployed on HTTPS)Without this change the error should be identical in all situation making it quite hard to troubleshoot the issue without having access to the OMERO.web logs where the underlying caused is logged at WARN level.
With this change, the response message should include the failure reason: