Skip to content

Commit 2757e66

Browse files
authored
feat: including metric table in Console (#3215)
1 parent 0c35c46 commit 2757e66

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

cve_bin_tool/output_engine/console.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,47 @@ def validate_cell_length(cell_name, cell_type):
268268
table.add_row(*cells)
269269
# Print the table to the console
270270
console.print(table)
271+
272+
table = Table()
273+
# Add Head Columns to the Table
274+
table.add_column("CVE")
275+
table.add_column("CVSS_version")
276+
table.add_column("CVSS_score")
277+
table.add_column("EPSS_propability")
278+
table.add_column("EPSS_percentile")
279+
color = "green"
280+
281+
cve_by_metrics: defaultdict[Remarks, list[dict[str, str]]] = defaultdict(list)
282+
# group cve_data by its remarks and separately by paths
283+
for product_info, cve_data in all_cve_data.items():
284+
for cve in cve_data["cves"]:
285+
propability = "-"
286+
percentile = "-"
287+
for metric, field in cve.metric.items():
288+
if metric == "EPSS":
289+
propability = round(field[0] * 100, 4)
290+
percentile = field[1]
291+
cve_by_metrics[cve.remarks].append(
292+
{
293+
"cve_number": cve.cve_number,
294+
"cvss_version": str(cve.cvss_version),
295+
"cvss_score": str(cve.score),
296+
"epss_propability": str(propability),
297+
"epss_percentile": str(percentile),
298+
"severity": cve.severity,
299+
}
300+
)
301+
302+
for remarks in sorted(cve_by_remarks):
303+
color = remarks_colors[remarks]
304+
for cve in cve_by_metrics[remarks]:
305+
color = cve["severity"].split("-")[0].lower()
306+
cells = [
307+
Text.styled(cve["cve_number"], color),
308+
Text.styled(cve["cvss_version"], color),
309+
Text.styled(str(cve["cvss_score"]), color),
310+
Text.styled(cve["epss_propability"], color),
311+
Text.styled(cve["epss_percentile"], color),
312+
]
313+
table.add_row(*cells)
314+
console.print(table)

test/test_output_engine.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,18 @@ def test_output_console(self):
902902
"│ vendor1 │ product1 │ 3.2.1.0 │ CVE-1234-1234 │ OSV │ HIGH │ 7.5 (v2) │ 4.68 │ 0.34072 │\n"
903903
"└─────────┴──────────┴─────────┴───────────────┴────────┴──────────┴────────────────┴─────────────────┴────────────────┘\n"
904904
)
905+
expected_output_2 = (
906+
"│ CVE-1234-1234 │ 2 │ 4.2 │ 0.126 │ 0.46387 │\n"
907+
"│ CVE-1234-1234 │ 2 │ 1.2 │ 1.836 │ 0.79673 │\n"
908+
"│ CVE-1234-1234 │ 3 │ 2.5 │ 3.895 │ 0.37350 │\n"
909+
"│ CVE-1234-1234 │ 2 │ 7.5 │ 4.68 │ 0.34072 │\n"
910+
"└───────────────┴──────────────┴────────────┴──────────────────┴─────────────────┘\n"
911+
)
905912

906913
self.mock_file.seek(0) # reset file position
907914
result = self.mock_file.read()
908915
self.assertIn(expected_output, result)
916+
self.assertIn(expected_output_2, result)
909917

910918
def test_output_console_affected_versions(self):
911919
"""Test Formatting Output as console with affected-versions"""
@@ -980,10 +988,18 @@ def test_output_console_outfile(self):
980988
"│ vendor1 │ product1 │ 3.2.1.0 │ CVE-1234-1234 │ OSV │ HIGH │ 7.5 (v2) │ 4.68 │ 0.34072 │\n"
981989
"└─────────┴──────────┴─────────┴───────────────┴────────┴──────────┴────────────────┴─────────────────┴────────────────┘\n"
982990
)
991+
expected_output_2 = (
992+
"│ CVE-1234-1234 │ 2 │ 4.2 │ 0.126 │ 0.46387 │\n"
993+
"│ CVE-1234-1234 │ 2 │ 1.2 │ 1.836 │ 0.79673 │\n"
994+
"│ CVE-1234-1234 │ 3 │ 2.5 │ 3.895 │ 0.37350 │\n"
995+
"│ CVE-1234-1234 │ 2 │ 7.5 │ 4.68 │ 0.34072 │\n"
996+
"└───────────────┴──────────────┴────────────┴──────────────────┴─────────────────┘\n"
997+
)
983998

984999
with open(tmpf.name, encoding="utf-8") as f:
9851000
result = f.read()
9861001
self.assertIn(expected_output, result)
1002+
self.assertIn(expected_output_2, result)
9871003
Path(tmpf.name).unlink() # deleting tempfile
9881004

9891005
def test_output_file(self):

0 commit comments

Comments
 (0)