Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
django-cookies-samesite
Advanced tools
Middleware which sets SameSite flag for session and csrf cookies in legacy versions of Django.
.. image:: https://badge.fury.io/py/django-cookies-samesite.svg :target: https://badge.fury.io/py/django-cookies-samesite
.. image:: https://travis-ci.org/jotes/django-cookies-samesite.svg?branch=master :target: https://travis-ci.org/jotes/django-cookies-samesite
.. image:: https://codecov.io/gh/jotes/django-cookies-samesite/branch/master/graph/badge.svg :target: https://codecov.io/gh/jotes/django-cookies-samesite
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.
Install django-cookies-samesite::
pip install django-cookies-samesite
Add the middleware to the top of MIDDLEWARE_CLASSES
:
.. code-block:: python
MIDDLEWARE_CLASSES = (
'django_cookies_samesite.middleware.CookiesSameSite',
...
)
.. important::
If you're planning to use this middleware together with the newer version of Django (>3.0.x), and you want to e.g.
set the SameSite attribute to none
, then you have to add DCS_
prefix to all django-cookies-samesite
settings (see examples below).
It will help you to avoid bugs caused by the conflicting settings names.
Related bug:
https://github.com/jotes/django-cookies-samesite/issues/19
Set your preferred SameSite policy in settings.py
:
.. code-block:: python
SESSION_COOKIE_SAMESITE = 'lax'
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:
.. code-block:: python
SESSION_COOKIE_SAMESITE_KEYS = {'my-custom-cookies'}
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):
.. code-block:: python
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:
.. code-block:: python
SESSION_COOKIE_SAMESITE_FORCE_CORE = False
DCS_SESSION_COOKIE_SAMESITE_FORCE_CORE = False
Does the code actually work?
::
source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox
Tools used in rendering this package:
cookiecutter-djangopackage
_.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _cookiecutter-djangopackage
: https://github.com/pydanny/cookiecutter-djangopackage
FAQs
Middleware which sets SameSite flag for session and csrf cookies in legacy versions of Django.
We found that django-cookies-samesite demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.