@@ -90,6 +90,7 @@ def test_process_response_get_token_not_used(self):
90
90
# does use the csrf request processor. By using this, we are testing
91
91
# that the view processor is properly lazy and doesn't call get_token()
92
92
# until needed.
93
+ self .mw .process_request (req )
93
94
self .mw .process_view (req , non_token_view_using_request_processor , (), {})
94
95
resp = non_token_view_using_request_processor (req )
95
96
resp2 = self .mw .process_response (req , resp )
@@ -105,6 +106,7 @@ def test_process_request_no_csrf_cookie(self):
105
106
"""
106
107
with patch_logger ('django.security.csrf' , 'warning' ) as logger_calls :
107
108
req = self ._get_POST_no_csrf_cookie_request ()
109
+ self .mw .process_request (req )
108
110
req2 = self .mw .process_view (req , post_form_view , (), {})
109
111
self .assertEqual (403 , req2 .status_code )
110
112
self .assertEqual (logger_calls [0 ], 'Forbidden (%s): ' % REASON_NO_CSRF_COOKIE )
@@ -116,6 +118,7 @@ def test_process_request_csrf_cookie_no_token(self):
116
118
"""
117
119
with patch_logger ('django.security.csrf' , 'warning' ) as logger_calls :
118
120
req = self ._get_POST_csrf_cookie_request ()
121
+ self .mw .process_request (req )
119
122
req2 = self .mw .process_view (req , post_form_view , (), {})
120
123
self .assertEqual (403 , req2 .status_code )
121
124
self .assertEqual (logger_calls [0 ], 'Forbidden (%s): ' % REASON_BAD_TOKEN )
@@ -125,6 +128,7 @@ def test_process_request_csrf_cookie_and_token(self):
125
128
If both a cookie and a token is present, the middleware lets it through.
126
129
"""
127
130
req = self ._get_POST_request_with_token ()
131
+ self .mw .process_request (req )
128
132
req2 = self .mw .process_view (req , post_form_view , (), {})
129
133
self .assertIsNone (req2 )
130
134
@@ -134,6 +138,7 @@ def test_process_request_csrf_cookie_no_token_exempt_view(self):
134
138
has been applied to the view, the middleware lets it through
135
139
"""
136
140
req = self ._get_POST_csrf_cookie_request ()
141
+ self .mw .process_request (req )
137
142
req2 = self .mw .process_view (req , csrf_exempt (post_form_view ), (), {})
138
143
self .assertIsNone (req2 )
139
144
@@ -143,6 +148,7 @@ def test_csrf_token_in_header(self):
143
148
"""
144
149
req = self ._get_POST_csrf_cookie_request ()
145
150
req .META ['HTTP_X_CSRFTOKEN' ] = self ._csrf_id
151
+ self .mw .process_request (req )
146
152
req2 = self .mw .process_view (req , post_form_view , (), {})
147
153
self .assertIsNone (req2 )
148
154
@@ -153,6 +159,7 @@ def test_csrf_token_in_header_with_customized_name(self):
153
159
"""
154
160
req = self ._get_POST_csrf_cookie_request ()
155
161
req .META ['HTTP_X_CSRFTOKEN_CUSTOMIZED' ] = self ._csrf_id
162
+ self .mw .process_request (req )
156
163
req2 = self .mw .process_view (req , post_form_view , (), {})
157
164
self .assertIsNone (req2 )
158
165
@@ -181,12 +188,14 @@ def test_put_and_delete_allowed(self):
181
188
req = self ._get_GET_csrf_cookie_request ()
182
189
req .method = 'PUT'
183
190
req .META ['HTTP_X_CSRFTOKEN' ] = self ._csrf_id
191
+ self .mw .process_request (req )
184
192
req2 = self .mw .process_view (req , post_form_view , (), {})
185
193
self .assertIsNone (req2 )
186
194
187
195
req = self ._get_GET_csrf_cookie_request ()
188
196
req .method = 'DELETE'
189
197
req .META ['HTTP_X_CSRFTOKEN' ] = self ._csrf_id
198
+ self .mw .process_request (req )
190
199
req2 = self .mw .process_view (req , post_form_view , (), {})
191
200
self .assertIsNone (req2 )
192
201
@@ -220,6 +229,7 @@ def test_token_node_with_csrf_cookie(self):
220
229
CsrfTokenNode works when a CSRF cookie is set.
221
230
"""
222
231
req = self ._get_GET_csrf_cookie_request ()
232
+ self .mw .process_request (req )
223
233
self .mw .process_view (req , token_view , (), {})
224
234
resp = token_view (req )
225
235
self ._check_token_present (resp )
@@ -229,6 +239,7 @@ def test_get_token_for_exempt_view(self):
229
239
get_token still works for a view decorated with 'csrf_exempt'.
230
240
"""
231
241
req = self ._get_GET_csrf_cookie_request ()
242
+ self .mw .process_request (req )
232
243
self .mw .process_view (req , csrf_exempt (token_view ), (), {})
233
244
resp = token_view (req )
234
245
self ._check_token_present (resp )
@@ -260,6 +271,7 @@ def test_cookie_not_reset_on_accepted_request(self):
260
271
requests. If it appears in the response, it should keep its value.
261
272
"""
262
273
req = self ._get_POST_request_with_token ()
274
+ self .mw .process_request (req )
263
275
self .mw .process_view (req , token_view , (), {})
264
276
resp = token_view (req )
265
277
resp = self .mw .process_response (req , resp )
@@ -333,6 +345,7 @@ def test_https_good_referer(self):
333
345
req ._is_secure_override = True
334
346
req .META ['HTTP_HOST' ] = 'www.example.com'
335
347
req .META ['HTTP_REFERER' ] = 'https://www.example.com/somepage'
348
+ self .mw .process_request (req )
336
349
req2 = self .mw .process_view (req , post_form_view , (), {})
337
350
self .assertIsNone (req2 )
338
351
@@ -347,6 +360,7 @@ def test_https_good_referer_2(self):
347
360
req ._is_secure_override = True
348
361
req .META ['HTTP_HOST' ] = 'www.example.com'
349
362
req .META ['HTTP_REFERER' ] = 'https://www.example.com'
363
+ self .mw .process_request (req )
350
364
req2 = self .mw .process_view (req , post_form_view , (), {})
351
365
self .assertIsNone (req2 )
352
366
@@ -360,6 +374,7 @@ def _test_https_good_referer_behind_proxy(self):
360
374
'HTTP_X_FORWARDED_HOST' : 'www.example.com' ,
361
375
'HTTP_X_FORWARDED_PORT' : '443' ,
362
376
})
377
+ self .mw .process_request (req )
363
378
req2 = self .mw .process_view (req , post_form_view , (), {})
364
379
self .assertIsNone (req2 )
365
380
@@ -373,6 +388,7 @@ def test_https_csrf_trusted_origin_allowed(self):
373
388
req ._is_secure_override = True
374
389
req .META ['HTTP_HOST' ] = 'www.example.com'
375
390
req .META ['HTTP_REFERER' ] = 'https://dashboard.example.com'
391
+ self .mw .process_request (req )
376
392
req2 = self .mw .process_view (req , post_form_view , (), {})
377
393
self .assertIsNone (req2 )
378
394
@@ -386,6 +402,7 @@ def test_https_csrf_wildcard_trusted_origin_allowed(self):
386
402
req ._is_secure_override = True
387
403
req .META ['HTTP_HOST' ] = 'www.example.com'
388
404
req .META ['HTTP_REFERER' ] = 'https://dashboard.example.com'
405
+ self .mw .process_request (req )
389
406
response = self .mw .process_view (req , post_form_view , (), {})
390
407
self .assertIsNone (response )
391
408
@@ -394,6 +411,7 @@ def _test_https_good_referer_matches_cookie_domain(self):
394
411
req ._is_secure_override = True
395
412
req .META ['HTTP_REFERER' ] = 'https://foo.example.com/'
396
413
req .META ['SERVER_PORT' ] = '443'
414
+ self .mw .process_request (req )
397
415
response = self .mw .process_view (req , post_form_view , (), {})
398
416
self .assertIsNone (response )
399
417
@@ -403,6 +421,7 @@ def _test_https_good_referer_matches_cookie_domain_with_different_port(self):
403
421
req .META ['HTTP_HOST' ] = 'www.example.com'
404
422
req .META ['HTTP_REFERER' ] = 'https://foo.example.com:4443/'
405
423
req .META ['SERVER_PORT' ] = '4443'
424
+ self .mw .process_request (req )
406
425
response = self .mw .process_view (req , post_form_view , (), {})
407
426
self .assertIsNone (response )
408
427
@@ -467,11 +486,13 @@ def _set_post(self, post):
467
486
token = ('ABC' + self ._csrf_id )[:CSRF_TOKEN_LENGTH ]
468
487
469
488
req = CsrfPostRequest (token , raise_error = False )
489
+ self .mw .process_request (req )
470
490
resp = self .mw .process_view (req , post_form_view , (), {})
471
491
self .assertIsNone (resp )
472
492
473
493
req = CsrfPostRequest (token , raise_error = True )
474
494
with patch_logger ('django.security.csrf' , 'warning' ) as logger_calls :
495
+ self .mw .process_request (req )
475
496
resp = self .mw .process_view (req , post_form_view , (), {})
476
497
self .assertEqual (resp .status_code , 403 )
477
498
self .assertEqual (logger_calls [0 ], 'Forbidden (%s): ' % REASON_BAD_TOKEN )
@@ -609,6 +630,7 @@ def test_bare_secret_accepted_and_replaced(self):
609
630
The csrf token is reset from a bare secret.
610
631
"""
611
632
req = self ._get_POST_bare_secret_csrf_cookie_request_with_token ()
633
+ self .mw .process_request (req )
612
634
req2 = self .mw .process_view (req , token_view , (), {})
613
635
self .assertIsNone (req2 )
614
636
resp = token_view (req )
@@ -680,7 +702,7 @@ def test_no_session_on_request(self):
680
702
'SessionMiddleware must appear before CsrfViewMiddleware in MIDDLEWARE.'
681
703
)
682
704
with self .assertRaisesMessage (ImproperlyConfigured , msg ):
683
- self .mw .process_view (HttpRequest (), None , (), {} )
705
+ self .mw .process_request (HttpRequest ())
684
706
685
707
def test_process_response_get_token_used (self ):
686
708
"""The ensure_csrf_cookie() decorator works without middleware."""
@@ -754,3 +776,16 @@ def test_https_reject_insecure_referer(self):
754
776
'Referer checking failed - Referer is insecure while host is secure.' ,
755
777
status_code = 403 ,
756
778
)
779
+
780
+
781
+ @override_settings (ROOT_URLCONF = 'csrf_tests.csrf_token_error_handler_urls' , DEBUG = False )
782
+ class CsrfInErrorHandlingViewsTests (SimpleTestCase ):
783
+ def test_csrf_token_on_404_stays_constant (self ):
784
+ response = self .client .get ('/does not exist/' )
785
+ # The error handler returns status code 599.
786
+ self .assertEqual (response .status_code , 599 )
787
+ token1 = response .content
788
+ response = self .client .get ('/does not exist/' )
789
+ self .assertEqual (response .status_code , 599 )
790
+ token2 = response .content
791
+ self .assertTrue (equivalent_tokens (token1 .decode ('ascii' ), token2 .decode ('ascii' )))
0 commit comments