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.http import HttpResponseForbidden
|
|
|
|
from django.template import Context, Template
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
# We include the template inline since we need to be able to reliably display
|
|
|
|
# this error message, especially for the sake of developers, and there isn't any
|
|
|
|
# other way of making it available independent of what is in the settings file.
|
|
|
|
|
|
|
|
CSRF_FAILRE_TEMPLATE = """
|
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
|
|
<title>403 Forbidden</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>403 Forbidden</h1>
|
|
|
|
<p>CSRF verification failed. Request aborted.</p>
|
|
|
|
{% if DEBUG %}
|
|
|
|
<h2>Help</h2>
|
|
|
|
{% if reason %}
|
|
|
|
<p>Reason given for failure:</p>
|
|
|
|
<pre>
|
|
|
|
{{ reason }}
|
|
|
|
</pre>
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
<p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when
|
|
|
|
<a
|
|
|
|
href='http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ref-contrib-csrf'>Django's
|
|
|
|
CSRF mechanism</a> has not been used correctly. For POST forms, you need to
|
|
|
|
ensure:</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li>The view function uses <a
|
2009-10-29 22:17:39 +08:00
|
|
|
href='http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext'><code>RequestContext</code></a>
|
|
|
|
for the template, instead of <code>Context</code>.</li>
|
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
|
|
|
|
2009-10-29 22:17:39 +08:00
|
|
|
<li>In the template, there is a <code>{% templatetag openblock %} csrf_token
|
|
|
|
{% templatetag closeblock %}</code> template tag inside each POST form that
|
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
|
|
|
targets an internal URL.</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>You're seeing the help section of this page because you have <code>DEBUG =
|
|
|
|
True</code> in your Django settings file. Change that to <code>False</code>,
|
|
|
|
and only the initial error message will be displayed. </p>
|
|
|
|
|
|
|
|
<p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p>
|
2009-10-28 06:20:03 +08:00
|
|
|
{% else %}
|
|
|
|
<p><small>More information is available with DEBUG=True.</small></p>
|
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
|
|
|
|
|
|
|
{% endif %}
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
def csrf_failure(request, reason=""):
|
|
|
|
"""
|
|
|
|
Default view used when request fails CSRF protection
|
|
|
|
"""
|
|
|
|
t = Template(CSRF_FAILRE_TEMPLATE)
|
|
|
|
c = Context({'DEBUG': settings.DEBUG,
|
|
|
|
'reason': reason})
|
|
|
|
return HttpResponseForbidden(t.render(c), mimetype='text/html')
|