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