Skip to content

Commit

Permalink
pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…
Browse files Browse the repository at this point in the history
…path()` (python#118489)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
(cherry picked from commit caf6064)
  • Loading branch information
barneygale committed May 18, 2024
1 parent d76d95e commit b2cc8c2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,16 @@ def _joinrealpath(path, rest, strict, seen):
else:
# Return already resolved part + rest of the path unchanged.
return join(newpath, rest), False
try:
target = os.readlink(newpath)
except OSError:
if strict:
raise
else:
# Return already resolved part + rest of the path unchanged.
return join(newpath, rest), False
seen[newpath] = None # not resolved symlink
path, ok = _joinrealpath(path, os.readlink(newpath), strict, seen)
path, ok = _joinrealpath(path, target, strict, seen)
if not ok:
return join(path, rest), False
seen[newpath] = path # resolved symlink
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,23 @@ def test_realpath_resolve_first(self):
safe_rmdir(ABSTFN + "/k")
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):
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`os.path.realpath` now suppresses any :exc:`OSError` from
:func:`os.readlink` when *strict* mode is disabled (the default).

0 comments on commit b2cc8c2

Please sign in to comment.