-
Notifications
You must be signed in to change notification settings - Fork 49
/
test_seasurf.py
839 lines (647 loc) · 27.4 KB
/
test_seasurf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
from __future__ import with_statement
import mock
import unittest
from flask import Flask, render_template_string, request
from flask_seasurf import SeaSurf, REASON_NO_REQUEST
from werkzeug.exceptions import Forbidden
from werkzeug.http import parse_cookie
b = lambda s: s.encode('utf-8')
def get_cookie(response, cookie_name):
cookies = response.headers.getlist('Set-Cookie')
for cookie in cookies:
value = parse_cookie(cookie).get(cookie_name)
if value:
return value
return None
class BaseTestCase(unittest.TestCase):
# Methods for backwards compatibility with python 2.5 & 2.6
def assertIn(self, value, container, err=None):
self.assertTrue(value in container, err)
def assertNotIn(self, value, container, err=None):
self.assertTrue(value not in container, err)
def assertIsNotNone(self, value, err=None):
self.assertNotEqual(value, None, err)
class SeaSurfTestCase(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@csrf.exempt
@app.route('/foo', methods=['POST'])
@app.route('/foo/<term>', methods=['POST'])
def foo(term=None):
return 'bar'
@app.route('/bar', methods=['POST'])
@app.route('/bar/<term>', methods=['POST'])
def bar(term=None):
return 'foo'
def test_generate_token(self):
self.assertIsNotNone(self.csrf._generate_token())
def test_unique_generation(self):
token_a = self.csrf._generate_token()
token_b = self.csrf._generate_token()
self.assertNotEqual(token_a, token_b)
def test_token_is_string(self):
token = self.csrf._generate_token()
self.assertEqual(type(token), str)
def test_exempt_view(self):
rv = self.app.test_client().post('/foo')
self.assertIn(b('bar'), rv.data)
rv = self.app.test_client().post(u'/foo/\xf8')
self.assertIn(b('bar'), rv.data)
def test_token_validation(self):
# should produce a logger warning
rv = self.app.test_client().post('/bar')
self.assertIn(b('403 Forbidden'), rv.data)
rv = self.app.test_client().post(u'/bar/\xf8')
self.assertIn(b('403 Forbidden'), rv.data)
def test_json_token_validation_bad(self):
"""Should fail with 403 JSON _csrf_token differers from session token"""
tokenA = self.csrf._generate_token()
tokenB = self.csrf._generate_token()
data = {'_csrf_token': tokenB}
with self.app.test_client() as client:
with client.session_transaction() as sess:
sess[self.csrf._csrf_name] = tokenA
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=tokenB)
rv = client.post('/bar', data=data)
self.assertEqual(rv.status_code, 403, rv)
rv = client.post(u'/bar/\xf8', data=data)
self.assertEqual(rv.status_code, 403, rv)
def test_json_token_validation_good(self):
"""Should succeed error if JSON has _csrf_token set"""
token = self.csrf._generate_token()
data = {'_csrf_token': token}
with self.app.test_client() as client:
with client.session_transaction() as sess:
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=token)
sess[self.csrf._csrf_name] = token
rv = client.post('/bar', data=data)
self.assertEqual(rv.status_code, 200, rv)
rv = client.post(u'/bar/\xf8', data=data)
self.assertEqual(rv.status_code, 200, rv)
def test_token_with_non_ascii_chars(self):
"""Should fail with 403"""
tokenA = self.csrf._generate_token()
tokenB = '🥳'
data = {'_csrf_token': tokenB}
with self.app.test_client() as client:
with client.session_transaction() as sess:
sess[self.csrf._csrf_name] = tokenA
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=tokenB)
rv = client.post('/bar', data=data)
self.assertEqual(rv.status_code, 403, rv)
def test_https_bad_referer(self):
with self.app.test_client() as client:
with client.session_transaction() as sess:
token = self.csrf._generate_token()
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=token)
sess[self.csrf._csrf_name] = token
# once this is reached the session was stored
rv = client.post('/bar',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': 'https://www.evil.com/foobar'})
self.assertEqual(403, rv.status_code)
rv = client.post(u'/bar/\xf8',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': u'https://www.evil.com/\xf8'})
self.assertEqual(403, rv.status_code)
def test_https_good_referer(self):
with self.app.test_client() as client:
with client.session_transaction(base_url="https://www.example.com") as sess:
token = self.csrf._generate_token()
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=token)
sess[self.csrf._csrf_name] = token
# once this is reached the session was stored
rv = client.post('/bar',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': 'https://www.example.com/foobar'})
self.assertEqual(rv.status_code, 200)
rv = client.post(u'/bar/\xf8',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': 'https://www.example.com/foobar\xf8'})
self.assertEqual(rv.status_code, 200)
def test_malformed_referer(self):
with self.app.test_client() as client:
with client.session_transaction() as sess:
token = self.csrf._generate_token()
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=token)
sess[self.csrf._csrf_name] = token
rv = client.post('/bar',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': u'https://foobar:abc'})
self.assertEqual(403, rv.status_code)
def test_token_in_header(self):
with self.app.test_client() as client:
with client.session_transaction() as sess:
token = self.csrf._generate_token()
sess[self.csrf._csrf_name] = token
headers = {
self.csrf._csrf_header_name: token,
}
rv = client.post('/bar', headers=headers)
self.assertEqual(rv.status_code, 200, rv)
rv = client.post(u'/bar/\xf8', headers=headers)
self.assertEqual(rv.status_code, 200, rv)
def test_token_in_form_data(self):
with self.app.test_client() as client:
with client.session_transaction() as sess:
token = self.csrf._generate_token()
sess[self.csrf._csrf_name] = token
data = '{0}={1}'.format(self.csrf._csrf_name, token)
content_type = 'application/x-www-form-urlencoded'
rv = client.post('/bar', data=data, content_type=content_type)
self.assertEqual(rv.status_code, 200, rv)
rv = client.post(u'/bar/\xf8', data=data, content_type=content_type)
self.assertEqual(rv.status_code, 200, rv)
def test_invalid_json_does_not_return_400(self):
"""Flask with Python3 raises a BadRequest anytime someone accesses
`request.json` with invalid JSON. Flask-Seasurf should not cause a
400 response when checking for a csrf token.
"""
with self.app.test_client() as client:
with client.session_transaction() as sess:
token = self.csrf._generate_token()
sess[self.csrf._csrf_name] = token
headers = {
self.csrf._csrf_header_name: token,
}
data = '{]]{'
content_type = 'application/json'
rv = client.post('/bar',
data=data,
content_type=content_type,
headers=headers)
self.assertEqual(rv.status_code, 200, rv)
rv = client.post(u'/bar/\xf8',
data=data,
content_type=content_type,
headers=headers)
self.assertEqual(rv.status_code, 200, rv)
def test_cannot_validate_without_request(self):
with self.assertRaises(Forbidden) as ex:
self.csrf.validate()
expected_exception_message = '403 Forbidden: {0}'.format(REASON_NO_REQUEST)
self.assertEqual(str(ex.exception), expected_exception_message)
def test_secrets(self):
try:
import secrets
with mock.patch('secrets.token_hex', return_value='3b0b5a5c1de3c2ed'):
self.assertEqual(self.csrf._generate_token(), '3b0b5a5c1de3c2ed')
except ImportError:
with mock.patch('random.randrange', return_value=4123476):
with mock.patch('random.SystemRandom.randrange', return_value=4123476):
self.assertEqual(self.csrf._generate_token(), 'a22372bcac286ccf659ddda312e6311149edd6b0')
class SeaSurfTestCaseExemptViews(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
app.config['SEASURF_INCLUDE_OR_EXEMPT_VIEWS'] = 'exempt'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@csrf.exempt
@app.route('/foo', methods=['POST'])
def foo():
return 'bar'
@app.route('/bar', methods=['POST'])
def bar():
return 'foo'
def test_exempt_view(self):
with self.app.test_client() as c:
rv = c.post('/foo')
self.assertIn(b('bar'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
self.assertEqual(cookie, None)
def test_token_validation(self):
# should produce a logger warning
rv = self.app.test_client().post('/bar')
self.assertIn(b('403 Forbidden'), rv.data)
class SeaSurfTestCaseIncludeViews(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
app.config['SEASURF_INCLUDE_OR_EXEMPT_VIEWS'] = 'include'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@csrf.include
@app.route('/foo', methods=['POST'])
@app.route('/foo/<term>', methods=['POST'])
def foo(term=None):
return 'bar'
@app.route('/bar', methods=['POST'])
@app.route('/bar/<term>', methods=['POST'])
def bar(term=None):
return 'foo'
def test_include_view(self):
rv = self.app.test_client().post('/foo')
self.assertIn(b('403 Forbidden'), rv.data)
rv = self.app.test_client().post(u'/foo/\xf8')
self.assertIn(b('403 Forbidden'), rv.data)
def test_token_validation(self):
# should produce a logger warning
rv = self.app.test_client().post('/bar')
self.assertIn(b('foo'), rv.data)
rv = self.app.test_client().post(u'/bar/\xf8')
self.assertIn(b('foo'), rv.data)
class SeaSurfTestCaseExemptUrls(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
self.csrf.exempt_urls(('/foo',))
@app.route('/foo/baz', methods=['POST'])
def foobaz():
return 'bar'
@app.route('/foo/quz', methods=['POST'])
def fooquz():
return 'bar'
@app.route('/bar', methods=['POST'])
def bar():
return 'foo'
def test_exempt_view(self):
rv = self.app.test_client().post('/foo/baz')
self.assertIn(b('bar'), rv.data)
with self.app.test_client() as c:
rv = c.post('/foo/quz')
self.assertIn(b('bar'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
self.assertEqual(cookie, None)
def test_token_validation(self):
with self.app.test_client() as c:
# should produce a logger warning
rv = c.post('/bar')
self.assertIn(b('403 Forbidden'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
token = self.csrf._get_token()
self.assertEqual(cookie, token)
class SeaSurfTestCaseDisableCookie(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@self.csrf.disable_cookie
def disable_cookie(response):
if request.path == '/foo/baz':
return True
if request.path == '/manual':
return True
return False
@app.route('/foo/baz', methods=['GET'])
def foobaz():
return 'bar'
@app.route('/foo/quz', methods=['GET'])
def fooquz():
return 'bar'
@csrf.exempt
@app.route('/manual', methods=['POST'])
def manual():
csrf.validate()
return 'bar'
def test_has_csrf_cookie(self):
with self.app.test_client() as c:
rv = c.get('/foo/quz')
self.assertIn(b('bar'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
token = self.csrf._get_token()
self.assertEqual(cookie, token)
def test_no_csrf_cookie(self):
with self.app.test_client() as c:
rv = c.get('/foo/baz')
cookie = get_cookie(rv, self.csrf._csrf_name)
self.assertEqual(cookie, None)
def test_no_csrf_cookie_even_after_manually_validated(self):
with self.app.test_client() as c:
rv = c.post('/manual')
self.assertIn(b('403 Forbidden'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
self.assertEqual(cookie, None)
class SeaSurfTestCaseEnableCookie(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@csrf.exempt
@app.route('/exempt', methods=['GET', 'POST'])
def exempt():
return 'exempt'
@csrf.exempt
@csrf.set_cookie
@app.route('/exempt_with_cookie', methods=['GET', 'POST'])
def exempt_with_cookie():
return 'exempt_with_cookie'
def test_no_csrf_cookie(self):
with self.app.test_client() as c:
rv = c.get('/exempt')
cookie = get_cookie(rv, self.csrf._csrf_name)
self.assertEqual(cookie, None)
def test_has_csrf_cookie(self):
with self.app.test_client() as c:
rv = c.post('/exempt_with_cookie')
cookie = get_cookie(rv, self.csrf._csrf_name)
token = self.csrf._get_token()
self.assertEqual(cookie, token)
def test_has_csrf_cookie_but_doesnt_validate(self):
with self.app.test_client() as c:
rv = c.post('/exempt_with_cookie')
self.assertIn(b('exempt_with_cookie'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
token = self.csrf._get_token()
self.assertEqual(cookie, token)
class SeaSurfTestCaseSkipValidation(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@self.csrf.skip_validation
def skip_validation(request):
if request.path == '/foo/quz':
return True
if request.path == '/manual':
return True
return False
@app.route('/foo/baz', methods=['GET'])
def get_foobaz():
return 'bar'
@app.route('/foo/baz', methods=['DELETE'])
def foobaz():
return 'bar'
@app.route('/foo/quz', methods=['POST'])
def fooquz():
return 'bar'
@app.route('/manual', methods=['POST'])
def manual():
csrf.validate()
return 'bar'
def test_skips_validation(self):
with self.app.test_client() as c:
rv = c.post('/foo/quz')
self.assertIn(b('bar'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
token = self.csrf._get_token()
self.assertEqual(cookie, token)
def test_enforces_validation_reject(self):
with self.app.test_client() as c:
rv = c.delete('/foo/baz')
self.assertIn(b('403 Forbidden'), rv.data)
def test_enforces_validation_accept(self):
with self.app.test_client() as c:
# GET generates CSRF token
c.get('/foo/baz')
rv = c.delete('/foo/baz',
headers={'X-CSRFToken': self.csrf._get_token()})
self.assertIn(b('bar'), rv.data)
def test_manual_validation(self):
with self.app.test_client() as c:
rv = c.post('/manual')
self.assertIn(b('403 Forbidden'), rv.data)
class SeaSurfTestManualValidation(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@csrf.exempt
@app.route('/manual', methods=['POST'])
def manual():
csrf.validate()
return 'bar'
def test_can_manually_validate_exempt_views(self):
with self.app.test_client() as c:
rv = c.post('/manual')
self.assertIn(b('403 Forbidden'), rv.data)
cookie = get_cookie(rv, self.csrf._csrf_name)
token = self.csrf._get_token()
self.assertEqual(cookie, token)
class SeaSurfTestCaseSave(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
@app.after_request
def after_request(response):
from flask import session
response.headers['X-Session-Modified'] = str(session.modified)
return response
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@app.route('/foo', methods=['GET'])
def foo():
return 'bar'
def test_save(self):
with self.app.test_client() as client:
rv = client.get('/foo')
self.assertIn(b('bar'), rv.data)
self.assertEqual(rv.headers['X-Session-Modified'], 'True')
rv = client.get('/foo')
self.assertIn(b('bar'), rv.data)
self.assertEqual(rv.headers['X-Session-Modified'], 'False')
class SeaSurfTestCaseReferer(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
app.config['CSRF_CHECK_REFERER'] = False
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@csrf.exempt
@app.route('/foo', methods=['POST'])
@app.route('/foo/<term>', methods=['POST'])
def foo(term=None):
return 'bar'
@app.route('/bar', methods=['POST'])
@app.route('/bar/<term>', methods=['POST'])
def bar(term=None):
return 'foo'
def test_https_referer_check_disabled(self):
with self.app.test_client() as client:
with client.session_transaction(base_url="https://www.example.com") as sess:
token = self.csrf._generate_token()
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=token)
sess[self.csrf._csrf_name] = token
# once this is reached the session was stored
rv = client.post('/bar',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': 'https://www.evil.com/foobar'})
self.assertEqual(200, rv.status_code)
rv = client.post(u'/bar/\xf8',
data={self.csrf._csrf_name: token},
base_url='https://www.example.com',
headers={'Referer': u'https://www.evil.com/\xf8'})
self.assertEqual(200, rv.status_code)
class SeaSurfTestCaseSetCookie(BaseTestCase):
'''
Tests the Set-Cookie header behavior
SeaSurf should add the Set-Cookie header for `_csrf_name` when
the request does not have a matching cookie or a token is requested
in the template for that request.
Other requests (e.g, AJAX calls) do not require the Set-Cookie header,
which has the side affect of breaking caches.
'''
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
self.csrf.init_app(app)
@app.route('/foo', methods=['GET'])
def foo():
return 'bar'
@app.route('/bar', methods=['POST'])
def bar():
return 'foo'
@app.route('/baz', methods=['GET'])
def baz():
return render_template_string('{{ csrf_token() }}')
def test_header_set_cookie(self):
'''
Test that the Set-Cookie header was passed on a new request
'''
with self.app.test_client() as client:
res1 = client.get('/foo')
self.assertIn(self.csrf._csrf_name,
res1.headers.get('Set-Cookie', ''),
'CSRF cookie should have been set if the client has no cookie')
res2 = client.get('/foo')
self.assertNotIn(self.csrf._csrf_name,
res2.headers.get('Set-Cookie', ''),
'CSRF cookie should not be set if the client provided a matching cookie')
res3 = client.get('/baz')
self.assertIn(self.csrf._csrf_name,
res3.headers.get('Set-Cookie', ''),
'CSRF cookie always be re-set if a token is requested by the template')
client.delete_cookie(key=self.csrf._csrf_name)
res4 = client.get('/foo')
self.assertIn(self.csrf._csrf_name,
res4.headers.get('Set-Cookie', ''),
'CSRF cookie always be re-set if a token is requested by the template')
def test_header_set_on_post(self):
with self.app.test_client() as client:
headers = {}
res1 = client.post('/bar', headers=headers)
self.assertEqual(res1.status_code, 403)
cookie = client.get_cookie(self.csrf._csrf_name)
if cookie:
headers[self.csrf._csrf_header_name] = cookie.value
res2 = client.post('/bar', headers=headers)
self.assertEqual(res2.status_code, 200)
def test_header_set_cookie_samesite(self):
samesite = 'Strict'
self.app.config['CSRF_COOKIE_SAMESITE'] = samesite
self.csrf.init_app(self.app)
with self.app.test_client() as client:
res = client.get('/foo')
cookie = res.headers.get('Set-Cookie')
c = parse_cookie(cookie)
self.assertEqual(c['SameSite'], samesite)
class SeaSurfTestCaseGenerateNewToken(BaseTestCase):
def setUp(self):
app = Flask(__name__)
app.debug = True
app.secret_key = '1234'
self.app = app
csrf = SeaSurf()
csrf._csrf_disable = False
self.csrf = csrf
# Initialize CSRF protection.
self.csrf.init_app(app)
@app.route('/foo', methods=['GET'])
def foo(term=None):
return 'bar'
@app.route('/bar', methods=['POST'])
def bar(term=None):
self.csrf.generate_new_token()
return 'foo'
def test_generate_new_token(self):
with self.app.test_client() as client:
client.get('/foo')
tokenA = self.csrf._get_token()
client.set_cookie(domain='www.example.com', key=self.csrf._csrf_name, value=tokenA)
with client.session_transaction() as sess:
sess[self.csrf._csrf_name] = tokenA
data = {'_csrf_token': tokenA}
rv = client.post('/bar', data=data)
tokenB = self.csrf._get_token()
self.assertEqual(rv.status_code, 200, rv)
self.assertNotEqual(tokenA, tokenB)
self.assertIn(tokenB,
rv.headers.get('Set-Cookie', ''),
'CSRF cookie should have been set to the new token')
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseExemptViews))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseIncludeViews))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseExemptUrls))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseDisableCookie))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseEnableCookie))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseSkipValidation))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseSave))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseSetCookie))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseReferer))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestManualValidation))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SeaSurfTestCaseGenerateNewToken))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')