-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ninoseki/use-cvss
feat: use cvss lib to calculate rating
- Loading branch information
Showing
7 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Optional, Union, cast | ||
|
||
from cvss import CVSS2, CVSS3 | ||
from cyclonedx.model.vulnerability import VulnerabilityRating | ||
|
||
from .cve_search import get_cve_search | ||
from .exceptions import CVESearchQueryException | ||
from .osv.schemas import Severity, Vuln | ||
|
||
|
||
def severity_to_rating(severity: Severity) -> Optional[VulnerabilityRating]: | ||
severity_type_to_cvss = { | ||
"CVSS_V3": CVSS3, | ||
"CVSS_V2": CVSS2, | ||
} | ||
|
||
klass = severity_type_to_cvss.get(severity.type) | ||
if klass is None: | ||
return None | ||
|
||
c = cast(Union[CVSS2, CVSS3], klass(severity.score)) | ||
score, _, _ = c.scores() | ||
severity_str, _, _ = c.severities() | ||
return VulnerabilityRating(score=score, severity=severity_str) | ||
|
||
|
||
class RatingFactory: | ||
@staticmethod | ||
def from_osv_vuln(vuln: Vuln) -> Optional[VulnerabilityRating]: | ||
rating: Optional[VulnerabilityRating] = None | ||
|
||
if len(vuln.severity) > 0: | ||
# TODO: consider a case when there are multi severities | ||
severity = vuln.severity[0] | ||
rating = severity_to_rating(severity) | ||
if rating is not None: | ||
return rating | ||
|
||
if vuln.cve_id is not None: | ||
try: | ||
cve_search = get_cve_search() | ||
res = cve_search.query(vuln.cve_id) | ||
rating = VulnerabilityRating(score=res.cvss, severity=res.severity) | ||
except CVESearchQueryException: | ||
pass | ||
|
||
return rating |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
from pycomponents.osv.schemas import Severity | ||
from pycomponents.rating import severity_to_rating | ||
|
||
|
||
@pytest.fixture | ||
def severity(): | ||
return Severity( | ||
type="CVSS_V3", score="CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:N/A:N" | ||
) | ||
|
||
|
||
def test_severity_to_rating(severity: Severity): | ||
rating = severity_to_rating(severity) | ||
assert rating.score == 8.6 | ||
assert rating.severity == "High" |