Skip to main content

Middleware which sets SameSite flag for session and csrf cookies in legacy versions of Django.

Project description

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

This repository contains a middleware which automatically sets SameSite attribute for session and csrf cookies in legacy versions of Django e.g. 1.11.x, 2.2.x or 3.0.x.

This module is not needed for Django 3.1.x which introduces full support of SameSite flag for session and csrf cookie.

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

The sessionid and csrftoken cookies are automatically handled by the middleware. This behavior can be disabled with:

SESSION_COOKIE_SAMESITE_FORCE_CORE = False
# or
DCS_SESSION_COOKIE_SAMESITE_FORCE_CORE = False

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-cookies-samesite-0.9.0.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file django-cookies-samesite-0.9.0.tar.gz.

File metadata

  • Download URL: django-cookies-samesite-0.9.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.7

File hashes

Hashes for django-cookies-samesite-0.9.0.tar.gz
Algorithm Hash digest
SHA256 08a0c528e252c4ef7796d8edde486062ccf054fe24f6990466ae06ce151976be
MD5 00a755e8734163f8c2eb7e04583b3462
BLAKE2b-256 5b7db1a0f6a4286e96d4958a43f3773d66d7f1eeff59fde0559cb43a6113dd70

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page