-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Check symlink target in tar extraction fallback for Pythons without data_filter
#13538
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?