Skip to content

Commit ed60655

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 b0fe278 commit ed60655

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/python_inspector/utils_pypi.py

Lines changed: 15 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,20 @@ 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+
try:
1706+
if not zipfile.is_zipfile(cached):
1707+
if TRACE_DEEP:
1708+
print(f" FILE CACHE INVALID (corrupted zip): {path_or_url}")
1709+
cache_valid = False
1710+
except (FileNotFoundError, OSError):
1711+
# File was deleted/modified by another task - treat as cache miss
1712+
if TRACE_DEEP:
1713+
print(f" FILE CACHE VANISHED during validation: {path_or_url}")
1714+
cache_valid = False
1715+
17011716
if force or not cache_valid:
17021717
if not cache_valid and os.path.exists(cached):
17031718
if TRACE_DEEP:

0 commit comments

Comments
 (0)