[3.13] GH-126766: url2pathname(): handle 'localhost' authority (GH-127129) (#127130)

GH-126766: `url2pathname()`: handle 'localhost' authority (GH-127129)

Discard any 'localhost' authority from the beginning of a `file:` URI. As a
result, file URIs like `//localhost/etc/hosts` are correctly decoded as
`/etc/hosts`.
(cherry picked from commit ebf564a1d3)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-11-22 04:42:26 +01:00 committed by GitHub
parent 8cdd636f87
commit 4b9068eeea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 5 deletions

View file

@ -15,14 +15,17 @@ def url2pathname(url):
# become # become
# C:\foo\bar\spam.foo # C:\foo\bar\spam.foo
import string, urllib.parse import string, urllib.parse
if url[:3] == '///':
# URL has an empty authority section, so the path begins on the third
# character.
url = url[2:]
elif url[:12] == '//localhost/':
# Skip past 'localhost' authority.
url = url[11:]
# Windows itself uses ":" even in URLs. # Windows itself uses ":" even in URLs.
url = url.replace(':', '|') url = url.replace(':', '|')
if not '|' in url: if not '|' in url:
# No drive specifier, just convert slashes # No drive specifier, just convert slashes
if url[:3] == '///':
# URL has an empty authority section, so the path begins on the
# third character.
url = url[2:]
# make sure not to convert quoted slashes :-) # make sure not to convert quoted slashes :-)
return urllib.parse.unquote(url.replace('/', '\\')) return urllib.parse.unquote(url.replace('/', '\\'))
comp = url.split('|') comp = url.split('|')

View file

@ -1595,6 +1595,8 @@ class Pathname_Tests(unittest.TestCase):
# Localhost paths # Localhost paths
self.assertEqual(fn('//localhost/C:/path/to/file'), 'C:\\path\\to\\file') self.assertEqual(fn('//localhost/C:/path/to/file'), 'C:\\path\\to\\file')
self.assertEqual(fn('//localhost/C|/path/to/file'), 'C:\\path\\to\\file') self.assertEqual(fn('//localhost/C|/path/to/file'), 'C:\\path\\to\\file')
self.assertEqual(fn('//localhost/path/to/file'), '\\path\\to\\file')
self.assertEqual(fn('//localhost//server/path/to/file'), '\\\\server\\path\\to\\file')
# Percent-encoded forward slashes are preserved for backwards compatibility # Percent-encoded forward slashes are preserved for backwards compatibility
self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar') self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar')
self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar') self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar')
@ -1613,7 +1615,7 @@ class Pathname_Tests(unittest.TestCase):
self.assertEqual(fn('//foo/bar'), '//foo/bar') self.assertEqual(fn('//foo/bar'), '//foo/bar')
self.assertEqual(fn('///foo/bar'), '/foo/bar') self.assertEqual(fn('///foo/bar'), '/foo/bar')
self.assertEqual(fn('////foo/bar'), '//foo/bar') self.assertEqual(fn('////foo/bar'), '//foo/bar')
self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar') self.assertEqual(fn('//localhost/foo/bar'), '/foo/bar')
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII') @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
def test_url2pathname_nonascii(self): def test_url2pathname_nonascii(self):

View file

@ -1660,6 +1660,9 @@ else:
# URL has an empty authority section, so the path begins on the # URL has an empty authority section, so the path begins on the
# third character. # third character.
pathname = pathname[2:] pathname = pathname[2:]
elif pathname[:12] == '//localhost/':
# Skip past 'localhost' authority.
pathname = pathname[11:]
encoding = sys.getfilesystemencoding() encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors() errors = sys.getfilesystemencodeerrors()
return unquote(pathname, encoding=encoding, errors=errors) return unquote(pathname, encoding=encoding, errors=errors)

View file

@ -0,0 +1,2 @@
Fix issue where :func:`urllib.request.url2pathname` failed to discard any
'localhost' authority present in the URL.