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