I have a very specific question that I haven't found within the many questions regarding this topic.
Initially my Set-Cookie headers were blocked. I managed to resolve that, but the Cookies are still not stored and I have no clue as to why.
I have a DRF + React web application which I recently changed from Token Authentication to Session Authentication for server side expiration. In production this works great, upon login I receive two Set-Cookies, a session id and a csrftoken. However, in my development environment, the Set-Cookies are blocked as This Set-Cookie was blocked because it had the 'SameSite=Lax' attribute but came from a cross-site response which was not the repsonse to a top-level navigation..
Cookies without SameSite must be secure in chrome://flags.CORS_ALLOWED_ORIGINS allows Set-Cookie, but somehow an old cookie is set. This does not work for localhost.{withCredentials: true} to the requests, this however does not solve my issue as I do receive the Set-Cookie headers, they're just blocked.Cookie/CORS related Settings.py
# CSRF / CORS / Cookies
ALLOWED_HOSTS = ['*']
CORS_ALLOWED_ORIGINS = [
'http://127.0.0.1:3000',
'http://localhost:3000',
]
CSRF_TRUSTED_ORIGINS = [
'http://localhost:3000',
'http://127.0.0.1:3000',
]
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CSRF_USE_SESSIONS = False
CSRF_COOKIE_HTTPONLY = False # Not accessible by client (not important)
CSRF_COOKIE_AGE = 8 * 3600 # Expires after 8 hr
CSRF_COOKIE_SECURE = False # Only HTTPS
SESSION_COOKIE_HTTPONLY = False # Not accessible by client
SESSION_COOKIE_AGE = 8 * 3600 # Expires after 8 hr
SESSION_COOKIE_SECURE = False # Only HTTPS
Eventually I found the solution, which contains two parts:
Set-Cookie headers aren't blocked by adding 'http://127.0.0.1:3000' to CORS_ALLOWED_ORIGINS.{withCredentials: true}. The CSRF check will fail with old/invalid cookies. However, with the check set to false, the new cookies aren't stored.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With