From 1ceb8fbf85408a7b5312e0646246783b2f0f7710 Mon Sep 17 00:00:00 2001 From: kannkyo <15080890+kannkyo@users.noreply.github.com> Date: Mon, 9 Dec 2024 23:09:04 +0900 Subject: [PATCH 1/2] add ge and le function --- src/epss_api/epss.py | 58 ++++++++++++++++++++++++++++++++++++- tests/epss_api/test_epss.py | 36 +++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/epss_api/epss.py b/src/epss_api/epss.py index 84346f1..879abc5 100644 --- a/src/epss_api/epss.py +++ b/src/epss_api/epss.py @@ -136,6 +136,34 @@ def percentile_gt(self, min: float) -> list[Score]: return list(self._sortedScoresByPercentile[i:]) + def epss_ge(self, min: float) -> list[Score]: + """Get CVEs with EPSS score greater than or equal to the parameter + + Args: + min (float): limit of EPSS score + + Returns: + list[Score] | None: EPSS score object list + """ + i = bisect_left(self._sortedScoresByEpss, min, + key=lambda x: x.epss) + + return list(self._sortedScoresByEpss[i:]) + + def percentile_ge(self, min: float) -> list[Score]: + """Get CVEs with percentile greater than or equal to the parameter + + Args: + min (float): limit of percentile + + Returns: + list[Score] | None: EPSS score object list + """ + i = bisect_left(self._sortedScoresByPercentile, min, + key=lambda x: x.percentile) + + return list(self._sortedScoresByPercentile[i:]) + def epss_lt(self, max: float) -> list[Score]: """Get CVEs with EPSS score lower than the parameter @@ -151,7 +179,7 @@ def epss_lt(self, max: float) -> list[Score]: return list(self._sortedScoresByEpss[:i]) def percentile_lt(self, max: float) -> list[Score]: - """Get CVEs with percentile lower than the parameter + """Get CVEs with percentile less than the parameter Args: max (float): limit of percentile @@ -164,6 +192,34 @@ def percentile_lt(self, max: float) -> list[Score]: return list(self._sortedScoresByPercentile[:i]) + def epss_le(self, max: float) -> list[Score]: + """Get CVEs with EPSS score less than or equal to the parameter + + Args: + max (float): limit of EPSS score + + Returns: + list[Score] | []: EPSS score object list + """ + i = bisect_right(self._sortedScoresByEpss, max, + key=lambda x: x.epss) + + return list(self._sortedScoresByEpss[:i]) + + def percentile_le(self, max: float) -> list[Score]: + """Get CVEs with percentile less than or equal to the parameter + + Args: + max (float): limit of percentile + + Returns: + list[Score] | []: EPSS score object list + """ + i = bisect_right(self._sortedScoresByPercentile, max, + key=lambda x: x.percentile) + + return list(self._sortedScoresByPercentile[:i]) + def csv(self) -> list[str]: """Get csv data containing all epss scores. diff --git a/tests/epss_api/test_epss.py b/tests/epss_api/test_epss.py index f15c29d..abfde47 100644 --- a/tests/epss_api/test_epss.py +++ b/tests/epss_api/test_epss.py @@ -42,6 +42,24 @@ def test_percentile_gt(min): assert s.percentile <= min +@pytest.mark.parametrize("min", [-1, 0, 0.5, 1, 2]) +def test_epss_ge(min): + scores = epss.epss_ge(min) + for s in scores: + assert s.epss >= min + for s in list(set(scores) - set(epss.scores())): + assert s.epss < min + + +@pytest.mark.parametrize("min", [-1, 0, 0.5, 1, 2]) +def test_percentile_ge(min): + scores = epss.percentile_ge(min) + for s in scores: + assert s.percentile >= min + for s in list(set(scores) - set(epss.scores())): + assert s.percentile < min + + @pytest.mark.parametrize("max", [-1, 0, 0.5, 1, 2]) def test_epss_lt(max): scores = epss.epss_lt(max) @@ -60,6 +78,24 @@ def test_percentile_lt(max): assert s.percentile >= max +@pytest.mark.parametrize("max", [-1, 0, 0.5, 1, 2]) +def test_epss_le(max): + scores = epss.epss_le(max) + for s in scores: + assert s.epss <= max + for s in list(set(scores) - set(epss.scores())): + assert s.epss > max + + +@pytest.mark.parametrize("max", [-1, 0, 0.5, 1, 2]) +def test_percentile_le(max): + scores = epss.percentile_le(max) + for s in scores: + assert s.percentile <= max + for s in list(set(scores) - set(epss.scores())): + assert s.percentile > max + + def test_score(): score = epss.score(cve_id='CVE-2022-0669') assert score.cve.startswith('CVE-') From f6ebed91144e6cbacef4eb7affc9717b4759d616 Mon Sep 17 00:00:00 2001 From: kannkyo <15080890+kannkyo@users.noreply.github.com> Date: Mon, 9 Dec 2024 23:11:08 +0900 Subject: [PATCH 2/2] fix W291, trailing whitespace failed --- src/epss_api/epss.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/epss_api/epss.py b/src/epss_api/epss.py index 879abc5..c2b75be 100644 --- a/src/epss_api/epss.py +++ b/src/epss_api/epss.py @@ -10,7 +10,7 @@ class Score(object): """ EPSS Score Object""" def __init__(self, cve: str, epss: str, percentile: str): - """Initialize EPSS Score Object + """Initialize EPSS Score Object Args: cve (str): CVE-yyyy-n