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