1616BASE_URL = "https://cve.circl.lu/api"
1717NVD_BASE_URL = "https://services.nvd.nist.gov/rest/json/cves/2.0"
1818
19+ REQUEST_TIMEOUT = (10 , 30 )
20+ session = requests .Session ()
21+ session .headers .update (
22+ {
23+ "User-Agent" : "cvecli (https://github.com/DebaA17/CVE-scanner-cli)" ,
24+ "Accept" : "application/json" ,
25+ }
26+ )
27+
1928def format_date (date_str ):
2029 if not date_str :
2130 return "N/A"
@@ -26,9 +35,22 @@ def format_date(date_str):
2635
2736def search_cve_by_id (cve_id ):
2837 url = f"{ BASE_URL } /cve/{ cve_id } "
29- resp = requests .get (url )
38+ try :
39+ resp = session .get (url , timeout = REQUEST_TIMEOUT )
40+ except requests .RequestException as exc :
41+ console .print (
42+ f"[bold red]Network error while fetching CVE data.[/]\n { exc } \n "
43+ "[dim]If you installed via Snap, ensure the 'network' plug is connected: "
44+ "'snap connections cvecli'.[/]"
45+ )
46+ return
47+
3048 if resp .status_code == 200 :
31- data = resp .json ()
49+ try :
50+ data = resp .json ()
51+ except ValueError :
52+ console .print ("[bold red]Error: API returned invalid JSON.[/]" )
53+ return
3254 cve_id = data .get ('cveMetadata' , {}).get ('cveId' , data .get ('id' ))
3355 published = format_date (data .get ('cveMetadata' , {}).get ('datePublished' , data .get ('Published' )))
3456 modified = format_date (data .get ('cveMetadata' , {}).get ('dateUpdated' , data .get ('Modified' )))
@@ -68,14 +90,31 @@ def search_cve_by_id(cve_id):
6890 for ref in references :
6991 panel_text .append (f" { ref } \n " , style = "blue underline" )
7092 console .print (Panel (panel_text , title = f"{ cve_id } " , expand = False , border_style = "red" ))
93+ elif resp .status_code == 404 :
94+ console .print (f"[bold red]Error: CVE { cve_id } not found.[/]" )
7195 else :
72- console .print (f"[bold red]Error: CVE { cve_id } not found." )
96+ console .print (
97+ f"[bold red]Error: Failed to fetch CVE { cve_id } (HTTP { resp .status_code } ).[/]"
98+ )
7399
74100def search_cve_by_keyword (keyword ):
75101 params = {"keywordSearch" : keyword , "resultsPerPage" : 5 }
76- resp = requests .get (NVD_BASE_URL , params = params )
102+ try :
103+ resp = session .get (NVD_BASE_URL , params = params , timeout = REQUEST_TIMEOUT )
104+ except requests .RequestException as exc :
105+ console .print (
106+ f"[bold red]Network error while searching NVD.[/]\n { exc } \n "
107+ "[dim]If you installed via Snap, ensure the 'network' plug is connected: "
108+ "'snap connections cvecli'.[/]"
109+ )
110+ return
111+
77112 if resp .status_code == 200 :
78- data = resp .json ()
113+ try :
114+ data = resp .json ()
115+ except ValueError :
116+ console .print ("[bold red]Error: NVD API returned invalid JSON.[/]" )
117+ return
79118 results = data .get ('vulnerabilities' , [])
80119 if not results :
81120 console .print (f"[bold red]No CVEs found for keyword: { keyword } " )
@@ -86,7 +125,9 @@ def search_cve_by_keyword(keyword):
86125 if cve_id :
87126 search_cve_by_id (cve_id )
88127 else :
89- console .print (f"[bold red]Error: Could not fetch CVEs for keyword: { keyword } " )
128+ console .print (
129+ f"[bold red]Error: Could not fetch CVEs for keyword '{ keyword } ' (HTTP { resp .status_code } ).[/]"
130+ )
90131
91132def interactive_menu ():
92133 console .print ("[bold green]\n ==============================\n [bold yellow]Made by DEBASIS - CVE Checker CLI\n ==============================\n " , style = "bold green" )
0 commit comments