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
|
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
|
|
|
Web site contains a link, a form button or some javascript that is intended to
|
|
|
|
perform some action on your Web site, using the credentials of a logged-in user
|
|
|
|
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
|
2011-09-05 05:17:30 +08:00
|
|
|
'safe' methods, as defined by 9.1.1 Safe Methods, HTTP 1.1,
|
|
|
|
:rfc:`2616#section-9.1.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
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _Cross Site Request Forgeries: http://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
|
|
|
|
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
|
|
|
To enable CSRF protection for your views, follow these steps:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
1. Add the middleware
|
|
|
|
``'django.middleware.csrf.CsrfViewMiddleware'`` to your list of
|
|
|
|
middleware classes, :setting:`MIDDLEWARE_CLASSES`. (It 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
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Alternatively, you can use the decorator
|
|
|
|
: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
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
2. In any template that uses a POST form, use the :ttag:`csrf_token` tag inside
|
|
|
|
the ``<form>`` element if the form is for an internal URL, e.g.::
|
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
|
|
|
<form action="." 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
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
3. In the corresponding view functions, ensure that the
|
|
|
|
``'django.core.context_processors.csrf'`` context processor is
|
|
|
|
being used. Usually, this can be done in one of two ways:
|
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
|
|
|
1. Use RequestContext, which always uses
|
|
|
|
``'django.core.context_processors.csrf'`` (no matter what your
|
|
|
|
TEMPLATE_CONTEXT_PROCESSORS setting). If you are using
|
|
|
|
generic views or contrib apps, you are covered already, since these
|
|
|
|
apps use RequestContext throughout.
|
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
|
|
|
2. Manually import and use the processor to generate the CSRF token and
|
|
|
|
add it to the template context. e.g.::
|
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
|
|
|
from django.core.context_processors import csrf
|
|
|
|
from django.shortcuts import render_to_response
|
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
|
|
|
def my_view(request):
|
|
|
|
c = {}
|
|
|
|
c.update(csrf(request))
|
|
|
|
# ... view code here
|
|
|
|
return render_to_response("a_template.html", c)
|
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
|
|
|
You may want to write your own
|
|
|
|
:func:`~django.shortcuts.render_to_response()` wrapper that takes care
|
|
|
|
of this step for you.
|
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 utility script ``extras/csrf_migration_helper.py`` can help to automate the
|
2011-03-31 01:34:26 +08:00
|
|
|
finding of code and templates that may need these steps. It contains full help
|
|
|
|
on how to use it.
|
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
|
|
|
|
XMLHttpRequest, set a custom `X-CSRFToken` header 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. In jQuery, you can use the
|
2011-02-13 07:37:35 +08:00
|
|
|
``ajaxSend`` event as follows:
|
2011-02-09 10:06:27 +08:00
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
2011-05-09 23:40:01 +08:00
|
|
|
$(document).ajaxSend(function(event, xhr, settings) {
|
2011-02-13 07:37:35 +08:00
|
|
|
function getCookie(name) {
|
|
|
|
var cookieValue = null;
|
|
|
|
if (document.cookie && document.cookie != '') {
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
var cookie = jQuery.trim(cookies[i]);
|
|
|
|
// Does this cookie string begin with the name we want?
|
|
|
|
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
|
|
break;
|
2011-02-09 10:06:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-13 07:37:35 +08:00
|
|
|
return cookieValue;
|
|
|
|
}
|
2011-05-09 23:40:01 +08:00
|
|
|
function sameOrigin(url) {
|
|
|
|
// url could be relative or scheme relative or absolute
|
|
|
|
var host = document.location.host; // host + port
|
|
|
|
var protocol = document.location.protocol;
|
|
|
|
var sr_origin = '//' + host;
|
|
|
|
var origin = protocol + sr_origin;
|
|
|
|
// Allow absolute or scheme relative URLs to same origin
|
|
|
|
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
|
|
|
|
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
|
|
|
|
// or any other URL that isn't scheme relative or absolute i.e relative.
|
|
|
|
!(/^(\/\/|http:|https:).*/.test(url));
|
|
|
|
}
|
2011-05-10 03:06:57 +08:00
|
|
|
function safeMethod(method) {
|
2011-05-10 07:46:02 +08:00
|
|
|
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
|
2011-05-10 03:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
|
2011-02-13 07:37:35 +08:00
|
|
|
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
|
2011-02-09 10:06:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-07-15 02:36:05 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Due to a bug introduced in jQuery 1.5, the example above will not work
|
|
|
|
correctly on that version. Make sure you are running at least jQuery 1.5.1.
|
|
|
|
|
2011-02-09 10:06:27 +08:00
|
|
|
Adding this to a javascript file that is included on your site will ensure that
|
|
|
|
AJAX POST requests that are made via jQuery will not be caught by the CSRF
|
|
|
|
protection.
|
|
|
|
|
2011-05-10 03:06:49 +08:00
|
|
|
The above code could be simplified by using the `jQuery cookie plugin
|
|
|
|
<http://plugins.jquery.com/project/Cookie>`_ to replace ``getCookie``, and
|
|
|
|
`settings.crossDomain <http://api.jquery.com/jQuery.ajax>`_ in jQuery 1.5 and
|
|
|
|
later to replace ``sameOrigin``.
|
|
|
|
|
2011-05-10 05:35:24 +08:00
|
|
|
In addition, if the CSRF cookie has not been sent to the client by use of
|
|
|
|
:ttag:`csrf_token`, you may need to ensure the client receives the cookie by
|
|
|
|
using :func:`~django.views.decorators.csrf.ensure_csrf_cookie`.
|
|
|
|
|
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.views.decorators.csrf import csrf_protect
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
@csrf_protect
|
|
|
|
def my_view(request):
|
|
|
|
c = {}
|
|
|
|
# ...
|
|
|
|
return render(request, "a_template.html", c)
|
2008-12-08 12:15:19 +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
|
|
|
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
|
|
|
|
own view for handling this condition. To do this, simply set the
|
2011-05-10 07:00:10 +08:00
|
|
|
:setting:`CSRF_FAILURE_VIEW` setting.
|
2006-05-09 07:03:08 +08:00
|
|
|
|
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:
|
|
|
|
|
|
|
|
1. A CSRF cookie that is set to a random value (a session independent nonce, as
|
|
|
|
it is called), which other sites will not have access to.
|
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
|
|
|
This cookie is set by ``CsrfViewMiddleware``. It is meant to be permanent,
|
|
|
|
but since there is no way to set a cookie that never expires, it is sent with
|
2009-10-27 08:36:34 +08:00
|
|
|
every response that has called ``django.middleware.csrf.get_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
|
|
|
(the function used internally to retrieve the CSRF token).
|
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
|
|
|
2. A hidden form field with the name 'csrfmiddlewaretoken' present in all
|
|
|
|
outgoing POST forms. The value of this field is the value of the CSRF
|
|
|
|
cookie.
|
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
|
|
|
|
2011-05-10 07:45:54 +08:00
|
|
|
3. For all incoming requests that are not using HTTP GET, HEAD, OPTIONS or
|
|
|
|
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
|
|
|
|
|
|
|
This check is done by ``CsrfViewMiddleware``.
|
|
|
|
|
|
|
|
4. In addition, for HTTPS requests, strict referer checking is done by
|
|
|
|
``CsrfViewMiddleware``. This is necessary to address a Man-In-The-Middle
|
|
|
|
attack that is possible under HTTPS when using a session independent nonce,
|
|
|
|
due to the fact that HTTP 'Set-Cookie' headers are (unfortunately) accepted
|
|
|
|
by clients that are talking to a site under HTTPS. (Referer checking is not
|
|
|
|
done for HTTP requests because the presence of the Referer header is not
|
|
|
|
reliable enough under HTTP.)
|
|
|
|
|
|
|
|
This ensures that only forms that have originated from your Web site 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
|
2011-09-05 05:17:30 +08:00
|
|
|
'safe' by :rfc:`2616`). These requests ought never to have any potentially
|
2011-05-10 07:45:54 +08:00
|
|
|
dangerous side effects , and so a CSRF attack with a GET request ought to be
|
2011-09-05 05:17:30 +08:00
|
|
|
harmless. :rfc:`2616` defines POST, PUT and DELETE as 'unsafe', and all other
|
2011-05-10 07:45:54 +08:00
|
|
|
methods are assumed to be unsafe, for maximum protection.
|
2007-08-16 22:09:41 +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):
|
|
|
|
# ...
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
---------
|
|
|
|
|
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::
|
|
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
@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.views.decorators.csrf import requires_csrf_token
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
@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
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
There are cases when may not have run 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: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
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
A view needs CRSF protection under one set of conditions only, and mustn't have
|
|
|
|
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
|
|
|
|
========
|
|
|
|
|
2011-05-13 12:33:42 +08:00
|
|
|
A number of settings can be used to control Django's CSRF behavior.
|
2011-05-10 07:00:10 +08:00
|
|
|
|
|
|
|
CSRF_COOKIE_DOMAIN
|
|
|
|
------------------
|
|
|
|
|
|
|
|
.. versionadded:: 1.2
|
|
|
|
|
|
|
|
Default: ``None``
|
|
|
|
|
|
|
|
The domain to be used when setting the CSRF cookie. This can be useful for
|
2011-08-13 19:58:19 +08:00
|
|
|
easily allowing cross-subdomain requests to be excluded from the normal cross
|
2011-05-10 07:00:10 +08:00
|
|
|
site request forgery protection. It should be set to a string such as
|
|
|
|
``".lawrence.com"`` to allow a POST request from a form on one subdomain to be
|
|
|
|
accepted by accepted by a view served from another subdomain.
|
|
|
|
|
|
|
|
Please note that, with or without use of this setting, this CSRF protection
|
|
|
|
mechanism is not safe against cross-subdomain attacks -- see `Limitations`_.
|
|
|
|
|
|
|
|
CSRF_COOKIE_NAME
|
|
|
|
----------------
|
|
|
|
|
|
|
|
.. versionadded:: 1.2
|
|
|
|
|
|
|
|
Default: ``'csrftoken'``
|
|
|
|
|
|
|
|
The name of the cookie to use for the CSRF authentication token. This can be
|
|
|
|
whatever you want.
|
|
|
|
|
2011-05-10 07:00:22 +08:00
|
|
|
CSRF_COOKIE_PATH
|
|
|
|
----------------
|
|
|
|
|
|
|
|
.. versionadded:: 1.4
|
|
|
|
|
|
|
|
Default: ``'/'``
|
|
|
|
|
|
|
|
The path set on the CSRF cookie. This should either match the URL path of your
|
|
|
|
Django installation or be a parent of that path.
|
|
|
|
|
|
|
|
This is useful if you have multiple Django instances running under the same
|
|
|
|
hostname. They can use different cookie paths, and each instance will only see
|
|
|
|
its own CSRF cookie.
|
|
|
|
|
|
|
|
CSRF_COOKIE_SECURE
|
|
|
|
------------------
|
|
|
|
|
|
|
|
.. versionadded:: 1.4
|
|
|
|
|
|
|
|
Default: ``False``
|
|
|
|
|
|
|
|
Whether to use a secure cookie for the CSRF cookie. If this is set to ``True``,
|
|
|
|
the cookie will be marked as "secure," which means browsers may ensure that the
|
|
|
|
cookie is only sent under an HTTPS connection.
|
|
|
|
|
2011-05-10 07:00:10 +08:00
|
|
|
CSRF_FAILURE_VIEW
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. versionadded:: 1.2
|
|
|
|
|
|
|
|
Default: ``'django.views.csrf.csrf_failure'``
|
|
|
|
|
|
|
|
A dotted path to the view function to be used when an incoming request
|
|
|
|
is rejected by the CSRF protection. The function should have this signature::
|
|
|
|
|
|
|
|
def csrf_failure(request, reason="")
|
|
|
|
|
|
|
|
where ``reason`` is a short message (intended for developers or logging, not for
|
|
|
|
end users) indicating the reason the request was rejected.
|