Skip to content

Commit

Permalink
added tests, modified cve format validation and fixed analyzer config
Browse files Browse the repository at this point in the history
  • Loading branch information
spoiicy authored and spoiicy committed Nov 10, 2024
1 parent 7c83986 commit 992e801
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Migration(migrations.Migration):
atomic = False
dependencies = [
("api_app", "0063_singleton_and_elastic_report"),
("analyzers_manager", "0127_analyzer_config_dshield"),
("analyzers_manager", "0129_analyzer_config_phishing_extractor"),
]

operations = [migrations.RunPython(migrate, reverse_migrate)]
10 changes: 6 additions & 4 deletions api_app/analyzers_manager/observable_analyzers/nvd_cve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

import requests
from django.conf import settings

from api_app.analyzers_manager.classes import AnalyzerRunException, ObservableAnalyzer
from tests.mock_utils import MockUpResponse, if_mock_connections, patch
Expand All @@ -21,17 +22,18 @@ def run(self):
headers.update({"apiKey": self._nvd_api_key})

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):
# Validate if CVE format is correct E.g CVE-2014-1234 or cve-2022-1234567
if not settings.STAGE_CI and not re.match(
self.cve_pattern, self.observable_name, flags=re.IGNORECASE
):
raise ValueError(f"Invalid CVE format: {self.observable_name}")

params = {"cveId": self.observable_name}
params = {"cveId": self.observable_name.upper()}
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)

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.test import TestCase

from api_app.analyzers_manager.classes import AnalyzerRunException
from api_app.analyzers_manager.models import AnalyzerConfig
from api_app.analyzers_manager.observable_analyzers.nvd_cve import NVDDetails


class NVDCVETestCase(TestCase):
config = AnalyzerConfig.objects.get(python_module=NVDDetails.python_module)

def test_valid_cve_format(self):
"""Test that a valid CVE format passes without raising an exception"""

analyzer = NVDDetails(self.config)
analyzer.observable_name = "cve-2024-51181" # Valid format

try:
analyzer.run()
except AnalyzerRunException:
self.fail("AnalyzerRunException raised with valid CVE format")

def test_invalid_cve_format(self):
"""Test that an invalid CVE format raises an AnalyzerRunException"""
analyzer = NVDDetails(self.config)
analyzer.observable_name = "2024-51181" # Invalid format

with self.assertRaises(AnalyzerRunException):
analyzer.run()

0 comments on commit 992e801

Please sign in to comment.