Regression in Set-Cookie which affects Django users

578 views
Skip to first unread message

אורי

unread,
Aug 22, 2020, 1:23:34 PM8/22/20
to Django developers (Contributions to Django itself)
Django developers,

I just created issue #31933:

It seems that there is a regression in Set-Cookie in browsers such as Chrome and Dolphin, which affects Django users. SESSION_COOKIE_SAMESITE = None does not work any more with those browsers. This affects all versions of Django, and especially where it's not possible to explicitly set cookies to SameSite=None (Django <= 3.0).

You can read about it in the following links:

Cookies default to SameSite=Lax
Reject insecure SameSite=None cookies

You can see more information in the question I just asked on Stack Overflow.

I think it should be made possible to explicitly set cookies to SameSite=None, also in settings such as SESSION_COOKIE_SAMESITE, and backport it to all working versions of Django.

Adam Johnson

unread,
Aug 22, 2020, 2:34:29 PM8/22/20
to django-d...@googlegroups.com
Hi Uri

You implied it, but to make it explicit - Django 3.1 allows setting the value "None" (string) for samesite cookies: https://docs.djangoproject.com/en/dev/releases/3.1/#django-contrib-sessions . Essentially you're asking for a backport of this feature.

I think a backport is probably reasonable if sites are broken. You didn't write in your ticket in what way SameSite=Lax breaks your sites - can you explain the use cases you need SameSite=None for?

It's also possible to workaround this by using a middleware that's earlier in MIDDLEWARE than e.g. SessionMiddleware to mutate the cookie in response.cookies . The cookie object can have its samesite flag changed with the update() method:

>>> from django.http import HttpResponse
>>> resp = HttpResponse()
>>> resp.set_cookie('foo', 'bar', samesite='Lax')
>>> resp.cookies['foo']
<Morsel: foo=bar; Path=/; SameSite=Lax>
>>> resp.cookies['foo'].update({"samesite": "None"})
>>> resp.cookies["foo"]
<Morsel: foo=bar; Path=/; SameSite=None>


Hope that helps,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CABD5YeFnd0p5WmaLsePKzbeO_pR4xrZ5cE7%2BVgfhzHyjgB7uTw%40mail.gmail.com.


--
Adam

Mariusz Felisiak

unread,
Aug 22, 2020, 2:48:17 PM8/22/20
to Django developers (Contributions to Django itself)
We decided that it's a new feature that will not be backported to Django 3.0, see #30862, and discussion in PR (with few simple workarounds).

Best,
Mariusz

Adam Johnson

unread,
Aug 22, 2020, 3:07:56 PM8/22/20
to django-d...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.


--
Adam

אורי

unread,
Aug 22, 2020, 9:34:26 PM8/22/20
to Django developers (Contributions to Django itself)
On Sat, Aug 22, 2020 at 9:34 PM Adam Johnson <m...@adamj.eu> wrote:
Hi Uri

You implied it, but to make it explicit - Django 3.1 allows setting the value "None" (string) for samesite cookies: https://docs.djangoproject.com/en/dev/releases/3.1/#django-contrib-sessions . Essentially you're asking for a backport of this feature.

Yes. But this may also affect other settings such as CSRF_COOKIE_SAMESITE.

You can also see this answer on Stack Overflow.
 

I think a backport is probably reasonable if sites are broken. You didn't write in your ticket in what way SameSite=Lax breaks your sites - can you explain the use cases you need SameSite=None for?

אורי

unread,
Aug 22, 2020, 9:35:28 PM8/22/20
to Django developers (Contributions to Django itself)
On Sat, Aug 22, 2020 at 9:48 PM Mariusz Felisiak <felisiak...@gmail.com> wrote:
We decided that it's a new feature that will not be backported to Django 3.0, see #30862, and discussion in PR (with few simple workarounds).

These decisions were probably before the breaking changes in Chrome.

אורי.

אורי

unread,
Aug 22, 2020, 9:43:47 PM8/22/20
to Django developers (Contributions to Django itself)
On Sat, Aug 22, 2020 at 10:07 PM Adam Johnson <m...@adamj.eu> wrote:

Thank you. I was not aware of this package and middleware.

אורי.

אורי

unread,
Aug 23, 2020, 12:05:23 AM8/23/20
to Django developers (Contributions to Django itself)
Hi,

I looked at it and I think PR #11894 should be backported to all working versions of Django. It doesn't look like it will introduce new bugs or regressions. All I need is these 2 lines:


if samesite.lower() not in ('lax', 'strict'):
raise ValueError('samesite must be "lax" or "strict".')
if samesite.lower() not in ('lax', 'none', 'strict'):
raise ValueError('samesite must be "lax", "none", or "strict".')

And the relevant documentation. This will allow setting cookies explicitly to SameSite=None. Since Django 2.2 should be supported until 2022, I think it makes sense to backport it to Django 2.2 and 3.0.

On Sat, Aug 22, 2020 at 9:48 PM Mariusz Felisiak <felisiak...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.

Mariusz Felisiak

unread,
Aug 23, 2020, 1:19:02 AM8/23/20
to Django developers (Contributions to Django itself)
It's not about the number of lines but about our backporting policy. We don't backport new features. Moreover Django 2.2 and 3.0 are in extended support. Per our backporting policy this means it doesn't qualify for a backport.

> These decisions were probably before the breaking changes in Chrome.

We were aware of them.

Best,
Mariusz

אורי

unread,
Aug 23, 2020, 8:33:27 AM8/23/20
to Django developers (Contributions to Django itself)
On Sun, Aug 23, 2020 at 8:19 AM Mariusz Felisiak <felisiak...@gmail.com> wrote:
It's not about the number of lines but about our backporting policy. We don't backport new features. Moreover Django 2.2 and 3.0 are in extended support. Per our backporting policy this means it doesn't qualify for a backport.

  OK, I will try to fork Django and backport it myself. Thank you.

Hanne Moa

unread,
Sep 9, 2020, 2:32:55 PM9/9/20
to django-d...@googlegroups.com
django-cookie-samesite has a browser version guesser, because
different browsers interpret samesite differently.

The best solution I've heard of is setting two cookies with two
different names, one the old way and one the google way. Then check
for both where checking needs done, one of them being a fallback.
> --
> You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CABD5YeGTtzHQMrsB%3Dc%3DHBeymNxWFJc1En9vqj%3DF1HEtO5P0odA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages