예제 #1
0
 def test_csrf_exempt(self):
     # This is an odd test. We're testing that, when a view is csrf_exempt,
     # process_view will bail without performing any processing.
     request = RequestFactory().post('/', HTTP_X_CSRFTOKEN="aB$AHM")
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, csrf_exempt(test_view), (), {})
     self.assertEqual("aB$AHM", request.META['HTTP_X_CSRFTOKEN'])
예제 #2
0
 def test_header_encoded(self):
     request = RequestFactory().post(
         '/', HTTP_X_CSRFTOKEN='WaMeyTIUS6hOoTcm$TOKqMT3J0Gx2b15UH1MkRg==',
     )
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertEqual(request.META.get('HTTP_X_CSRFTOKEN'), b'abc123')
예제 #3
0
 def test_header_encoded(self):
     request = RequestFactory().post(
         '/', HTTP_X_CSRFTOKEN='aBcDeF$ACAAdVd1',
     )
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertEqual(request.META.get('HTTP_X_CSRFTOKEN'), 'abc123')
예제 #4
0
 def test_csrf_exempt(self):
     # This is an odd test. We're testing that, when a view is csrf_exempt,
     # process_view will bail without performing any processing.
     request = RequestFactory().post('/', HTTP_X_CSRFTOKEN="aB$AHM")
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, csrf_exempt(test_view), (), {})
     self.assertEqual("aB$AHM", request.META['HTTP_X_CSRFTOKEN'])
예제 #5
0
 def test_encoded(self):
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken': 'aBcDeF$ACAAdVd1'}
     )
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertEqual(request.POST.get('csrfmiddlewaretoken'), 'abc123')
예제 #6
0
 def test_header_encoded(self):
     request = RequestFactory().post(
         '/',
         HTTP_X_CSRFTOKEN='aBcDeF$ACAAdVd1',
     )
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertEqual(request.META.get('HTTP_X_CSRFTOKEN'), 'abc123')
예제 #7
0
 def test_encoded(self):
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken':
             'WaMeyTIUS6hOoTcm$TOKqMT3J0Gx2b15UH1MkRg=='}
     )
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertEqual(request.POST.get('csrfmiddlewaretoken'), 'abc123')
예제 #8
0
 def test_encoded(self):
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken':
             'Ump3NGw2b0t1R1J5VlZiOQ==$sBDBSs99N2pbyLVHloLaLg=='}
     )
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertEqual(request.POST.get('csrfmiddlewaretoken'), 'abc123')
예제 #9
0
 def test_round_trip_loop(self):
     '''
     Checks a wide range of input tokens and keys
     '''
     for _ in range(1000):
         request = RequestFactory().get('/')
         csrf_token = get_random_string(32)
         request.META['CSRF_COOKIE'] = csrf_token
         token = force_text(csrf(request)['csrf_token'])
         request = RequestFactory().post('/',
                                         {'csrfmiddlewaretoken': token})
         middleware = CSRFCryptMiddleware()
         middleware.process_view(request, test_view, (), {})
         self.assertEqual(
             force_text(request.POST.get('csrfmiddlewaretoken')),
             force_text(csrf_token))
예제 #10
0
 def test_round_trip_loop_header(self):
     '''
     Checks a wide range of input tokens and keys
     '''
     for _ in range(1000):
         request = RequestFactory().get('/')
         csrf_token = get_random_string(32)
         request.META['CSRF_COOKIE'] = csrf_token
         token = csrf(request)['csrf_token']
         request = RequestFactory().post(
             '/',
             HTTP_X_CSRFTOKEN=force_text(token),
             HTTP_X_REQUESTED_WITH='XMLHttpRequest')
         middleware = CSRFCryptMiddleware()
         middleware.process_view(request, test_view, (), {})
         self.assertEqual(force_text(request.META.get('HTTP_X_CSRFTOKEN')),
                          force_text(csrf_token))
예제 #11
0
 def test_round_trip_loop(self):
     '''
     Checks a wide range of input tokens and keys
     '''
     for _ in range(1000):
         request = RequestFactory().get('/')
         csrf_token = get_random_string(32)
         request.META['CSRF_COOKIE'] = csrf_token
         token = force_text(csrf(request)['csrf_token'])
         request = RequestFactory().post(
             '/', {'csrfmiddlewaretoken': token})
         middleware = CSRFCryptMiddleware()
         middleware.process_view(request, test_view, (), {})
         self.assertEqual(
             force_text(request.POST.get('csrfmiddlewaretoken')),
             force_text(csrf_token)
         )
예제 #12
0
 def test_round_trip_loop_header(self):
     '''
     Checks a wide range of input tokens and keys
     '''
     for _ in range(1000):
         request = RequestFactory().get('/')
         csrf_token = get_random_string(32)
         request.META['CSRF_COOKIE'] = csrf_token
         token = csrf(request)['csrf_token']
         request = RequestFactory().post(
             '/',
             HTTP_X_CSRFTOKEN=force_text(token),
             HTTP_X_REQUESTED_WITH='XMLHttpRequest'
         )
         middleware = CSRFCryptMiddleware()
         middleware.process_view(request, test_view, (), {})
         self.assertEqual(
             force_text(request.META.get('HTTP_X_CSRFTOKEN')),
             force_text(csrf_token)
         )
예제 #13
0
 def test_mutable_status(self):
     request = RequestFactory().post(
         '/', {'csrfmiddlewaretoken': 'aBcDeF$ACAAdVd1'})
     request.POST._mutable = False
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertFalse(request.POST._mutable)
     request = RequestFactory().post(
         '/', {'csrfmiddlewaretoken': 'aBcDeF$ACAAdVd1'})
     request.POST._mutable = True
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertTrue(request.POST._mutable)
예제 #14
0
 def test_mutable_status(self):
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken': 'aBcDeF$ACAAdVd1'}
     )
     request.POST._mutable = False
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertFalse(request.POST._mutable)
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken': 'aBcDeF$ACAAdVd1'}
     )
     request.POST._mutable = True
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertTrue(request.POST._mutable)
예제 #15
0
 def test_mutable_status(self):
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken':
             'WaMeyTIUS6hOoTcm$TOKqMT3J0Gx2b15UH1MkRg=='}
     )
     request.POST._mutable = False
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertFalse(request.POST._mutable)
     request = RequestFactory().post(
         '/',
         {'csrfmiddlewaretoken':
             'WaMeyTIUS6hOoTcm$TOKqMT3J0Gx2b15UH1MkRg=='}
     )
     request.POST._mutable = True
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertTrue(request.POST._mutable)
예제 #16
0
 def test_tampering(self):
     request = RequestFactory().post(
         '/', {'csrfmiddlewaretoken': '123456$abc'})
     middleware = CSRFCryptMiddleware()
     with self.assertRaises(SuspiciousOperation):
         middleware.process_view(request, test_view, (), {})
예제 #17
0
 def test_header_tampering(self):
     request = RequestFactory().post('/', HTTP_X_CSRFTOKEN='123456$abc')
     middleware = CSRFCryptMiddleware()
     with self.assertRaises(SuspiciousOperation):
         middleware.process_view(request, test_view, (), {})
예제 #18
0
 def test_tampering(self):
     request = RequestFactory().post(
         '/', {'csrfmiddlewaretoken': '123456$abc'})
     middleware = CSRFCryptMiddleware()
     with self.assertRaises(SuspiciousOperation):
         middleware.process_view(request, test_view, (), {})
예제 #19
0
 def test_header_not_encoded(self):
     request = RequestFactory().post('/', HTTP_X_CSRFTOKEN='abc123')
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertEqual(request.META.get('HTTP_X_CSRFTOKEN'), 'abc123')
예제 #20
0
 def test_not_encoded(self):
     request = RequestFactory().post('/', {'csrfmiddlewaretoken': 'abc123'})
     middleware = CSRFCryptMiddleware()
     middleware.process_request(request)
     self.assertEqual(request.POST.get('csrfmiddlewaretoken'), 'abc123')
예제 #21
0
 def test_header_tampering(self):
     request = RequestFactory().post('/', HTTP_X_CSRFTOKEN='123456$abc')
     middleware = CSRFCryptMiddleware()
     with self.assertRaises(SuspiciousOperation):
         middleware.process_view(request, test_view, (), {})
예제 #22
0
 def test_middleware_raises_improperly_configured(self):
     with self.assertRaises(ImproperlyConfigured):
         CSRFCryptMiddleware()
예제 #23
0
 def test_encoded(self):
     request = RequestFactory().post(
         '/', {'csrfmiddlewaretoken': 'aBcDeF$ACAAdVd1'})
     middleware = CSRFCryptMiddleware()
     middleware.process_view(request, test_view, (), {})
     self.assertEqual(request.POST.get('csrfmiddlewaretoken'), 'abc123')