tests.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # -*- coding: utf-8 -*-
  2. from django.test import TestCase
  3. from django.http import HttpRequest, HttpResponse
  4. from django.middleware.csrf import CsrfMiddleware, CsrfViewMiddleware
  5. from django.views.decorators.csrf import csrf_exempt
  6. from django.core.context_processors import csrf
  7. from django.contrib.sessions.middleware import SessionMiddleware
  8. from django.utils.importlib import import_module
  9. from django.conf import settings
  10. from django.template import RequestContext, Template
  11. # Response/views used for CsrfResponseMiddleware and CsrfViewMiddleware tests
  12. def post_form_response():
  13. resp = HttpResponse(content="""
  14. <html><body><form method="POST"><input type="text" /></form></body></html>
  15. """, mimetype="text/html")
  16. return resp
  17. def post_form_response_non_html():
  18. resp = post_form_response()
  19. resp["Content-Type"] = "application/xml"
  20. return resp
  21. def post_form_view(request):
  22. """A view that returns a POST form (without a token)"""
  23. return post_form_response()
  24. # Response/views used for template tag tests
  25. def _token_template():
  26. return Template("{% csrf_token %}")
  27. def _render_csrf_token_template(req):
  28. context = RequestContext(req, processors=[csrf])
  29. template = _token_template()
  30. return template.render(context)
  31. def token_view(request):
  32. """A view that uses {% csrf_token %}"""
  33. return HttpResponse(_render_csrf_token_template(request))
  34. def non_token_view_using_request_processor(request):
  35. """
  36. A view that doesn't use the token, but does use the csrf view processor.
  37. """
  38. context = RequestContext(request, processors=[csrf])
  39. template = Template("")
  40. return HttpResponse(template.render(context))
  41. class TestingHttpRequest(HttpRequest):
  42. """
  43. A version of HttpRequest that allows us to change some things
  44. more easily
  45. """
  46. def is_secure(self):
  47. return getattr(self, '_is_secure', False)
  48. class CsrfMiddlewareTest(TestCase):
  49. _csrf_id = "1"
  50. # This is a valid session token for this ID and secret key. This was generated using
  51. # the old code that we're to be backwards-compatible with. Don't use the CSRF code
  52. # to generate this hash, or we're merely testing the code against itself and not
  53. # checking backwards-compatibility. This is also the output of (echo -n test1 | md5sum).
  54. _session_token = "5a105e8b9d40e1329780d62ea2265d8a"
  55. _session_id = "1"
  56. _secret_key_for_session_test= "test"
  57. def _get_GET_no_csrf_cookie_request(self):
  58. return TestingHttpRequest()
  59. def _get_GET_csrf_cookie_request(self):
  60. req = TestingHttpRequest()
  61. req.COOKIES[settings.CSRF_COOKIE_NAME] = self._csrf_id
  62. return req
  63. def _get_POST_csrf_cookie_request(self):
  64. req = self._get_GET_csrf_cookie_request()
  65. req.method = "POST"
  66. return req
  67. def _get_POST_no_csrf_cookie_request(self):
  68. req = self._get_GET_no_csrf_cookie_request()
  69. req.method = "POST"
  70. return req
  71. def _get_POST_request_with_token(self):
  72. req = self._get_POST_csrf_cookie_request()
  73. req.POST['csrfmiddlewaretoken'] = self._csrf_id
  74. return req
  75. def _get_POST_session_request_with_token(self):
  76. req = self._get_POST_no_csrf_cookie_request()
  77. req.COOKIES[settings.SESSION_COOKIE_NAME] = self._session_id
  78. req.POST['csrfmiddlewaretoken'] = self._session_token
  79. return req
  80. def _get_POST_session_request_no_token(self):
  81. req = self._get_POST_no_csrf_cookie_request()
  82. req.COOKIES[settings.SESSION_COOKIE_NAME] = self._session_id
  83. return req
  84. def _check_token_present(self, response, csrf_id=None):
  85. self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % (csrf_id or self._csrf_id))
  86. # Check the post processing and outgoing cookie
  87. def test_process_response_no_csrf_cookie(self):
  88. """
  89. When no prior CSRF cookie exists, check that the cookie is created and a
  90. token is inserted.
  91. """
  92. req = self._get_GET_no_csrf_cookie_request()
  93. CsrfMiddleware().process_view(req, post_form_view, (), {})
  94. resp = post_form_response()
  95. resp_content = resp.content # needed because process_response modifies resp
  96. resp2 = CsrfMiddleware().process_response(req, resp)
  97. csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
  98. self.assertNotEqual(csrf_cookie, False)
  99. self.assertNotEqual(resp_content, resp2.content)
  100. self._check_token_present(resp2, csrf_cookie.value)
  101. # Check the Vary header got patched correctly
  102. self.assert_('Cookie' in resp2.get('Vary',''))
  103. def test_process_response_no_csrf_cookie_view_only_get_token_used(self):
  104. """
  105. When no prior CSRF cookie exists, check that the cookie is created, even
  106. if only CsrfViewMiddleware is used.
  107. """
  108. # This is checking that CsrfViewMiddleware has the cookie setting
  109. # code. Most of the other tests use CsrfMiddleware.
  110. req = self._get_GET_no_csrf_cookie_request()
  111. # token_view calls get_token() indirectly
  112. CsrfViewMiddleware().process_view(req, token_view, (), {})
  113. resp = token_view(req)
  114. resp2 = CsrfViewMiddleware().process_response(req, resp)
  115. csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
  116. self.assertNotEqual(csrf_cookie, False)
  117. def test_process_response_get_token_not_used(self):
  118. """
  119. Check that if get_token() is not called, the view middleware does not
  120. add a cookie.
  121. """
  122. # This is important to make pages cacheable. Pages which do call
  123. # get_token(), assuming they use the token, are not cacheable because
  124. # the token is specific to the user
  125. req = self._get_GET_no_csrf_cookie_request()
  126. # non_token_view_using_request_processor does not call get_token(), but
  127. # does use the csrf request processor. By using this, we are testing
  128. # that the view processor is properly lazy and doesn't call get_token()
  129. # until needed.
  130. CsrfViewMiddleware().process_view(req, non_token_view_using_request_processor, (), {})
  131. resp = non_token_view_using_request_processor(req)
  132. resp2 = CsrfViewMiddleware().process_response(req, resp)
  133. csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False)
  134. self.assertEqual(csrf_cookie, False)
  135. def test_process_response_existing_csrf_cookie(self):
  136. """
  137. Check that the token is inserted when a prior CSRF cookie exists
  138. """
  139. req = self._get_GET_csrf_cookie_request()
  140. CsrfMiddleware().process_view(req, post_form_view, (), {})
  141. resp = post_form_response()
  142. resp_content = resp.content # needed because process_response modifies resp
  143. resp2 = CsrfMiddleware().process_response(req, resp)
  144. self.assertNotEqual(resp_content, resp2.content)
  145. self._check_token_present(resp2)
  146. def test_process_response_non_html(self):
  147. """
  148. Check the the post-processor does nothing for content-types not in _HTML_TYPES.
  149. """
  150. req = self._get_GET_no_csrf_cookie_request()
  151. CsrfMiddleware().process_view(req, post_form_view, (), {})
  152. resp = post_form_response_non_html()
  153. resp_content = resp.content # needed because process_response modifies resp
  154. resp2 = CsrfMiddleware().process_response(req, resp)
  155. self.assertEquals(resp_content, resp2.content)
  156. def test_process_response_exempt_view(self):
  157. """
  158. Check that no post processing is done for an exempt view
  159. """
  160. req = self._get_POST_csrf_cookie_request()
  161. resp = csrf_exempt(post_form_view)(req)
  162. resp_content = resp.content
  163. resp2 = CsrfMiddleware().process_response(req, resp)
  164. self.assertEquals(resp_content, resp2.content)
  165. # Check the request processing
  166. def test_process_request_no_session_no_csrf_cookie(self):
  167. """
  168. Check that if neither a CSRF cookie nor a session cookie are present,
  169. the middleware rejects the incoming request. This will stop login CSRF.
  170. """
  171. req = self._get_POST_no_csrf_cookie_request()
  172. req2 = CsrfMiddleware().process_view(req, post_form_view, (), {})
  173. self.assertEquals(403, req2.status_code)
  174. def test_process_request_csrf_cookie_no_token(self):
  175. """
  176. Check that if a CSRF cookie is present but no token, the middleware
  177. rejects the incoming request.
  178. """
  179. req = self._get_POST_csrf_cookie_request()
  180. req2 = CsrfMiddleware().process_view(req, post_form_view, (), {})
  181. self.assertEquals(403, req2.status_code)
  182. def test_process_request_csrf_cookie_and_token(self):
  183. """
  184. Check that if both a cookie and a token is present, the middleware lets it through.
  185. """
  186. req = self._get_POST_request_with_token()
  187. req2 = CsrfMiddleware().process_view(req, post_form_view, (), {})
  188. self.assertEquals(None, req2)
  189. def test_process_request_session_cookie_no_csrf_cookie_token(self):
  190. """
  191. When no CSRF cookie exists, but the user has a session, check that a token
  192. using the session cookie as a legacy CSRF cookie is accepted.
  193. """
  194. orig_secret_key = settings.SECRET_KEY
  195. settings.SECRET_KEY = self._secret_key_for_session_test
  196. try:
  197. req = self._get_POST_session_request_with_token()
  198. req2 = CsrfMiddleware().process_view(req, post_form_view, (), {})
  199. self.assertEquals(None, req2)
  200. finally:
  201. settings.SECRET_KEY = orig_secret_key
  202. def test_process_request_session_cookie_no_csrf_cookie_no_token(self):
  203. """
  204. Check that if a session cookie is present but no token and no CSRF cookie,
  205. the request is rejected.
  206. """
  207. req = self._get_POST_session_request_no_token()
  208. req2 = CsrfMiddleware().process_view(req, post_form_view, (), {})
  209. self.assertEquals(403, req2.status_code)
  210. def test_process_request_csrf_cookie_no_token_exempt_view(self):
  211. """
  212. Check that if a CSRF cookie is present and no token, but the csrf_exempt
  213. decorator has been applied to the view, the middleware lets it through
  214. """
  215. req = self._get_POST_csrf_cookie_request()
  216. req2 = CsrfMiddleware().process_view(req, csrf_exempt(post_form_view), (), {})
  217. self.assertEquals(None, req2)
  218. def test_ajax_exemption(self):
  219. """
  220. Check that AJAX requests are automatically exempted.
  221. """
  222. req = self._get_POST_csrf_cookie_request()
  223. req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
  224. req2 = CsrfMiddleware().process_view(req, post_form_view, (), {})
  225. self.assertEquals(None, req2)
  226. # Tests for the template tag method
  227. def test_token_node_no_csrf_cookie(self):
  228. """
  229. Check that CsrfTokenNode works when no CSRF cookie is set
  230. """
  231. req = self._get_GET_no_csrf_cookie_request()
  232. resp = token_view(req)
  233. self.assertEquals(u"", resp.content)
  234. def test_token_node_with_csrf_cookie(self):
  235. """
  236. Check that CsrfTokenNode works when a CSRF cookie is set
  237. """
  238. req = self._get_GET_csrf_cookie_request()
  239. CsrfViewMiddleware().process_view(req, token_view, (), {})
  240. resp = token_view(req)
  241. self._check_token_present(resp)
  242. def test_token_node_with_new_csrf_cookie(self):
  243. """
  244. Check that CsrfTokenNode works when a CSRF cookie is created by
  245. the middleware (when one was not already present)
  246. """
  247. req = self._get_GET_no_csrf_cookie_request()
  248. CsrfViewMiddleware().process_view(req, token_view, (), {})
  249. resp = token_view(req)
  250. resp2 = CsrfViewMiddleware().process_response(req, resp)
  251. csrf_cookie = resp2.cookies[settings.CSRF_COOKIE_NAME]
  252. self._check_token_present(resp, csrf_id=csrf_cookie.value)
  253. def test_response_middleware_without_view_middleware(self):
  254. """
  255. Check that CsrfResponseMiddleware finishes without error if the view middleware
  256. has not been called, as is the case if a request middleware returns a response.
  257. """
  258. req = self._get_GET_no_csrf_cookie_request()
  259. resp = post_form_view(req)
  260. CsrfMiddleware().process_response(req, resp)
  261. def test_https_bad_referer(self):
  262. """
  263. Test that a POST HTTPS request with a bad referer is rejected
  264. """
  265. req = self._get_POST_request_with_token()
  266. req._is_secure = True
  267. req.META['HTTP_HOST'] = 'www.example.com'
  268. req.META['HTTP_REFERER'] = 'https://www.evil.org/somepage'
  269. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  270. self.assertNotEqual(None, req2)
  271. self.assertEquals(403, req2.status_code)
  272. def test_https_good_referer(self):
  273. """
  274. Test that a POST HTTPS request with a good referer is accepted
  275. """
  276. req = self._get_POST_request_with_token()
  277. req._is_secure = True
  278. req.META['HTTP_HOST'] = 'www.example.com'
  279. req.META['HTTP_REFERER'] = 'https://www.example.com/somepage'
  280. req2 = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
  281. self.assertEquals(None, req2)