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

Disambiguate RequestInit object's credentials property where related to the Set-Cookie header #34798

Merged
merged 3 commits into from
Jul 12, 2024

Conversation

amaralis
Copy link
Contributor

Description

I have changed 3 files, adding to existing sentences that mention that the credentials property determine whether the browser sends credentials. These sentences now reflect that this credentials property also affects the browser's respect for the Set-Cookie header.

Motivation

I spent the last day or two debugging an issue with authentication and authorization between an SPA and a back end using Laravel and Sanctum: my browsers weren't storing the CSRF token cookies that the back end sent in Set-Cookie headers when its /csrf-token endpoint was hit. Which resulted in javascript being unable to actually read the token and set it as an X-XSRF-TOKEN request header in subsequent requests, and a complete breakdown of the application.

Up to the 5th page of several google searches, the matter of the browser sending credentials is mentioned. Nowhere I looked did I find a single mention - not one - of the credentials: 'include' request option affecting the browser's respect for the Set-Cookie response header (except in the actual specification linked in the docs for using the fetch api, and even there only as an example). I believe this is caused by overuse of http client libraries that set credentials: 'include' on every fetch request, whether necessary or not. Since I want to avoid that and bloat, I use vanilla javascript as much as possible.

Additional details

See https://fetch.spec.whatwg.org/#cors-protocol-examples, last example:

The developer of foo.invalid returns, now fetching some data from bar.invalid while including credentials. [...] This also makes any Set-Cookie response headers bar.invalid includes fully functional (they are ignored otherwise).

(bold italic mine)

Related issues and pull requests

None to my knowledge

The first paragraph states that the `credentials` property "determines whether or not the browser sends credentials with the request". This fails to address an obscure and major characteristic of the property, which is allowing the browser to store any cookies present in `Set-Cookie` response headers.

See https://fetch.spec.whatwg.org/#cors-protocol-examples last example: "The developer of foo.invalid returns, now fetching some data from bar.invalid while including credentials. [...] This also makes any `Set-Cookie` response headers bar.invalid includes fully functional (they are ignored otherwise)."

I believe this change is fundamentally necessary to help developers like myself, who are facing the issue of the user agent not storing a cookie even though it is present in the response headers list and all CORS rules are being followed. Some http clients do this automatically, but for fine control, manually deciding when to set the `credentials` property to 'include' is necessary.
This description states that the `credentials` property "determines whether or not the browser sends credentials with the request". This fails to address an obscure and major characteristic of the property, which is allowing the browser to store any cookies present in `Set-Cookie` response headers.

See https://fetch.spec.whatwg.org/#cors-protocol-examples last example: "The developer of foo.invalid returns, now fetching some data from bar.invalid while including credentials. [...] This also makes any `Set-Cookie` response headers bar.invalid includes fully functional (they are ignored otherwise)."

I believe this change is fundamentally necessary to help developers like myself, who are facing the issue of the user agent not storing a cookie even though it is present in the response headers list and all CORS rules are being followed. Some http clients do this automatically, but for fine control, manually deciding when to set the `credentials` property to 'include' is necessary.
The second paragraph of this section offers instructions on how to "control whether or not the browser sends credentials". This fails to address an obscure and major characteristic of the property, which is allowing the browser to store any cookies present in `Set-Cookie` response headers.

See https://fetch.spec.whatwg.org/#cors-protocol-examples last example: "The developer of foo.invalid returns, now fetching some data from bar.invalid while including credentials. [...] This also makes any `Set-Cookie` response headers bar.invalid includes fully functional (they are ignored otherwise)."

I believe this characteristic may be what the doc's author meant with "If the server does set the correct headers, then the response, including credentials, will be delivered to the caller". This is the only instance I could find where credentials are mentioned in the context of the server's response, and not just what can be sent by the user agent.
@amaralis amaralis requested a review from a team as a code owner July 12, 2024 06:49
@amaralis amaralis requested review from sideshowbarker and removed request for a team July 12, 2024 06:49
@github-actions github-actions bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Jul 12, 2024
@sideshowbarker sideshowbarker merged commit 09937df into mdn:main Jul 12, 2024
7 checks passed
@sideshowbarker
Copy link
Collaborator

Francisco, thanks much, and congrats on landing your first docs change here — welcome aboard 🎉

Copy link
Contributor

amaralis added a commit to amaralis/mdn-set-cookie that referenced this pull request Jul 13, 2024
Extends the first warning of the article, also related to browser behavior, to alert readers to to a relatively obscure and hard to debug characteristic of the Set-Cookie response header.

Ties together the additions in PR 34798 (mdn#34798), offering the most relevant links to better help developers understand and fix issues if they arise.
bsmth added a commit that referenced this pull request Aug 1, 2024
* Add warning to set-cookie article

Extends the first warning of the article, also related to browser behavior, to alert readers to to a relatively obscure and hard to debug characteristic of the Set-Cookie response header.

Ties together the additions in PR 34798 (#34798), offering the most relevant links to better help developers understand and fix issues if they arise.

* Update files/en-us/web/http/headers/set-cookie/index.md

Remove extra whitespace

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Clarify that behavior applies to CORS requests

* Update files/en-us/web/http/headers/set-cookie/index.md

Co-authored-by: Francisco Amaral <[email protected]>

* Update files/en-us/web/http/headers/set-cookie/index.md

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Brian Thomas Smith <[email protected]>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants