@@ -329,7 +329,7 @@ class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):
329329 pass
330330
331331 def setUp (self ):
332- BaseTestCase .setUp (self )
332+ super () .setUp ()
333333 self .cwd = os .getcwd ()
334334 basetempdir = tempfile .gettempdir ()
335335 os .chdir (basetempdir )
@@ -357,7 +357,7 @@ def tearDown(self):
357357 except :
358358 pass
359359 finally :
360- BaseTestCase .tearDown (self )
360+ super () .tearDown ()
361361
362362 def check_status_and_reason (self , response , status , data = None ):
363363 def close_conn ():
@@ -413,6 +413,55 @@ def test_undecodable_filename(self):
413413 self .check_status_and_reason (response , HTTPStatus .OK ,
414414 data = support .TESTFN_UNDECODABLE )
415415
416+ def test_get_dir_redirect_location_domain_injection_bug (self ):
417+ """Ensure //evil.co/..%2f../../X does not put //evil.co/ in Location.
418+
419+ //netloc/ in a Location header is a redirect to a new host.
420+ https://github.com/python/cpython/issues/87389
421+
422+ This checks that a path resolving to a directory on our server cannot
423+ resolve into a redirect to another server.
424+ """
425+ os .mkdir (os .path .join (self .tempdir , 'existing_directory' ))
426+ url = f'/python.org/..%2f..%2f..%2f..%2f..%2f../%0a%0d/../{ self .tempdir_name } /existing_directory'
427+ expected_location = f'{ url } /' # /python.org.../ single slash single prefix, trailing slash
428+ # Canonicalizes to /tmp/tempdir_name/existing_directory which does
429+ # exist and is a dir, triggering the 301 redirect logic.
430+ response = self .request (url )
431+ self .check_status_and_reason (response , HTTPStatus .MOVED_PERMANENTLY )
432+ location = response .getheader ('Location' )
433+ self .assertEqual (location , expected_location , msg = 'non-attack failed!' )
434+
435+ # //python.org... multi-slash prefix, no trailing slash
436+ attack_url = f'/{ url } '
437+ response = self .request (attack_url )
438+ self .check_status_and_reason (response , HTTPStatus .MOVED_PERMANENTLY )
439+ location = response .getheader ('Location' )
440+ self .assertFalse (location .startswith ('//' ), msg = location )
441+ self .assertEqual (location , expected_location ,
442+ msg = 'Expected Location header to start with a single / and '
443+ 'end with a / as this is a directory redirect.' )
444+
445+ # ///python.org... triple-slash prefix, no trailing slash
446+ attack3_url = f'//{ url } '
447+ response = self .request (attack3_url )
448+ self .check_status_and_reason (response , HTTPStatus .MOVED_PERMANENTLY )
449+ self .assertEqual (response .getheader ('Location' ), expected_location )
450+
451+ # If the second word in the http request (Request-URI for the http
452+ # method) is a full URI, we don't worry about it, as that'll be parsed
453+ # and reassembled as a full URI within BaseHTTPRequestHandler.send_head
454+ # so no errant scheme-less //netloc//evil.co/ domain mixup can happen.
455+ attack_scheme_netloc_2slash_url = f'https://pypi.org/{ url } '
456+ expected_scheme_netloc_location = f'{ attack_scheme_netloc_2slash_url } /'
457+ response = self .request (attack_scheme_netloc_2slash_url )
458+ self .check_status_and_reason (response , HTTPStatus .MOVED_PERMANENTLY )
459+ location = response .getheader ('Location' )
460+ # We're just ensuring that the scheme and domain make it through, if
461+ # there are or aren't multiple slashes at the start of the path that
462+ # follows that isn't important in this Location: header.
463+ self .assertTrue (location .startswith ('https://pypi.org/' ), msg = location )
464+
416465 def test_get (self ):
417466 #constructs the path relative to the root directory of the HTTPServer
418467 response = self .request (self .base_url + '/test' )
0 commit comments