2008-12-03 06:40:00 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
2013-05-18 19:09:38 +08:00
|
|
|
import logging
|
2008-12-03 06:40:00 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.context_processors import csrf
|
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 HttpRequest, HttpResponse
|
2012-02-11 12:18:15 +08:00
|
|
|
from django.middleware.csrf import CsrfViewMiddleware, CSRF_KEY_LENGTH
|
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.template import RequestContext, Template
|
2013-12-23 23:01:13 +08:00
|
|
|
from django.test import TestCase, override_settings
|
2011-10-14 02:51:33 +08:00
|
|
|
from django.views.decorators.csrf import csrf_exempt, requires_csrf_token, ensure_csrf_cookie
|
|
|
|
|
2008-12-03 06:40:00 +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
|
|
|
# Response/views used for CsrfResponseMiddleware and CsrfViewMiddleware tests
|
2008-12-03 08:34:18 +08:00
|
|
|
def post_form_response():
|
2012-06-08 00:08:47 +08:00
|
|
|
resp = HttpResponse(content="""
|
2010-09-11 06:56:56 +08:00
|
|
|
<html><body><h1>\u00a1Unicode!<form method="post"><input type="text" /></form></body></html>
|
2008-12-03 08:34:18 +08:00
|
|
|
""", mimetype="text/html")
|
|
|
|
return resp
|
|
|
|
|
2013-11-03 12:36:09 +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
|
|
|
def post_form_view(request):
|
|
|
|
"""A view that returns a POST form (without a token)"""
|
2008-12-03 08:34:18 +08:00
|
|
|
return post_form_response()
|
|
|
|
|
2013-11-03 12:36:09 +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
|
|
|
# Response/views used for template tag tests
|
|
|
|
|
|
|
|
def token_view(request):
|
|
|
|
"""A view that uses {% csrf_token %}"""
|
2011-03-31 01:35:50 +08:00
|
|
|
context = RequestContext(request, processors=[csrf])
|
|
|
|
template = Template("{% csrf_token %}")
|
|
|
|
return HttpResponse(template.render(context))
|
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
|
|
|
|
2013-11-03 12:36:09 +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
|
|
|
def non_token_view_using_request_processor(request):
|
|
|
|
"""
|
|
|
|
A view that doesn't use the token, but does use the csrf view processor.
|
|
|
|
"""
|
|
|
|
context = RequestContext(request, processors=[csrf])
|
|
|
|
template = Template("")
|
|
|
|
return HttpResponse(template.render(context))
|
|
|
|
|
2013-11-03 12:36:09 +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
|
|
|
class TestingHttpRequest(HttpRequest):
|
|
|
|
"""
|
|
|
|
A version of HttpRequest that allows us to change some things
|
|
|
|
more easily
|
|
|
|
"""
|
|
|
|
def is_secure(self):
|
2011-12-17 08:17:26 +08:00
|
|
|
return getattr(self, '_is_secure_override', False)
|
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
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2011-03-31 01:34:26 +08:00
|
|
|
class CsrfViewMiddlewareTest(TestCase):
|
2010-09-09 08:34:54 +08:00
|
|
|
# The csrf token is potentially from an untrusted source, so could have
|
2010-09-11 06:56:56 +08:00
|
|
|
# characters that need dealing with.
|
2012-05-19 23:43:34 +08:00
|
|
|
_csrf_id_cookie = b"<1>\xc2\xa1"
|
2010-09-11 06:56:56 +08:00
|
|
|
_csrf_id = "1"
|
2008-12-03 06:40:00 +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
|
|
|
def _get_GET_no_csrf_cookie_request(self):
|
|
|
|
return TestingHttpRequest()
|
2008-12-03 06:40:00 +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
|
|
|
def _get_GET_csrf_cookie_request(self):
|
|
|
|
req = TestingHttpRequest()
|
2010-09-11 06:56:56 +08:00
|
|
|
req.COOKIES[settings.CSRF_COOKIE_NAME] = self._csrf_id_cookie
|
2008-12-03 06:40:00 +08:00
|
|
|
return req
|
|
|
|
|
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 _get_POST_csrf_cookie_request(self):
|
|
|
|
req = self._get_GET_csrf_cookie_request()
|
2008-12-03 07:00:06 +08:00
|
|
|
req.method = "POST"
|
|
|
|
return req
|
|
|
|
|
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 _get_POST_no_csrf_cookie_request(self):
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
2008-12-03 07:00:06 +08:00
|
|
|
req.method = "POST"
|
|
|
|
return req
|
|
|
|
|
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 _get_POST_request_with_token(self):
|
|
|
|
req = self._get_POST_csrf_cookie_request()
|
|
|
|
req.POST['csrfmiddlewaretoken'] = self._csrf_id
|
2008-12-03 07:00:06 +08:00
|
|
|
return req
|
|
|
|
|
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 _check_token_present(self, response, csrf_id=None):
|
2010-09-11 06:56:56 +08:00
|
|
|
self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % (csrf_id or self._csrf_id))
|
2008-12-03 08:31:31 +08:00
|
|
|
|
2013-05-18 19:09:38 +08:00
|
|
|
def test_process_view_token_too_long(self):
|
|
|
|
"""
|
|
|
|
Check that if the token is longer than expected, it is ignored and
|
|
|
|
a new token is created.
|
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
req.COOKIES[settings.CSRF_COOKIE_NAME] = 'x' * 10000000
|
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
|
|
|
|
self.assertEqual(len(csrf_cookie.value), CSRF_KEY_LENGTH)
|
2012-02-11 12:18:15 +08:00
|
|
|
|
2011-03-31 01:34:26 +08:00
|
|
|
def test_process_response_get_token_used(self):
|
2010-06-08 22:35:48 +08:00
|
|
|
"""
|
2011-03-31 01:34:26 +08:00
|
|
|
When get_token is used, check that the cookie is created and headers
|
|
|
|
patched.
|
2010-06-08 22:35:48 +08:00
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
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:00:22 +08:00
|
|
|
# Put tests for CSRF_COOKIE_* settings here
|
|
|
|
with self.settings(CSRF_COOKIE_NAME='myname',
|
|
|
|
CSRF_COOKIE_DOMAIN='.example.com',
|
|
|
|
CSRF_COOKIE_PATH='/test/',
|
2013-02-07 16:48:08 +08:00
|
|
|
CSRF_COOKIE_SECURE=True,
|
|
|
|
CSRF_COOKIE_HTTPONLY=True):
|
2011-05-10 07:00:22 +08:00
|
|
|
# token_view calls get_token() indirectly
|
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
csrf_cookie = resp2.cookies.get('myname', False)
|
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
|
|
|
self.assertNotEqual(csrf_cookie, False)
|
2011-05-10 07:00:22 +08:00
|
|
|
self.assertEqual(csrf_cookie['domain'], '.example.com')
|
|
|
|
self.assertEqual(csrf_cookie['secure'], True)
|
2013-02-07 16:48:08 +08:00
|
|
|
self.assertEqual(csrf_cookie['httponly'], True)
|
2011-05-10 07:00:22 +08:00
|
|
|
self.assertEqual(csrf_cookie['path'], '/test/')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('Cookie', resp2.get('Vary', ''))
|
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 test_process_response_get_token_not_used(self):
|
2008-12-03 06:40:00 +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
|
|
|
Check that if get_token() is not called, the view middleware does not
|
|
|
|
add a cookie.
|
2008-12-03 06:40:00 +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 is important to make pages cacheable. Pages which do call
|
|
|
|
# get_token(), assuming they use the token, are not cacheable because
|
|
|
|
# the token is specific to the user
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
# non_token_view_using_request_processor does not call get_token(), but
|
|
|
|
# does use the csrf request processor. By using this, we are testing
|
|
|
|
# that the view processor is properly lazy and doesn't call get_token()
|
|
|
|
# until needed.
|
|
|
|
CsrfViewMiddleware().process_view(req, non_token_view_using_request_processor, (), {})
|
|
|
|
resp = non_token_view_using_request_processor(req)
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
|
|
|
|
csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
|
|
|
|
self.assertEqual(csrf_cookie, False)
|
|
|
|
|
2008-12-03 07:00:06 +08:00
|
|
|
# Check the request processing
|
2011-03-31 01:34:14 +08:00
|
|
|
def test_process_request_no_csrf_cookie(self):
|
2008-12-03 07:00:06 +08:00
|
|
|
"""
|
2011-03-31 01:34:14 +08:00
|
|
|
Check that if no CSRF cookies is present, the middleware rejects the
|
|
|
|
incoming request. This will stop login CSRF.
|
2008-12-03 07:00:06 +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
|
|
|
req = self._get_POST_no_csrf_cookie_request()
|
2011-03-31 01:34:26 +08:00
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(403, req2.status_code)
|
2008-12-03 07:00:06 +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
|
|
|
def test_process_request_csrf_cookie_no_token(self):
|
2008-12-03 07:00:06 +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
|
|
|
Check that if a CSRF cookie is present but no token, the middleware
|
|
|
|
rejects the incoming request.
|
2008-12-03 07:00:06 +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
|
|
|
req = self._get_POST_csrf_cookie_request()
|
2011-03-31 01:34:26 +08:00
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(403, req2.status_code)
|
2008-12-03 07:00:06 +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
|
|
|
def test_process_request_csrf_cookie_and_token(self):
|
2008-12-03 07:00:06 +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
|
|
|
Check that if both a cookie and a token is present, the middleware lets it through.
|
2008-12-03 07:00:06 +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
|
|
|
req = self._get_POST_request_with_token()
|
2011-03-31 01:34:26 +08:00
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(None, req2)
|
2008-12-03 08:34:18 +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
|
|
|
def test_process_request_csrf_cookie_no_token_exempt_view(self):
|
|
|
|
"""
|
|
|
|
Check that if a CSRF cookie is present and no token, but the csrf_exempt
|
2008-12-03 08:34:18 +08:00
|
|
|
decorator has been applied to the view, the middleware lets it through
|
|
|
|
"""
|
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
|
|
|
req = self._get_POST_csrf_cookie_request()
|
2011-03-31 01:34:26 +08:00
|
|
|
req2 = CsrfViewMiddleware().process_view(req, csrf_exempt(post_form_view), (), {})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(None, req2)
|
2008-12-03 08:34:18 +08:00
|
|
|
|
2011-02-09 10:06:27 +08:00
|
|
|
def test_csrf_token_in_header(self):
|
2008-12-03 08:34:18 +08:00
|
|
|
"""
|
2011-02-09 10:06:27 +08:00
|
|
|
Check that we can pass in the token in a header instead of in the form
|
2008-12-03 08:34:18 +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
|
|
|
req = self._get_POST_csrf_cookie_request()
|
2011-02-09 10:06:27 +08:00
|
|
|
req.META['HTTP_X_CSRFTOKEN'] = self._csrf_id
|
2011-03-31 01:34:26 +08:00
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(None, req2)
|
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
|
|
|
def test_put_and_delete_rejected(self):
|
|
|
|
"""
|
|
|
|
Tests that HTTP PUT and DELETE methods have protection
|
|
|
|
"""
|
|
|
|
req = TestingHttpRequest()
|
|
|
|
req.method = 'PUT'
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(403, req2.status_code)
|
|
|
|
|
|
|
|
req = TestingHttpRequest()
|
|
|
|
req.method = 'DELETE'
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(403, req2.status_code)
|
|
|
|
|
|
|
|
def test_put_and_delete_allowed(self):
|
|
|
|
"""
|
|
|
|
Tests that HTTP PUT and DELETE methods can get through with
|
|
|
|
X-CSRFToken and a cookie
|
|
|
|
"""
|
|
|
|
req = self._get_GET_csrf_cookie_request()
|
|
|
|
req.method = 'PUT'
|
|
|
|
req.META['HTTP_X_CSRFTOKEN'] = self._csrf_id
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(None, req2)
|
|
|
|
|
|
|
|
req = self._get_GET_csrf_cookie_request()
|
|
|
|
req.method = 'DELETE'
|
|
|
|
req.META['HTTP_X_CSRFTOKEN'] = self._csrf_id
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(None, req2)
|
|
|
|
|
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
|
|
|
# Tests for the template tag method
|
|
|
|
def test_token_node_no_csrf_cookie(self):
|
|
|
|
"""
|
|
|
|
Check that CsrfTokenNode works when no CSRF cookie is set
|
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
resp = token_view(req)
|
2012-08-13 17:34:40 +08:00
|
|
|
self.assertEqual(resp.content, b'')
|
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
|
|
|
|
2010-09-11 06:56:56 +08:00
|
|
|
def test_token_node_empty_csrf_cookie(self):
|
|
|
|
"""
|
|
|
|
Check that we get a new token if the csrf_cookie is the empty string
|
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
2012-06-08 00:08:47 +08:00
|
|
|
req.COOKIES[settings.CSRF_COOKIE_NAME] = b""
|
2010-09-11 06:56:56 +08:00
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertNotEqual("", resp.content)
|
2010-09-11 06:56: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
|
|
|
def test_token_node_with_csrf_cookie(self):
|
|
|
|
"""
|
|
|
|
Check that CsrfTokenNode works when a CSRF cookie is set
|
|
|
|
"""
|
|
|
|
req = self._get_GET_csrf_cookie_request()
|
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
self._check_token_present(resp)
|
|
|
|
|
2010-06-08 22:35:48 +08:00
|
|
|
def test_get_token_for_exempt_view(self):
|
|
|
|
"""
|
2011-03-31 01:35:41 +08:00
|
|
|
Check that get_token still works for a view decorated with 'csrf_exempt'.
|
2010-06-08 22:35:48 +08:00
|
|
|
"""
|
|
|
|
req = self._get_GET_csrf_cookie_request()
|
2011-03-31 01:35:41 +08:00
|
|
|
CsrfViewMiddleware().process_view(req, csrf_exempt(token_view), (), {})
|
2010-06-08 22:35:48 +08:00
|
|
|
resp = token_view(req)
|
|
|
|
self._check_token_present(resp)
|
|
|
|
|
2010-10-28 19:47:15 +08:00
|
|
|
def test_get_token_for_requires_csrf_token_view(self):
|
|
|
|
"""
|
|
|
|
Check that get_token works for a view decorated solely with requires_csrf_token
|
|
|
|
"""
|
|
|
|
req = self._get_GET_csrf_cookie_request()
|
|
|
|
resp = requires_csrf_token(token_view)(req)
|
|
|
|
self._check_token_present(resp)
|
|
|
|
|
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 test_token_node_with_new_csrf_cookie(self):
|
|
|
|
"""
|
|
|
|
Check that CsrfTokenNode works when a CSRF cookie is created by
|
|
|
|
the middleware (when one was not already present)
|
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
csrf_cookie = resp2.cookies[settings.CSRF_COOKIE_NAME]
|
|
|
|
self._check_token_present(resp, csrf_id=csrf_cookie.value)
|
|
|
|
|
2013-02-10 01:17:01 +08:00
|
|
|
@override_settings(ALLOWED_HOSTS=['www.example.com'])
|
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 test_https_bad_referer(self):
|
|
|
|
"""
|
|
|
|
Test that a POST HTTPS request with a bad referer is rejected
|
|
|
|
"""
|
|
|
|
req = self._get_POST_request_with_token()
|
2011-12-17 08:17:26 +08:00
|
|
|
req._is_secure_override = True
|
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
|
|
|
req.META['HTTP_HOST'] = 'www.example.com'
|
|
|
|
req.META['HTTP_REFERER'] = 'https://www.evil.org/somepage'
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertNotEqual(None, req2)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(403, req2.status_code)
|
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
|
|
|
|
2013-05-18 18:32:47 +08:00
|
|
|
@override_settings(ALLOWED_HOSTS=['www.example.com'])
|
|
|
|
def test_https_malformed_referer(self):
|
|
|
|
"""
|
|
|
|
Test that a POST HTTPS request with a bad referer is rejected
|
|
|
|
"""
|
|
|
|
req = self._get_POST_request_with_token()
|
|
|
|
req._is_secure_override = True
|
|
|
|
req.META['HTTP_HOST'] = 'www.example.com'
|
|
|
|
req.META['HTTP_REFERER'] = 'http://http://www.example.com/'
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertNotEqual(None, req2)
|
|
|
|
self.assertEqual(403, req2.status_code)
|
|
|
|
|
2013-02-10 01:17:01 +08:00
|
|
|
@override_settings(ALLOWED_HOSTS=['www.example.com'])
|
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 test_https_good_referer(self):
|
|
|
|
"""
|
|
|
|
Test that a POST HTTPS request with a good referer is accepted
|
|
|
|
"""
|
|
|
|
req = self._get_POST_request_with_token()
|
2011-12-17 08:17:26 +08:00
|
|
|
req._is_secure_override = True
|
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
|
|
|
req.META['HTTP_HOST'] = 'www.example.com'
|
|
|
|
req.META['HTTP_REFERER'] = 'https://www.example.com/somepage'
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(None, req2)
|
2011-03-16 04:37:09 +08:00
|
|
|
|
2013-02-10 01:17:01 +08:00
|
|
|
@override_settings(ALLOWED_HOSTS=['www.example.com'])
|
2011-03-16 04:37:09 +08:00
|
|
|
def test_https_good_referer_2(self):
|
|
|
|
"""
|
|
|
|
Test that a POST HTTPS request with a good referer is accepted
|
|
|
|
where the referer contains no trailing slash
|
|
|
|
"""
|
|
|
|
# See ticket #15617
|
|
|
|
req = self._get_POST_request_with_token()
|
2011-12-17 08:17:26 +08:00
|
|
|
req._is_secure_override = True
|
2011-03-16 04:37:09 +08:00
|
|
|
req.META['HTTP_HOST'] = 'www.example.com'
|
|
|
|
req.META['HTTP_REFERER'] = 'https://www.example.com'
|
|
|
|
req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(None, req2)
|
2011-05-10 05:35:24 +08:00
|
|
|
|
|
|
|
def test_ensures_csrf_cookie_no_middleware(self):
|
|
|
|
"""
|
|
|
|
Tests that ensures_csrf_cookie decorator fulfils its promise
|
|
|
|
with no middleware
|
|
|
|
"""
|
|
|
|
@ensure_csrf_cookie
|
|
|
|
def view(request):
|
|
|
|
# Doesn't insert a token or anything
|
|
|
|
return HttpResponse(content="")
|
|
|
|
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
resp = view(req)
|
|
|
|
self.assertTrue(resp.cookies.get(settings.CSRF_COOKIE_NAME, False))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('Cookie', resp.get('Vary', ''))
|
2011-05-10 05:35:24 +08:00
|
|
|
|
|
|
|
def test_ensures_csrf_cookie_with_middleware(self):
|
|
|
|
"""
|
|
|
|
Tests that ensures_csrf_cookie decorator fulfils its promise
|
|
|
|
with the middleware enabled.
|
|
|
|
"""
|
|
|
|
@ensure_csrf_cookie
|
|
|
|
def view(request):
|
|
|
|
# Doesn't insert a token or anything
|
|
|
|
return HttpResponse(content="")
|
|
|
|
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
CsrfViewMiddleware().process_view(req, view, (), {})
|
|
|
|
resp = view(req)
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
self.assertTrue(resp2.cookies.get(settings.CSRF_COOKIE_NAME, False))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('Cookie', resp2.get('Vary', ''))
|
2013-05-18 19:09:38 +08:00
|
|
|
|
|
|
|
def test_ensures_csrf_cookie_no_logging(self):
|
|
|
|
"""
|
|
|
|
Tests that ensure_csrf_cookie doesn't log warnings. See #19436.
|
|
|
|
"""
|
|
|
|
@ensure_csrf_cookie
|
|
|
|
def view(request):
|
|
|
|
# Doesn't insert a token or anything
|
|
|
|
return HttpResponse(content="")
|
|
|
|
|
|
|
|
class TestHandler(logging.Handler):
|
|
|
|
def emit(self, record):
|
|
|
|
raise Exception("This shouldn't have happened!")
|
|
|
|
|
|
|
|
logger = logging.getLogger('django.request')
|
|
|
|
test_handler = TestHandler()
|
|
|
|
old_log_level = logger.level
|
|
|
|
try:
|
|
|
|
logger.addHandler(test_handler)
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
2013-09-09 03:20:01 +08:00
|
|
|
view(req)
|
2013-05-18 19:09:38 +08:00
|
|
|
finally:
|
|
|
|
logger.removeHandler(test_handler)
|
|
|
|
logger.setLevel(old_log_level)
|
2014-03-04 08:52:28 +08:00
|
|
|
|
|
|
|
def test_csrf_cookie_age(self):
|
|
|
|
"""
|
|
|
|
Test to verify CSRF cookie age can be set using
|
|
|
|
settings.CSRF_COOKIE_AGE.
|
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
|
|
|
|
MAX_AGE = 123
|
|
|
|
with self.settings(CSRF_COOKIE_NAME='csrfcookie',
|
|
|
|
CSRF_COOKIE_DOMAIN='.example.com',
|
|
|
|
CSRF_COOKIE_AGE=MAX_AGE,
|
|
|
|
CSRF_COOKIE_PATH='/test/',
|
|
|
|
CSRF_COOKIE_SECURE=True,
|
|
|
|
CSRF_COOKIE_HTTPONLY=True):
|
|
|
|
# token_view calls get_token() indirectly
|
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
max_age = resp2.cookies.get('csrfcookie').get('max-age')
|
|
|
|
self.assertEqual(max_age, MAX_AGE)
|
|
|
|
|
|
|
|
def test_csrf_cookie_age_none(self):
|
|
|
|
"""
|
|
|
|
Test to verify CSRF cookie age does not have max age set and therefore
|
|
|
|
uses session-based cookies.
|
|
|
|
"""
|
|
|
|
req = self._get_GET_no_csrf_cookie_request()
|
|
|
|
|
|
|
|
MAX_AGE = None
|
|
|
|
with self.settings(CSRF_COOKIE_NAME='csrfcookie',
|
|
|
|
CSRF_COOKIE_DOMAIN='.example.com',
|
|
|
|
CSRF_COOKIE_AGE=MAX_AGE,
|
|
|
|
CSRF_COOKIE_PATH='/test/',
|
|
|
|
CSRF_COOKIE_SECURE=True,
|
|
|
|
CSRF_COOKIE_HTTPONLY=True):
|
|
|
|
# token_view calls get_token() indirectly
|
|
|
|
CsrfViewMiddleware().process_view(req, token_view, (), {})
|
|
|
|
resp = token_view(req)
|
|
|
|
|
|
|
|
resp2 = CsrfViewMiddleware().process_response(req, resp)
|
|
|
|
max_age = resp2.cookies.get('csrfcookie').get('max-age')
|
|
|
|
self.assertEqual(max_age, '')
|
2014-06-25 18:52:25 +08:00
|
|
|
|
|
|
|
def test_post_data_read_failure(self):
|
|
|
|
"""
|
|
|
|
#20128 -- IOErrors during POST data reading should be caught and
|
|
|
|
treated as if the POST data wasn't there.
|
|
|
|
"""
|
|
|
|
class CsrfPostRequest(HttpRequest):
|
|
|
|
"""
|
|
|
|
HttpRequest that can raise an IOError when accessing POST data
|
|
|
|
"""
|
|
|
|
def __init__(self, token, raise_error):
|
|
|
|
super(CsrfPostRequest, self).__init__()
|
|
|
|
self.method = 'POST'
|
|
|
|
|
|
|
|
self.raise_error = False
|
|
|
|
self.COOKIES[settings.CSRF_COOKIE_NAME] = token
|
|
|
|
self.POST['csrfmiddlewaretoken'] = token
|
|
|
|
self.raise_error = raise_error
|
|
|
|
|
|
|
|
def _load_post_and_files(self):
|
|
|
|
raise IOError('error reading input data')
|
|
|
|
|
|
|
|
def _get_post(self):
|
|
|
|
if self.raise_error:
|
|
|
|
self._load_post_and_files()
|
|
|
|
return self._post
|
|
|
|
|
|
|
|
def _set_post(self, post):
|
|
|
|
self._post = post
|
|
|
|
|
|
|
|
POST = property(_get_post, _set_post)
|
|
|
|
|
|
|
|
token = 'ABC'
|
|
|
|
|
|
|
|
req = CsrfPostRequest(token, raise_error=False)
|
|
|
|
resp = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(resp, None)
|
|
|
|
|
|
|
|
req = CsrfPostRequest(token, raise_error=True)
|
|
|
|
resp = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
|
|
|
|
self.assertEqual(resp.status_code, 403)
|