-
-
Notifications
You must be signed in to change notification settings - Fork 234
Modify NPM importer to support package-first mode #1941
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,19 @@ | |
|
||
# Author: Navonil Das (@NavonilDas) | ||
|
||
import json | ||
import os | ||
import tempfile | ||
from pathlib import Path | ||
from typing import Iterable | ||
|
||
import pytz | ||
import requests | ||
from dateutil.parser import parse | ||
from fetchcode.vcs import fetch_via_vcs | ||
from packageurl import PackageURL | ||
from univers.version_range import NpmVersionRange | ||
from univers.versions import SemverVersion | ||
|
||
from vulnerabilities.importer import AdvisoryData | ||
from vulnerabilities.importer import AffectedPackage | ||
|
@@ -39,14 +44,24 @@ class NpmImporterPipeline(VulnerableCodeBaseImporterPipeline): | |
repo_url = "git+https://github.com/nodejs/security-wg" | ||
importer_name = "Npm Importer" | ||
|
||
is_batch_run = True | ||
|
||
def __init__(self, *args, purl=None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.purl = purl | ||
if self.purl: | ||
NpmImporterPipeline.is_batch_run = False | ||
if self.purl.type != "npm": | ||
print(f"Warning: This importer handles NPM packages. Current PURL: {self.purl!s}") | ||
|
||
@classmethod | ||
def steps(cls): | ||
return ( | ||
return [ | ||
cls.clone, | ||
cls.collect_and_store_advisories, | ||
cls.import_new_advisories, | ||
cls.clean_downloads, | ||
) | ||
] | ||
|
||
def clone(self): | ||
self.log(f"Cloning `{self.repo_url}`") | ||
|
@@ -58,9 +73,26 @@ def advisories_count(self): | |
|
||
def collect_advisories(self) -> Iterable[AdvisoryData]: | ||
vuln_directory = Path(self.vcs_response.dest_dir) / "vuln" / "npm" | ||
|
||
for advisory in vuln_directory.glob("*.json"): | ||
yield from self.to_advisory_data(advisory) | ||
advisory_files = list(vuln_directory.glob("*.json")) | ||
|
||
if not self.is_batch_run: | ||
package_name = self.purl.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we not actually check in our DB if the purl exists, before even checking all files ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does that guarantee that we won't find new advisories if we have the purl in DB? |
||
filtered_files = [] | ||
for advisory_file in advisory_files: | ||
try: | ||
data = load_json(advisory_file) | ||
if data.get("module_name") == package_name: | ||
affected_package = self.get_affected_package(data, package_name) | ||
if not self.purl.version or self._version_is_affected(affected_package): | ||
filtered_files.append(advisory_file) | ||
except Exception as e: | ||
self.log(f"Error processing advisory file {advisory_file}: {str(e)}") | ||
advisory_files = filtered_files | ||
|
||
for advisory in list(advisory_files): | ||
for result in self.to_advisory_data(advisory): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we are not using yield from here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean?
|
||
if result: | ||
yield result | ||
|
||
def to_advisory_data(self, file: Path) -> Iterable[AdvisoryData]: | ||
data = load_json(file) | ||
|
@@ -112,6 +144,11 @@ def to_advisory_data(self, file: Path) -> Iterable[AdvisoryData]: | |
affected_packages.append(self.get_affected_package(data, package_name)) | ||
advsisory_aliases = data.get("cves") or [] | ||
|
||
if self.purl and self.purl.version: | ||
affected_package = affected_packages[0] if affected_packages else None | ||
if affected_package and not self._version_is_affected(affected_package): | ||
return | ||
|
||
for alias in advsisory_aliases: | ||
yield AdvisoryData( | ||
summary=build_description(summary=summary, description=description), | ||
|
@@ -122,6 +159,13 @@ def to_advisory_data(self, file: Path) -> Iterable[AdvisoryData]: | |
url=f"https://github.com/nodejs/security-wg/blob/main/vuln/npm/{id}.json", | ||
) | ||
|
||
def _version_is_affected(self, affected_package): | ||
if not self.purl.version or not affected_package.affected_version_range: | ||
return True | ||
|
||
purl_version = SemverVersion(self.purl.version) | ||
return purl_version in affected_package.affected_version_range | ||
|
||
def get_affected_package(self, data, package_name): | ||
affected_version_range = None | ||
unaffected_version_range = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be a tuple as well to maintain code consistency. I will change this.