Ticket #9172: 9172_r9084.diff
File 9172_r9084.diff, 5.3 KB (added by , 16 years ago) |
---|
-
django/test/client.py
18 18 from django.utils.encoding import smart_str 19 19 from django.utils.http import urlencode 20 20 from django.utils.itercompat import is_iterable 21 from django.contrib.csrf.middleware import CSRF_TOKEN_NAME, _make_token 21 22 22 23 BOUNDARY = 'BoUnDaRyStRiNg' 23 24 MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY … … 268 269 """ 269 270 Requests a response from the server using POST. 270 271 """ 272 if ('django.contrib.csrf.middleware.CsrfMiddleware' in 273 settings.MIDDLEWARE_CLASSES and 274 hasattr(self.session, 'session_key')): 275 data[CSRF_TOKEN_NAME] = _make_token(self.session.session_key) 276 271 277 if content_type is MULTIPART_CONTENT: 272 278 post_data = encode_multipart(BOUNDARY, data) 273 279 else: -
django/contrib/csrf/middleware.py
13 13 from django.utils.hashcompat import md5_constructor 14 14 from django.utils.safestring import mark_safe 15 15 16 CSRF_TOKEN_NAME = 'csrfmiddlewaretoken' 17 16 18 _ERROR_MSG = mark_safe('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><body><h1>403 Forbidden</h1><p>Cross Site Request Forgery detected. Request aborted.</p></body></html>') 17 19 18 20 _POST_FORM_RE = \ … … 52 54 csrf_token = _make_token(session_id) 53 55 # check incoming token 54 56 try: 55 request_csrf_token = request.POST[ 'csrfmiddlewaretoken']57 request_csrf_token = request.POST[CSRF_TOKEN_NAME] 56 58 except KeyError: 57 59 return HttpResponseForbidden(_ERROR_MSG) 58 60 … … 80 82 response['Content-Type'].split(';')[0] in _HTML_TYPES: 81 83 82 84 # ensure we don't add the 'id' attribute twice (HTML validity) 83 idattributes = itertools.chain(("id=' csrfmiddlewaretoken'",),85 idattributes = itertools.chain(("id='%s'" % (CSRF_TOKEN_NAME,),), 84 86 itertools.repeat('')) 85 87 def add_csrf_field(match): 86 88 """Returns the matched <form> tag plus the added <input> element""" 87 89 return mark_safe(match.group() + "<div style='display:none;'>" + \ 88 90 "<input type='hidden' " + idattributes.next() + \ 89 " name=' csrfmiddlewaretoken' value='" + csrf_token + \91 " name='" + CSRF_TOKEN_NAME + "' value='" + csrf_token + \ 90 92 "' /></div>") 91 93 92 94 # Modify any POST forms -
tests/regressiontests/test_client_regress/models.py
382 382 response = self.client.get('/test_client_regress/check_session/') 383 383 self.assertEqual(response.status_code, 200) 384 384 self.assertEqual(response.content, 'YES') 385 386 No newline at end of file 385 386 class CsrfMiddlewareAvoidanceTests(TestCase): 387 fixtures = ['testdata.json'] 388 389 def setUp(self): 390 self.old_middleware_classes = settings.MIDDLEWARE_CLASSES 391 settings.MIDDLEWARE_CLASSES = ( 392 'django.contrib.csrf.middleware.CsrfMiddleware', 393 'django.contrib.sessions.middleware.SessionMiddleware', 394 'django.contrib.auth.middleware.AuthenticationMiddleware', 395 'django.middleware.common.CommonMiddleware', 396 ) 397 398 def tearDown(self): 399 settings.MIDDLEWARE_CLASSES = self.old_middleware_classes 400 401 def test_circumvent_csrf_middleware(self): 402 # Csrf middleware only activates if there is a session 403 login = self.client.login(username='testclient',password='password') 404 self.failUnless(login, 'Could not log in') 405 406 response = self.client.post('/test_client_regress/no_template_view/', 407 {'some': 'post data'}) 408 409 # if we'd gotten caught by CsrfMiddleware, we'd get a 403 410 self.assertEqual(response.status_code, 200) 411 self.client.logout() 412 413 def test_logged_out_post_still_works(self): 414 response = self.client.post('/test_client_regress/no_template_view/', 415 {'some': 'post data'}) 416 self.assertEqual(response.status_code, 200) -
docs/ref/contrib/csrf.txt
73 73 you might bypass the filter that adds the hidden field to the form, 74 74 in which case form submission will always fail. It may still be possible 75 75 to use the middleware, provided you can find some way to get the 76 CSRF token and ensure that is included when your form is submitted. 77 No newline at end of file 76 CSRF token and ensure that is included when your form is submitted. 77 78 Testing 79 ======= 80 81 The :mod:`Django test client <django.test.client>` automatically 82 bypasses CsrfMiddleware, for easier testing of POST requests.