Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions news/13537.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Changed the tar unpacking fallback to check the existence of a symlink target file
before extraction. This tar unpacking fallback is only used on Python versions
that don't implement ``data_filter`` for the ``tarfile`` module.
9 changes: 9 additions & 0 deletions src/pip/_internal/utils/unpacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ def _untar_without_filter(
ensure_dir(path)
elif member.issym():
try:
# Try to verify that the link points to
# a file that exists within the tar file.
try:
tar._find_link_target(member) # type: ignore[attr-defined]
except AttributeError:
logger.warning(
"Could not verify link %s in tar file %s", member.name, filename
)

tar._extract_member(member, path)
except Exception as exc:
# Some corrupt tar files seem to produce this
Expand Down
124 changes: 124 additions & 0 deletions tests/unit/test_utils_unpacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,130 @@ def test_unpack_tar_links(self, input_prefix: str, unpack_prefix: str) -> None:
with open(os.path.join(unpack_dir, "symlink.txt"), "rb") as f:
assert f.read() == content

@pytest.mark.skipif(
not hasattr(tarfile, "data_filter"),
reason="tarfile filters (PEP-721) not available",
)
def test_unpack_tar_data_filter_bad_links_outside_dir(self) -> None:
"""
Test unpacking a *.tar with symlinks outside the directory.
"""
evil_path = os.path.join(tempfile.gettempdir(), "evil")
test_tar = os.path.join(self.tempdir, "test_tar_bad_abs_links.tar")
with tarfile.open(test_tar, "w") as tar:
file_data = io.BytesIO(b"normal\n")
normal_tarinfo = tarfile.TarInfo(name="normal.txt")
normal_tarinfo.size = len(file_data.getbuffer())
tar.addfile(normal_tarinfo, fileobj=file_data)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.SYMTYPE
info.linkpath = evil_path
tar.addfile(info)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.REGTYPE
data = io.BytesIO(b"evil\n")
info.size = len(data.getbuffer())
tar.addfile(info, fileobj=data)

# Errors due to data_filter
with pytest.raises(InstallationError) as e:
untar_file(test_tar, self.tempdir)
assert "'symbol_link' is a link to an absolute path" in str(e.value)

@pytest.mark.skipif(
hasattr(tarfile, "data_filter"),
reason="tarfile filters (PEP-721) must be absent",
)
def test_unpack_tar_no_data_filter_bad_links_outside_dir(self) -> None:
"""
Test unpacking a *.tar with symlinks outside the directory.
"""
evil_path = os.path.join(tempfile.gettempdir(), "evil")
test_tar = os.path.join(self.tempdir, "test_tar_bad_abs_links.tar")
with tarfile.open(test_tar, "w") as tar:
file_data = io.BytesIO(b"normal\n")
normal_tarinfo = tarfile.TarInfo(name="normal.txt")
normal_tarinfo.size = len(file_data.getbuffer())
tar.addfile(normal_tarinfo, fileobj=file_data)

assert not os.path.isfile(evil_path)
info = tarfile.TarInfo("symbol_link")
info.type = tarfile.SYMTYPE
info.linkpath = evil_path
tar.addfile(info)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.REGTYPE
data = io.BytesIO(b"evil\n")
info.size = len(data.getbuffer())
tar.addfile(info, fileobj=data)

# Does not error, but the file also shouldn't exist.
untar_file(test_tar, self.tempdir)
assert not os.path.isfile(evil_path)

@pytest.mark.skipif(
not hasattr(tarfile, "data_filter"),
reason="tarfile filters (PEP-721) must be absent",
)
def test_unpack_tar_data_filter_bad_links_parent_dir(self) -> None:
evil_link = "../../../evil"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When these test fails it leaves a file at /tmp/evil which is not cleaned up. Which causes subsequent runs to fail even if they are good.

Can you do something to handle this? Maybe extract to a sub directory of this temporary directory?

evil_path = os.path.realpath(os.path.join(self.tempdir, evil_link))
test_tar = os.path.join(self.tempdir, "test_tar_bad_rel_links.tar")
with tarfile.open(test_tar, "w") as tar:
file_data = io.BytesIO(b"normal\n")
normal_tarinfo = tarfile.TarInfo(name="normal.txt")
normal_tarinfo.size = len(file_data.getbuffer())
tar.addfile(normal_tarinfo, fileobj=file_data)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.SYMTYPE
info.linkpath = evil_link
tar.addfile(info)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.REGTYPE
data = io.BytesIO(b"evil\n")
info.size = len(data.getbuffer())
tar.addfile(info, fileobj=data)

with pytest.raises(InstallationError) as e:
untar_file(test_tar, self.tempdir)
assert "'symbol_link' would link to" in str(e.value)
assert "which is outside the destination" in str(e.value)
assert not os.path.exists(evil_path)

@pytest.mark.skipif(
hasattr(tarfile, "data_filter"),
reason="tarfile filters (PEP-721) not available",
)
def test_unpack_tar_no_data_filter_bad_links_parent_dir(self) -> None:
evil_link = "../../../evil"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one too I think.

evil_path = os.path.realpath(os.path.join(self.tempdir, evil_link))
test_tar = os.path.join(self.tempdir, "test_tar_bad_rel_links.tar")
with tarfile.open(test_tar, "w") as tar:
file_data = io.BytesIO(b"normal\n")
normal_tarinfo = tarfile.TarInfo(name="normal.txt")
normal_tarinfo.size = len(file_data.getbuffer())
tar.addfile(normal_tarinfo, fileobj=file_data)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.SYMTYPE
info.linkpath = evil_link
tar.addfile(info)

info = tarfile.TarInfo("symbol_link")
info.type = tarfile.REGTYPE
data = io.BytesIO(b"evil\n")
info.size = len(data.getbuffer())
tar.addfile(info, fileobj=data)

# Does not error, but the file also shouldn't exist.
untar_file(test_tar, self.tempdir)
assert not os.path.exists(evil_path)


def test_unpack_tar_unicode(tmpdir: Path) -> None:
test_tar = tmpdir / "test.tar"
Expand Down