Skip to main content

CSRF protection for Django without cookies.

Project description

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:

MIDDLEWARE_CLASSES = (
    ...
    'session_csrf.CsrfMiddleware',
    ...
)

Everything else should be identical to the built-in CSRF protection.

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. You want CSRF protection for anonymous users. django-session-csrf does not create CSRF tokens for anonymous users since we’re worried about the scalability of that.

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-session-csrf-0.1.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file django-session-csrf-0.1.tar.gz.

File metadata

File hashes

Hashes for django-session-csrf-0.1.tar.gz
Algorithm Hash digest
SHA256 0e0b6ee13c056ce3d4961e9d1de4391e67be4e334ea24f8e3dbab2d51fce4aec
MD5 d63ce7e278095ebe44c6abc4b548ffb8
BLAKE2b-256 9c23decd7b61a77e2851512786be15edd5776a86ff26e7764ffc32359d9ffee8

See more details on using hashes here.

Supported by

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