|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import datetime |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import pytz |
| 14 | +from django.test import TestCase |
| 15 | + |
| 16 | +from vulnerabilities.models import Advisory |
| 17 | +from vulnerabilities.models import Alias |
| 18 | +from vulnerabilities.models import Vulnerability |
| 19 | +from vulnerabilities.pipelines.populate_vulnerability_summary_pipeline import ( |
| 20 | + PopulateVulnerabilitySummariesPipeline, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +class PopulateVulnerabilitySummariesPipelineTest(TestCase): |
| 25 | + def setUp(self): |
| 26 | + self.data = Path(__file__).parent.parent / "test_data" |
| 27 | + |
| 28 | + def test_populate_missing_summaries_from_nvd(self): |
| 29 | + """ |
| 30 | + Test that vulnerabilities without summaries get them from NVD advisories. |
| 31 | + """ |
| 32 | + |
| 33 | + # Create a vulnerability without a summary |
| 34 | + vulnerability = Vulnerability.objects.create( |
| 35 | + vulnerability_id="VCID-1234", |
| 36 | + summary="", |
| 37 | + ) |
| 38 | + alias = Alias.objects.create(alias="CVE-2024-1234", vulnerability=vulnerability) |
| 39 | + |
| 40 | + # Create an NVD advisory with a summary |
| 41 | + adv = Advisory.objects.create( |
| 42 | + summary="Test vulnerability summary", |
| 43 | + created_by="nvd_importer", |
| 44 | + date_collected=datetime.datetime(2024, 1, 1, tzinfo=pytz.UTC), |
| 45 | + unique_content_id="Test", |
| 46 | + ) |
| 47 | + adv.aliases.add(alias) |
| 48 | + |
| 49 | + # Run the pipeline |
| 50 | + pipeline = PopulateVulnerabilitySummariesPipeline() |
| 51 | + pipeline.populate_missing_summaries() |
| 52 | + |
| 53 | + # Check that the vulnerability now has a summary |
| 54 | + vulnerability.refresh_from_db() |
| 55 | + self.assertEqual(vulnerability.summary, "Test vulnerability summary") |
| 56 | + |
| 57 | + def test_no_matching_advisory(self): |
| 58 | + """ |
| 59 | + Test handling of vulnerabilities that have no matching NVD advisory. |
| 60 | + """ |
| 61 | + # Create a vulnerability without a summary |
| 62 | + vulnerability = Vulnerability.objects.create( |
| 63 | + vulnerability_id="VCID-1234", |
| 64 | + summary="", |
| 65 | + ) |
| 66 | + Alias.objects.create(alias="CVE-2024-1234", vulnerability=vulnerability) |
| 67 | + |
| 68 | + # Run the pipeline |
| 69 | + pipeline = PopulateVulnerabilitySummariesPipeline() |
| 70 | + pipeline.populate_missing_summaries() |
| 71 | + |
| 72 | + # Check that the vulnerability still has no summary |
| 73 | + vulnerability.refresh_from_db() |
| 74 | + self.assertEqual(vulnerability.summary, "") |
| 75 | + |
| 76 | + def test_vulnerability_without_alias(self): |
| 77 | + """ |
| 78 | + Test handling of vulnerabilities that have no aliases. |
| 79 | + """ |
| 80 | + |
| 81 | + # Create a vulnerability without a summary or alias |
| 82 | + vulnerability = Vulnerability.objects.create( |
| 83 | + vulnerability_id="VCID-1234", |
| 84 | + summary="", |
| 85 | + ) |
| 86 | + |
| 87 | + # Run the pipeline |
| 88 | + pipeline = PopulateVulnerabilitySummariesPipeline() |
| 89 | + pipeline.populate_missing_summaries() |
| 90 | + |
| 91 | + # Check that the vulnerability still has no summary |
| 92 | + vulnerability.refresh_from_db() |
| 93 | + self.assertEqual(vulnerability.summary, "") |
| 94 | + |
| 95 | + def test_non_nvd_advisory_ignored(self): |
| 96 | + """ |
| 97 | + Test that advisories from sources other than NVD are ignored. |
| 98 | + """ |
| 99 | + |
| 100 | + # Create a vulnerability without a summary |
| 101 | + vulnerability = Vulnerability.objects.create( |
| 102 | + vulnerability_id="VCID-1234", |
| 103 | + summary="", |
| 104 | + ) |
| 105 | + alias = Alias.objects.create(alias="CVE-2024-1234", vulnerability=vulnerability) |
| 106 | + |
| 107 | + # Create a non-NVD advisory with a summary |
| 108 | + adv = Advisory.objects.create( |
| 109 | + summary="Test vulnerability summary", |
| 110 | + created_by="other_importer", |
| 111 | + date_collected=datetime.datetime(2024, 1, 1, tzinfo=pytz.UTC), |
| 112 | + unique_content_id="Test", |
| 113 | + ) |
| 114 | + |
| 115 | + adv.aliases.add(alias) |
| 116 | + |
| 117 | + # Run the pipeline |
| 118 | + pipeline = PopulateVulnerabilitySummariesPipeline() |
| 119 | + pipeline.populate_missing_summaries() |
| 120 | + |
| 121 | + # Check that the vulnerability still has no summary |
| 122 | + vulnerability.refresh_from_db() |
| 123 | + self.assertEqual(vulnerability.summary, "") |
| 124 | + |
| 125 | + def test_multiple_matching_advisories(self): |
| 126 | + """ |
| 127 | + Test that the most recent matching advisory is used when there are multiple. |
| 128 | + """ |
| 129 | + vulnerability = Vulnerability.objects.create( |
| 130 | + vulnerability_id="VCID-1234", |
| 131 | + summary="", |
| 132 | + ) |
| 133 | + alias = Alias.objects.create(alias="CVE-2024-1234", vulnerability=vulnerability) |
| 134 | + |
| 135 | + # Create two NVD advisories with the same alias |
| 136 | + adv1 = Advisory.objects.create( |
| 137 | + summary="First matching advisory", |
| 138 | + created_by="nvd_importer", |
| 139 | + date_collected=datetime.datetime(2024, 1, 1, tzinfo=pytz.UTC), |
| 140 | + unique_content_id="Test", |
| 141 | + ) |
| 142 | + |
| 143 | + adv1.aliases.add(alias) |
| 144 | + |
| 145 | + adv2 = Advisory.objects.create( |
| 146 | + summary="Second matching advisory", |
| 147 | + created_by="nvd_importer", |
| 148 | + date_collected=datetime.datetime(2024, 1, 2, tzinfo=pytz.UTC), |
| 149 | + unique_content_id="Test-1", |
| 150 | + ) |
| 151 | + |
| 152 | + adv2.aliases.add(alias) |
| 153 | + |
| 154 | + # Run the pipeline |
| 155 | + pipeline = PopulateVulnerabilitySummariesPipeline() |
| 156 | + pipeline.populate_missing_summaries() |
| 157 | + |
| 158 | + # Check that the vulnerability now has the most recent summary |
| 159 | + vulnerability.refresh_from_db() |
| 160 | + self.assertEqual(vulnerability.summary, "Second matching advisory") |
0 commit comments