Skip to content

Allow for preflight requests with OPTIONS method #14991

Closed

Description

@mountiny

Links to comments explaining the CORS issue

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

  1. locked and limited conversation to collaborators on Feb 9, 2023
  2. unlocked this conversation on Feb 9, 2023
  3. mountiny commented on Feb 9, 2023

    @mountiny
    ContributorAuthor
  4. added
    Weekly
    Daily
    and removed
    Daily
    Weekly
    on Feb 9, 2023
  5. MelvinBot commented on Feb 9, 2023

    @MelvinBot
    Contributor

    Triggered auto assignment to @johnmlee101 (ring0), see https://stackoverflow.com/c/expensify/questions/6102 for more details.

  6. added
    Weekly
    and removed
    Daily
    on Feb 9, 2023
  7. mountiny commented on Feb 9, 2023

    @mountiny
    ContributorAuthor

    👋 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-Token in 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.

  8. 86 remaining items

  9. trjExpensify commented on Oct 20, 2023

    @trjExpensify
    Contributor

    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!

  10. justinpersaud commented on Oct 20, 2023

    @justinpersaud
    Contributor

    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?

  11. trjExpensify commented on Oct 23, 2023

    @trjExpensify
    Contributor

    I'm always hoping for sooner 😆 ! Jests aside, somewhere in the ballpark of weekly to monthly sounds 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.

  12. justinpersaud commented on Oct 23, 2023

    @justinpersaud
    Contributor

    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.

  13. justinpersaud commented on Oct 24, 2023

    @justinpersaud
    Contributor

    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.

  14. justinpersaud commented on Oct 25, 2023

    @justinpersaud
    Contributor

    I submitted a PR to get this into staging/production. Once deployed, maybe someone here can help test this out again and help validate.

  15. changed the title [-][HOLD Server Migration E/E #277714] Allow for preflight requests with OPTIONS method[/-] [+]Allow for preflight requests with OPTIONS method[/+] on Oct 26, 2023
  16. justinpersaud commented on Oct 26, 2023

    @justinpersaud
    Contributor

    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:

    OPTIONS /chat-attachments/8923768874934211380/w_2bbeb8dd4d2843915d9bb8c6c0e085c14dfa8225.png.1024.jpg HTTP/2
    Host: www.expensify.com
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0
    Accept: */*
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate, br
    Access-Control-Request-Method: GET
    Access-Control-Request-Headers: x-chat-attachment-token
    Referer: https://new.expensify.com/
    Origin: https://new.expensify.com
    Connection: keep-alive
    Sec-Fetch-Dest: empty
    Sec-Fetch-Mode: cors
    Sec-Fetch-Site: same-site
    TE: trailers
    

    Response:

    HTTP/2 204 
    date: Thu, 26 Oct 2023 14:19:29 GMT
    access-control-allow-origin: https://new.expensify.com
    access-control-allow-methods: GET, OPTIONS
    access-control-allow-headers: X-Chat-Attachment-Token
    cf-cache-status: DYNAMIC
    set-cookie: __cf_bm=LiO2iiAyH5JuJoh0KH2wVGGlZru0oDRx2b1SAOJzhFk-1698329969-0-AciTxSxU2tcuHgwnMVBDsIO4KLiIrfky8/J4Br4MACcI+cicej0bsangkTWXiE1n4tuwEhU9CHB5Pi/1zLEoGLM=; path=/; expires=Thu, 26-Oct-23 14:49:29 GMT; domain=.expensify.com; HttpOnly; Secure; SameSite=None
    set-cookie: _cfuvid=mLPpEYXWmCK8lOb6vUCUZrFu7pwfDkdIbfLI7MqE3.w-1698329969788-0-604800000; path=/; domain=.expensify.com; HttpOnly; Secure; SameSite=None
    strict-transport-security: max-age=31536000; includeSubDomains; preload
    server: cloudflare
    cf-ray: 81c355a679c939c3-YYZ
    cf-team: 1c0df3dc08000039c35c1cd400000001
    X-Firefox-Spdy: h2
    
  17. added
    Reviewing
    on Oct 26, 2023
  18. trjExpensify commented on Oct 30, 2023

    @trjExpensify
    Contributor

    Niceeeee! @Beamanator @mollfpr perhaps for a second confirmation?

  19. justinpersaud commented on Nov 1, 2023

    @justinpersaud
    Contributor
  20. Beamanator commented on Nov 2, 2023

    @Beamanator
    Contributor

    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!

  21. melvin-bot commented on Dec 7, 2023

    @melvin-bot

    ⚠️ Looks like this issue was linked to a Deploy Blocker here

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

EngineeringInternalMonthlyNewFeatureReviewing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions