@@ -1377,59 +1377,76 @@ def _proc_pax(self, tarfile):
1377
1377
else :
1378
1378
pax_headers = tarfile .pax_headers .copy ()
1379
1379
1380
- # Check if the pax header contains a hdrcharset field. This tells us
1381
- # the encoding of the path, linkpath, uname and gname fields. Normally,
1382
- # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1383
- # implementations are allowed to store them as raw binary strings if
1384
- # the translation to UTF-8 fails.
1385
- match = re .search (br"\d+ hdrcharset=([^\n]+)\n" , buf )
1386
- if match is not None :
1387
- pax_headers ["hdrcharset" ] = match .group (1 ).decode ("utf-8" )
1388
-
1389
- # For the time being, we don't care about anything other than "BINARY".
1390
- # The only other value that is currently allowed by the standard is
1391
- # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1392
- hdrcharset = pax_headers .get ("hdrcharset" )
1393
- if hdrcharset == "BINARY" :
1394
- encoding = tarfile .encoding
1395
- else :
1396
- encoding = "utf-8"
1397
-
1398
1380
# Parse pax header information. A record looks like that:
1399
1381
# "%d %s=%s\n" % (length, keyword, value). length is the size
1400
1382
# of the complete record including the length field itself and
1401
- # the newline. keyword and value are both UTF-8 encoded strings.
1402
- regex = re .compile (br"(\d+) ([^=]+)=" )
1383
+ # the newline.
1403
1384
pos = 0
1404
- while True :
1405
- match = regex .match (buf , pos )
1406
- if not match :
1407
- break
1385
+ encoding = None
1386
+ raw_headers = []
1387
+ while len (buf ) > pos and buf [pos ] != 0x00 :
1388
+ if not (match := _header_length_prefix_re .match (buf , pos )):
1389
+ raise InvalidHeaderError ("invalid header" )
1390
+ try :
1391
+ length = int (match .group (1 ))
1392
+ except ValueError :
1393
+ raise InvalidHeaderError ("invalid header" )
1394
+ # Headers must be at least 5 bytes, shortest being '5 x=\n'.
1395
+ # Value is allowed to be empty.
1396
+ if length < 5 :
1397
+ raise InvalidHeaderError ("invalid header" )
1398
+ if pos + length > len (buf ):
1399
+ raise InvalidHeaderError ("invalid header" )
1400
+
1401
+ header_value_end_offset = match .start (1 ) + length - 1 # Last byte of the header
1402
+ keyword_and_value = buf [match .end (1 ) + 1 :header_value_end_offset ]
1403
+ raw_keyword , equals , raw_value = keyword_and_value .partition (b"=" )
1408
1404
1409
- length , keyword = match .groups ()
1410
- length = int (length )
1411
- if length == 0 :
1405
+ # Check the framing of the header. The last character must be '\n' (0x0A)
1406
+ if not raw_keyword or equals != b"=" or buf [header_value_end_offset ] != 0x0A :
1412
1407
raise InvalidHeaderError ("invalid header" )
1413
- value = buf [match .end (2 ) + 1 :match .start (1 ) + length - 1 ]
1408
+ raw_headers .append ((length , raw_keyword , raw_value ))
1409
+
1410
+ # Check if the pax header contains a hdrcharset field. This tells us
1411
+ # the encoding of the path, linkpath, uname and gname fields. Normally,
1412
+ # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1413
+ # implementations are allowed to store them as raw binary strings if
1414
+ # the translation to UTF-8 fails. For the time being, we don't care about
1415
+ # anything other than "BINARY". The only other value that is currently
1416
+ # allowed by the standard is "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1417
+ # Note that we only follow the initial 'hdrcharset' setting to preserve
1418
+ # the initial behavior of the 'tarfile' module.
1419
+ if raw_keyword == b"hdrcharset" and encoding is None :
1420
+ if raw_value == b"BINARY" :
1421
+ encoding = tarfile .encoding
1422
+ else : # This branch ensures only the first 'hdrcharset' header is used.
1423
+ encoding = "utf-8"
1414
1424
1425
+ pos += length
1426
+
1427
+ # If no explicit hdrcharset is set, we use UTF-8 as a default.
1428
+ if encoding is None :
1429
+ encoding = "utf-8"
1430
+
1431
+ # After parsing the raw headers we can decode them to text.
1432
+ for length , raw_keyword , raw_value in raw_headers :
1415
1433
# Normally, we could just use "utf-8" as the encoding and "strict"
1416
1434
# as the error handler, but we better not take the risk. For
1417
1435
# example, GNU tar <= 1.23 is known to store filenames it cannot
1418
1436
# translate to UTF-8 as raw strings (unfortunately without a
1419
1437
# hdrcharset=BINARY header).
1420
1438
# We first try the strict standard encoding, and if that fails we
1421
1439
# fall back on the user's encoding and error handler.
1422
- keyword = self ._decode_pax_field (keyword , "utf-8" , "utf-8" ,
1440
+ keyword = self ._decode_pax_field (raw_keyword , "utf-8" , "utf-8" ,
1423
1441
tarfile .errors )
1424
1442
if keyword in PAX_NAME_FIELDS :
1425
- value = self ._decode_pax_field (value , encoding , tarfile .encoding ,
1443
+ value = self ._decode_pax_field (raw_value , encoding , tarfile .encoding ,
1426
1444
tarfile .errors )
1427
1445
else :
1428
- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1446
+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
1429
1447
tarfile .errors )
1430
1448
1431
1449
pax_headers [keyword ] = value
1432
- pos += length
1433
1450
1434
1451
# Fetch the next header.
1435
1452
try :
@@ -1444,7 +1461,7 @@ def _proc_pax(self, tarfile):
1444
1461
1445
1462
elif "GNU.sparse.size" in pax_headers :
1446
1463
# GNU extended sparse format version 0.0.
1447
- self ._proc_gnusparse_00 (next , pax_headers , buf )
1464
+ self ._proc_gnusparse_00 (next , raw_headers )
1448
1465
1449
1466
elif pax_headers .get ("GNU.sparse.major" ) == "1" and pax_headers .get ("GNU.sparse.minor" ) == "0" :
1450
1467
# GNU extended sparse format version 1.0.
@@ -1466,15 +1483,24 @@ def _proc_pax(self, tarfile):
1466
1483
1467
1484
return next
1468
1485
1469
- def _proc_gnusparse_00 (self , next , pax_headers , buf ):
1486
+ def _proc_gnusparse_00 (self , next , raw_headers ):
1470
1487
"""Process a GNU tar extended sparse header, version 0.0.
1471
1488
"""
1472
1489
offsets = []
1473
- for match in re .finditer (br"\d+ GNU.sparse.offset=(\d+)\n" , buf ):
1474
- offsets .append (int (match .group (1 )))
1475
1490
numbytes = []
1476
- for match in re .finditer (br"\d+ GNU.sparse.numbytes=(\d+)\n" , buf ):
1477
- numbytes .append (int (match .group (1 )))
1491
+ for _ , keyword , value in raw_headers :
1492
+ if keyword == b"GNU.sparse.offset" :
1493
+ try :
1494
+ offsets .append (int (value .decode ()))
1495
+ except ValueError :
1496
+ raise InvalidHeaderError ("invalid header" )
1497
+
1498
+ elif keyword == b"GNU.sparse.numbytes" :
1499
+ try :
1500
+ numbytes .append (int (value .decode ()))
1501
+ except ValueError :
1502
+ raise InvalidHeaderError ("invalid header" )
1503
+
1478
1504
next .sparse = list (zip (offsets , numbytes ))
1479
1505
1480
1506
def _proc_gnusparse_01 (self , next , pax_headers ):
0 commit comments