@@ -1377,59 +1377,76 @@ def _proc_pax(self, tarfile):
13771377 else :
13781378 pax_headers = tarfile .pax_headers .copy ()
13791379
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-
13981380 # Parse pax header information. A record looks like that:
13991381 # "%d %s=%s\n" % (length, keyword, value). length is the size
14001382 # 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.
14031384 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"=" )
14081404
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 :
14121407 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"
14141424
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 :
14151433 # Normally, we could just use "utf-8" as the encoding and "strict"
14161434 # as the error handler, but we better not take the risk. For
14171435 # example, GNU tar <= 1.23 is known to store filenames it cannot
14181436 # translate to UTF-8 as raw strings (unfortunately without a
14191437 # hdrcharset=BINARY header).
14201438 # We first try the strict standard encoding, and if that fails we
14211439 # 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" ,
14231441 tarfile .errors )
14241442 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 ,
14261444 tarfile .errors )
14271445 else :
1428- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1446+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
14291447 tarfile .errors )
14301448
14311449 pax_headers [keyword ] = value
1432- pos += length
14331450
14341451 # Fetch the next header.
14351452 try :
@@ -1444,7 +1461,7 @@ def _proc_pax(self, tarfile):
14441461
14451462 elif "GNU.sparse.size" in pax_headers :
14461463 # GNU extended sparse format version 0.0.
1447- self ._proc_gnusparse_00 (next , pax_headers , buf )
1464+ self ._proc_gnusparse_00 (next , raw_headers )
14481465
14491466 elif pax_headers .get ("GNU.sparse.major" ) == "1" and pax_headers .get ("GNU.sparse.minor" ) == "0" :
14501467 # GNU extended sparse format version 1.0.
@@ -1466,15 +1483,24 @@ def _proc_pax(self, tarfile):
14661483
14671484 return next
14681485
1469- def _proc_gnusparse_00 (self , next , pax_headers , buf ):
1486+ def _proc_gnusparse_00 (self , next , raw_headers ):
14701487 """Process a GNU tar extended sparse header, version 0.0.
14711488 """
14721489 offsets = []
1473- for match in re .finditer (br"\d+ GNU.sparse.offset=(\d+)\n" , buf ):
1474- offsets .append (int (match .group (1 )))
14751490 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+
14781504 next .sparse = list (zip (offsets , numbytes ))
14791505
14801506 def _proc_gnusparse_01 (self , next , pax_headers ):
0 commit comments