Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use cveb.in mirror by default #3265

Merged
merged 6 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions cve_bin_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ def main(argv=None):
"-n",
"--nvd",
action="store",
choices=["api", "api2", "json"],
choices=["api", "api2", "json-mirror", "json-nvd"],
help="choose method for getting CVE lists from NVD",
default="api",
default="json-mirror",
)
data_sources_group.add_argument(
"-u",
Expand Down Expand Up @@ -532,16 +532,17 @@ def main(argv=None):
if not args["nvd_api_key"] and os.getenv("NVD_API_KEY"):
args["nvd_api_key"] = os.getenv("NVD_API_KEY")

if args["nvd_api_key"]:
nvd_type = "api"

# If you're not using an NVD key, let you know how to get one
if not args["nvd_api_key"] and not args["offline"]:
if nvd_type == "json-nvd" and not args["nvd_api_key"] and not args["offline"]:
LOGGER.info("Not using an NVD API key. Your access may be rate limited by NVD.")
LOGGER.info(
"Get an NVD API key here: https://nvd.nist.gov/developers/request-an-api-key"
)
# Default NVD access to use JSON
nvd_type = "json"

if nvd_type == "json":
if nvd_type == "json-nvd":
LOGGER.warning("Using legacy JSON interface")

if platform.system() != "Linux":
Expand Down
27 changes: 19 additions & 8 deletions cve_bin_tool/data_sources/nvd_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,29 @@ class NVD_Source(Data_Source):
SOURCE = "NVD"
CACHEDIR = DISK_LOCATION_DEFAULT
BACKUPCACHEDIR = DISK_LOCATION_BACKUP
FEED = "https://nvd.nist.gov/vuln/data-feeds"
FEED_NVD = "https://nvd.nist.gov/vuln/data-feeds"
FEED_MIRROR = "https://mirror.cveb.in/nvd/json/cve/1.1"
LOGGER = LOGGER.getChild("CVEDB")
NVDCVE_FILENAME_TEMPLATE = NVD_FILENAME_TEMPLATE
META_LINK = "https://nvd.nist.gov"
META_REGEX = re.compile(r"\/feeds\/json\/.*-[0-9]*\.[0-9]*-[0-9]*\.meta")
META_LINK_NVD = "https://nvd.nist.gov"
META_LINK_MIRROR = "https://mirror.cveb.in/nvd/json/cve/1.1"
META_REGEX_NVD = re.compile(r"feeds\/json\/.*-[0-9]*\.[0-9]*-[0-9]*\.meta")
META_REGEX_MIRROR = re.compile(r"nvdcve-[0-9]*\.[0-9]*-[0-9]*\.meta")
RANGE_UNSET = ""

def __init__(
self,
feed: str | None = None,
session: RateLimiter | None = None,
error_mode: ErrorMode = ErrorMode.TruncTrace,
nvd_type: str = "json",
nvd_type: str = "json-mirror",
incremental_update: bool = False,
nvd_api_key: str = "",
):
self.feed = feed if feed is not None else self.FEED
if feed is None:
self.feed = self.FEED_NVD if nvd_type == "json-nvd" else self.FEED_MIRROR
else:
self.feed = feed
self.cachedir = self.CACHEDIR
self.backup_cachedir = self.BACKUPCACHEDIR
self.error_mode = error_mode
Expand Down Expand Up @@ -327,7 +333,7 @@ async def fetch_cves(self):
total_tasks = len(tasks)

# error_mode.value will only be greater than 1 if quiet mode.
if self.error_mode.value > 1 and self.nvd_type == "json":
if self.error_mode.value > 1 and self.nvd_type.startswith("json"):
iter_tasks = track(
asyncio.as_completed(tasks),
description="Downloading CVEs...",
Expand Down Expand Up @@ -401,11 +407,16 @@ async def nist_scrape(self, session: RateLimiter):
async with await session.get(self.feed) as response:
response.raise_for_status()
page = await response.text()
json_meta_links = self.META_REGEX.findall(page)
if self.nvd_type == "json-nvd":
json_meta_links = self.META_REGEX_NVD.findall(page)
meta_host = self.META_LINK_NVD
else:
json_meta_links = self.META_REGEX_MIRROR.findall(page)
meta_host = self.META_LINK_MIRROR
return dict(
await asyncio.gather(
*(
self.getmeta(session, f"{self.META_LINK}{meta_url}")
self.getmeta(session, f"{meta_host}/{meta_url}")
for meta_url in json_meta_links
)
)
Expand Down
Loading