Skip to content

Commit 5dbdc22

Browse files
committed
gh-160506: Fix ntpath.realpath() for broken links via directory links
Resolve the directory containing a broken relative symlink before joining the link target to it, so that '..' in the target is applied to the real directory. ANT-2026-27II3A20
1 parent 3b3c692 commit 5dbdc22

4 files changed

Lines changed: 99 additions & 5 deletions

File tree

‎Lib/ntpath.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,16 @@ def _readlink_deep(path, ignored_error=OSError):
594594
if not islink(old_path):
595595
path = old_path
596596
break
597-
path = normpath(join(dirname(old_path), path))
597+
# Resolve the directory holding the link first, so that
598+
# '..' in the target is not applied to a directory link
599+
# in old_path.
600+
link_dir = dirname(old_path)
601+
try:
602+
link_dir = _getfinalpathname(link_dir)
603+
except ignored_error as ex:
604+
if ex.winerror not in allowed_winerror:
605+
raise
606+
path = normpath(join(link_dir, path))
598607
except ignored_error as ex:
599608
if ex.winerror in allowed_winerror:
600609
break

‎Lib/test/test_ntpath.py‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,9 @@ def test_realpath_broken_symlinks(self):
701701
self.assertPathEqual(ntpath.realpath(r"broken1\baz"),
702702
ABSTFN + r"\broken\bar\baz")
703703
self.assertPathEqual(ntpath.realpath("broken2"),
704-
ABSTFN + r"\self\self\missing")
704+
ABSTFN + r"\missing")
705705
self.assertPathEqual(ntpath.realpath("broken3"),
706-
ABSTFN + r"\subdir\parent\subdir\parent\missing")
706+
ABSTFN + r"\missing")
707707
self.assertPathEqual(ntpath.realpath("broken4"),
708708
ABSTFN + r"\missing")
709709
self.assertPathEqual(ntpath.realpath("broken5"),
@@ -718,14 +718,39 @@ def test_realpath_broken_symlinks(self):
718718
self.assertPathEqual(ntpath.realpath(rb"broken1\baz"),
719719
os.fsencode(ABSTFN + r"\broken\bar\baz"))
720720
self.assertPathEqual(ntpath.realpath(b"broken2"),
721-
os.fsencode(ABSTFN + r"\self\self\missing"))
721+
os.fsencode(ABSTFN + r"\missing"))
722722
self.assertPathEqual(ntpath.realpath(rb"broken3"),
723-
os.fsencode(ABSTFN + r"\subdir\parent\subdir\parent\missing"))
723+
os.fsencode(ABSTFN + r"\missing"))
724724
self.assertPathEqual(ntpath.realpath(b"broken4"),
725725
os.fsencode(ABSTFN + r"\missing"))
726726
self.assertPathEqual(ntpath.realpath(b"broken5"),
727727
os.fsencode(ABSTFN + r"\missing"))
728728

729+
@os_helper.skip_unless_symlink
730+
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
731+
@_parameterize({}, {'strict': ALL_BUT_LAST}, {'strict': ALLOW_MISSING})
732+
def test_realpath_broken_symlink_in_linked_dir(self, kwargs):
733+
# A relative target is resolved against the directory containing
734+
# the link, not against the path by which the link was reached.
735+
ABSTFN = ntpath.abspath(os_helper.TESTFN)
736+
os.makedirs(ABSTFN + r"\sub")
737+
self.addCleanup(os_helper.rmtree, ABSTFN)
738+
os.symlink(".", ABSTFN + r"\sub\alias", target_is_directory=True)
739+
os.symlink(r"..\missing", ABSTFN + r"\sub\broken")
740+
os.symlink(r"alias\alias\broken", ABSTFN + r"\sub\broken2")
741+
742+
expected = ABSTFN + r"\missing"
743+
for name in "broken", r"alias\broken", "broken2", r"alias\broken2":
744+
path = ABSTFN + "\\sub\\" + name
745+
with self.subTest(name=name):
746+
self.assertPathEqual(ntpath.realpath(path, **kwargs), expected)
747+
self.assertPathEqual(
748+
ntpath.realpath(os.fsencode(path), **kwargs),
749+
os.fsencode(expected))
750+
self.assertPathEqual(
751+
ntpath.realpath("\\\\?\\" + path, **kwargs),
752+
"\\\\?\\" + expected)
753+
729754
@os_helper.skip_unless_symlink
730755
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
731756
def test_realpath_symlink_loops(self):

‎Lib/test/test_tarfile.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4506,6 +4506,62 @@ def test_parent_symlink2(self):
45064506
self.expect_file('current/')
45074507
self.expect_file('parent/evil')
45084508

4509+
@symlink_test
4510+
def test_parent_symlink3(self):
4511+
# Test a dangling symlink reached through a symlink to a directory
4512+
4513+
with ArchiveMaker() as arc:
4514+
4515+
# `current` links to `.` which is both the destination directory
4516+
# and `current` itself
4517+
arc.add('current', symlink_to='.')
4518+
4519+
# `current/link` is also available as `./link`, and points
4520+
# to a file outside the destination which does not exist yet
4521+
arc.add('current/link', symlink_to='../evil')
4522+
4523+
# Writing through the link creates `../evil`
4524+
arc.add('current/link', content='evil')
4525+
4526+
with self.check_context(arc.open(), 'fully_trusted'):
4527+
if os_helper.can_symlink():
4528+
self.expect_file('current', symlink_to='.')
4529+
self.expect_file('link', symlink_to='../evil')
4530+
self.expect_file('../evil', content='evil')
4531+
else:
4532+
self.expect_file('current/link', content='evil')
4533+
4534+
with self.check_context(arc.open(), 'tar'):
4535+
if os_helper.can_symlink():
4536+
# Fail when extracting a file outside destination
4537+
self.expect_exception(
4538+
tarfile.OutsideDestinationError,
4539+
"'current/link' would be extracted to "
4540+
+ """['"].*evil['"], which is outside """
4541+
+ "the destination")
4542+
else:
4543+
self.expect_file('current/link', content='evil')
4544+
4545+
with self.check_context(arc.open(), 'data'):
4546+
if os_helper.can_symlink():
4547+
if self.dotdot_resolves_early:
4548+
# 'current/../evil' normalises to 'evil', so the link is
4549+
# created; fail when extracting a file through it
4550+
self.expect_exception(
4551+
tarfile.OutsideDestinationError,
4552+
"'current/link' would be extracted to "
4553+
+ """['"].*evil['"], which is outside """
4554+
+ "the destination")
4555+
else:
4556+
# Fail as soon as we have a symlink outside the destination
4557+
self.expect_exception(
4558+
tarfile.LinkOutsideDestinationError,
4559+
"'current/link' would link to "
4560+
+ """['"].*evil['"], which is outside """
4561+
+ "the destination")
4562+
else:
4563+
self.expect_file('current/link', content='evil')
4564+
45094565
@symlink_test
45104566
def test_absolute_symlink(self):
45114567
# Test symlink to an absolute path
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :func:`os.path.realpath` on Windows returning the wrong path for a broken
2+
relative symbolic link reached through a symbolic link to a directory. This
3+
allowed :mod:`tarfile` extraction with the ``'data'`` or ``'tar'`` filter to
4+
create a file outside the destination directory.

0 commit comments

Comments
 (0)