Middleware which sets SameSite flag for session and csrf cookies in legacy versions of Django.
Project description
Django 3.1.x introduces full support of SameSite flag for session and csrf cookie.
Unfortunately, this functionality will not be ported to older versions of Django e.g. 1.11.x or 2.2.x
This repository contains a middleware which automatically sets SameSite attribute for session and csrf cookies in legacy versions of Django.
Quickstart
Install django-cookies-samesite:
pip install django-cookies-samesite
Add the middleware to the top of MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
'django_cookies_samesite.middleware.CookiesSameSite',
...
)
Set your preferred SameSite policy in settings.py: .. code-block:: python
SESSION_COOKIE_SAMESITE = ‘lax’ # or DCS_SESSION_COOKIE_SAMESITE = ‘Lax’
This can be ‘Lax’, ‘None’, ‘Strict’, or None to disable the flag. Also, you can set this flag in your custom cookies:
SESSION_COOKIE_SAMESITE_KEYS = {'my-custom-cookies'}
# or
DCS_SESSION_COOKIE_SAMESITE_KEYS = {'my-custom-cookies'}
After that you should be able to see the SameSite flag set for session and csrf cookies.
You can set the SameSite flag on all cookies (even on those coming from third-party Django apps):
SESSION_COOKIE_SAMESITE_FORCE_ALL = True
# or
DCS_SESSION_COOKIE_SAMESITE_FORCE_ALL = True
Running Tests
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install tox (myenv) $ tox
Credits
Tools used in rendering this package:
Contributors
Code Hugger (Matthew Jones) <jonespm@umich.edu>
Jarosław Śmiejczak <poke@jotes.work>
Jørn Lomax <northlomax@gmail.com>
Liuyang Wan <noreply@github.com>
Mykolas Kvieska <noreply@github.com>
Tim McCormack <tmccormack@edx.org>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-cookies-samesite-0.6.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5df9a7af91ad874ab7ee614525d6c1d6f97506e7cd90f6bbb272b0b7ab2928cc |
|
MD5 | 6c2047264669f5a9287f70f562256bb8 |
|
BLAKE2b-256 | 6e26e74b8fe26bef963d9fd9e53f8dbab39a79d352053659b26c83b86b68e5b4 |