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