Giter Site home page Giter Site logo

gilgtc / django-cookies-samesite Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jotes/django-cookies-samesite

0.0 0.0 0.0 103 KB

This repository contains a middleware which automatically sets SameSite attribute for session and csrf cookies in legacy versions of Django.

License: BSD 3-Clause "New" or "Revised" License

Makefile 4.09% Python 90.50% HTML 5.41%

django-cookies-samesite's Introduction

django-cookies-samesite

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

Repository status

The project isn't actively maintained anymore. If you want to help and add a link to your fork, add it there: jotes#50 .

Description

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',
    ...
)

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: jotes#19

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

django-cookies-samesite's People

Contributors

jotes avatar abdul-rafiu avatar browniebroke avatar js-clearcode avatar nedbat avatar antoinelb avatar challet avatar jonespm avatar lurimax-north avatar sfdye avatar mahyard avatar unformatt avatar mykolas921 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.