Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions vulnerabilities/pipelines/github_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def steps(cls):
"RUBYGEMS": "gem",
"NPM": "npm",
"RUST": "cargo",
# "GO": "golang",
"GO": "golang",
}

def advisories_count(self):
Expand All @@ -79,7 +79,7 @@ def collect_advisories(self) -> Iterable[AdvisoryData]:
# TODO: We will try to gather more info from GH API
# Check https://github.com/nexB/vulnerablecode/issues/1039#issuecomment-1366458885
# Check https://github.com/nexB/vulnerablecode/issues/645
# set of all possible values of first '%s' = {'MAVEN','COMPOSER', 'NUGET', 'RUBYGEMS', 'PYPI', 'NPM', 'RUST'}
# set of all possible values of first '%s' = {'MAVEN','COMPOSER', 'NUGET', 'RUBYGEMS', 'PYPI', 'NPM', 'RUST', 'GO'}
# second '%s' is interesting, it will have the value '' for the first request,
advisory_query = """
query{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def collect_advisories(self) -> Iterable[AdvisoryDataV2]:
"pypi",
"npm",
"maven",
# "golang",
"golang",
"composer",
"hex",
"gem",
Expand Down
37 changes: 37 additions & 0 deletions vulnerabilities/tests/pipelines/test_github_importer_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,43 @@ def test_process_response_github_importer(pkg_type, regen=REGEN):
assert result == expected


def test_collect_advisories_includes_golang_ecosystem():
with open(TEST_DATA / "golang.json") as f:
golang_response = json.load(f)

queried_ecosystems = []
golang_pages = [golang_response]

def fake_fetch(graphql_query):
query = graphql_query["query"]
for ecosystem in GitHubAPIImporterPipeline.package_type_by_github_ecosystem:
if f"ecosystem: {ecosystem}" in query:
queried_ecosystems.append(ecosystem)
if ecosystem == "GO" and golang_pages:
return golang_pages.pop()
return {
"data": {
"securityVulnerabilities": {
"edges": [],
"pageInfo": {"hasNextPage": False, "endCursor": None},
}
}
}

with mock.patch(
"vulnerabilities.pipelines.github_importer.utils.fetch_github_graphql_query",
side_effect=fake_fetch,
):
advisories = list(GitHubAPIImporterPipeline().collect_advisories())

assert "GO" in queried_ecosystems
golang_packages = [
affected.package for advisory in advisories for affected in advisory.affected_packages
]
assert golang_packages
assert all(package.type == "golang" for package in golang_packages)


def test_process_response_with_empty_vulnaribilities():
logger = TestLogger()
list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,47 @@ def delete(self):
assert advisory.original_advisory_text.strip().startswith("{")
assert advisory.affected_packages
assert advisory.affected_packages[0].package.type == "pypi"


@pytest.fixture
def sample_go_osv_advisory(tmp_path: Path):
advisory_data = {
"id": "GHSA-golang-test-0001",
"summary": "Sample Go summary",
"details": "Sample Go details",
"aliases": ["CVE-2021-99998"],
"affected": [
{
"package": {"name": "github.com/moby/moby", "ecosystem": "Go"},
"ranges": [{"type": "SEMVER", "events": [{"introduced": "0"}, {"fixed": "1.3.3"}]}],
"versions": ["1.3.2"],
}
],
"database_specific": {"cwe_ids": ["CWE-22"]},
}

advisory_dir = tmp_path / "advisories/github-reviewed/sample_go_project"
advisory_dir.mkdir(parents=True)

advisory_file = advisory_dir / "GHSA-golang-test-0001.json"
advisory_file.write_text(json.dumps(advisory_data, indent=2))

return tmp_path


def test_collect_advisories_from_github_osv_golang(sample_go_osv_advisory):
class DummyVCSResponse:
dest_dir = str(sample_go_osv_advisory)

def delete(self):
pass

importer = GithubOSVImporterPipeline()
importer.vcs_response = DummyVCSResponse()

advisories = list(importer.collect_advisories())
assert len(advisories) == 1

advisory = advisories[0]
assert advisory.affected_packages
assert advisory.affected_packages[0].package.type == "golang"