Skip to content
This repository contains a middleware which automatically sets SameSite attribute for session and csrf cookies in legacy versions of Django.
Python HTML Makefile
Branch: master
Clone or download
This branch is 7 commits behind jotes:master.

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
django_cookies_samesite
example
tests
.coveragerc
.editorconfig
.gitignore
.travis.yml
CONTRIBUTING.rst
LICENSE
MANIFEST.in
Makefile
README.rst
manage.py
requirements.txt
requirements_dev.txt
requirements_test.txt
runtests.py
setup.cfg
setup.py
tox.ini

README.rst

django-cookies-samesite

https://travis-ci.org/jotes/django-cookies-samesite.svg?branch=master

Django 2.1.x introduces 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.

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:

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'}

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

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

You can’t perform that action at this time.