Skip to content

Commit e0028f8

Browse files
Validate cached zip files before reuse
We've seen corrupted wheel files in Python Inspector's cache leading to `BadZipFile` exceptions. Add additional validation that ensures that files that will be reused from the cache are actual zip files. Signed-off-by: Marcel Bochtler <[email protected]>
1 parent 9db72eb commit e0028f8

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/python_inspector/utils_pypi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import attr
3535
import packageurl
3636
import requests
37+
import zipfile
3738
from bs4 import BeautifulSoup
3839
from commoncode import fileutils
3940
from commoncode.hash import multi_checksums
@@ -1698,6 +1699,15 @@ async def get(
16981699

16991700
cache_valid = os.path.exists(cached) and os.path.getsize(cached) > 0
17001701

1702+
# Validate cached wheel/egg files.
1703+
if cache_valid and not as_text:
1704+
if path_or_url.endswith((".whl", ".egg", ".zip")):
1705+
if not zipfile.is_zipfile(cached):
1706+
if TRACE_DEEP:
1707+
print(f" FILE CACHE INVALID (corrupted zip): {path_or_url}")
1708+
cache_valid = False
1709+
os.remove(cached)
1710+
17011711
if force or not cache_valid:
17021712
if not cache_valid and os.path.exists(cached):
17031713
if TRACE_DEEP:

0 commit comments

Comments
 (0)