Skip to content

Commit 2f24224

Browse files
committed
Also rename test to testCsrfProtection
Add tests that include valid and invalid @csrf nonce
1 parent 19800d3 commit 2f24224

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

test/test_cgi.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from roundup.cgi import client, actions, exceptions
1414
from roundup.cgi.exceptions import FormError
15-
from roundup.cgi.templating import HTMLItem, HTMLRequest, NoTemplate
15+
from roundup.cgi.templating import HTMLItem, HTMLRequest, NoTemplate, anti_csrf_nonce
1616
from roundup.cgi.templating import HTMLProperty, _HTMLItem
1717
from roundup.cgi.form_parser import FormParser
1818
from roundup import init, instance, password, hyperdb, date
@@ -857,7 +857,7 @@ def hasPermission(s, p, classname=None, d=None, e=None, **kw):
857857
</html>
858858
""".strip ())
859859

860-
def testCsrfHeaderProtection(self):
860+
def testCsrfProtection(self):
861861
# need to set SENDMAILDEBUG to prevent
862862
# downstream issue when email is sent on successful
863863
# issue creation. Also delete the file afterwards
@@ -894,7 +894,6 @@ def testCsrfHeaderProtection(self):
894894
pt = RoundupPageTemplate()
895895
pt.pt_edit(page_template, 'text/html')
896896
out = []
897-
print "out1: ", id(out), out
898897
def wh(s):
899898
out.append(s)
900899
cl.write_html = wh
@@ -923,7 +922,6 @@ def hasPermission(s, p, classname=None, d=None, e=None, **kw):
923922
# test with no headers and config by default requires 1
924923
cl.inner_main()
925924
match_at=out[0].find('Unable to verify sufficient headers')
926-
print out[0]
927925
self.assertNotEqual(match_at, -1)
928926
del(out[0])
929927

@@ -971,6 +969,32 @@ def hasPermission(s, p, classname=None, d=None, e=None, **kw):
971969
cl.inner_main()
972970
match_at=out[0].find('Invalid X-FORWARDED-HOST whoami.net')
973971
self.assertNotEqual(match_at, -1)
972+
del(cl.env['HTTP_X-FORWARDED-HOST'])
973+
del(out[0])
974+
975+
import copy
976+
977+
form2 = copy.copy(form)
978+
form2.update({'@csrf': 'booogus'})
979+
# add a bogus csrf field to the form and rerun the inner_main
980+
cl.form = makeForm(form2)
981+
982+
cl.env['HTTP_REFERER'] = 'http://whoami.com/path/'
983+
cl.inner_main()
984+
match_at=out[0].find('Invalid csrf token found: booogus')
985+
self.assertEqual(match_at, 36)
986+
del(out[0])
987+
988+
form2 = copy.copy(form)
989+
nonce = anti_csrf_nonce(cl, cl)
990+
form2.update({'@csrf': nonce})
991+
# add a real csrf field to the form and rerun the inner_main
992+
cl.form = makeForm(form2)
993+
cl.inner_main()
994+
# csrf passes and redirects to the new issue.
995+
match_at=out[0].find('Redirecting to <a href="http://whoami.com/path/issue1?@ok_message')
996+
self.assertEqual(match_at, 0)
997+
del(cl.env['HTTP_REFERER'])
974998
del(out[0])
975999

9761000
# clean up from email log

0 commit comments

Comments
 (0)