PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/csrf_tests/tests.py

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