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