-
Notifications
You must be signed in to change notification settings - Fork 568
fix: handle unknown values in metric_finder (Fixes #4578) #4682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79e91d1
Fixes #4578 ([CVEDB] Why does the function metric_finder returns unkn…
vedpawar2254 b7369fe
Update cvedb.py
vedpawar2254 37d5460
Merge branch 'intel:main' into Fixes-#4578
vedpawar2254 168d40e
changed metric from 0 to UNKNOWN_METRIC_ID
vedpawar2254 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -45,6 +45,7 @@ | |
DBNAME = "cve.db" | ||
OLD_CACHE_DIR = Path("~") / ".cache" / "cvedb" | ||
|
||
UNKNOWN_METRIC_ID = 0 | ||
EPSS_METRIC_ID = 1 | ||
CVSS_2_METRIC_ID = 2 | ||
CVSS_3_METRIC_ID = 3 | ||
|
@@ -416,6 +417,8 @@ def init_database(self) -> None: | |
for table in self.TABLE_SCHEMAS: | ||
cursor.execute(self.TABLE_SCHEMAS[table]) | ||
|
||
# Ensure the UNKNOWN metric exists | ||
self.ensure_unknown_metric(cursor) | ||
# add indexes | ||
for index in self.INDEXES: | ||
cursor.execute(self.INDEXES[index]) | ||
|
@@ -619,6 +622,7 @@ def populate_metrics(self): | |
# Insert a row without specifying cve_metrics_id | ||
insert_metrics = self.INSERT_QUERIES["insert_metrics"] | ||
data = [ | ||
(UNKNOWN_METRIC_ID, "UNKNOWN"), | ||
(EPSS_METRIC_ID, "EPSS"), | ||
(CVSS_2_METRIC_ID, "CVSS-2"), | ||
(CVSS_3_METRIC_ID, "CVSS-3"), | ||
|
@@ -632,15 +636,15 @@ def populate_metrics(self): | |
def metric_finder(self, cursor, cve): | ||
""" | ||
SQL query to retrieve the metrics_name based on the metrics_id | ||
currently cve["CVSS_version"] return 2,3 based on there version and they are mapped accordingly to there metrics name in metrics table. | ||
currently cve["CVSS_version"] return 2,3 based on their version and they are mapped accordingly to their metrics name in metrics table. | ||
""" | ||
query = """ | ||
SELECT metrics_id FROM metrics | ||
WHERE metrics_id=? | ||
""" | ||
metric = None | ||
if cve["CVSS_version"] == "unknown": | ||
metric = "unknown" | ||
metric = 0 | ||
else: | ||
cursor.execute(query, [cve.get("CVSS_version")]) | ||
# Fetch all the results of the query and use 'map' to extract only the 'metrics_name' from the result | ||
|
@@ -1173,8 +1177,18 @@ def fetch_from_mirror(self, mirror, pubkey, ignore_signature, log_signature_erro | |
|
||
@contextlib.contextmanager | ||
def with_cursor(self): | ||
"""Context manager for database cursor.""" | ||
cursor = self.db_open_and_get_cursor() | ||
try: | ||
yield cursor | ||
finally: | ||
self.db_close() | ||
|
||
def ensure_unknown_metric(self, cursor): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not moving this to |
||
"""Ensure that the UNKNOWN metric exists in the metrics table.""" | ||
insert_metrics = self.INSERT_QUERIES["insert_metrics"] | ||
try: | ||
cursor.execute(insert_metrics, (UNKNOWN_METRIC_ID, "UNKNOWN")) | ||
except sqlite3.IntegrityError: | ||
# The metric already exists, no action needed | ||
pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use
UNKNOWN_METRIC_ID
just to avoid magic numbers it increases the readability.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1