Skip to main content

Verbatim copy of Django's CSRF middleware, but with more verbose error messages.

Project description

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 5.2's csrf middleware, with changes for verbosity. There were no (meaningful) changes between Django 5.2 and Django 6.0 to that code. So the middleware is compatible with

  • Django 5.2
  • Django 6.0

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

verbose_csrf_middleware-5.2.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

verbose_csrf_middleware-5.2.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file verbose_csrf_middleware-5.2.0.tar.gz.

File metadata

  • Download URL: verbose_csrf_middleware-5.2.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for verbose_csrf_middleware-5.2.0.tar.gz
Algorithm Hash digest
SHA256 01f6ec6e29a2dfeeb7a83a6af56469f5100992ef9ec39f74dc44142cfdf7e862
MD5 b30bfccccbcc0e3245704e647c400c34
BLAKE2b-256 d2e180b2c8316192e3a45406bfa323ce5ef6ee4da7e4d178727e0fe9688cd2a8

See more details on using hashes here.

File details

Details for the file verbose_csrf_middleware-5.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for verbose_csrf_middleware-5.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 154f4b414ce64e52d5d17d7b9dad3a1aca0b0d3ee7eb44e86712833925488ef7
MD5 a44b8635d54802f2c0445814c737b192
BLAKE2b-256 f3466b2059b07165e14991244e72e1d320a51b89fa9c3a1445b5c9d3a9d404a7

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