Skip to content

Commit 13c5b53

Browse files
authored
fix: skip invalid year files
Very lazy attempt to avoid processing invalid year files. Signed-off-by: Terri Oda <[email protected]>
1 parent d5dbc0c commit 13c5b53

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

cve_bin_tool/data_sources/nvd_source.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import asyncio
77
import datetime
8-
import glob
8+
9+
# import glob
910
import gzip
1011
import hashlib
1112
import json
@@ -583,17 +584,20 @@ async def cache_update(
583584
if len(gzip_data) == 0:
584585
self.LOGGER.debug(f"Missing data for {filename}")
585586
return
586-
json_data = gzip.decompress(gzip_data)
587-
gotsha = hashlib.sha256(json_data).hexdigest().upper()
588-
async with FileIO(filepath, "wb") as filepath_handle:
589-
await filepath_handle.write(gzip_data)
590-
# Raise error if there was an issue with the sha
591-
if gotsha != sha:
592-
# Remove the file if there was an issue
593-
# exit(100)
594-
filepath.unlink()
595-
with ErrorHandler(mode=self.error_mode, logger=self.LOGGER):
596-
raise SHAMismatch(f"{url} (have: {gotsha}, want: {sha})")
587+
try:
588+
json_data = gzip.decompress(gzip_data)
589+
gotsha = hashlib.sha256(json_data).hexdigest().upper()
590+
async with FileIO(filepath, "wb") as filepath_handle:
591+
await filepath_handle.write(gzip_data)
592+
# Raise error if there was an issue with the sha
593+
if gotsha != sha:
594+
# Remove the file if there was an issue
595+
# exit(100)
596+
filepath.unlink()
597+
with ErrorHandler(mode=self.error_mode, logger=self.LOGGER):
598+
raise SHAMismatch(f"{url} (have: {gotsha}, want: {sha})")
599+
except Exception:
600+
self.LOGGER.warning(f"Invalid data in {filename}, skipping")
597601

598602
def load_nvd_year(self, year: int) -> dict[str, str | object]:
599603
"""
@@ -617,7 +621,9 @@ def nvd_years(self) -> list[int]:
617621
"""
618622
Return the years we have NVD data for.
619623
"""
620-
return sorted(
621-
int(filename.split(".")[-3].split("-")[-1])
622-
for filename in glob.glob(str(Path(self.cachedir) / "nvdcve-1.1-*.json.gz"))
623-
)
624+
# return sorted(
625+
# int(filename.split(".")[-3].split("-")[-1])
626+
# for filename in glob.glob(str(Path(self.cachedir) / "nvdcve-1.1-*.json.gz"))
627+
# )
628+
# FIXME: temporary workaround so we don't try to load bad year data
629+
return list(range(2020, 2025))

0 commit comments

Comments
 (0)