Skip to content

Commit 2a8f435

Browse files
committed
Refactor github api importer
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 937cd46 commit 2a8f435

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

vulnerabilities/importers/github.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from vulnerabilities.data_source import Advisory
3939
from vulnerabilities.data_source import DataSource
4040
from vulnerabilities.data_source import DataSourceConfiguration
41+
from vulnerabilities.data_source import VulnerabilityReferenceUnit
4142

4243

4344
# set of all possible values of first '%s' = {'MAVEN','COMPOSER', 'NUGET'}
@@ -109,7 +110,8 @@ def fetch(self) -> Mapping[str, List[Mapping]]:
109110
while True:
110111

111112
query_json = {"query": query % (ecosystem, end_cursor_exp)}
112-
resp = requests.post(self.config.endpoint, headers=headers, json=query_json).json()
113+
resp = requests.post(self.config.endpoint,
114+
headers=headers, json=query_json).json()
113115
print(resp)
114116
if resp.get("message") == "Bad credentials":
115117
raise GitHubTokenError("Invalid GitHub token")
@@ -168,40 +170,50 @@ def process_response(self) -> List[Advisory]:
168170
)
169171

170172
affected_purls = {
171-
PackageURL(name=pkg_name, namespace=ns, version=version, type=pkg_type)
173+
PackageURL(name=pkg_name, namespace=ns,
174+
version=version, type=pkg_type)
172175
for version in aff_vers
173176
}
174177

175178
unaffected_purls = {
176-
PackageURL(name=pkg_name, namespace=ns, version=version, type=pkg_type)
179+
PackageURL(name=pkg_name, namespace=ns,
180+
version=version, type=pkg_type)
177181
for version in unaff_vers
178182
}
179183

180184
cve_ids = set()
181-
ref_ids = set()
185+
vuln_references = []
182186
vuln_desc = adv["node"]["advisory"]["summary"]
183187

184188
for vuln in adv["node"]["advisory"]["identifiers"]:
185189
if vuln["type"] == "CVE":
186190
cve_ids.add(vuln["value"])
187-
else:
188-
ref_ids.add(vuln["value"])
191+
192+
elif vuln["type"] == "GHSA":
193+
ghsa = vuln['value']
194+
vuln_references.append(VulnerabilityReferenceUnit(
195+
reference_id=ghsa,
196+
url="https://github.com/advisories/{}".format(
197+
ghsa)
198+
))
199+
189200
for cve_id in cve_ids:
190201
adv_list.append(
191202
Advisory(
192203
cve_id=cve_id,
193204
summary=vuln_desc,
194205
impacted_package_urls=affected_purls,
195206
resolved_package_urls=unaffected_purls,
196-
reference_ids=ref_ids,
207+
vuln_references=vuln_references,
197208
)
198209
)
199210
return adv_list
200211

201212
@staticmethod
202213
def categorize_versions(version_range: str, all_versions: Set[str]) -> Tuple[Set[str], Set[str]]: # nopep8
203214
version_range = RangeSpecifier(version_range)
204-
affected_versions = {version for version in all_versions if version in version_range}
215+
affected_versions = {
216+
version for version in all_versions if version in version_range}
205217
return (affected_versions, all_versions - affected_versions)
206218

207219

vulnerabilities/tests/test_github.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
from requests.models import Response
3333
from packageurl import PackageURL
3434

35+
from vulnerabilities.data_source import Advisory
36+
from vulnerabilities.data_source import VulnerabilityReferenceUnit
3537
from vulnerabilities.importers.github import GitHubAPIDataSource
3638
from vulnerabilities.importers.github import MavenVersionAPI
3739
from vulnerabilities.importers.github import ComposerVersionAPI
3840
from vulnerabilities.importers.github import NugetVersionAPI
3941
from vulnerabilities.importers.github import GitHubTokenError
4042
from vulnerabilities.importers.github import query
41-
from vulnerabilities.data_source import Advisory
4243

4344
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
4445
TEST_DATA = os.path.join(BASE_DIR, 'test_data')
@@ -186,8 +187,9 @@ def test_process_response(self):
186187
subpath=None,
187188
),
188189
},
189-
reference_urls=[],
190-
reference_ids={'GHSA-qcxh-w3j9-58qr'},
190+
vuln_references=[VulnerabilityReferenceUnit(
191+
url='https://github.com/advisories/GHSA-qcxh-w3j9-58qr',
192+
reference_id='GHSA-qcxh-w3j9-58qr')],
191193
cve_id='CVE-2019-0199',
192194
),
193195
Advisory(
@@ -212,8 +214,9 @@ def test_process_response(self):
212214
subpath=None,
213215
)
214216
},
215-
reference_urls=[],
216-
reference_ids={'GHSA-qcxh-w3j9-58qr'},
217+
vuln_references=[VulnerabilityReferenceUnit(
218+
url='https://github.com/advisories/GHSA-qcxh-w3j9-58qr',
219+
reference_id='GHSA-qcxh-w3j9-58qr')],
217220
cve_id='CVE-2019-0199',
218221
),
219222
Advisory(
@@ -237,8 +240,9 @@ def test_process_response(self):
237240
subpath=None,
238241
),
239242
},
240-
reference_urls=[],
241-
reference_ids={'GHSA-c9hw-wf7x-jp9j'},
243+
vuln_references=[VulnerabilityReferenceUnit(
244+
url='https://github.com/advisories/GHSA-c9hw-wf7x-jp9j',
245+
reference_id='GHSA-c9hw-wf7x-jp9j')],
242246
cve_id='CVE-2020-1938',
243247
),
244248
Advisory(
@@ -262,8 +266,9 @@ def test_process_response(self):
262266
subpath=None,
263267
),
264268
},
265-
reference_urls=[],
266-
reference_ids={'GHSA-c9hw-wf7x-jp9j'},
269+
vuln_references=[VulnerabilityReferenceUnit(
270+
url='https://github.com/advisories/GHSA-c9hw-wf7x-jp9j',
271+
reference_id='GHSA-c9hw-wf7x-jp9j')],
267272
cve_id='CVE-2020-1938',
268273
),
269274
Advisory(
@@ -288,8 +293,9 @@ def test_process_response(self):
288293
subpath=None,
289294
)
290295
},
291-
reference_urls=[],
292-
reference_ids={'GHSA-c9hw-wf7x-jp9j'},
296+
vuln_references=[VulnerabilityReferenceUnit(
297+
url='https://github.com/advisories/GHSA-c9hw-wf7x-jp9j',
298+
reference_id='GHSA-c9hw-wf7x-jp9j')],
293299
cve_id='CVE-2020-1938',
294300
),
295301
]
@@ -433,7 +439,8 @@ def test_artifact_url(self):
433439

434440
def test_extract_versions(self):
435441
expected_versions = {'1.2.2', '1.2.3', '1.3.0'}
436-
assert expected_versions == self.version_api.extract_versions(self.response)
442+
assert expected_versions == self.version_api.extract_versions(
443+
self.response)
437444

438445
def test_load_to_api(self):
439446

0 commit comments

Comments
 (0)