@@ -845,6 +845,9 @@ def data_filter(member, dest_path):
845
845
# Sentinel for replace() defaults, meaning "don't change the attribute"
846
846
_KEEP = object ()
847
847
848
+ # Header length is digits followed by a space.
849
+ _header_length_prefix_re = re .compile (br"([0-9]{1,20}) " )
850
+
848
851
class TarInfo (object ):
849
852
"""Informational class which holds the details about an
850
853
archive member given by a tar header block.
@@ -1432,55 +1435,76 @@ def _proc_pax(self, tarfile):
1432
1435
else :
1433
1436
pax_headers = tarfile .pax_headers .copy ()
1434
1437
1435
- # Check if the pax header contains a hdrcharset field. This tells us
1436
- # the encoding of the path, linkpath, uname and gname fields. Normally,
1437
- # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1438
- # implementations are allowed to store them as raw binary strings if
1439
- # the translation to UTF-8 fails.
1440
- match = re .search (br"\d+ hdrcharset=([^\n]+)\n" , buf )
1441
- if match is not None :
1442
- pax_headers ["hdrcharset" ] = match .group (1 ).decode ("utf-8" )
1443
-
1444
- # For the time being, we don't care about anything other than "BINARY".
1445
- # The only other value that is currently allowed by the standard is
1446
- # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1447
- hdrcharset = pax_headers .get ("hdrcharset" )
1448
- if hdrcharset == "BINARY" :
1449
- encoding = tarfile .encoding
1450
- else :
1451
- encoding = "utf-8"
1452
-
1453
1438
# Parse pax header information. A record looks like that:
1454
1439
# "%d %s=%s\n" % (length, keyword, value). length is the size
1455
1440
# of the complete record including the length field itself and
1456
- # the newline. keyword and value are both UTF-8 encoded strings.
1457
- regex = re .compile (br"(\d+) ([^=]+)=" )
1441
+ # the newline.
1458
1442
pos = 0
1459
- while match := regex .match (buf , pos ):
1460
- length , keyword = match .groups ()
1461
- length = int (length )
1462
- if length == 0 :
1443
+ encoding = None
1444
+ raw_headers = []
1445
+ while len (buf ) > pos and buf [pos ] != 0x00 :
1446
+ if not (match := _header_length_prefix_re .match (buf , pos )):
1447
+ raise InvalidHeaderError ("invalid header" )
1448
+ try :
1449
+ length = int (match .group (1 ))
1450
+ except ValueError :
1451
+ raise InvalidHeaderError ("invalid header" )
1452
+ # Headers must be at least 5 bytes, shortest being '5 x=\n'.
1453
+ # Value is allowed to be empty.
1454
+ if length < 5 :
1455
+ raise InvalidHeaderError ("invalid header" )
1456
+ if pos + length > len (buf ):
1457
+ raise InvalidHeaderError ("invalid header" )
1458
+
1459
+ header_value_end_offset = match .start (1 ) + length - 1 # Last byte of the header
1460
+ keyword_and_value = buf [match .end (1 ) + 1 :header_value_end_offset ]
1461
+ raw_keyword , equals , raw_value = keyword_and_value .partition (b"=" )
1462
+
1463
+ # Check the framing of the header. The last character must be '\n' (0x0A)
1464
+ if not raw_keyword or equals != b"=" or buf [header_value_end_offset ] != 0x0A :
1463
1465
raise InvalidHeaderError ("invalid header" )
1464
- value = buf [match .end (2 ) + 1 :match .start (1 ) + length - 1 ]
1466
+ raw_headers .append ((length , raw_keyword , raw_value ))
1467
+
1468
+ # Check if the pax header contains a hdrcharset field. This tells us
1469
+ # the encoding of the path, linkpath, uname and gname fields. Normally,
1470
+ # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1471
+ # implementations are allowed to store them as raw binary strings if
1472
+ # the translation to UTF-8 fails. For the time being, we don't care about
1473
+ # anything other than "BINARY". The only other value that is currently
1474
+ # allowed by the standard is "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1475
+ # Note that we only follow the initial 'hdrcharset' setting to preserve
1476
+ # the initial behavior of the 'tarfile' module.
1477
+ if raw_keyword == b"hdrcharset" and encoding is None :
1478
+ if raw_value == b"BINARY" :
1479
+ encoding = tarfile .encoding
1480
+ else : # This branch ensures only the first 'hdrcharset' header is used.
1481
+ encoding = "utf-8"
1465
1482
1483
+ pos += length
1484
+
1485
+ # If no explicit hdrcharset is set, we use UTF-8 as a default.
1486
+ if encoding is None :
1487
+ encoding = "utf-8"
1488
+
1489
+ # After parsing the raw headers we can decode them to text.
1490
+ for length , raw_keyword , raw_value in raw_headers :
1466
1491
# Normally, we could just use "utf-8" as the encoding and "strict"
1467
1492
# as the error handler, but we better not take the risk. For
1468
1493
# example, GNU tar <= 1.23 is known to store filenames it cannot
1469
1494
# translate to UTF-8 as raw strings (unfortunately without a
1470
1495
# hdrcharset=BINARY header).
1471
1496
# We first try the strict standard encoding, and if that fails we
1472
1497
# fall back on the user's encoding and error handler.
1473
- keyword = self ._decode_pax_field (keyword , "utf-8" , "utf-8" ,
1498
+ keyword = self ._decode_pax_field (raw_keyword , "utf-8" , "utf-8" ,
1474
1499
tarfile .errors )
1475
1500
if keyword in PAX_NAME_FIELDS :
1476
- value = self ._decode_pax_field (value , encoding , tarfile .encoding ,
1501
+ value = self ._decode_pax_field (raw_value , encoding , tarfile .encoding ,
1477
1502
tarfile .errors )
1478
1503
else :
1479
- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1504
+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
1480
1505
tarfile .errors )
1481
1506
1482
1507
pax_headers [keyword ] = value
1483
- pos += length
1484
1508
1485
1509
# Fetch the next header.
1486
1510
try :
@@ -1495,7 +1519,7 @@ def _proc_pax(self, tarfile):
1495
1519
1496
1520
elif "GNU.sparse.size" in pax_headers :
1497
1521
# GNU extended sparse format version 0.0.
1498
- self ._proc_gnusparse_00 (next , pax_headers , buf )
1522
+ self ._proc_gnusparse_00 (next , raw_headers )
1499
1523
1500
1524
elif pax_headers .get ("GNU.sparse.major" ) == "1" and pax_headers .get ("GNU.sparse.minor" ) == "0" :
1501
1525
# GNU extended sparse format version 1.0.
@@ -1517,15 +1541,24 @@ def _proc_pax(self, tarfile):
1517
1541
1518
1542
return next
1519
1543
1520
- def _proc_gnusparse_00 (self , next , pax_headers , buf ):
1544
+ def _proc_gnusparse_00 (self , next , raw_headers ):
1521
1545
"""Process a GNU tar extended sparse header, version 0.0.
1522
1546
"""
1523
1547
offsets = []
1524
- for match in re .finditer (br"\d+ GNU.sparse.offset=(\d+)\n" , buf ):
1525
- offsets .append (int (match .group (1 )))
1526
1548
numbytes = []
1527
- for match in re .finditer (br"\d+ GNU.sparse.numbytes=(\d+)\n" , buf ):
1528
- numbytes .append (int (match .group (1 )))
1549
+ for _ , keyword , value in raw_headers :
1550
+ if keyword == b"GNU.sparse.offset" :
1551
+ try :
1552
+ offsets .append (int (value .decode ()))
1553
+ except ValueError :
1554
+ raise InvalidHeaderError ("invalid header" )
1555
+
1556
+ elif keyword == b"GNU.sparse.numbytes" :
1557
+ try :
1558
+ numbytes .append (int (value .decode ()))
1559
+ except ValueError :
1560
+ raise InvalidHeaderError ("invalid header" )
1561
+
1529
1562
next .sparse = list (zip (offsets , numbytes ))
1530
1563
1531
1564
def _proc_gnusparse_01 (self , next , pax_headers ):
0 commit comments