8000 GH-123599: Reject non-local authority in `pathlib.Path.from_uri()` on POSIX by barneygale · Pull Request #123650 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-123599: Reject non-local authority in pathlib.Path.from_uri() on POSIX #123650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
GH-123599: Reject non-local authority in pathlib.Path.from_uri() on…
… POSIX

Raise `ValueError` in `pathlib.Path.from_uri()` if the given `file:` URI
specifies a non-empty, non-`localhost` authority, and we're running on a
platform without support for UNC paths.
  • Loading branch information
barneygale committed Sep 3, 2024
commit 0cee0c4d94fe1b3fc86041416cd60170212ed108
6 changes: 6 additions & 0 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@ conforming to :rfc:`8089`.
:exc:`ValueError` is raised if the URI does not start with ``file:``, or
the parsed path isn't absolute.

On POSIX systems, :exc:`ValueError` is raised if the URI specifies a
non-local authority::

>>> Path.from_uri('file://server/share')
ValueError: URI is not local: 'file://server/share'

.. versionadded:: 3.13


Expand Down
3 changes: 3 additions & 0 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,9 @@ def from_uri(cls, uri):
elif path[:12] == '//localhost/':
# Remove 'localhost' authority
path = path[11:]
elif path[:2] == '//' and os.name != 'nt':
# UNC paths aren't supported on POSIX
raise ValueError(f"URI is not local: {uri!r}")
if path[:3] == '///' or (path[:1] == '/' and path[2:3] in ':|'):
# Remove slash before DOS device/UNC path
path = path[1:]
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,21 +1698,22 @@ def test_handling_bad_descriptor(self):
def test_from_uri_posix(self):
P = self.cls
self.assertEqual(P.from_uri('file:/foo/bar'), P('/foo/bar'))
self.assertEqual(P.from_uri('file://foo/bar'), P('//foo/bar'))
self.assertEqual(P.from_uri('file:///foo/bar'), P('/foo/bar'))
self.assertEqual(P.from_uri('file:////foo/bar'), P('//foo/bar'))
self.assertEqual(P.from_uri('file://localhost/foo/bar'), P('/foo/bar'))
self.assertEqual(P.from_uri('file://localhost//foo/bar'), P('//foo/bar'))
self.assertRaises(ValueError, P.from_uri, 'foo/bar')
self.assertRaises(ValueError, P.from_uri, '/foo/bar')
self.assertRaises(ValueError, P.from_uri, '//foo/bar')
self.assertRaises(ValueError, P.from_uri, 'file:foo/bar')
self.assertRaises(ValueError, P.from_uri, 'file://foo/bar')
self.assertRaises(ValueError, P.from_uri, 'http://foo/bar')

@needs_posix
def test_from_uri_pathname2url_posix(self):
P = self.cls
self.assertEqual(P.from_uri('file:' + pathname2url('/foo/bar')), P('/foo/bar'))
self.assertEqual(P.from_uri('file:' + pathname2url('//foo/bar')), P('//foo/bar'))
self.assertEqual(P.from_uri('file://' + pathname2url('/foo/bar')), P('/foo/bar'))
self.assertEqual(P.from_uri('file://' + pathname2url('//foo/bar')), P('//foo/bar'))

@needs_windows
def test_absolute_windows(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix issue where :meth:`pathlib.Path.from_uri` accepted URIs with non-local
authorities on POSIX. This method now raises `ValueError` when given a URI
like ``file://server/share`` on a non-Windows system.
Loading
0