Skip to main content

django_referer_csrf

This project has 2 goals:

  1. Simplifiy django's CSRF protection so that developers don't need to worry about the token
  2. Make it easier for view code to invoke CSRF protection dynamically

The token isn't needed

https://security.stackexchange.com/a/197269

On https sites, django's csrf protection requires that the request's referer header matches the request's host header. This check makes the entire CSRF token redundant. The token check provides no extra security on top of the referer check.

Django skips the referer check on non-https sites, which is somewhat advantageous (it means that users who configure their web browsers not submit a referer header can still submit forms).

Our validator actually looks for a valid origin header or referer header. Modern browsers follow the (newish) spec, which is to send an origin header with every request other than head/get. This means that if users have the referer header disabled, they can still pass our CSRF check.

Usage

  • pip install django_referer_csrf
  • in your MIDDLEWARE setting, replace django.middleware.csrf.CsrfViewMiddleware with django_referer_csrf.Middleware

With this middleware, you can still use Djangos's csrf_exempt decorators.

If you want to apply the CSRF protection based on dynamic conditions in view code, just check to see what the Middleware.process_view() does and replicate that.

Metadata

Release files for django-referer-csrf 0.3.0

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-referer-csrf 0.3.0
File Size Uploaded Provenance
django_referer_csrf-0.3.0.tar.gz 3.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-referer-csrf 0.3.0
File Interpreter ABI Platform
django_referer_csrf-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / django_referer_csrf-0.3.0.tar.gz

Download URL django_referer_csrf-0.3.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
143706ad7ead6418f11bead9a68890f98cc694abb972e9178f59defaa13b84c3
BLAKE2b-256 checksum
How to use checksums
5c4130b4f8fd898479fd5e3a66579d639377e1583f36bfb70e6cb6c7a3a91faa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.10

Release files / django_referer_csrf-0.3.0-py3-none-any.whl

Download URL django_referer_csrf-0.3.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c2098c31ba1c57f9132d971fa8b50b4320c9d5de472414346eb6cee0434a4d9
BLAKE2b-256 checksum
How to use checksums
c06a8c94372e2fadc8edb3fda3e15858b8e87b51ff8377a0b9e42c053a28b23b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.10

Security for django-referer-csrf 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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