Skip to main content

What is this?

django-session-csrf is an alternative implementation of Django’s CSRF protection that does not use cookies. Instead, it maintains the CSRF token on the server using Django’s session backend. The csrf token must still be included in all POST requests (either with csrfmiddlewaretoken in the form or with the X-CSRFTOKEN header).

Installation

From PyPI:

pip install django-session-csrf

From github:

git clone git://github.com/mozilla/django-session-csrf.git

Replace django.core.context_processors.csrf with session_csrf.context_processor in your TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'session_csrf.context_processor',
    ...
)

Replace django.middleware.csrf.CsrfViewMiddleware with session_csrf.CsrfMiddleware in your MIDDLEWARE_CLASSES and make sure it is listed after the AuthenticationMiddleware:

MIDDLEWARE_CLASSES = (
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
    'session_csrf.CsrfMiddleware',
    ...
)

Then we have to monkeypatch Django to fix the @csrf_protect decorator:

import session_csrf
session_csrf.monkeypatch()

Make sure that’s in something like your root urls.py so the patch gets applied before your views are imported.

Differences from Django

django-session-csrf does not assign CSRF tokens to anonymous users because we don’t want to support a session for every anonymous user. Instead, views that need anonymous forms can be decorated with @anonymous_csrf:

from session_csrf import anonymous_csrf

@anonymous_csrf
def login(request):
    ...

anonymous_csrf uses the cache to give anonymous users a lightweight session. It sends a cookie to uniquely identify the user and stores the CSRF token in the cache. It can be controlled through these settings:

ANON_COOKIE

the name used for the anonymous user’s cookie

Default: anoncsrf

ANON_TIMEOUT

the cache timeout (in seconds) to use for the anonymous CSRF tokens

Default: 60 * 60 * 2 # 2 hours

Note that by default Django uses local-memory caching, which will not work with anonymous CSRF if there is more than one web server thread. To use anonymous CSRF, you must configure a cache that’s shared between web server instances, such as Memcached. See the Django cache documentation for more information.

If you only want a view to have CSRF protection for logged-in users, you can use the anonymous_csrf_exempt decorator. This could be useful if the anonymous view is protected through a CAPTCHA, for example.

from session_csrf import anonymous_csrf_exempt

@anonymous_csrf_exempt
def protected_in_another_way(request):
    ...

If you want all views to have CSRF protection for anonymous users, use the following setting:

ANON_ALWAYS

always provide CSRF protection for anonymous users

Default: False

Why do I want this?

  1. Your site is on a subdomain with other sites that are not under your control, so cookies could come from anywhere.

  2. You’re worried about attackers using Flash to forge HTTP headers.

  3. You’re tired of requiring a Referer header.

Why don’t I want this?

  1. Storing tokens in sessions means you have to hit your session store more often.

  2. It’s a little bit more work to CSRF-protect forms for anonymous users.

Release files for django-session-csrf 0.7.1

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-session-csrf 0.7.1
File Size Uploaded
django-session-csrf-0.7.1.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-session-csrf 0.7.1
File Interpreter ABI Platform
django_session_csrf-0.7.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 19.4 kB

Release files / django-session-csrf-0.7.1.tar.gz

Download URL django-session-csrf-0.7.1.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e17177e6e2e6518ec7ce6693ad10a5c747f8571d09f4cfa9082599334421605d
BLAKE2b-256 checksum
How to use checksums
8bdc4bdbea0f023011d04851afdf271e0f7b1871efb04c18e1e6eea8cc7dc0f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_session_csrf-0.7.1-py2.py3-none-any.whl

Download URL django_session_csrf-0.7.1-py2.py3-none-any.whl
Size 10.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ff8c10e30d312c77fc6a6db7710e22b9383e28c03b7fe958876ca96f39aa6cf2
BLAKE2b-256 checksum
How to use checksums
ea454dea5e300c2349012ae3d49a930bf164fba7a9f1a3ef5d026c652ee3eb59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.7.1 This release

2 release files

0.7.0

2 release files

0.6

2 release files

0.5

1 release file

0.3

1 release file

0.2

1 release file

0.1

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