mirror of
https://github.com/python/cpython.git
synced 2025-07-23 19:25:40 +00:00
[3.13] GH-118447: Fix handling of unreadable symlinks in os.path.realpath()
(GH-118489) (#119163)
(cherry picked from commit caf6064a1b
)
Co-authored-by: Barney Gale <barney.gale@gmail.com>
Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
This commit is contained in:
parent
91296146d5
commit
7407267ce4
3 changed files with 32 additions and 13 deletions
|
@ -471,26 +471,26 @@ symbolic links encountered in the path."""
|
||||||
if not stat.S_ISLNK(st.st_mode):
|
if not stat.S_ISLNK(st.st_mode):
|
||||||
path = newpath
|
path = newpath
|
||||||
continue
|
continue
|
||||||
|
if newpath in seen:
|
||||||
|
# Already seen this path
|
||||||
|
path = seen[newpath]
|
||||||
|
if path is not None:
|
||||||
|
# use cached value
|
||||||
|
continue
|
||||||
|
# The symlink is not resolved, so we must have a symlink loop.
|
||||||
|
if strict:
|
||||||
|
# Raise OSError(errno.ELOOP)
|
||||||
|
os.stat(newpath)
|
||||||
|
path = newpath
|
||||||
|
continue
|
||||||
|
target = os.readlink(newpath)
|
||||||
except OSError:
|
except OSError:
|
||||||
if strict:
|
if strict:
|
||||||
raise
|
raise
|
||||||
path = newpath
|
path = newpath
|
||||||
continue
|
continue
|
||||||
# Resolve the symbolic link
|
# Resolve the symbolic link
|
||||||
if newpath in seen:
|
|
||||||
# Already seen this path
|
|
||||||
path = seen[newpath]
|
|
||||||
if path is not None:
|
|
||||||
# use cached value
|
|
||||||
continue
|
|
||||||
# The symlink is not resolved, so we must have a symlink loop.
|
|
||||||
if strict:
|
|
||||||
# Raise OSError(errno.ELOOP)
|
|
||||||
os.stat(newpath)
|
|
||||||
path = newpath
|
|
||||||
continue
|
|
||||||
seen[newpath] = None # not resolved symlink
|
seen[newpath] = None # not resolved symlink
|
||||||
target = os.readlink(newpath)
|
|
||||||
if target.startswith(sep):
|
if target.startswith(sep):
|
||||||
# Symlink target is absolute; reset resolved path.
|
# Symlink target is absolute; reset resolved path.
|
||||||
path = sep
|
path = sep
|
||||||
|
|
|
@ -660,6 +660,23 @@ class PosixPathTest(unittest.TestCase):
|
||||||
safe_rmdir(ABSTFN + "/k")
|
safe_rmdir(ABSTFN + "/k")
|
||||||
safe_rmdir(ABSTFN)
|
safe_rmdir(ABSTFN)
|
||||||
|
|
||||||
|
@os_helper.skip_unless_symlink
|
||||||
|
@skip_if_ABSTFN_contains_backslash
|
||||||
|
@unittest.skipIf(os.chmod not in os.supports_follow_symlinks, "Can't set symlink permissions")
|
||||||
|
def test_realpath_unreadable_symlink(self):
|
||||||
|
try:
|
||||||
|
os.symlink(ABSTFN+"1", ABSTFN)
|
||||||
|
os.chmod(ABSTFN, 0o000, follow_symlinks=False)
|
||||||
|
self.assertEqual(realpath(ABSTFN), ABSTFN)
|
||||||
|
self.assertEqual(realpath(ABSTFN + '/foo'), ABSTFN + '/foo')
|
||||||
|
self.assertEqual(realpath(ABSTFN + '/../foo'), dirname(ABSTFN) + '/foo')
|
||||||
|
self.assertEqual(realpath(ABSTFN + '/foo/..'), ABSTFN)
|
||||||
|
with self.assertRaises(PermissionError):
|
||||||
|
realpath(ABSTFN, strict=True)
|
||||||
|
finally:
|
||||||
|
os.chmod(ABSTFN, 0o755, follow_symlinks=False)
|
||||||
|
os.unlink(ABSTFN)
|
||||||
|
|
||||||
def test_relpath(self):
|
def test_relpath(self):
|
||||||
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
|
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:func:`os.path.realpath` now suppresses any :exc:`OSError` from
|
||||||
|
:func:`os.readlink` when *strict* mode is disabled (the default).
|
Loading…
Add table
Add a link
Reference in a new issue