@@ -1202,59 +1202,76 @@ def _proc_pax(self, tarfile):
1202
1202
else :
1203
1203
pax_headers = tarfile .pax_headers .copy ()
1204
1204
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
-
1223
1205
# Parse pax header information. A record looks like that:
1224
1206
# "%d %s=%s\n" % (length, keyword, value). length is the size
1225
1207
# 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.
1228
1209
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"=" )
1233
1229
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 :
1237
1232
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"
1239
1249
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 :
1240
1258
# Normally, we could just use "utf-8" as the encoding and "strict"
1241
1259
# as the error handler, but we better not take the risk. For
1242
1260
# example, GNU tar <= 1.23 is known to store filenames it cannot
1243
1261
# translate to UTF-8 as raw strings (unfortunately without a
1244
1262
# hdrcharset=BINARY header).
1245
1263
# We first try the strict standard encoding, and if that fails we
1246
1264
# 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" ,
1248
1266
tarfile .errors )
1249
1267
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 ,
1251
1269
tarfile .errors )
1252
1270
else :
1253
- value = self ._decode_pax_field (value , "utf-8" , "utf-8" ,
1271
+ value = self ._decode_pax_field (raw_value , "utf-8" , "utf-8" ,
1254
1272
tarfile .errors )
1255
1273
1256
1274
pax_headers [keyword ] = value
1257
- pos += length
1258
1275
1259
1276
# Fetch the next header.
1260
1277
try :
@@ -1269,7 +1286,7 @@ def _proc_pax(self, tarfile):
1269
1286
1270
1287
elif "GNU.sparse.size" in pax_headers :
1271
1288
# GNU extended sparse format version 0.0.
1272
- self ._proc_gnusparse_00 (next , pax_headers , buf )
1289
+ self ._proc_gnusparse_00 (next , raw_headers )
1273
1290
1274
1291
elif pax_headers .get ("GNU.sparse.major" ) == "1" and pax_headers .get ("GNU.sparse.minor" ) == "0" :
1275
1292
# GNU extended sparse format version 1.0.
@@ -1291,15 +1308,24 @@ def _proc_pax(self, tarfile):
1291
1308
1292
1309
return next
1293
1310
1294
- def _proc_gnusparse_00 (self , next , pax_headers , buf ):
1311
+ def _proc_gnusparse_00 (self , next , raw_headers ):
1295
1312
"""Process a GNU tar extended sparse header, version 0.0.
1296
1313
"""
1297
1314
offsets = []
1298
- for match in re .finditer (br"\d+ GNU.sparse.offset=(\d+)\n" , buf ):
1299
- offsets .append (int (match .group (1 )))
1300
1315
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
+
1303
1329
next .sparse = list (zip (offsets , numbytes ))
1304
1330
1305
1331
def _proc_gnusparse_01 (self , next , pax_headers ):
0 commit comments