@@ -1202,59 +1202,76 @@ def _proc_pax(self, tarfile):
12021202 else :
12031203 pax_headers = tarfile .pax_headers .copy ()
12041204
1205- # Check if the pax header contains a hdrcharset field. This tells us
1206- # the encoding of the path, linkpath, uname and gname fields. Normally,
1207- # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1208- # implementations are allowed to store them as raw binary strings if
1209- # the translation to UTF-8 fails.
1210- match = re .search (br"\d+ hdrcharset=([^\n]+)\n" , buf )
1211- if match is not None :
1212- pax_headers ["hdrcharset" ] = match .group (1 ).decode ("utf-8" )
1213-
1214- # For the time being, we don't care about anything other than "BINARY".
1215- # The only other value that is currently allowed by the standard is
1216- # "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1217- hdrcharset = pax_headers .get ("hdrcharset" )
1218- if hdrcharset == "BINARY" :
1219- encoding = tarfile .encoding
1220- else :
1221- encoding = "utf-8"
1222-
12231205 # Parse pax header information. A record looks like that:
12241206 # "%d %s=%s\n" % (length, keyword, value). length is the size
12251207 # of the complete record including the length field itself and
1226- # the newline. keyword and value are both UTF-8 encoded strings.
1227- regex = re .compile (br"(\d+) ([^=]+)=" )
1208+ # the newline.
12281209 pos = 0
1229- while True :
1230- match = regex .match (buf , pos )
1231- if not match :
1232- break
1210+ encoding = None
1211+ raw_headers = []
1212+ while len (buf ) > pos and buf [pos ] != 0x00 :
1213+ if not (match := _header_length_prefix_re .match (buf , pos )):
1214+ raise InvalidHeaderError ("invalid header" )
1215+ try :
1216+ length = int (match .group (1 ))
1217+ except ValueError :
1218+ raise InvalidHeaderError ("invalid header" )
1219+ # Headers must be at least 5 bytes, shortest being '5 x=\n'.
1220+ # Value is allowed to be empty.
1221+ if length < 5 :
1222+ raise InvalidHeaderError ("invalid header" )
1223+ if pos + length > len (buf ):
1224+ raise InvalidHeaderError ("invalid header" )
1225+
1226+ header_value_end_offset = match .start (1 ) + length - 1 # Last byte of the header
1227+ keyword_and_value = buf [match .end (1 ) + 1 :header_value_end_offset ]
1228+ raw_keyword , equals , raw_value = keyword_and_value .partition (b"=" )
12331229
1234- length , keyword = match .groups ()
1235- length = int (length )
1236- if length == 0 :
1230+ # Check the framing of the header. The last character must be '\n' (0x0A)
1231+ if not raw_keyword or equals != b"=" or buf [header_value_end_offset ] != 0x0A :
12371232 raise InvalidHeaderError ("invalid header" )
1238- value = buf [match .end (2 ) + 1 :match .start (1 ) + length - 1 ]
1233+ raw_headers .append ((length , raw_keyword , raw_value ))
1234+
1235+ # Check if the pax header contains a hdrcharset field. This tells us
1236+ # the encoding of the path, linkpath, uname and gname fields. Normally,
1237+ # these fields are UTF-8 encoded but since POSIX.1-2008 tar
1238+ # implementations are allowed to store them as raw binary strings if
1239+ # the translation to UTF-8 fails. For the time being, we don't care about
1240+ # anything other than "BINARY". The only other value that is currently
1241+ # allowed by the standard is "ISO-IR 10646 2000 UTF-8" in other words UTF-8.
1242+ # Note that we only follow the initial 'hdrcharset' setting to preserve
1243+ # the initial behavior of the 'tarfile' module.
1244+ if raw_keyword == b"hdrcharset" and encoding is None :
1245+ if raw_value == b"BINARY" :
1246+ encoding = tarfile .encoding
1247+ else : # This branch ensures only the first 'hdrcharset' header is used.
1248+ encoding = "utf-8"
12391249
1250+ pos += length
1251+
1252+ # If no explicit hdrcharset is set, we use UTF-8 as a default.
1253+ if encoding is None :
1254+ encoding = "utf-8"
1255+
1256+ # After parsing the raw headers we can decode them to text.
1257+ for length , raw_keyword , raw_value in raw_headers :
12401258 # Normally, we could just use "utf-8" as the encoding and "strict"
12411259 # as the error handler, but we better not take the risk. For
12421260 # example, GNU tar <= 1.23 is known to store filenames it cannot
12431261 # translate to UTF-8 as raw strings (unfortunately without a
12441262 # hdrcharset=BINARY header).
12451263 # We first try the strict standard encoding, and if that fails we
12461264 # fall back on the user's encoding and error handler.
1247- keyword = self ._decode_pax_field (keyword , "utf-8" , "utf-8" ,
1265+ keyword = self ._decode_pax_field (raw_keyword , "utf-8" , "utf-8" ,
12481266 tarfile .errors )
12491267 if keyword in PAX_NAME_FIELDS :
1250- value = self ._decode_pax_field (value , encoding , tarfile .encoding ,
1268+ value = self ._decode_pax_field (raw_value , encoding , tarfile .encoding ,
12511269 tarfile .errors )
12521270 else :
1253- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1271+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
12541272 tarfile .errors )
12551273
12561274 pax_headers [keyword ] = value
1257- pos += length
12581275
12591276 # Fetch the next header.
12601277 try :
@@ -1269,7 +1286,7 @@ def _proc_pax(self, tarfile):
12691286
12701287 elif "GNU.sparse.size" in pax_headers :
12711288 # GNU extended sparse format version 0.0.
1272- self ._proc_gnusparse_00 (next , pax_headers , buf )
1289+ self ._proc_gnusparse_00 (next , raw_headers )
12731290
12741291 elif pax_headers .get ("GNU.sparse.major" ) == "1" and pax_headers .get ("GNU.sparse.minor" ) == "0" :
12751292 # GNU extended sparse format version 1.0.
@@ -1291,15 +1308,24 @@ def _proc_pax(self, tarfile):
12911308
12921309 return next
12931310
1294- def _proc_gnusparse_00 (self , next , pax_headers , buf ):
1311+ def _proc_gnusparse_00 (self , next , raw_headers ):
12951312 """Process a GNU tar extended sparse header, version 0.0.
12961313 """
12971314 offsets = []
1298- for match in re .finditer (br"\d+ GNU.sparse.offset=(\d+)\n" , buf ):
1299- offsets .append (int (match .group (1 )))
13001315 numbytes = []
1301- for match in re .finditer (br"\d+ GNU.sparse.numbytes=(\d+)\n" , buf ):
1302- numbytes .append (int (match .group (1 )))
1316+ for _ , keyword , value in raw_headers :
1317+ if keyword == b"GNU.sparse.offset" :
1318+ try :
1319+ offsets .append (int (value .decode ()))
1320+ except ValueError :
1321+ raise InvalidHeaderError ("invalid header" )
1322+
1323+ elif keyword == b"GNU.sparse.numbytes" :
1324+ try :
1325+ numbytes .append (int (value .decode ()))
1326+ except ValueError :
1327+ raise InvalidHeaderError ("invalid header" )
1328+
13031329 next .sparse = list (zip (offsets , numbytes ))
13041330
13051331 def _proc_gnusparse_01 (self , next , pax_headers ):
0 commit comments