supertokens / supertokens-node

Node SDK for SuperTokens core
https://supertokens.com
Other
278 stars 72 forks source link

Set Cookie Behaviour in Frontend and Backend across browsers #874

Closed anku255 closed 3 days ago

anku255 commented 3 days ago

Backend Cookies ("Set-Cookie" Header)

Case 1:

Response Headers Cookies
image image
  1. Firefox - .example.com
Response Headers Cookies
image image
  1. Safari - .example.com
Response Headers Cookies
image image

Frontend Cookies (document.cookie)

Summary

  1. If the cookie is set using a domain (supertokens.com) then a leading dot is added by the browsers. To remove this cookie the domain must be present in the remove cookie string.
  2. If the cookie is set without a domain then browsers do not add the leading dot. To remove this cookie the domain must not be present in the remove cookie string.

The test is performed on https://supertokens.com using the following code sample:

// Setting cookie with a domain

document.cookie = "foo=bar; expires=Fri, 23 Jun 2025 12:00:00 UTC; path=/; domain=supertokens.com"

// Removing cookie with a domain

document.cookie = "foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=supertokens.com"

// Setting cookie without a domain

document.cookie = "foo=bar; expires=Fri, 23 Jun 2025 12:00:00 UTC; path=/;"

// Removing cookie without a domain

document.cookie = "foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;"

Screen Recording -

https://github.com/supertokens/supertokens-node/assets/22813027/59b515ed-8447-495f-8fc2-05beed0cc5c6

https://github.com/supertokens/supertokens-node/assets/22813027/e2ced156-66cd-4ebf-a2ea-9a5ee2d205ad

https://github.com/supertokens/supertokens-node/assets/22813027/484ed050-8f70-4f8d-8d44-688a576eb1b0

st-website sessionTokenFrontendDomain behaviour

Case 1:

Cookies
image
  1. Firefox - example.com
Cookies
image
  1. Safari - example.com
Cookies
image

Case 2:

Cookies
image
  1. Firefox - example.com
Cookies
image
  1. Safari - example.com
Cookies
image

NOTE:

The behaviour on the st-website (leading dot not getting added by the browsers) can be attributed to the fact that we remove the domain attribute while setting the cookie if sessionTokenFrontendDomain matches the hostname which would be the case if the url is example.com:3000 and the sessionTokenFrontendDomain is example.com. This code can be found here.

Browser Versions

This test used the following browser versions -