tests.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # -*- coding: utf-8 -*-
  2. import warnings
  3. from django.test import TestCase
  4. from django.http import HttpRequest, HttpResponse
  5. from django.middleware.csrf import CsrfViewMiddleware
  6. from django.views.decorators.csrf import csrf_exempt, requires_csrf_token
  7. from django.core.context_processors import csrf
  8. from django.conf import settings
  9. from django.template import RequestContext, Template
  10. # Response/views used for CsrfResponseMiddleware and CsrfViewMiddleware tests
  11. def post_form_response():
  12. resp = HttpResponse(content=u"""
  13. <html><body><h1>\u00a1Unicode!<form method="post"><input type="text" /></form></body></html>
  14. """, mimetype="text/html")
  15. return resp
  16. def post_form_view(request):
  17. """A view that returns a POST form (without a token)"""
  18. return post_form_response()
  19. # Response/views used for template tag tests
  20. def token_view(request):
  21. """A view that uses {% csrf_token %}"""
  22. context = RequestContext(request, processors=[csrf])
  23. template = Template("{% csrf_token %}")
  24. return HttpResponse(template.render(context))
  25. def non_token_view_using_request_processor(request):
  26. """
  27. A view that doesn't use the token, but does use the csrf view processor.
  28. """
  29. context = RequestContext(request, processors=[csrf])
  30. template = Template("")
  31. return HttpResponse(template.render(context))
  32. class TestingHttpRequest(HttpRequest):
  33. """
  34. A version of HttpRequest that allows us to change some things
  35. more easily
  36. """
  37. def is_secure(self):
  38. return getattr(self, '_is_secure', False)
  39. class CsrfViewMiddlewareTest(TestCase):
  40. # The csrf token is potentially from an untrusted source, so could have
  41. # characters that need dealing with.
  42. _csrf_id_cookie = "<1>\xc2\xa1"
  43. _csrf_id = "1"
  44. def _get_GET_no_csrf_cookie_request(self):
  45. return TestingHttpRequest()
  46. def _get_GET_csrf_cookie_request(self):
  47. req = TestingHttpRequest()
  48. req.COOKIES[settings.CSRF_COOKIE_NAME] = self._csrf_id_cookie
  49. return req
  50. def _get_POST_csrf_cookie_request(self):
  51. req = self._get_GET_csrf_cookie_request()
  52. req.method = "POST"
  53. return req
  54. def _get_POST_no_csrf_cookie_request(self):
  55. req = self._get_GET_no_csrf_cookie_request()
  56. req.method = "POST"
  57. return req
  58. def _get_POST_request_with_token(self):
  59. req = self._get_POST_csrf_cookie_request()
  60. req.POST['csrfmiddlewaretoken'] = self._csrf_id
  61. return req
  62. def _check_token_present(self, response, csrf_id=None):
  63. self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % (csrf_id or self._csrf_id))
  64. def test_process_response_get_token_used(self):
  65. """
  66. When get_token is used, check that the cookie is created and headers
  67. patched.
  68. """
  69. req = self._get_GET_no_csrf_cookie_request()
  70. # token_view calls get_token() indirectly
  71. CsrfViewMiddleware().process_view(req, token_view, (), {})
  72. resp = token_view(req)
  73. resp2 = CsrfViewMiddleware().process_response(req, resp)
  74. csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
  75. self.assertNotEqual(csrf_cookie, False)
  76. self.assertTrue('Cookie' in resp2.get('Vary',''))
  77. def test_process_response_get_token_not_used(self):
  78. """
  79. Check that if get_token() is not called, the view middleware does not
  80. add a cookie.
  81. """
  82. # This is important to make pages cacheable. Pages which do call
  83. # get_token(), assuming they use the token, are not cacheable because
  84. # the token is specific to the user
  85. req = self._get_GET_no_csrf_cookie_request()
  86. # non_token_view_using_request_processor does not call get_token(), but
  87. # does use the csrf request processor. By using this, we are testing
  88. # that the view processor is properly lazy and doesn't call get_token()
  89. # until needed.
  90. CsrfViewMiddleware().process_view(req, non_token_view_using_request_processor, (), {})
  91. resp = non_token_view_using_request_processor(req)
  92. resp2 = CsrfViewMiddleware().process_response(req, resp)
  93. csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
  94. self.assertEqual(csrf_cookie, False)
  95. # Check the request processing
  96. def test_process_request_no_csrf_cookie(self):
  97. """
  98. Check that if no CSRF cookies is present, the middleware rejects the
  99. incoming request. This will stop login CSRF.
  100. """
  101. req = self._get_POST_no_csrf_cookie_request()
  102. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  103. self.assertEqual(403, req2.status_code)
  104. def test_process_request_csrf_cookie_no_token(self):
  105. """
  106. Check that if a CSRF cookie is present but no token, the middleware
  107. rejects the incoming request.
  108. """
  109. req = self._get_POST_csrf_cookie_request()
  110. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  111. self.assertEqual(403, req2.status_code)
  112. def test_process_request_csrf_cookie_and_token(self):
  113. """
  114. Check that if both a cookie and a token is present, the middleware lets it through.
  115. """
  116. req = self._get_POST_request_with_token()
  117. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  118. self.assertEqual(None, req2)
  119. def test_process_request_csrf_cookie_no_token_exempt_view(self):
  120. """
  121. Check that if a CSRF cookie is present and no token, but the csrf_exempt
  122. decorator has been applied to the view, the middleware lets it through
  123. """
  124. req = self._get_POST_csrf_cookie_request()
  125. req2 = CsrfViewMiddleware().process_view(req, csrf_exempt(post_form_view), (), {})
  126. self.assertEqual(None, req2)
  127. def test_csrf_token_in_header(self):
  128. """
  129. Check that we can pass in the token in a header instead of in the form
  130. """
  131. req = self._get_POST_csrf_cookie_request()
  132. req.META['HTTP_X_CSRFTOKEN'] = self._csrf_id
  133. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  134. self.assertEqual(None, req2)
  135. # Tests for the template tag method
  136. def test_token_node_no_csrf_cookie(self):
  137. """
  138. Check that CsrfTokenNode works when no CSRF cookie is set
  139. """
  140. req = self._get_GET_no_csrf_cookie_request()
  141. resp = token_view(req)
  142. self.assertEqual(u"", resp.content)
  143. def test_token_node_empty_csrf_cookie(self):
  144. """
  145. Check that we get a new token if the csrf_cookie is the empty string
  146. """
  147. req = self._get_GET_no_csrf_cookie_request()
  148. req.COOKIES[settings.CSRF_COOKIE_NAME] = ""
  149. CsrfViewMiddleware().process_view(req, token_view, (), {})
  150. resp = token_view(req)
  151. self.assertNotEqual(u"", resp.content)
  152. def test_token_node_with_csrf_cookie(self):
  153. """
  154. Check that CsrfTokenNode works when a CSRF cookie is set
  155. """
  156. req = self._get_GET_csrf_cookie_request()
  157. CsrfViewMiddleware().process_view(req, token_view, (), {})
  158. resp = token_view(req)
  159. self._check_token_present(resp)
  160. def test_get_token_for_exempt_view(self):
  161. """
  162. Check that get_token still works for a view decorated with 'csrf_exempt'.
  163. """
  164. req = self._get_GET_csrf_cookie_request()
  165. CsrfViewMiddleware().process_view(req, csrf_exempt(token_view), (), {})
  166. resp = token_view(req)
  167. self._check_token_present(resp)
  168. def test_get_token_for_requires_csrf_token_view(self):
  169. """
  170. Check that get_token works for a view decorated solely with requires_csrf_token
  171. """
  172. req = self._get_GET_csrf_cookie_request()
  173. resp = requires_csrf_token(token_view)(req)
  174. self._check_token_present(resp)
  175. def test_token_node_with_new_csrf_cookie(self):
  176. """
  177. Check that CsrfTokenNode works when a CSRF cookie is created by
  178. the middleware (when one was not already present)
  179. """
  180. req = self._get_GET_no_csrf_cookie_request()
  181. CsrfViewMiddleware().process_view(req, token_view, (), {})
  182. resp = token_view(req)
  183. resp2 = CsrfViewMiddleware().process_response(req, resp)
  184. csrf_cookie = resp2.cookies[settings.CSRF_COOKIE_NAME]
  185. self._check_token_present(resp, csrf_id=csrf_cookie.value)
  186. def test_https_bad_referer(self):
  187. """
  188. Test that a POST HTTPS request with a bad referer is rejected
  189. """
  190. req = self._get_POST_request_with_token()
  191. req._is_secure = True
  192. req.META['HTTP_HOST'] = 'www.example.com'
  193. req.META['HTTP_REFERER'] = 'https://www.evil.org/somepage'
  194. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  195. self.assertNotEqual(None, req2)
  196. self.assertEqual(403, req2.status_code)
  197. def test_https_good_referer(self):
  198. """
  199. Test that a POST HTTPS request with a good referer is accepted
  200. """
  201. req = self._get_POST_request_with_token()
  202. req._is_secure = True
  203. req.META['HTTP_HOST'] = 'www.example.com'
  204. req.META['HTTP_REFERER'] = 'https://www.example.com/somepage'
  205. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  206. self.assertEqual(None, req2)
  207. def test_https_good_referer_2(self):
  208. """
  209. Test that a POST HTTPS request with a good referer is accepted
  210. where the referer contains no trailing slash
  211. """
  212. # See ticket #15617
  213. req = self._get_POST_request_with_token()
  214. req._is_secure = True
  215. req.META['HTTP_HOST'] = 'www.example.com'
  216. req.META['HTTP_REFERER'] = 'https://www.example.com'
  217. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  218. self.assertEqual(None, req2)