Repository navigation
Description
Links to comments explaining the CORS issue
- [HOLD for payment 2023-02-08] [$1000] Backend - Attachment links capture the environment they are from #14227 (comment)
- [HOLD for payment 2023-02-08] [$1000] Backend - Attachment links capture the environment they are from #14227 (comment)
Summary
We are removing the auth token from the URL part of an attachment/image and instead placing it in a HTTP header called X-Chat-Attachment-Token
We're doing this to improve file caching (when the token is part of the URL, the URL changes often and cannot be cached)
As a consequence (for adding this header) browsers start to make preflight (OPTIONS method) requests
(This also applies to Desktop since under the hoods it's using Chromium)
The preflight requests are being made because the API url (we request the image from) is from a different origin
than the place where webapp is hosted
e.g. website is new.expensify.com while API is www.expensify.com
Right now the OPTIONS request, seems unexpected and the API redirects with a response to an error page and we get the following error
Web:
[Error] Preflight response is not successful. Status code: 302
[Error] Fetch API cannot load https://www.expensify.com/chat-attachments/4198796077944938521/w_6f21f50b3af2ee93c4e6e288ba5b986858166367.jpg.1024.jpg due to access control checks.
[Error] Failed to load resource: Preflight response is not successful. Status code: 302 (w_6f21f50b3af2ee93c4e6e288ba5b986858166367.jpg.1024.jpg, line 0)
[Error] Preflight response is not successful. Status code: 302
Desktop:
Access to fetch at 'https://www.expensify.com/chat-attachments/962115596/w_e989bfe450cb8e872f52dac2d082c391f35e2e0a.jpg.1024.jpg' from origin 'http://localhost:8081'
has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
What can we do
I think this needs to be planned internally
We can start a conversation in Slack to get a general consensus on how to deal with the CORS issue
Perhaps the simplest thing would be to update the backend to handle cross origin request coming from these origins
- localhost
- desktop app
- staging url
- prod url
Typically servers support a CORS configuration that can specify to allow and properly respond to request (including preflight) from these origin
Alternatively we can avoid CORS altogether if the API is resolved from the same origin where the app is hosted
this can be a simple proxy that takes requests coming to new.expensify.com/api and resolves them from www.expensify.com/api
Perhaps more alternative exist and someone can propose something better
Upwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~01a085d801f68f616f
- Upwork Job ID: 1628430561761431552
- Last Price Increase: 2023-02-22
Activity
mountiny commentedon Feb 9, 2023
cc @kidroca
MelvinBot commentedon Feb 9, 2023
Triggered auto assignment to @johnmlee101 (
ring0), see https://stackoverflow.com/c/expensify/questions/6102 for more details.mountiny commentedon Feb 9, 2023
👋 I have created this ring0 issue since we will have to update the way we handle requests with attachments so we dont run into CORS issues. I dont have much knowledge on how this can be updated in our backend, I think we need to allow for the OPTIONS request with the
X-Chat-Attachment-Tokenin headers but dont know much about how to execute it.Happy to learn and help you here! Making this issue Weekly as its not a blocker right now but we have some PRs which will need to be on hold until this will be enables in backend.
Thanks! Also @kidroca is probably best to explain the issue if there is anything not clear from the OP.
86 remaining items
trjExpensify commentedon Oct 20, 2023
Thanks, @kidroca for the overview. Super helpful! @justinpersaud, do you a better idea of where we're at now and any suggestions for our next steps? Thanks!
justinpersaud commentedon Oct 20, 2023
Yeah, I have some ideas. Let me put them together and run them by some other infra folks to validate them. What's the priority on this? Is it still monthly or are you hoping for sooner?
trjExpensify commentedon Oct 23, 2023
I'm always hoping for sooner 😆 ! Jests aside, somewhere in the ballpark of
weeklytomonthlysounds about right. It's not attached to a wave, but it unblocks fixing our image upload experience, which isn't really premium as we gear up to get existing customers migrated across.justinpersaud commentedon Oct 23, 2023
Got it. I was able to reproduce the issue using @kidroca's steps. I have an idea on how to solve this and posted it for some feedback https://expensify.slack.com/archives/C094TGUTZ/p1698098986137179
I will try and test it out locally tomorrow and see if it works.
justinpersaud commentedon Oct 24, 2023
I have a working fix for this in my dev environments
https://github.com/Expensify/Expensidev/pull/809
Once reviewed/approved, I'll work on getting it out to staging/production to test further.
justinpersaud commentedon Oct 25, 2023
I submitted a PR to get this into staging/production. Once deployed, maybe someone here can help test this out again and help validate.
[-][HOLD Server Migration E/E #277714] Allow for preflight requests with OPTIONS method[/-][+]Allow for preflight requests with OPTIONS method[/+]justinpersaud commentedon Oct 26, 2023
This is working in staging and production now using the example @kidroca provided.
Can anyone else help confirm and see if we can close this?
Request:
Response:
trjExpensify commentedon Oct 30, 2023
Niceeeee! @Beamanator @mollfpr perhaps for a second confirmation?
justinpersaud commentedon Nov 1, 2023
bump @Beamanator
Beamanator commentedon Nov 2, 2023
Looking at where we left off in this PR, I think the purpose of this issue has been completed so let's close! We'll continue the App work / discussion in #12603. Thanks @justinpersaud & everyone involved!
melvin-bot commentedon Dec 7, 2023
If you are the assigned CME please investigate whether the linked PR caused a regression and leave a comment with the results.
If a regression has occurred and you are the assigned CM follow the instructions here.
If this regression could have been avoided please consider also proposing a recommendation to the PR checklist so that we can avoid it in the future.