Ticket #13716: 13716.diff
File 13716.diff, 3.3 KB (added by , 14 years ago) |
---|
-
django/middleware/csrf.py
62 62 tag. 63 63 """ 64 64 def process_view(self, request, callback, callback_args, callback_kwargs): 65 if getattr(callback, 'csrf_exempt', False):66 return None67 68 65 if getattr(request, 'csrf_processing_done', False): 69 66 return None 70 67 … … 90 87 # place of a CSRF cookie for this request only. 91 88 cookie_is_new = True 92 89 90 # Wait until request.META["CSRF_COOKIE"] has been manipulated before 91 # bailing out, so that get_token still works 92 if getattr(callback, 'csrf_exempt', False): 93 return None 94 93 95 if request.method == 'POST': 94 96 if getattr(request, '_dont_enforce_csrf_checks', False): 95 97 # Mechanism to turn off CSRF checks for test suite. It comes after -
tests/regressiontests/csrf_tests/tests.py
3 3 from django.test import TestCase 4 4 from django.http import HttpRequest, HttpResponse 5 5 from django.middleware.csrf import CsrfMiddleware, CsrfViewMiddleware 6 from django.views.decorators.csrf import csrf_exempt 6 from django.views.decorators.csrf import csrf_exempt, csrf_view_exempt 7 7 from django.core.context_processors import csrf 8 8 from django.contrib.sessions.middleware import SessionMiddleware 9 9 from django.utils.importlib import import_module … … 123 123 # Check the Vary header got patched correctly 124 124 self.assert_('Cookie' in resp2.get('Vary','')) 125 125 126 def test_process_response_view_exempt(self): 127 """ 128 Check that a view decorated with 'csrf_view_exempt' is still 129 post-processed to add the CSRF token. 130 """ 131 req = self._get_GET_no_csrf_cookie_request() 132 CsrfMiddleware().process_view(req, csrf_view_exempt(post_form_view), (), {}) 133 134 resp = post_form_response() 135 resp_content = resp.content # needed because process_response modifies resp 136 resp2 = CsrfMiddleware().process_response(req, resp) 137 138 csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False) 139 self.assertNotEqual(csrf_cookie, False) 140 self.assertNotEqual(resp_content, resp2.content) 141 self._check_token_present(resp2, csrf_cookie.value) 142 126 143 def test_process_response_no_csrf_cookie_view_only_get_token_used(self): 127 144 """ 128 145 When no prior CSRF cookie exists, check that the cookie is created, even … … 279 296 resp = token_view(req) 280 297 self._check_token_present(resp) 281 298 299 def test_get_token_for_exempt_view(self): 300 """ 301 Check that get_token still works for a view decorated with 'csrf_view_exempt'. 302 """ 303 req = self._get_GET_csrf_cookie_request() 304 CsrfViewMiddleware().process_view(req, csrf_view_exempt(token_view), (), {}) 305 resp = token_view(req) 306 self._check_token_present(resp) 307 282 308 def test_token_node_with_new_csrf_cookie(self): 283 309 """ 284 310 Check that CsrfTokenNode works when a CSRF cookie is created by