2

Will a cookie with the HttpOnly and Secure attributes be sent using Fetch API in case {credentials: "include"} is present in options?

fetch("https://some.url", {
  mode: "same-origin",
  credentials: "include",
  redirect: "manual"
})
Shura
  • 507
  • 5
  • 8
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies The answer seems to be, yes. – Chris Apr 10 '22 at 18:11

1 Answers1

1

There are several conditions that have to be met, but yes they are.

  1. Client initializes asynchronously a fetch request with credentials: 'include'. See [here][1] for more details.
  2. To do CORS, server response header must contain Access-Control-Allow-Origin explicitly set to a domain, could be different from the server domain. For example, in a Single-Page-App architecture, your frontend site is temporarily hosted at localhost:3000 and your backend server hosted at localhost:8000, then the header should be Access-Control-Allow-Origin: http://localhost:3000. See [here][2] and [here][3].
  3. To allow client to process cookies, which is obviously a sensitive resource, server response header must further contain Access-Control-Allow-Credentials: true. See [here][4]. Note that this enforces a non-wildcard setting for Access-Control-Allow-Origin. See [here][6] - that's why in point 2 above, it has to be explicitly set to something like http://localhost:3000 rather than *
  4. When server sets the cookie, it has to include SameSite=None; Secure; HttpOnly. So overall something like Set-Cookie: session_id=12345; SameSite=None; Secure; HttpOnly. SameSite seems to be a relatively [new requirement][5] in latest browsers, and must be used with Secure together when SameSite is set to None.
  5. With regard to HttpOnly, I haven't found relevant materials, but in my experiment, omitting it caused the browser to ignore the Set-Cookie header.
  6. Further requests to the backend server also must have credentials: 'include' set.

Source: https://stackoverflow.com/a/67001424/368691

Gajus
  • 69,002
  • 70
  • 275
  • 438