-
Notifications
You must be signed in to change notification settings - Fork 0
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
BGDIINF_SB-2420: Fixed browser origin validation and CORS - #patch #47
Conversation
The browser doesn't add the origin header when the request is made from the same origin. However it does always send the 'Sec-Fetch-Site' header telling if the request is from the same origin, cross origin or from user. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Site. Unfortunately this 'Sec-Fetch-Site' header is not supported by Safari ! Moreover we cannot hack the web application to always set the Origin header as most browser don't allow it. So we need to check 2 headers: Sec-Fetch-Site and Origin with a fallback to the Referer for Safari. Although this is usually not an issue for shortlink as it is always used from a different origin, the code has been corrected for completeness and consistency with other services. Also to have the correct CORS header in case of same origin we need to add the correct allowed origin in CORS header which is the same as the request. In case of cross-site then we use the Origin header as allowed origin in CORS. In case where the Origin header is not allowed, we use the request domain as Access-Control-Allow-Origin header. In case of redirect, the `Access-Control-Allow-Origin` header was not set if the origin was not from the allowed origin. Now this header in case of redirect is always set to `*` which tell the client that all origins are allowed for the redirect request.
d0efe5e
to
d988ba5
Compare
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'll have a deeper look at this PR soon.
After a quick look: @pakb could this also help with the origin header problem with service-icons
? Seems so at a first glance..
Yes that is related, we have some issue with headers that we try to solve on the backend side |
{'Sec-Fetch-Site': 'none'}, | ||
{'Sec-Fetch-Site': 'cross-site'}, | ||
) | ||
def test_get_shortlink_redirect_origin_allowed(self, headers): |
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.
would it be useful to also add a test to cover a redirect with wrong headers?
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.
redirect should always be allowed and in this test I think I cover almost all possible headers including regular allowed and also non allowed header for the create.
The browser doesn't add the origin header when the request is made from the
same origin. However it does always send the 'Sec-Fetch-Site' header telling
if the request is from the same origin, cross origin or from user.
See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Site.
Unfortunately this 'Sec-Fetch-Site' header is not supported by Safari !
Moreover we cannot hack the web application to always set the Origin header as
most browser don't allow it.
So we need to check 2 headers: Sec-Fetch-Site and Origin with a fallback to the
Referer for Safari.
Although this is usually not an issue for shortlink as it is always used from a
different origin, the code has been corrected for completeness and consistency
with other services.
Also to have the correct CORS header in case of same origin we need to add the
correct allowed origin in CORS header which is the same as the request. In case
of cross-site then we use the Origin header as allowed origin in CORS. In case
where the Origin header is not allowed, we use the request domain as
Access-Control-Allow-Origin header.
In case of redirect, the
Access-Control-Allow-Origin
header was not setif the origin was not from the allowed origin. Now this header in case of
redirect is always set to
*
which tell the client that all origins areallowed for the redirect request.