2006-05-09 07:03:08 +08:00
|
|
|
=====================================
|
2006-12-18 11:59:45 +08:00
|
|
|
Cross Site Request Forgery protection
|
2006-05-09 07:03:08 +08:00
|
|
|
=====================================
|
|
|
|
|
2009-10-27 08:36:34 +08:00
|
|
|
.. module:: django.middleware.csrf
|
2008-08-24 06:25:40 +08:00
|
|
|
:synopsis: Protects against Cross Site Request Forgeries
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
The CSRF middleware and template tag provides easy-to-use protection against
|
2009-03-24 07:02:46 +08:00
|
|
|
`Cross Site Request Forgeries`_. This type of attack occurs when a malicious
|
2015-11-15 20:05:15 +08:00
|
|
|
website contains a link, a form button or some JavaScript that is intended to
|
|
|
|
perform some action on your website, using the credentials of a logged-in user
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
who visits the malicious site in their browser. A related type of attack,
|
|
|
|
'login CSRF', where an attacking site tricks a user's browser into logging into
|
|
|
|
a site with someone else's credentials, is also covered.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
2011-05-10 07:45:54 +08:00
|
|
|
The first defense against CSRF attacks is to ensure that GET requests (and other
|
2016-05-02 20:35:05 +08:00
|
|
|
'safe' methods, as defined by :rfc:`7231#section-4.2.1`) are side effect free.
|
|
|
|
Requests via 'unsafe' methods, such as POST, PUT, and DELETE, can then be
|
|
|
|
protected by following the steps below.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
.. _Cross Site Request Forgeries: https://www.squarefree.com/securitytips/web-developers.html#CSRF
|
2006-05-09 07:03:08 +08:00
|
|
|
|
2011-06-10 23:14:36 +08:00
|
|
|
.. _using-csrf:
|
|
|
|
|
2006-05-09 07:03:08 +08:00
|
|
|
How to use it
|
|
|
|
=============
|
2006-12-18 11:59:45 +08:00
|
|
|
|
2014-11-01 06:39:46 +08:00
|
|
|
To take advantage of CSRF protection in your views, follow these steps:
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. The CSRF middleware is activated by default in the :setting:`MIDDLEWARE`
|
2015-11-07 23:12:37 +08:00
|
|
|
setting. If you override that setting, remember that
|
|
|
|
``'django.middleware.csrf.CsrfViewMiddleware'`` should come before any view
|
|
|
|
middleware that assume that CSRF attacks have been dealt with.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2014-11-01 06:39:46 +08:00
|
|
|
If you disabled it, which is not recommended, you can use
|
2011-10-14 08:12:01 +08:00
|
|
|
:func:`~django.views.decorators.csrf.csrf_protect` on particular views
|
|
|
|
you want to protect (see below).
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. In any template that uses a POST form, use the :ttag:`csrf_token` tag inside
|
2016-06-03 05:08:30 +08:00
|
|
|
the ``<form>`` element if the form is for an internal URL, e.g.:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2018-05-02 21:20:04 +08:00
|
|
|
<form method="post">{% csrf_token %}
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
This should not be done for POST forms that target external URLs, since
|
|
|
|
that would cause the CSRF token to be leaked, leading to a vulnerability.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. In the corresponding view functions, ensure that
|
2015-12-22 23:21:24 +08:00
|
|
|
:class:`~django.template.RequestContext` is used to render the response so
|
|
|
|
that ``{% csrf_token %}`` will work properly. If you're using the
|
|
|
|
:func:`~django.shortcuts.render` function, generic views, or contrib apps,
|
|
|
|
you are covered already since these all use ``RequestContext``.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2011-02-22 19:27:58 +08:00
|
|
|
.. _csrf-ajax:
|
|
|
|
|
2011-02-09 10:06:27 +08:00
|
|
|
AJAX
|
|
|
|
----
|
|
|
|
|
|
|
|
While the above method can be used for AJAX POST requests, it has some
|
|
|
|
inconveniences: you have to remember to pass the CSRF token in as POST data with
|
|
|
|
every POST request. For this reason, there is an alternative method: on each
|
2018-10-19 07:44:15 +08:00
|
|
|
XMLHttpRequest, set a custom ``X-CSRFToken`` header (as specified by the
|
|
|
|
:setting:`CSRF_HEADER_NAME` setting) to the value of the CSRF token. This is
|
|
|
|
often easier because many JavaScript frameworks provide hooks that allow
|
|
|
|
headers to be set on every request.
|
2012-09-01 18:01:36 +08:00
|
|
|
|
2016-07-01 00:42:11 +08:00
|
|
|
First, you must get the CSRF token. How to do that depends on whether or not
|
2018-10-25 04:22:57 +08:00
|
|
|
the :setting:`CSRF_USE_SESSIONS` and :setting:`CSRF_COOKIE_HTTPONLY` settings
|
|
|
|
are enabled.
|
2016-07-01 00:42:11 +08:00
|
|
|
|
2018-10-25 04:22:57 +08:00
|
|
|
.. _acquiring-csrf-token-from-cookie:
|
|
|
|
|
|
|
|
Acquiring the token if :setting:`CSRF_USE_SESSIONS` and :setting:`CSRF_COOKIE_HTTPONLY` are ``False``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-07-01 00:42:11 +08:00
|
|
|
|
|
|
|
The recommended source for the token is the ``csrftoken`` cookie, which will be
|
|
|
|
set if you've enabled CSRF protection for your views as outlined above.
|
2012-09-01 18:01:36 +08:00
|
|
|
|
2018-10-19 07:44:15 +08:00
|
|
|
The CSRF token cookie is named ``csrftoken`` by default, but you can control
|
|
|
|
the cookie name via the :setting:`CSRF_COOKIE_NAME` setting.
|
2015-02-22 05:57:02 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
You can acquire the token like this:
|
2011-02-09 10:06:27 +08:00
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
2012-09-01 18:01:36 +08:00
|
|
|
function getCookie(name) {
|
2020-06-24 17:41:10 +08:00
|
|
|
let cookieValue = null;
|
2016-06-29 00:51:51 +08:00
|
|
|
if (document.cookie && document.cookie !== '') {
|
2020-06-24 17:41:10 +08:00
|
|
|
const cookies = document.cookie.split(';');
|
|
|
|
for (let i = 0; i < cookies.length; i++) {
|
|
|
|
const cookie = cookies[i].trim();
|
2012-09-01 18:01:36 +08:00
|
|
|
// Does this cookie string begin with the name we want?
|
2016-06-29 00:51:51 +08:00
|
|
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
2012-09-01 18:01:36 +08:00
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
|
|
break;
|
2011-02-09 10:06:27 +08:00
|
|
|
}
|
|
|
|
}
|
2011-05-10 03:06:57 +08:00
|
|
|
}
|
2012-09-01 18:01:36 +08:00
|
|
|
return cookieValue;
|
|
|
|
}
|
2020-06-24 17:41:10 +08:00
|
|
|
const csrftoken = getCookie('csrftoken');
|
2012-09-01 18:01:36 +08:00
|
|
|
|
2015-08-19 20:29:02 +08:00
|
|
|
The above code could be simplified by using the `JavaScript Cookie library
|
|
|
|
<https://github.com/js-cookie/js-cookie/>`_ to replace ``getCookie``:
|
2012-09-01 18:01:36 +08:00
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
2020-06-24 17:41:10 +08:00
|
|
|
const csrftoken = Cookies.get('csrftoken');
|
2012-09-01 18:01:36 +08:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The CSRF token is also present in the DOM, but only if explicitly included
|
|
|
|
using :ttag:`csrf_token` in a template. The cookie contains the canonical
|
|
|
|
token; the ``CsrfViewMiddleware`` will prefer the cookie to the token in
|
|
|
|
the DOM. Regardless, you're guaranteed to have the cookie if the token is
|
|
|
|
present in the DOM, so you should use the cookie!
|
2011-05-10 03:06:57 +08:00
|
|
|
|
2012-09-01 18:01:36 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
If your view is not rendering a template containing the :ttag:`csrf_token`
|
|
|
|
template tag, Django might not set the CSRF token cookie. This is common in
|
|
|
|
cases where forms are dynamically added to the page. To address this case,
|
|
|
|
Django provides a view decorator which forces setting of the cookie:
|
|
|
|
:func:`~django.views.decorators.csrf.ensure_csrf_cookie`.
|
|
|
|
|
2018-10-25 04:22:57 +08:00
|
|
|
.. _acquiring-csrf-token-from-html:
|
|
|
|
|
|
|
|
Acquiring the token if :setting:`CSRF_USE_SESSIONS` or :setting:`CSRF_COOKIE_HTTPONLY` is ``True``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-07-01 00:42:11 +08:00
|
|
|
|
2018-10-25 04:22:57 +08:00
|
|
|
If you activate :setting:`CSRF_USE_SESSIONS` or
|
|
|
|
:setting:`CSRF_COOKIE_HTTPONLY`, you must include the CSRF token in your HTML
|
|
|
|
and read the token from the DOM with JavaScript:
|
2016-07-01 00:42:11 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% csrf_token %}
|
2019-12-11 16:49:54 +08:00
|
|
|
<script>
|
2020-06-24 17:41:10 +08:00
|
|
|
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
|
2016-07-01 00:42:11 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
Setting the token on the AJAX request
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2019-12-12 23:34:03 +08:00
|
|
|
Finally, you'll need to set the header on your AJAX request. Using the
|
|
|
|
`fetch()`_ API:
|
2011-02-09 10:06:27 +08:00
|
|
|
|
2012-09-01 18:01:36 +08:00
|
|
|
.. code-block:: javascript
|
|
|
|
|
2020-06-24 17:41:10 +08:00
|
|
|
const request = new Request(
|
2019-12-12 23:34:03 +08:00
|
|
|
/* URL */,
|
2021-09-13 12:36:27 +08:00
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {'X-CSRFToken': csrftoken},
|
|
|
|
mode: 'same-origin' // Do not send CSRF token to another domain.
|
|
|
|
}
|
2019-12-12 23:34:03 +08:00
|
|
|
);
|
2021-09-13 12:36:27 +08:00
|
|
|
fetch(request).then(function(response) {
|
2019-12-12 23:34:03 +08:00
|
|
|
// ...
|
2012-09-01 18:01:36 +08:00
|
|
|
});
|
|
|
|
|
2019-12-12 23:34:03 +08:00
|
|
|
.. _fetch(): https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
2016-01-14 22:04:38 +08:00
|
|
|
|
2016-06-03 02:05:57 +08:00
|
|
|
Using CSRF in Jinja2 templates
|
|
|
|
------------------------------
|
2011-12-30 22:55:44 +08:00
|
|
|
|
2016-06-03 02:05:57 +08:00
|
|
|
Django's :class:`~django.template.backends.jinja2.Jinja2` template backend
|
|
|
|
adds ``{{ csrf_input }}`` to the context of all templates which is equivalent
|
|
|
|
to ``{% csrf_token %}`` in the Django template language. For example:
|
2011-12-30 22:55:44 +08:00
|
|
|
|
2016-06-03 02:05:57 +08:00
|
|
|
.. code-block:: html+jinja
|
2011-12-30 22:55:44 +08:00
|
|
|
|
2018-05-02 21:20:04 +08:00
|
|
|
<form method="post">{{ csrf_input }}
|
2011-12-30 22:55:44 +08:00
|
|
|
|
2009-11-03 22:40:37 +08:00
|
|
|
The decorator method
|
|
|
|
--------------------
|
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
.. module:: django.views.decorators.csrf
|
|
|
|
|
2009-11-03 22:40:37 +08:00
|
|
|
Rather than adding ``CsrfViewMiddleware`` as a blanket protection, you can use
|
|
|
|
the ``csrf_protect`` decorator, which has exactly the same functionality, on
|
2011-05-10 02:27:52 +08:00
|
|
|
particular views that need the protection. It must be used **both** on views
|
2009-11-03 22:40:37 +08:00
|
|
|
that insert the CSRF token in the output, and on those that accept the POST form
|
2011-05-10 02:27:52 +08:00
|
|
|
data. (These are often the same view function, but not always).
|
2009-11-03 22:40:37 +08:00
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
Use of the decorator by itself is **not recommended**, since if you forget to
|
|
|
|
use it, you will have a security hole. The 'belt and braces' strategy of using
|
|
|
|
both is fine, and will incur minimal overhead.
|
2009-11-03 22:40:37 +08:00
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
.. function:: csrf_protect(view)
|
2009-11-03 22:40:37 +08:00
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
Decorator that provides the protection of ``CsrfViewMiddleware`` to a view.
|
2009-11-03 22:40:37 +08:00
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
Usage::
|
|
|
|
|
|
|
|
from django.shortcuts import render
|
2018-05-13 01:37:42 +08:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
2011-05-10 02:27:52 +08:00
|
|
|
|
|
|
|
@csrf_protect
|
|
|
|
def my_view(request):
|
|
|
|
c = {}
|
|
|
|
# ...
|
|
|
|
return render(request, "a_template.html", c)
|
2008-12-08 12:15:19 +08:00
|
|
|
|
2014-11-15 20:15:56 +08:00
|
|
|
If you are using class-based views, you can refer to
|
|
|
|
:ref:`Decorating class-based views<decorating-class-based-views>`.
|
|
|
|
|
2016-06-03 08:24:48 +08:00
|
|
|
.. _csrf-rejected-requests:
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
Rejected requests
|
|
|
|
=================
|
|
|
|
|
|
|
|
By default, a '403 Forbidden' response is sent to the user if an incoming
|
|
|
|
request fails the checks performed by ``CsrfViewMiddleware``. This should
|
|
|
|
usually only be seen when there is a genuine Cross Site Request Forgery, or
|
|
|
|
when, due to a programming error, the CSRF token has not been included with a
|
|
|
|
POST form.
|
2009-02-08 01:47:02 +08:00
|
|
|
|
2011-04-20 19:39:10 +08:00
|
|
|
The error page, however, is not very friendly, so you may want to provide your
|
2019-06-17 22:54:55 +08:00
|
|
|
own view for handling this condition. To do this, set the
|
2011-05-10 07:00:10 +08:00
|
|
|
:setting:`CSRF_FAILURE_VIEW` setting.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
2016-06-03 08:24:48 +08:00
|
|
|
CSRF failures are logged as warnings to the :ref:`django.security.csrf
|
|
|
|
<django-security-logger>` logger.
|
|
|
|
|
2011-06-10 23:14:36 +08:00
|
|
|
.. _how-csrf-works:
|
|
|
|
|
2006-05-09 07:03:08 +08:00
|
|
|
How it works
|
|
|
|
============
|
2006-12-18 11:59:45 +08:00
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
The CSRF protection is based on the following things:
|
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. A CSRF cookie that is based on a random secret value, which other sites
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
will not have access to.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
This cookie is set by ``CsrfViewMiddleware``. It is sent with every
|
|
|
|
response that has called ``django.middleware.csrf.get_token()`` (the
|
|
|
|
function used internally to retrieve the CSRF token), if it wasn't already
|
|
|
|
set on the request.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
In order to protect against `BREACH`_ attacks, the token is not simply the
|
2020-02-25 21:16:19 +08:00
|
|
|
secret; a random mask is prepended to the secret and used to scramble it.
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
|
|
|
|
For security reasons, the value of the secret is changed each time a
|
2016-04-03 17:35:24 +08:00
|
|
|
user logs in.
|
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. A hidden form field with the name 'csrfmiddlewaretoken' present in all
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
outgoing POST forms. The value of this field is, again, the value of the
|
2020-02-25 21:16:19 +08:00
|
|
|
secret, with a mask which is both added to it and used to scramble it. The
|
|
|
|
mask is regenerated on every call to ``get_token()`` so that the form field
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
value is changed in every such response.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
2011-03-31 01:34:26 +08:00
|
|
|
This part is done by the template tag.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. For all incoming requests that are not using HTTP GET, HEAD, OPTIONS or
|
2011-05-10 07:45:54 +08:00
|
|
|
TRACE, a CSRF cookie must be present, and the 'csrfmiddlewaretoken' field
|
|
|
|
must be present and correct. If it isn't, the user will get a 403 error.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
When validating the 'csrfmiddlewaretoken' field value, only the secret,
|
|
|
|
not the full token, is compared with the secret in the cookie value.
|
|
|
|
This allows the use of ever-changing tokens. While each request may use its
|
|
|
|
own token, the secret remains common to all.
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
This check is done by ``CsrfViewMiddleware``.
|
|
|
|
|
2021-01-03 07:46:17 +08:00
|
|
|
#. ``CsrfViewMiddleware`` verifies the `Origin header`_, if provided by the
|
|
|
|
browser, against the current host and the :setting:`CSRF_TRUSTED_ORIGINS`
|
|
|
|
setting. This provides protection against cross-subdomain attacks.
|
|
|
|
|
|
|
|
#. In addition, for HTTPS requests, if the ``Origin`` header isn't provided,
|
|
|
|
``CsrfViewMiddleware`` performs strict referer checking. This means that
|
|
|
|
even if a subdomain can set or modify cookies on your domain, it can't force
|
|
|
|
a user to post to your application since that request won't come from your
|
|
|
|
own exact domain.
|
2016-02-04 02:29:45 +08:00
|
|
|
|
|
|
|
This also addresses a man-in-the-middle attack that's possible under HTTPS
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
when using a session independent secret, due to the fact that HTTP
|
2016-02-04 02:29:45 +08:00
|
|
|
``Set-Cookie`` headers are (unfortunately) accepted by clients even when
|
|
|
|
they are talking to a site under HTTPS. (Referer checking is not done for
|
|
|
|
HTTP requests because the presence of the ``Referer`` header isn't reliable
|
|
|
|
enough under HTTP.)
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2015-03-17 17:52:55 +08:00
|
|
|
If the :setting:`CSRF_COOKIE_DOMAIN` setting is set, the referer is compared
|
2020-10-06 16:12:04 +08:00
|
|
|
against it. You can allow cross-subdomain requests by including a leading
|
|
|
|
dot. For example, ``CSRF_COOKIE_DOMAIN = '.example.com'`` will allow POST
|
|
|
|
requests from ``www.example.com`` and ``api.example.com``. If the setting is
|
|
|
|
not set, then the referer must match the HTTP ``Host`` header.
|
2015-03-17 17:52:55 +08:00
|
|
|
|
|
|
|
Expanding the accepted referers beyond the current host or cookie domain can
|
|
|
|
be done with the :setting:`CSRF_TRUSTED_ORIGINS` setting.
|
|
|
|
|
2021-01-03 07:46:17 +08:00
|
|
|
.. versionadded:: 4.0
|
|
|
|
|
|
|
|
``Origin`` checking was added, as described above.
|
|
|
|
|
2015-03-17 17:52:55 +08:00
|
|
|
This ensures that only forms that have originated from trusted domains can be
|
|
|
|
used to POST data back.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
2011-05-10 07:45:54 +08:00
|
|
|
It deliberately ignores GET requests (and other requests that are defined as
|
2019-11-23 20:42:57 +08:00
|
|
|
'safe' by :rfc:`7231#section-4.2.1`). These requests ought never to have any
|
|
|
|
potentially dangerous side effects, and so a CSRF attack with a GET request
|
|
|
|
ought to be harmless. :rfc:`7231#section-4.2.1` defines POST, PUT, and DELETE
|
|
|
|
as 'unsafe', and all other methods are also assumed to be unsafe, for maximum
|
|
|
|
protection.
|
2016-02-04 02:29:45 +08:00
|
|
|
|
|
|
|
The CSRF protection cannot protect against man-in-the-middle attacks, so use
|
|
|
|
:ref:`HTTPS <security-recommendation-ssl>` with
|
|
|
|
:ref:`http-strict-transport-security`. It also assumes :ref:`validation of
|
|
|
|
the HOST header <host-headers-virtual-hosting>` and that there aren't any
|
|
|
|
:ref:`cross-site scripting vulnerabilities <cross-site-scripting>` on your site
|
|
|
|
(because XSS vulnerabilities already let an attacker do anything a CSRF
|
|
|
|
vulnerability allows and much worse).
|
2007-08-16 22:09:41 +08:00
|
|
|
|
2017-05-25 07:36:45 +08:00
|
|
|
.. admonition:: Removing the ``Referer`` header
|
|
|
|
|
|
|
|
To avoid disclosing the referrer URL to third-party sites, you might want
|
|
|
|
to `disable the referer`_ on your site's ``<a>`` tags. For example, you
|
|
|
|
might use the ``<meta name="referrer" content="no-referrer">`` tag or
|
|
|
|
include the ``Referrer-Policy: no-referrer`` header. Due to the CSRF
|
|
|
|
protection's strict referer checking on HTTPS requests, those techniques
|
|
|
|
cause a CSRF failure on requests with 'unsafe' methods. Instead, use
|
|
|
|
alternatives like ``<a rel="noreferrer" ...>"`` for links to third-party
|
|
|
|
sites.
|
|
|
|
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
.. _BREACH: http://breachattack.com/
|
2021-01-03 07:46:17 +08:00
|
|
|
.. _Origin header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin
|
2017-05-25 07:36:45 +08:00
|
|
|
.. _disable the referer: https://www.w3.org/TR/referrer-policy/#referrer-policy-delivery
|
Fixed #20869 -- made CSRF tokens change every request by salt-encrypting them
Note that the cookie is not changed every request, just the token retrieved
by the `get_token()` method (used also by the `{% csrf_token %}` tag).
While at it, made token validation strict: Where, before, any length was
accepted and non-ASCII chars were ignored, we now treat anything other than
`[A-Za-z0-9]{64}` as invalid (except for 32-char tokens, which, for
backwards-compatibility, are accepted and replaced by 64-char ones).
Thanks Trac user patrys for reporting, github user adambrenecki
for initial patch, Tim Graham for help, and Curtis Maloney,
Collin Anderson, Florian Apolloner, Markus Holtermann & Jon Dufresne
for reviews.
2015-11-08 00:35:45 +08:00
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
Caching
|
|
|
|
=======
|
|
|
|
|
2011-03-31 01:34:26 +08:00
|
|
|
If the :ttag:`csrf_token` template tag is used by a template (or the
|
|
|
|
``get_token`` function is called some other way), ``CsrfViewMiddleware`` will
|
|
|
|
add a cookie and a ``Vary: Cookie`` header to the response. This means that the
|
|
|
|
middleware will play well with the cache middleware if it is used as instructed
|
|
|
|
(``UpdateCacheMiddleware`` goes before all other middleware).
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
|
|
|
However, if you use cache decorators on individual views, the CSRF middleware
|
2011-06-11 00:18:40 +08:00
|
|
|
will not yet have been able to set the Vary header or the CSRF cookie, and the
|
|
|
|
response will be cached without either one. In this case, on any views that
|
|
|
|
will require a CSRF token to be inserted you should use the
|
|
|
|
:func:`django.views.decorators.csrf.csrf_protect` decorator first::
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
|
|
|
from django.views.decorators.cache import cache_page
|
2011-06-11 00:18:40 +08:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
|
|
|
@cache_page(60 * 15)
|
2011-06-11 00:18:40 +08:00
|
|
|
@csrf_protect
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
def my_view(request):
|
2014-11-15 20:15:56 +08:00
|
|
|
...
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2014-11-15 20:15:56 +08:00
|
|
|
If you are using class-based views, you can refer to :ref:`Decorating
|
|
|
|
class-based views<decorating-class-based-views>`.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
|
|
|
Testing
|
|
|
|
=======
|
|
|
|
|
|
|
|
The ``CsrfViewMiddleware`` will usually be a big hindrance to testing view
|
|
|
|
functions, due to the need for the CSRF token which must be sent with every POST
|
|
|
|
request. For this reason, Django's HTTP client for tests has been modified to
|
|
|
|
set a flag on requests which relaxes the middleware and the ``csrf_protect``
|
|
|
|
decorator so that they no longer rejects requests. In every other respect
|
|
|
|
(e.g. sending cookies etc.), they behave the same.
|
|
|
|
|
2010-08-27 21:54:13 +08:00
|
|
|
If, for some reason, you *want* the test client to perform CSRF
|
|
|
|
checks, you can create an instance of the test client that enforces
|
|
|
|
CSRF checks::
|
|
|
|
|
|
|
|
>>> from django.test import Client
|
|
|
|
>>> csrf_client = Client(enforce_csrf_checks=True)
|
|
|
|
|
2011-05-10 06:59:52 +08:00
|
|
|
.. _csrf-limitations:
|
|
|
|
|
2006-05-09 07:03:08 +08:00
|
|
|
Limitations
|
|
|
|
===========
|
2006-12-18 11:59:45 +08:00
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
Subdomains within a site will be able to set cookies on the client for the whole
|
|
|
|
domain. By setting the cookie and using a corresponding token, subdomains will
|
|
|
|
be able to circumvent the CSRF protection. The only way to avoid this is to
|
|
|
|
ensure that subdomains are controlled by trusted users (or, are at least unable
|
|
|
|
to set cookies). Note that even without CSRF, there are other vulnerabilities,
|
|
|
|
such as session fixation, that make giving subdomains to untrusted parties a bad
|
|
|
|
idea, and these vulnerabilities cannot easily be fixed with current browsers.
|
|
|
|
|
2011-05-10 02:27:36 +08:00
|
|
|
Edge cases
|
|
|
|
==========
|
|
|
|
|
|
|
|
Certain views can have unusual requirements that mean they don't fit the normal
|
|
|
|
pattern envisaged here. A number of utilities can be useful in these
|
|
|
|
situations. The scenarios they might be needed in are described in the following
|
|
|
|
section.
|
|
|
|
|
|
|
|
Utilities
|
|
|
|
---------
|
|
|
|
|
2014-11-15 20:15:56 +08:00
|
|
|
The examples below assume you are using function-based views. If you
|
|
|
|
are working with class-based views, you can refer to :ref:`Decorating
|
|
|
|
class-based views<decorating-class-based-views>`.
|
|
|
|
|
2011-05-10 02:27:45 +08:00
|
|
|
.. function:: csrf_exempt(view)
|
|
|
|
|
|
|
|
This decorator marks a view as being exempt from the protection ensured by
|
|
|
|
the middleware. Example::
|
|
|
|
|
2013-05-19 18:22:40 +08:00
|
|
|
from django.http import HttpResponse
|
2018-05-13 01:37:42 +08:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2011-05-10 02:27:45 +08:00
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def my_view(request):
|
|
|
|
return HttpResponse('Hello world')
|
|
|
|
|
2011-05-10 02:27:36 +08:00
|
|
|
.. function:: requires_csrf_token(view)
|
|
|
|
|
|
|
|
Normally the :ttag:`csrf_token` template tag will not work if
|
|
|
|
``CsrfViewMiddleware.process_view`` or an equivalent like ``csrf_protect``
|
|
|
|
has not run. The view decorator ``requires_csrf_token`` can be used to
|
|
|
|
ensure the template tag does work. This decorator works similarly to
|
|
|
|
``csrf_protect``, but never rejects an incoming request.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
from django.shortcuts import render
|
2018-05-13 01:37:42 +08:00
|
|
|
from django.views.decorators.csrf import requires_csrf_token
|
2011-05-10 02:27:36 +08:00
|
|
|
|
|
|
|
@requires_csrf_token
|
|
|
|
def my_view(request):
|
|
|
|
c = {}
|
|
|
|
# ...
|
|
|
|
return render(request, "a_template.html", c)
|
|
|
|
|
2011-05-10 05:35:24 +08:00
|
|
|
.. function:: ensure_csrf_cookie(view)
|
|
|
|
|
|
|
|
This decorator forces a view to send the CSRF cookie.
|
|
|
|
|
2011-05-10 02:27:36 +08:00
|
|
|
Scenarios
|
|
|
|
---------
|
|
|
|
|
2011-05-10 02:27:45 +08:00
|
|
|
CSRF protection should be disabled for just a few views
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Most views requires CSRF protection, but a few do not.
|
|
|
|
|
|
|
|
Solution: rather than disabling the middleware and applying ``csrf_protect`` to
|
|
|
|
all the views that need it, enable the middleware and use
|
|
|
|
:func:`~django.views.decorators.csrf.csrf_exempt`.
|
|
|
|
|
2011-05-10 02:27:36 +08:00
|
|
|
CsrfViewMiddleware.process_view not used
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-02-27 22:40:27 +08:00
|
|
|
There are cases when ``CsrfViewMiddleware.process_view`` may not have run
|
2011-11-19 18:53:26 +08:00
|
|
|
before your view is run - 404 and 500 handlers, for example - but you still
|
|
|
|
need the CSRF token in a form.
|
2011-05-10 02:27:36 +08:00
|
|
|
|
2011-05-10 02:27:45 +08:00
|
|
|
Solution: use :func:`~django.views.decorators.csrf.requires_csrf_token`
|
2011-05-10 02:27:36 +08:00
|
|
|
|
|
|
|
Unprotected view needs the CSRF token
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
There may be some views that are unprotected and have been exempted by
|
|
|
|
``csrf_exempt``, but still need to include the CSRF token.
|
|
|
|
|
2011-05-10 02:27:45 +08:00
|
|
|
Solution: use :func:`~django.views.decorators.csrf.csrf_exempt` followed by
|
2011-05-10 07:00:02 +08:00
|
|
|
:func:`~django.views.decorators.csrf.requires_csrf_token`. (i.e. ``requires_csrf_token``
|
|
|
|
should be the innermost decorator).
|
2011-05-10 02:27:36 +08:00
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
View needs protection for one path
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2014-01-30 00:54:02 +08:00
|
|
|
A view needs CSRF protection under one set of conditions only, and mustn't have
|
2011-05-10 02:27:52 +08:00
|
|
|
it for the rest of the time.
|
|
|
|
|
|
|
|
Solution: use :func:`~django.views.decorators.csrf.csrf_exempt` for the whole
|
|
|
|
view function, and :func:`~django.views.decorators.csrf.csrf_protect` for the
|
|
|
|
path within it that needs protection. Example::
|
|
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt, csrf_protect
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def my_view(request):
|
|
|
|
|
|
|
|
@csrf_protect
|
|
|
|
def protected_path(request):
|
|
|
|
do_something()
|
2011-05-10 02:27:36 +08:00
|
|
|
|
2011-05-10 02:27:52 +08:00
|
|
|
if some_condition():
|
|
|
|
return protected_path(request)
|
|
|
|
else:
|
|
|
|
do_something_else()
|
2011-05-10 02:27:36 +08:00
|
|
|
|
2011-05-10 05:35:24 +08:00
|
|
|
Page uses AJAX without any HTML form
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
A page makes a POST request via AJAX, and the page does not have an HTML form
|
|
|
|
with a :ttag:`csrf_token` that would cause the required CSRF cookie to be sent.
|
|
|
|
|
|
|
|
Solution: use :func:`~django.views.decorators.csrf.ensure_csrf_cookie` on the
|
|
|
|
view that sends the page.
|
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
Contrib and reusable apps
|
|
|
|
=========================
|
2006-05-09 07:03:08 +08:00
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
Because it is possible for the developer to turn off the ``CsrfViewMiddleware``,
|
|
|
|
all relevant views in contrib apps use the ``csrf_protect`` decorator to ensure
|
|
|
|
the security of these applications against CSRF. It is recommended that the
|
|
|
|
developers of other reusable apps that want the same guarantees also use the
|
|
|
|
``csrf_protect`` decorator on their views.
|
2011-05-10 07:00:10 +08:00
|
|
|
|
|
|
|
Settings
|
|
|
|
========
|
|
|
|
|
2013-01-13 07:44:53 +08:00
|
|
|
A number of settings can be used to control Django's CSRF behavior:
|
2011-05-10 07:00:10 +08:00
|
|
|
|
2014-03-04 08:52:28 +08:00
|
|
|
* :setting:`CSRF_COOKIE_AGE`
|
2013-01-13 07:44:53 +08:00
|
|
|
* :setting:`CSRF_COOKIE_DOMAIN`
|
2013-02-07 16:48:08 +08:00
|
|
|
* :setting:`CSRF_COOKIE_HTTPONLY`
|
2013-01-13 07:44:53 +08:00
|
|
|
* :setting:`CSRF_COOKIE_NAME`
|
|
|
|
* :setting:`CSRF_COOKIE_PATH`
|
2018-04-14 08:58:31 +08:00
|
|
|
* :setting:`CSRF_COOKIE_SAMESITE`
|
2013-01-13 07:44:53 +08:00
|
|
|
* :setting:`CSRF_COOKIE_SECURE`
|
|
|
|
* :setting:`CSRF_FAILURE_VIEW`
|
2015-02-22 05:57:02 +08:00
|
|
|
* :setting:`CSRF_HEADER_NAME`
|
2015-09-01 10:32:03 +08:00
|
|
|
* :setting:`CSRF_TRUSTED_ORIGINS`
|
2016-07-01 00:42:11 +08:00
|
|
|
* :setting:`CSRF_USE_SESSIONS`
|
2016-02-04 02:29:45 +08:00
|
|
|
|
|
|
|
Frequently Asked Questions
|
|
|
|
==========================
|
|
|
|
|
|
|
|
Is posting an arbitrary CSRF token pair (cookie and POST data) a vulnerability?
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
No, this is by design. Without a man-in-the-middle attack, there is no way for
|
|
|
|
an attacker to send a CSRF token cookie to a victim's browser, so a successful
|
|
|
|
attack would need to obtain the victim's browser's cookie via XSS or similar,
|
|
|
|
in which case an attacker usually doesn't need CSRF attacks.
|
|
|
|
|
|
|
|
Some security audit tools flag this as a problem but as mentioned before, an
|
|
|
|
attacker cannot steal a user's browser's CSRF cookie. "Stealing" or modifying
|
|
|
|
*your own* token using Firebug, Chrome dev tools, etc. isn't a vulnerability.
|
|
|
|
|
2017-01-20 04:56:39 +08:00
|
|
|
Is it a problem that Django's CSRF protection isn't linked to a session by default?
|
|
|
|
-----------------------------------------------------------------------------------
|
2016-02-04 02:29:45 +08:00
|
|
|
|
|
|
|
No, this is by design. Not linking CSRF protection to a session allows using
|
2020-03-31 16:37:38 +08:00
|
|
|
the protection on sites such as a *pastebin* that allow submissions from
|
2016-02-04 02:29:45 +08:00
|
|
|
anonymous users which don't have a session.
|
|
|
|
|
2017-01-20 04:56:39 +08:00
|
|
|
If you wish to store the CSRF token in the user's session, use the
|
|
|
|
:setting:`CSRF_USE_SESSIONS` setting.
|
|
|
|
|
2016-04-03 17:35:24 +08:00
|
|
|
Why might a user encounter a CSRF validation failure after logging in?
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
For security reasons, CSRF tokens are rotated each time a user logs in. Any
|
|
|
|
page with a form generated before a login will have an old, invalid CSRF token
|
|
|
|
and need to be reloaded. This might happen if a user uses the back button after
|
2018-07-18 23:24:07 +08:00
|
|
|
a login or if they log in a different browser tab.
|