|
| 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 pytest |
| 11 | + |
| 12 | +from vulnerabilities.models import AdvisoryV2 |
| 13 | +from vulnerabilities.pipelines.v2_improvers.relate_severities import RelateSeveritiesPipeline |
| 14 | +from vulnerabilities.severity_systems import EPSS |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.django_db |
| 18 | +def test_relate_severities_by_advisory_id(): |
| 19 | + base = AdvisoryV2.objects.create( |
| 20 | + advisory_id="CVE-2024-0001", |
| 21 | + datasource_id="nvd", |
| 22 | + avid="nvd/CVE-2024-0001", |
| 23 | + unique_content_id="ab1", |
| 24 | + url="https://example.com/advisory/CVE-2024-0001", |
| 25 | + date_collected="2024-01-01", |
| 26 | + ) |
| 27 | + |
| 28 | + severity_advisory = AdvisoryV2.objects.create( |
| 29 | + advisory_id="CVE-2024-0001", |
| 30 | + datasource_id="epss_importer_v2", |
| 31 | + avid="epss/CVE-2024-0001", |
| 32 | + unique_content_id="ab2", |
| 33 | + url="https://example.com/epss/CVE-2024-0001", |
| 34 | + date_collected="2024-01-02", |
| 35 | + ) |
| 36 | + severity_advisory.severities.create( |
| 37 | + scoring_system=EPSS.identifier, |
| 38 | + value="0.5", |
| 39 | + ) |
| 40 | + |
| 41 | + pipeline = RelateSeveritiesPipeline() |
| 42 | + pipeline.relate_severities() |
| 43 | + |
| 44 | + assert base.related_advisory_severities.filter(id=severity_advisory.id).exists() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.django_db |
| 48 | +def test_relate_severities_via_alias(): |
| 49 | + base = AdvisoryV2.objects.create( |
| 50 | + advisory_id="CVE-2024-0002", |
| 51 | + datasource_id="nvd", |
| 52 | + avid="nvd/CVE-2024-0002", |
| 53 | + unique_content_id="ab3", |
| 54 | + url="https://example.com/advisory/CVE-2024-0002", |
| 55 | + date_collected="2024-01-01", |
| 56 | + ) |
| 57 | + |
| 58 | + base.aliases.create(alias="CVE-2024-ALIAS") |
| 59 | + |
| 60 | + severity_advisory = AdvisoryV2.objects.create( |
| 61 | + advisory_id="CVE-2024-ALIAS", |
| 62 | + datasource_id="epss_importer_v2", |
| 63 | + avid="epss/CVE-2024-ALIAS", |
| 64 | + unique_content_id="ab4", |
| 65 | + url="https://example.com/epss/CVE-2024-ALIAS", |
| 66 | + date_collected="2024-01-02", |
| 67 | + ) |
| 68 | + severity_advisory.severities.create( |
| 69 | + scoring_system=EPSS.identifier, |
| 70 | + value="0.8", |
| 71 | + ) |
| 72 | + |
| 73 | + pipeline = RelateSeveritiesPipeline() |
| 74 | + pipeline.relate_severities() |
| 75 | + |
| 76 | + assert base.related_advisory_severities.filter(id=severity_advisory.id).exists() |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.django_db |
| 80 | +def test_no_self_relation_created(): |
| 81 | + advisory = AdvisoryV2.objects.create( |
| 82 | + advisory_id="CVE-2024-0003", |
| 83 | + datasource_id="epss_importer_v2", |
| 84 | + unique_content_id="ab5", |
| 85 | + url="https://example.com/advisory/CVE-2024-0003", |
| 86 | + date_collected="2024-01-03", |
| 87 | + avid="epss/CVE-2024-0003", |
| 88 | + ) |
| 89 | + advisory.severities.create( |
| 90 | + scoring_system=EPSS.identifier, |
| 91 | + value="0.2", |
| 92 | + ) |
| 93 | + |
| 94 | + pipeline = RelateSeveritiesPipeline() |
| 95 | + pipeline.relate_severities() |
| 96 | + |
| 97 | + assert not advisory.related_advisory_severities.filter(id=advisory.id).exists() |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.django_db |
| 101 | +def test_unsupported_severity_system_is_ignored(): |
| 102 | + base = AdvisoryV2.objects.create( |
| 103 | + advisory_id="CVE-2024-0004", |
| 104 | + datasource_id="nvd", |
| 105 | + unique_content_id="ab6", |
| 106 | + url="https://example.com/advisory/CVE-2024-0004", |
| 107 | + date_collected="2024-01-01", |
| 108 | + avid="nvd/CVE-2024-0004", |
| 109 | + ) |
| 110 | + |
| 111 | + severity_advisory = AdvisoryV2.objects.create( |
| 112 | + advisory_id="CVE-2024-0004", |
| 113 | + datasource_id="epss_importer_v2", |
| 114 | + unique_content_id="ab7", |
| 115 | + url="https://example.com/epss/CVE-2024-0004", |
| 116 | + date_collected="2024-01-02", |
| 117 | + avid="epss/CVE-2024-0004", |
| 118 | + ) |
| 119 | + severity_advisory.severities.create( |
| 120 | + scoring_system="UNKNOWN_SYSTEM", |
| 121 | + value="9.9", |
| 122 | + ) |
| 123 | + |
| 124 | + pipeline = RelateSeveritiesPipeline() |
| 125 | + pipeline.relate_severities() |
| 126 | + |
| 127 | + assert base.related_advisory_severities.count() == 0 |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.django_db |
| 131 | +def test_pipeline_is_idempotent(): |
| 132 | + base = AdvisoryV2.objects.create( |
| 133 | + advisory_id="CVE-2024-0005", |
| 134 | + datasource_id="nvd", |
| 135 | + unique_content_id="ab8", |
| 136 | + url="https://example.com/advisory/CVE-2024-0005", |
| 137 | + date_collected="2024-01-01", |
| 138 | + avid="nvd/CVE-2024-0005", |
| 139 | + ) |
| 140 | + |
| 141 | + severity = AdvisoryV2.objects.create( |
| 142 | + advisory_id="CVE-2024-0005", |
| 143 | + datasource_id="epss_importer_v2", |
| 144 | + unique_content_id="ab9", |
| 145 | + url="https://example.com/epss/CVE-2024-0005", |
| 146 | + date_collected="2024-01-02", |
| 147 | + avid="epss/CVE-2024-0005", |
| 148 | + ) |
| 149 | + severity.severities.create( |
| 150 | + scoring_system=EPSS.identifier, |
| 151 | + value="0.9", |
| 152 | + ) |
| 153 | + |
| 154 | + pipeline = RelateSeveritiesPipeline() |
| 155 | + |
| 156 | + pipeline.relate_severities() |
| 157 | + pipeline.relate_severities() |
| 158 | + |
| 159 | + assert base.related_advisory_severities.count() == 1 |
0 commit comments