Skip to content

Commit

Permalink
pythonGH-126766: url2pathname(): handle 'localhost' authority
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
barneygale committed Nov 22, 2024
1 parent fd133d4 commit 05e3b18
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Lib/nturl2path.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def url2pathname(url):
# 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:]
# make sure not to convert quoted slashes :-)
return urllib.parse.unquote(url.replace('/', '\\'))
comp = url.split('|')
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,8 @@ def test_url2pathname_win(self):
# 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/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
self.assertEqual(fn('C:/foo%2fbar'), 'C:\\foo/bar')
self.assertEqual(fn('//server/share/foo%2fbar'), '\\\\server\\share\\foo/bar')
Expand All @@ -1514,7 +1516,7 @@ def test_url2pathname_posix(self):
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')
def test_url2pathname_nonascii(self):
Expand Down
3 changes: 3 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,9 @@ def url2pathname(pathname):
# URL has an empty authority section, so the path begins on the
# third character.
pathname = pathname[2:]
elif pathname[:12] == '//localhost/':
# Skip past 'localhost' authority.
pathname = pathname[11:]
encoding = sys.getfilesystemencoding()
errors = sys.getfilesystemencodeerrors()
return unquote(pathname, encoding=encoding, errors=errors)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix issue where :func:`urllib.request.url2pathname` failed to discard any
'localhost' authority present in the URL.

0 comments on commit 05e3b18

Please sign in to comment.