[Python-checkins] gh-91387: Strip trailing slash from tarfile longname directories (GH-32423)

miss-islington webhook-mailer at python.org
Tue Jun 21 14:10:00 EDT 2022


https://github.com/python/cpython/commit/46d0e1c06e063d0d239dcef60bcafadcfcf2eed1
commit: 46d0e1c06e063d0d239dcef60bcafadcfcf2eed1
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-06-21T11:09:55-07:00
summary:

gh-91387: Strip trailing slash from tarfile longname directories (GH-32423)


Co-authored-by: Brett Cannon <brett at python.org>
(cherry picked from commit c1e19421c23d1261ecbbe7375316adc1c24f0a87)

Co-authored-by: Chris Fernald <chrisf671 at gmail.com>

files:
A Misc/NEWS.d/next/Library/2022-04-08-22-12-11.bpo-47231.lvyglt.rst
M Lib/tarfile.py
M Lib/test/test_tarfile.py

diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 6ada9a05db9cb..dea150e8dbbb6 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1163,6 +1163,11 @@ def _proc_builtin(self, tarfile):
         # header information.
         self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors)
 
+        # Remove redundant slashes from directories. This is to be consistent
+        # with frombuf().
+        if self.isdir():
+            self.name = self.name.rstrip("/")
+
         return self
 
     def _proc_gnulong(self, tarfile):
@@ -1185,6 +1190,11 @@ def _proc_gnulong(self, tarfile):
         elif self.type == GNUTYPE_LONGLINK:
             next.linkname = nts(buf, tarfile.encoding, tarfile.errors)
 
+        # Remove redundant slashes from directories. This is to be consistent
+        # with frombuf().
+        if next.isdir():
+            next.name = next.name.removesuffix("/")
+
         return next
 
     def _proc_sparse(self, tarfile):
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 4bf1ba38f5ea5..c658cca7a7806 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -228,6 +228,7 @@ def test_add_dir_getmember(self):
     def add_dir_and_getmember(self, name):
         with os_helper.temp_cwd():
             with tarfile.open(tmpname, 'w') as tar:
+                tar.format = tarfile.USTAR_FORMAT
                 try:
                     os.mkdir(name)
                     tar.add(name)
@@ -1006,11 +1007,26 @@ def test_header_offset(self):
                                               "iso8859-1", "strict")
             self.assertEqual(tarinfo.type, self.longnametype)
 
+    def test_longname_directory(self):
+        # Test reading a longlink directory. Issue #47231.
+        longdir = ('a' * 101) + '/'
+        with os_helper.temp_cwd():
+            with tarfile.open(tmpname, 'w') as tar:
+                tar.format = self.format
+                try:
+                    os.mkdir(longdir)
+                    tar.add(longdir)
+                finally:
+                    os.rmdir(longdir)
+            with tarfile.open(tmpname) as tar:
+                self.assertIsNotNone(tar.getmember(longdir))
+                self.assertIsNotNone(tar.getmember(longdir.removesuffix('/')))
 
 class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
 
     subdir = "gnu"
     longnametype = tarfile.GNUTYPE_LONGNAME
+    format = tarfile.GNU_FORMAT
 
     # Since 3.2 tarfile is supposed to accurately restore sparse members and
     # produce files with holes. This is what we actually want to test here.
@@ -1070,6 +1086,7 @@ class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase):
 
     subdir = "pax"
     longnametype = tarfile.XHDTYPE
+    format = tarfile.PAX_FORMAT
 
     def test_pax_global_headers(self):
         tar = tarfile.open(tarname, encoding="iso8859-1")
diff --git a/Misc/NEWS.d/next/Library/2022-04-08-22-12-11.bpo-47231.lvyglt.rst b/Misc/NEWS.d/next/Library/2022-04-08-22-12-11.bpo-47231.lvyglt.rst
new file mode 100644
index 0000000000000..ee05c5e285675
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-08-22-12-11.bpo-47231.lvyglt.rst
@@ -0,0 +1 @@
+Fixed an issue with inconsistent trailing slashes in tarfile longname directories.



More information about the Python-checkins mailing list