Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use window.fetch() with httpOnly cookies or basic auth

I'm playing around with window.fetch() in Firefox and Chrome. For some reasons, fetch() doesn't send any cookies. Now that wouldn't be a problem, as I can send them using

fetch('/something', { headers: { Cookie: document.cookie } }) 

But this won't work for httpOnly cookies.

like image 702
Damian Senn Avatar asked May 03 '15 11:05

Damian Senn


People also ask

How do you pass cookies in fetch request?

If you want to pass cookies with this request, you can do so by passing the credentials option to the fetch request. fetch("http://example.com/data.json", { credentials: "same-origin" }) . then(response => response. json()) .

Does fetch automatically include cookies?

If you set credentials to same-origin : Fetch will send 1st party cookies to its own server. It will not send cookies to other domains or subdomains. If you set credentials to include : Fetch will continue to send 1st party cookies to its own server.

How do I test HttpOnly cookies?

You can determine whether or not a session cookie is missing the HttpOnly flag by checking the domain against https://securityheaders.com. Alternatively, you can validate with the Google Chrome developer tools when examining the HTTP Response header Set-Cookie.

Are cookies sent with Fetch?

Unless fetch() is called with the credentials option set to include , fetch() : won't send cookies in cross-origin requests. won't set any cookies sent back in cross-origin responses. As of August 2018, the default credentials policy changed to same-origin.


1 Answers

Okay, I found out after reading on the Mozilla Developer Network a bit more and trying out the credentials option.

Looks like the credentials option is what I should have looked for.

fetch('/something', { credentials: 'same-origin' }) // or 'include' 

Will send the cookies.

like image 199
Damian Senn Avatar answered Sep 19 '22 15:09

Damian Senn