@@ -218,6 +218,12 @@ class CVEDB:
218218 VALUES (?, ?)
219219 """ ,
220220 }
221+ METRICS = [
222+ (UNKNOWN_METRIC_ID , "UNKNOWN" ),
223+ (EPSS_METRIC_ID , "EPSS" ),
224+ (CVSS_2_METRIC_ID , "CVSS-2" ),
225+ (CVSS_3_METRIC_ID , "CVSS-3" ),
226+ ]
221227
222228 def __init__ (
223229 self ,
@@ -315,7 +321,8 @@ def refresh_cache_and_update_db(self) -> None:
315321 if not self .latest_schema ("metrics" , self .TABLE_SCHEMAS ["metrics" ], cursor ):
316322 self .LOGGER .info ("Updating metrics data." )
317323 self .populate_metrics ()
318- self .connection .commit ()
324+ if self .connection is not None :
325+ self .connection .commit ()
319326 self .db_close ()
320327
321328 def get_cvelist_if_stale (self ) -> None :
@@ -341,6 +348,7 @@ def get_cvelist_if_stale(self) -> None:
341348 or not self .latest_schema (
342349 "cve_exploited" , self .TABLE_SCHEMAS ["cve_exploited" ]
343350 )
351+ or not self .latest_schema ("metrics" , self .TABLE_SCHEMAS ["metrics" ])
344352 ):
345353 self .refresh_cache_and_update_db ()
346354 self .time_of_last_update = datetime .datetime .today ()
@@ -368,7 +376,6 @@ def latest_schema(
368376
369377 # getting schema from command
370378 lines = table_schema .split ("(" )[1 ].split ("," )
371-
372379 table_schema = [x .split ("\n " )[1 ].strip ().split (" " )[0 ] for x in lines ]
373380 table_schema .pop ()
374381
@@ -378,13 +385,16 @@ def latest_schema(
378385 if table_schema == current_schema :
379386 schema_latest = True
380387
381- # Check for metrics table schema
388+ # Check for metrics table data integrity
382389 if table_name == "metrics" :
383- result = cursor .execute (
384- "SELECT * FROM metrics WHERE metrics_id=?" , (UNKNOWN_METRIC_ID ,)
385- )
386- if not result .fetchone ():
387- schema_latest = False
390+ for metric_id , metric_name in self .METRICS :
391+ result = cursor .execute (
392+ "SELECT * FROM metrics WHERE metrics_id=? AND metrics_name=?" ,
393+ (metric_id , metric_name ),
394+ )
395+ if not result .fetchone ():
396+ schema_latest = False
397+ break # Early exit if any metric is missing
388398
389399 return schema_latest
390400
@@ -640,18 +650,12 @@ def populate_affected(self, affected_data, cursor, data_source):
640650 def populate_metrics (self ):
641651 """Adding data to metric table."""
642652 cursor = self .db_open_and_get_cursor ()
643- # Insert a row without specifying cve_metrics_id
644653 insert_metrics = self .INSERT_QUERIES ["insert_metrics" ]
645- data = [
646- (UNKNOWN_METRIC_ID , "UNKNOWN" ),
647- (EPSS_METRIC_ID , "EPSS" ),
648- (CVSS_2_METRIC_ID , "CVSS-2" ),
649- (CVSS_3_METRIC_ID , "CVSS-3" ),
650- ]
651- # Execute the insert query for each row
652- for row in data :
654+ # Use the METRICS constant to populate the table
655+ for row in self .METRICS :
653656 cursor .execute (insert_metrics , row )
654- self .connection .commit ()
657+ if self .connection is not None :
658+ self .connection .commit ()
655659 self .db_close ()
656660
657661 def metric_finder (self , cursor , cve ):
@@ -869,23 +873,26 @@ def create_exploit_db(self):
869873 create_exploit_table = self .TABLE_SCHEMAS ["cve_exploited" ]
870874 cursor = self .db_open_and_get_cursor ()
871875 cursor .execute (create_exploit_table )
872- self .connection .commit ()
876+ if self .connection is not None :
877+ self .connection .commit ()
873878 self .db_close ()
874879
875880 def populate_exploit_db (self , exploits ):
876881 """Add exploits to the exploits database table."""
877882 insert_exploit = self .INSERT_QUERIES ["insert_exploit" ]
878883 cursor = self .db_open_and_get_cursor ()
879884 cursor .executemany (insert_exploit , exploits )
880- self .connection .commit ()
885+ if self .connection is not None :
886+ self .connection .commit ()
881887 self .db_close ()
882888
883889 def store_epss_data (self , epss_data ):
884890 """Insert Exploit Prediction Scoring System (EPSS) data into database."""
885891 insert_cve_metrics = self .INSERT_QUERIES ["insert_cve_metrics" ]
886892 cursor = self .db_open_and_get_cursor ()
887893 cursor .executemany (insert_cve_metrics , epss_data )
888- self .connection .commit ()
894+ if self .connection is not None :
895+ self .connection .commit ()
889896 self .db_close ()
890897
891898 def dict_factory (self , cursor , row ):
@@ -1157,7 +1164,8 @@ def json_to_db_wrapper(self, path, pubkey, ignore_signature, log_signature_error
11571164 shutil .rmtree (temp_gnupg_home )
11581165 return ERROR_CODES [SigningError ]
11591166 self .json_to_db (cursor , dir , json .loads (data ))
1160- self .connection .commit ()
1167+ if self .connection is not None :
1168+ self .connection .commit ()
11611169
11621170 if is_signed and not ignore_signature and temp_gnupg_home .exists ():
11631171 shutil .rmtree (temp_gnupg_home )
0 commit comments