@@ -843,6 +843,9 @@ def data_filter(member, dest_path):
843843# Sentinel for replace() defaults, meaning "don't change the attribute"
844844_KEEP = object ()
845845
846+ # Header length is digits followed by a space.
847+ _header_length_prefix_re = re .compile (br"([0-9]{1,20}) " )
848+
846849class TarInfo (object ):
847850 """Informational class which holds the details about an
848851 archive member given by a tar header block.
@@ -1412,55 +1415,76 @@ def _proc_pax(self, tarfile):
14121415 else :
14131416 pax_headers = tarfile .pax_headers .copy ()
14141417
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-
14331418 # Parse pax header information. A record looks like that:
14341419 # "%d %s=%s\n" % (length, keyword, value). length is the size
14351420 # 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.
14381422 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 :
14431445 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"
14451462
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 :
14461471 # Normally, we could just use "utf-8" as the encoding and "strict"
14471472 # as the error handler, but we better not take the risk. For
14481473 # example, GNU tar <= 1.23 is known to store filenames it cannot
14491474 # translate to UTF-8 as raw strings (unfortunately without a
14501475 # hdrcharset=BINARY header).
14511476 # We first try the strict standard encoding, and if that fails we
14521477 # 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" ,
14541479 tarfile .errors )
14551480 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 ,
14571482 tarfile .errors )
14581483 else :
1459- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1484+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
14601485 tarfile .errors )
14611486
14621487 pax_headers [keyword ] = value
1463- pos += length
14641488
14651489 # Fetch the next header.
14661490 try :
@@ -1475,7 +1499,7 @@ def _proc_pax(self, tarfile):
14751499
14761500 elif "GNU.sparse.size" in pax_headers :
14771501 # GNU extended sparse format version 0.0.
1478- self ._proc_gnusparse_00 (next , pax_headers , buf )
1502+ self ._proc_gnusparse_00 (next , raw_headers )
14791503
14801504 elif pax_headers .get ("GNU.sparse.major" ) == "1" and pax_headers .get ("GNU.sparse.minor" ) == "0" :
14811505 # GNU extended sparse format version 1.0.
@@ -1497,15 +1521,24 @@ def _proc_pax(self, tarfile):
14971521
14981522 return next
14991523
1500- def _proc_gnusparse_00 (self , next , pax_headers , buf ):
1524+ def _proc_gnusparse_00 (self , next , raw_headers ):
15011525 """Process a GNU tar extended sparse header, version 0.0.
15021526 """
15031527 offsets = []
1504- for match in re .finditer (br"\d+ GNU.sparse.offset=(\d+)\n" , buf ):
1505- offsets .append (int (match .group (1 )))
15061528 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+
15091542 next .sparse = list (zip (offsets , numbytes ))
15101543
15111544 def _proc_gnusparse_01 (self , next , pax_headers ):
0 commit comments