@@ -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,61 +1435,66 @@ 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
- if (
1441
- # Statement is both a contains check (!=-1) and a bounds check (>0)
1442
- (hdrcharset_offset := buf .find (b" hdrcharset=" ) - 1 ) > - 1
1443
- # Check that the character before is a digit (0x30-0x39 is 0-9)
1444
- and 0x30 <= buf [hdrcharset_offset ] <= 0x39
1445
- ):
1446
- match = re .match (br"^\d{1,20} hdrcharset=([^\n]+)\n" , buf [hdrcharset_offset :])
1447
- if match is not None :
1448
- pax_headers ["hdrcharset" ] = match .group (1 ).decode ("utf-8" )
1449
-
1450
- # For the time being, we don't care about anything other than "BINARY".
1451
- # The only other value that is currently allowed by the standard is
1452
- # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1453
- hdrcharset = pax_headers .get ("hdrcharset" )
1454
- if hdrcharset == "BINARY" :
1455
- encoding = tarfile .encoding
1456
- else :
1457
- encoding = "utf-8"
1458
-
1459
1438
# Parse pax header information. A record looks like that:
1460
1439
# "%d %s=%s\n" % (length, keyword, value). length is the size
1461
1440
# of the complete record including the length field itself and
1462
- # the newline. keyword and value are both UTF-8 encoded strings.
1463
- regex = re .compile (br"^(\d{1,20}) ([^=]+)=" )
1441
+ # the newline.
1464
1442
pos = 0
1465
- while match := regex .match (buf [pos :]):
1466
- length , keyword = match .groups ()
1467
- length = int (length )
1468
- if length == 0 :
1443
+ encoding = "utf-8"
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 3 bytes, one for each of keyword, '=', and '\n'.
1453
+ # Value is allowed to be empty.
1454
+ if length < 3 :
1469
1455
raise InvalidHeaderError ("invalid header" )
1470
- value = buf [match .end (2 ) + pos + 1 :match .start (1 ) + pos + length - 1 ]
1456
+ if pos + length > len (buf ):
1457
+ raise InvalidHeaderError ("invalid header" )
1458
+
1459
+ keyword_and_value = buf [match .end (1 ) + 1 :match .start (1 ) + length - 1 ]
1460
+ raw_keyword , equals , raw_value = keyword_and_value .partition (b"=" )
1461
+
1462
+ # Check the framing of the header. The last character must be '\n' (0x0A)
1463
+ if not raw_keyword or equals != b"=" or buf [match .start (1 ) + length - 1 ] != 0x0A :
1464
+ raise InvalidHeaderError ("invalid header" )
1465
+ raw_headers .append ((length , raw_keyword , raw_value ))
1466
+
1467
+ # Check if the pax header contains a hdrcharset field. This tells us
1468
+ # the encoding of the path, linkpath, uname and gname fields. Normally,
1469
+ # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1470
+ # implementations are allowed to store them as raw binary strings if
1471
+ # the translation to UTF-8 fails. For the time being, we don't care about
1472
+ # anything other than "BINARY". The only other value that is currently
1473
+ # allowed by the standard is "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1474
+ if raw_keyword == b"hdrcharset" and raw_value == b"BINARY" :
1475
+ encoding = tarfile .encoding
1471
1476
1477
+ pos += length
1478
+
1479
+ # After parsing the raw headers we can decode them to text.
1480
+ for length , raw_keyword , raw_value in raw_headers :
1472
1481
# Normally, we could just use "utf-8" as the encoding and "strict"
1473
1482
# as the error handler, but we better not take the risk. For
1474
1483
# example, GNU tar <= 1.23 is known to store filenames it cannot
1475
1484
# translate to UTF-8 as raw strings (unfortunately without a
1476
1485
# hdrcharset=BINARY header).
1477
1486
# We first try the strict standard encoding, and if that fails we
1478
1487
# fall back on the user's encoding and error handler.
1479
- keyword = self ._decode_pax_field (keyword , "utf-8" , "utf-8" ,
1488
+ keyword = self ._decode_pax_field (raw_keyword , "utf-8" , "utf-8" ,
1480
1489
tarfile .errors )
1481
1490
if keyword in PAX_NAME_FIELDS :
1482
- value = self ._decode_pax_field (value , encoding , tarfile .encoding ,
1491
+ value = self ._decode_pax_field (raw_value , encoding , tarfile .encoding ,
1483
1492
tarfile .errors )
1484
1493
else :
1485
- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1494
+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
1486
1495
tarfile .errors )
1487
1496
1488
1497
pax_headers [keyword ] = value
1489
- pos += length
1490
1498
1491
1499
# Fetch the next header.
1492
1500
try :
0 commit comments