Skip to main content

Verbose CSRF Middleware

This is a verbatim copy of the Django CSRF middleware, but it is more verbose in its failures.

This is especially useful when CSRF failures are happening due to some misconfiguration of your server, your reverse proxy, or some combination thereof.

Django 4.0 introduced various "more strict" CSRF checks, in particular checks on the Origin and Referer header. This middleware can help you debug problems with those checks in your setup.

Full documentaiton

Installation:

pip install verbose_csrf_middleware

In your settings.py file, in the MIDDLEWARE_CLASSES, search for this line:

    'django.middleware.csrf.CsrfViewMiddleware',  # search this to remove it

and then replace it with the line below:

    'verbose_csrf_middleware.CsrfViewMiddleware',

Seeing the output

You'll probably want to see the output of the middleware somewhere. You can either:

  1. Turn on DEBUG
  2. Make sure messages to the logger "django.security.csrf" (level: warning) end up in a location you can read.
  3. Add a template 403_csrf.html to your templates directory. Make sure the template renders "reason".
  4. Add a CSRF_FAILURE_VIEW

Note that optinos 1, 3 and 4 have at least theoretical security implications, because by the nature of "verbose" they expose some information to end-users.

Why is this better?

Compare the below; - is Django's standard message, + is the verbose one. You'll see the latter contains much more useful info.

- Origin checking failed - http://nonmatching does not match any trusted origins.
+ Origin header does not match (deduced) Host: 'http://nonmatching' != 'http://testserver'

- Origin checking failed - https://thisiswrong.example.org does not match any trusted origins.
+ Origin header does not match (deduced) Host: 'https://thisiswrong.example.org' != 'https://testserver'; nor any of the CSRF_TRUSTED_ORIGINS: ['https://subdomain.example.org']

- Origin checking failed - https://anything.example.org does not match any trusted origins.
+ Origin header does not match (deduced) Host: 'https://anything.example.org' != 'https://testserver'; nor any of the CSRF_TRUSTED_ORIGINS: ['http://*.example.org (wrong scheme)']

- Origin checking failed - null does not match any trusted origins.
+ Origin header does not match (deduced) Host: 'null' != 'http://testserver'

- Referer checking failed - https://refererheader.org/ does not match any trusted origins.
+ Referer checking failed - 'refererheader.org' does not match any of ['csrf_trusted_origin.org' (trusted), 'testserver' (host)].

- Referer checking failed - https://www.wrong.org/ does not match any trusted origins.
+ Referer checking failed - 'www.wrong.org' does not match any of ['testserver' (host)].

- Referer checking failed - https://nonmatching.example.org/ does not match any trusted origins.
+ Referer checking failed - 'nonmatching.example.org' does not match any of ['expected.example.org' (session_cookie)].

(this output is generated by running the test suite, but turning on Django's standard middleware)

Compatability

This middleware is a verbatim copy of Django's csrf middleware, with changes for verbosity. For each minor version of Django (from Django 5.2 onwards) a matching version of the middleware is provided.

Release files for verbose-csrf-middleware 6.0.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 verbose-csrf-middleware 6.0.0
File Size Uploaded
verbose_csrf_middleware-6.0.0.tar.gz 17.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for verbose-csrf-middleware 6.0.0
File Interpreter ABI Platform
verbose_csrf_middleware-6.0.0-py3-none-any.whl Python 3 none any Details

Total release size:31.3 kB

Release files / verbose_csrf_middleware-6.0.0.tar.gz

Download URL verbose_csrf_middleware-6.0.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9bd4fee530137cf1005416d8fac4e843df69eb665e95d446b62458d0f421504d
BLAKE2b-256 checksum
How to use checksums
a4443e535cc7d58664d68cb7e341317f5d3e1f7ecdee17079ef3e8e267e963b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / verbose_csrf_middleware-6.0.0-py3-none-any.whl

Download URL verbose_csrf_middleware-6.0.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
900a8dbfd6e67bf533aab8b3e6ae1d0c7c77e8797ea29a6649873e31b104a7f9
BLAKE2b-256 checksum
How to use checksums
61cdb1c691629f8d80be4997d464331bce198374158f6a1385f6d8dd7e469fa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

6.0.0 This release

2 release files

5.2.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

2 release files

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