Skip to content

Commit

Permalink
added CVE format validation
Browse files Browse the repository at this point in the history
  • Loading branch information
spoiicy authored and spoiicy committed Nov 5, 2024
1 parent c7d88e7 commit c4e1a70
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion api_app/analyzers_manager/observable_analyzers/nvd_cve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import re

from api_app.analyzers_manager.classes import AnalyzerRunException, ObservableAnalyzer
from tests.mock_utils import MockUpResponse, if_mock_connections, patch
Expand All @@ -7,6 +8,7 @@
class NVDDetails(ObservableAnalyzer):
url: str = "https://services.nvd.nist.gov/rest/json/cves/2.0"
_nvd_api_key: str = None
cve_pattern = r'^CVE-\d{4}-\d{4,7}$'

@classmethod
def update(self) -> bool:
Expand All @@ -16,12 +18,22 @@ def run(self):
headers = {}
if self._nvd_api_key:
headers.update({"apiKey": self._nvd_api_key})
params = {"cveId": self.observable_name}

try:
#Validate if CVE format is correct E.g CVE-2014-1234 or CVE-2022-1234567
if not re.match(self.cve_pattern,self.observable_name):
raise ValueError(f"Invalid CVE format: {self.observable_name}")

params = {"cveId": self.observable_name}
response = requests.get(url=self.url, params=params, headers=headers)
response.raise_for_status()

except ValueError as e:
raise AnalyzerRunException(e)

except requests.RequestException as e:
raise AnalyzerRunException(e)


return response.json()

Expand Down

0 comments on commit c4e1a70

Please sign in to comment.