Skip to content

Commit 0540c4e

Browse files
sethmlarsone-nomem
authored andcommitted
gh-141707: Skip TarInfo DIRTYPE normalization during GNU long name handling
(cherry picked from commit 42d754e) Co-authored-by: Seth Michael Larson <seth@python.org> Co-authored-by: Eashwar Ranganathan <eashwar@eashwar.com>
1 parent 57cc1bd commit 0540c4e

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

Lib/tarfile.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,20 @@ def _create_pax_generic_header(cls, pax_headers, type, encoding):
12461246
@classmethod
12471247
def frombuf(cls, buf, encoding, errors):
12481248
"""Construct a TarInfo object from a 512 byte bytes object.
1249+
1250+
To support the old v7 tar format AREGTYPE headers are
1251+
transformed to DIRTYPE headers if their name ends in '/'.
1252+
"""
1253+
return cls._frombuf(buf, encoding, errors)
1254+
1255+
@classmethod
1256+
def _frombuf(cls, buf, encoding, errors, *, dircheck=True):
1257+
"""Construct a TarInfo object from a 512 byte bytes object.
1258+
1259+
If ``dircheck`` is set to ``True`` then ``AREGTYPE`` headers will
1260+
be normalized to ``DIRTYPE`` if the name ends in a trailing slash.
1261+
``dircheck`` must be set to ``False`` if this function is called
1262+
on a follow-up header such as ``GNUTYPE_LONGNAME``.
12491263
"""
12501264
if len(buf) == 0:
12511265
raise EmptyHeaderError("empty header")
@@ -1276,7 +1290,7 @@ def frombuf(cls, buf, encoding, errors):
12761290

12771291
# Old V7 tar format represents a directory as a regular
12781292
# file with a trailing slash.
1279-
if obj.type == AREGTYPE and obj.name.endswith("/"):
1293+
if dircheck and obj.type == AREGTYPE and obj.name.endswith("/"):
12801294
obj.type = DIRTYPE
12811295

12821296
# The old GNU sparse format occupies some of the unused
@@ -1311,8 +1325,15 @@ def fromtarfile(cls, tarfile):
13111325
"""Return the next TarInfo object from TarFile object
13121326
tarfile.
13131327
"""
1328+
return cls._fromtarfile(tarfile)
1329+
1330+
@classmethod
1331+
def _fromtarfile(cls, tarfile, *, dircheck=True):
1332+
"""
1333+
See dircheck documentation in _frombuf().
1334+
"""
13141335
buf = tarfile.fileobj.read(BLOCKSIZE)
1315-
obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
1336+
obj = cls._frombuf(buf, tarfile.encoding, tarfile.errors, dircheck=dircheck)
13161337
obj.offset = tarfile.fileobj.tell() - BLOCKSIZE
13171338
return obj._proc_member(tarfile)
13181339

@@ -1370,7 +1391,7 @@ def _proc_gnulong(self, tarfile):
13701391

13711392
# Fetch the next header and process it.
13721393
try:
1373-
next = self.fromtarfile(tarfile)
1394+
next = self._fromtarfile(tarfile, dircheck=False)
13741395
except HeaderError as e:
13751396
raise SubsequentHeaderError(str(e)) from None
13761397

@@ -1505,7 +1526,7 @@ def _proc_pax(self, tarfile):
15051526

15061527
# Fetch the next header.
15071528
try:
1508-
next = self.fromtarfile(tarfile)
1529+
next = self._fromtarfile(tarfile, dircheck=False)
15091530
except HeaderError as e:
15101531
raise SubsequentHeaderError(str(e)) from None
15111532

Lib/test/test_tarfile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,25 @@ def test_longname_directory(self):
11341134
self.assertIsNotNone(tar.getmember(longdir))
11351135
self.assertIsNotNone(tar.getmember(longdir.removesuffix('/')))
11361136

1137+
def test_longname_file_not_directory(self):
1138+
# Test reading a longname file and ensure it is not handled as a directory
1139+
# Issue #141707
1140+
buf = io.BytesIO()
1141+
with tarfile.open(mode='w', fileobj=buf, format=self.format) as tar:
1142+
ti = tarfile.TarInfo()
1143+
ti.type = tarfile.AREGTYPE
1144+
ti.name = ('a' * 99) + '/' + ('b' * 3)
1145+
tar.addfile(ti)
1146+
1147+
expected = {t.name: t.type for t in tar.getmembers()}
1148+
1149+
buf.seek(0)
1150+
with tarfile.open(mode='r', fileobj=buf) as tar:
1151+
actual = {t.name: t.type for t in tar.getmembers()}
1152+
1153+
self.assertEqual(expected, actual)
1154+
1155+
11371156
class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
11381157

11391158
subdir = "gnu"

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ Dhushyanth Ramasamy
14921492
Ashwin Ramaswami
14931493
Jeff Ramnani
14941494
Bayard Randel
1495+
Eashwar Ranganathan
14951496
Varpu Rantala
14961497
Brodie Rao
14971498
Rémi Rampin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Don't change :class:`tarfile.TarInfo` type from ``AREGTYPE`` to ``DIRTYPE`` when parsing
2+
GNU long name or link headers.

0 commit comments

Comments
 (0)