Skip to content
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

Use same-origin as the default request mode #406

Open
Nisgrak opened this issue Dec 15, 2021 · 1 comment · May be fixed by #602
Open

Use same-origin as the default request mode #406

Nisgrak opened this issue Dec 15, 2021 · 1 comment · May be fixed by #602
Assignees
Labels
enhancement New feature or request

Comments

@Nisgrak
Copy link

Nisgrak commented Dec 15, 2021

If the mode has been set to no-cors the headers won't be sended.

I create a example stackblitz

  1. Open this in other window https://beeceptor.com/console/test-ky (this logs the requests in the stackblitz example)
  2. Open https://stackblitz.com/edit/js-zatpf9?file=index.js
  3. Go to first openend window (beeceptor)
  4. Check the headers of the two request, with-cors have x-api-key header and whitout-cors doesn't

image
Normal mode
image
No-cors mode
image

@sholladay
Copy link
Collaborator

sholladay commented Feb 23, 2022

Thank you for the great reproduction steps. This is not exactly a Ky issue but it's worth exploring.

I say that because I can reproduce the same behavior with just plain fetch, without Ky.

https://stackblitz.com/edit/js-mcdq89?file=index.js

Looking at section 2.2.5 of the fetch standard, I can see why.

https://fetch.spec.whatwg.org/#concept-request-mode

"no-cors"
Restricts requests to using CORS-safelisted methods and CORS-safelisted request-headers. Upon success, fetch will return an opaque filtered response.

So, this is working as intended because the default mode for fetch is 'no-cors' and that option restricts requests to using CORS-safelisted request headers, and x-api-key is not among them.

The only relevant thing that we could perhaps do in Ky would be to use a different default mode.

Interestingly, the fetch standard specifically calls out 'no-cors' as an unsafe mode, so it's definitely worth considering other options.

Even though the default request mode is "no-cors", standards are highly discouraged from using it for new features. It is rather unsafe.

So, should we continue using the same default as fetch or should we diverge here? Personally, I'd be happy for us to use 'same-origin', which causes an explicit error to be thrown for cross origin requests instead of this magical behavior.

@sholladay sholladay changed the title Headers not send with no-cors mode Change the default request mode to avoid CORS confusion Feb 23, 2022
@sholladay sholladay added the enhancement New feature or request label Mar 23, 2022
@sholladay sholladay self-assigned this Jun 25, 2024
@sholladay sholladay changed the title Change the default request mode to avoid CORS confusion Use same-origin as the default request mode Jun 25, 2024
@sholladay sholladay linked a pull request Jun 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants