Skip to content
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

[3.8] bpo-41316: Make tarfile follow specs for FNAME (GH-21511) #22141

Merged
merged 1 commit into from
Oct 21, 2020
Merged
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
2 changes: 2 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def _init_write_gz(self):
self.__write(b"\037\213\010\010" + timestamp + b"\002\377")
if self.name.endswith(".gz"):
self.name = self.name[:-3]
# Honor "directory components removed" from RFC1952
self.name = os.path.basename(self.name)
# RFC1952 says we must use ISO-8859-1 for the FNAME field.
self.__write(self.name.encode("iso-8859-1", "replace") + NUL)

Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,12 +1384,15 @@ def write(self, data):
pax_headers={'non': 'empty'})
self.assertFalse(f.closed)


class GzipWriteTest(GzipTest, WriteTest):
pass


class Bz2WriteTest(Bz2Test, WriteTest):
pass


class LzmaWriteTest(LzmaTest, WriteTest):
pass

Expand Down Expand Up @@ -1432,8 +1435,17 @@ def test_file_mode(self):
finally:
os.umask(original_umask)


class GzipStreamWriteTest(GzipTest, StreamWriteTest):
pass
def test_source_directory_not_leaked(self):
"""
Ensure the source directory is not included in the tar header
per bpo-41316.
"""
tarfile.open(tmpname, self.mode).close()
payload = pathlib.Path(tmpname).read_text(encoding='latin-1')
assert os.path.dirname(tmpname) not in payload


class Bz2StreamWriteTest(Bz2Test, StreamWriteTest):
decompressor = bz2.BZ2Decompressor if bz2 else None
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Colm Buckley
Erik de Bueger
Jan-Hein Bührman
Lars Buitinck
Artem Bulgakov
Dick Bulterman
Bill Bumgarner
Jimmy Burgett
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the :mod:`tarfile` module to write only basename of TAR file to GZIP compression header.