Skip to content

Commit ed7bbde

Browse files
committed
rename clean_url method to examine_url. the method doesn't realy clean anything, it throws a ValueError if it finds a problem
1 parent 002951b commit ed7bbde

File tree

2 files changed

+33
-54
lines changed

2 files changed

+33
-54
lines changed

roundup/cgi/actions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def execute(self):
4040
self.permission()
4141
return self.handle()
4242

43-
def clean_url(self, url):
43+
def examine_url(self, url):
4444
'''Return URL validated to be under self.base and properly escaped
4545
4646
If url not properly escaped or validation fails raise ValueError.
@@ -822,7 +822,7 @@ def handle(self):
822822
# Allow an option to stay on the page to create new things
823823
if '__redirect_to' in self.form:
824824
raise exceptions.Redirect('%s&@ok_message=%s'%(
825-
self.clean_url(self.form['__redirect_to'].value),
825+
self.examine_url(self.form['__redirect_to'].value),
826826
urllib_.quote(messages)))
827827

828828
# otherwise redirect to the new item's page
@@ -1141,7 +1141,7 @@ def handle(self):
11411141
# This will be redefined if there is a login error to include
11421142
# a new error message
11431143

1144-
clean_url = self.clean_url(self.form['__came_from'].value)
1144+
clean_url = self.examine_url(self.form['__came_from'].value)
11451145
redirect_url_tuple = urllib_.urlparse(clean_url)
11461146
# now I have a tuple form for the __came_from url
11471147
try:

test/test_cgi.py

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,83 +1173,70 @@ def testrenderContext(self):
11731173
self.assertNotEqual(-1, result.index('ok message'))
11741174
# print result
11751175

1176-
def testclean_url(self):
1177-
''' test the clean_url function '''
1176+
def testexamine_url(self):
1177+
''' test the examine_url function '''
1178+
1179+
def te(url, exception, raises=ValueError):
1180+
with self.assertRaises(raises) as cm:
1181+
examine_url(url)
1182+
self.assertEqual(cm.exception.message, exception)
1183+
1184+
11781185
action = actions.Action(self.client)
1179-
clean_url = action.clean_url
1186+
examine_url = action.examine_url
11801187

11811188
# Christmas tree url: test of every component that passes
11821189
self.assertEqual(
1183-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1190+
examine_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
11841191
'http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue')
11851192

11861193
# allow replacing http with https if base is http
11871194
self.assertEqual(
1188-
clean_url("https://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1195+
examine_url("https://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
11891196
'https://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue')
11901197

11911198

11921199
# change base to use https and make sure we don't redirect to http
11931200
saved_base = action.base
11941201
action.base = "https://tracker.example/cgi-bin/roundup.cgi/bugs/"
1195-
with self.assertRaises(ValueError) as cm:
1196-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue")
1197-
self.assertEqual(cm.exception.message, 'Base url https://tracker.example/cgi-bin/roundup.cgi/bugs/ requires https. Redirect url http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue uses http.')
1202+
te("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue",
1203+
'Base url https://tracker.example/cgi-bin/roundup.cgi/bugs/ requires https. Redirect url http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue uses http.')
11981204
action.base = saved_base
11991205

12001206
# url doesn't have to be valid to roundup, just has to be contained
12011207
# inside of roundup. No zoik class is defined
1202-
self.assertEqual(clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/zoik7;parm=bar?@template=foo&parm=(zot)#issue"), "http://tracker.example/cgi-bin/roundup.cgi/bugs/zoik7;parm=bar?@template=foo&parm=(zot)#issue")
1208+
self.assertEqual(examine_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/zoik7;parm=bar?@template=foo&parm=(zot)#issue"), "http://tracker.example/cgi-bin/roundup.cgi/bugs/zoik7;parm=bar?@template=foo&parm=(zot)#issue")
12031209

12041210
# test with wonky schemes
1205-
with self.assertRaises(ValueError) as cm:
1206-
clean_url("email://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1207-
self.assertEqual(cm.exception.message, 'Unrecognized scheme in email://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue')
1211+
te("email://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue",
1212+
'Unrecognized scheme in email://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue')
12081213

1209-
with self.assertRaises(ValueError) as cm:
1210-
clean_url("http%3a//tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1211-
self.assertEqual(cm.exception.message, 'Unrecognized scheme in http%3a//tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue')
1214+
te("http%3a//tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue", 'Unrecognized scheme in http%3a//tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue')
12121215

12131216
# test different netloc/path prefix
12141217
# assert port
1215-
with self.assertRaises(ValueError) as cm:
1216-
clean_url("http://tracker.example:1025/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1217-
self.assertEqual(cm.exception.message, 'Net location in http://tracker.example:1025/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
1218+
te("http://tracker.example:1025/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue",'Net location in http://tracker.example:1025/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
12181219

12191220
#assert user
1220-
with self.assertRaises(ValueError) as cm:
1221-
clean_url("http://[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1222-
self.assertEqual(cm.exception.message, 'Net location in http://[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
1221+
te("http://[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue", 'Net location in http://[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
12231222

12241223
#assert user:password
1225-
with self.assertRaises(ValueError) as cm:
1226-
clean_url("http://user:[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1227-
self.assertEqual(cm.exception.message, 'Net location in http://user:[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
1224+
te("http://user:[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue", 'Net location in http://user:[email protected]/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
12281225

12291226
# try localhost http scheme
1230-
with self.assertRaises(ValueError) as cm:
1231-
clean_url("http://localhost/cgi-bin/roundup.cgi/bugs/user3")
1232-
self.assertEqual(cm.exception.message, 'Net location in http://localhost/cgi-bin/roundup.cgi/bugs/user3 does not match base: tracker.example')
1227+
te("http://localhost/cgi-bin/roundup.cgi/bugs/user3", 'Net location in http://localhost/cgi-bin/roundup.cgi/bugs/user3 does not match base: tracker.example')
12331228

12341229
# try localhost https scheme
1235-
with self.assertRaises(ValueError) as cm:
1236-
clean_url("https://localhost/cgi-bin/roundup.cgi/bugs/user3")
1237-
self.assertEqual(cm.exception.message, 'Net location in https://localhost/cgi-bin/roundup.cgi/bugs/user3 does not match base: tracker.example')
1230+
te("https://localhost/cgi-bin/roundup.cgi/bugs/user3", 'Net location in https://localhost/cgi-bin/roundup.cgi/bugs/user3 does not match base: tracker.example')
12381231

12391232
# try different host
1240-
with self.assertRaises(ValueError) as cm:
1241-
clean_url("http://bad.guys.are.us/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1242-
self.assertEqual(cm.exception.message, 'Net location in http://bad.guys.are.us/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
1233+
te("http://bad.guys.are.us/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue", 'Net location in http://bad.guys.are.us/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#issue does not match base: tracker.example')
12431234

12441235
# change the base path to .../bug from .../bugs
1245-
with self.assertRaises(ValueError) as cm:
1246-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1247-
self.assertEqual(cm.exception.message, 'Base path /cgi-bin/roundup.cgi/bugs/ is not a prefix for url http://tracker.example/cgi-bin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue')
1236+
te("http://tracker.example/cgi-bin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue", 'Base path /cgi-bin/roundup.cgi/bugs/ is not a prefix for url http://tracker.example/cgi-bin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue')
12481237

12491238
# change the base path eliminate - in cgi-bin
1250-
with self.assertRaises(ValueError) as cm:
1251-
clean_url("http://tracker.example/cgibin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue"),
1252-
self.assertEqual(cm.exception.message, 'Base path /cgi-bin/roundup.cgi/bugs/ is not a prefix for url http://tracker.example/cgibin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue')
1239+
te("http://tracker.example/cgibin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue",'Base path /cgi-bin/roundup.cgi/bugs/ is not a prefix for url http://tracker.example/cgibin/roundup.cgi/bug/user3;parm=bar?@template=foo&parm=(zot)#issue')
12531240

12541241

12551242
# scan for unencoded characters
@@ -1258,24 +1245,16 @@ def testclean_url(self):
12581245
#
12591246
# break components with unescaped character '<'
12601247
# path component
1261-
with self.assertRaises(ValueError) as cm:
1262-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/<user3;parm=bar?@template=foo&parm=(zot)#issue"),
1263-
self.assertEqual(cm.exception.message, 'Path component (/cgi-bin/roundup.cgi/bugs/<user3) in http://tracker.example/cgi-bin/roundup.cgi/bugs/<user3;parm=bar?@template=foo&parm=(zot)#issue is not properly escaped')
1248+
te("http://tracker.example/cgi-bin/roundup.cgi/bugs/<user3;parm=bar?@template=foo&parm=(zot)#issue", 'Path component (/cgi-bin/roundup.cgi/bugs/<user3) in http://tracker.example/cgi-bin/roundup.cgi/bugs/<user3;parm=bar?@template=foo&parm=(zot)#issue is not properly escaped')
12641249

12651250
# params component
1266-
with self.assertRaises(ValueError) as cm:
1267-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=b<ar?@template=foo&parm=(zot)#issue"),
1268-
self.assertEqual(cm.exception.message, 'Params component (parm=b<ar) in http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=b<ar?@template=foo&parm=(zot)#issue is not properly escaped')
1251+
te("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=b<ar?@template=foo&parm=(zot)#issue", 'Params component (parm=b<ar) in http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=b<ar?@template=foo&parm=(zot)#issue is not properly escaped')
12691252

12701253
# query component
1271-
with self.assertRaises(ValueError) as cm:
1272-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=<foo>&parm=(zot)#issue"),
1273-
self.assertEqual(cm.exception.message, 'Query component (@template=<foo>&parm=(zot)) in http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=<foo>&parm=(zot)#issue is not properly escaped')
1254+
te("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=<foo>&parm=(zot)#issue", 'Query component (@template=<foo>&parm=(zot)) in http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=<foo>&parm=(zot)#issue is not properly escaped')
12741255

12751256
# fragment component
1276-
with self.assertRaises(ValueError) as cm:
1277-
clean_url("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#iss<ue"),
1278-
self.assertEqual(cm.exception.message, 'Fragment component (iss<ue) in http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#iss<ue is not properly escaped')
1257+
te("http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#iss<ue", 'Fragment component (iss<ue) in http://tracker.example/cgi-bin/roundup.cgi/bugs/user3;parm=bar?@template=foo&parm=(zot)#iss<ue is not properly escaped')
12791258

12801259
class TemplateTestCase(unittest.TestCase):
12811260
''' Test the template resolving code, i.e. what can be given to @template

0 commit comments

Comments
 (0)