Skip to main content
https://badge.fury.io/py/django-cookies-samesite.svg https://travis-ci.org/jotes/django-cookies-samesite.svg?branch=master https://codecov.io/gh/jotes/django-cookies-samesite/branch/master/graph/badge.svg

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

Release files for django-cookies-samesite 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-cookies-samesite 0.6.0
File Size Uploaded
django-cookies-samesite-0.6.0.tar.gz 6.8 kB Details

Release files / django-cookies-samesite-0.6.0.tar.gz

Download URL django-cookies-samesite-0.6.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f9251bae56313dbe08e1d40e212eca25682b68fa1f07fbe98444a651a5c34831
BLAKE2b-256 checksum
How to use checksums
63b969c97ea61ed231d18221bcdeec8e9819e124ca8a78748d10735fba097c69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/2.7.15

Release history Release notifications | RSS feed

0.9.0

1 release file

0.8.0

1 release file

0.7.0

2 release files

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

This release

0.6.0 This release

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page