Skip to content

Commit c877a15

Browse files
Handle UnicodeDecodeError for UTF-8 filenames
Add error handling for UTF-8 filename decoding in zipfile.
1 parent 80f9aa0 commit c877a15

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎Lib/zipfile/__init__.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2084,7 +2084,10 @@ def _RealGetContents(self):
20842084
flags = centdir[_CD_FLAG_BITS]
20852085
if flags & _MASK_UTF_FILENAME:
20862086
# UTF-8 file names extension
2087-
filename = filename.decode('utf-8')
2087+
try:
2088+
filename = filename.decode('utf-8')
2089+
except UnicodeDecodeError as e:
2090+
raise BadZipFile("Invalid UTF-8 filename in central directory") from e
20882091
else:
20892092
# Historical ZIP filename encoding
20902093
filename = filename.decode(self.metadata_encoding or 'cp437')
@@ -2264,7 +2267,10 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
22642267

22652268
if fheader[_FH_GENERAL_PURPOSE_FLAG_BITS] & _MASK_UTF_FILENAME:
22662269
# UTF-8 filename
2270+
try:
22672271
fname_str = fname.decode("utf-8")
2272+
except UnicodeDecodeError as e:
2273+
raise BadZipFile("Invalid UTF-8 filename in local file header") from e
22682274
else:
22692275
fname_str = fname.decode(self.metadata_encoding or "cp437")
22702276

0 commit comments

Comments
 (0)