|
| 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 | +from packageurl import PackageURL |
| 12 | +from univers.version_range import GitHubVersionRange |
| 13 | + |
| 14 | +from vulnerabilities.importer import AdvisoryData |
| 15 | +from vulnerabilities.pipelines.v2_importers.mattermost_importer import MattermostImporterPipeline |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def sample_mattermost_data(): |
| 20 | + return [ |
| 21 | + { |
| 22 | + "issue_id": "MMSA-2024-001", |
| 23 | + "cve_id": "CVE-2024-1234", |
| 24 | + "details": "Test vulnerability in Mattermost Server", |
| 25 | + "platform": "Mattermost Server", |
| 26 | + "severity": "HIGH", |
| 27 | + "fix_versions": ["v9.0.1", "v8.1.5"], |
| 28 | + } |
| 29 | + ] |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def importer(monkeypatch, sample_mattermost_data): |
| 34 | + """ |
| 35 | + Create an importer with fetch_response mocked. |
| 36 | + """ |
| 37 | + |
| 38 | + def mock_fetch_response(url): |
| 39 | + class MockResponse: |
| 40 | + def json(self_inner): |
| 41 | + return sample_mattermost_data |
| 42 | + |
| 43 | + return MockResponse() |
| 44 | + |
| 45 | + monkeypatch.setattr( |
| 46 | + "vulnerabilities.pipelines.v2_importers.mattermost_importer.fetch_response", |
| 47 | + mock_fetch_response, |
| 48 | + ) |
| 49 | + |
| 50 | + return MattermostImporterPipeline() |
| 51 | + |
| 52 | + |
| 53 | +def test_advisories_count(importer): |
| 54 | + assert importer.advisories_count() == 1 |
| 55 | + |
| 56 | + |
| 57 | +def test_collect_advisories_happy_path(importer): |
| 58 | + advisories = list(importer.collect_advisories()) |
| 59 | + |
| 60 | + assert len(advisories) == 1 |
| 61 | + advisory = advisories[0] |
| 62 | + |
| 63 | + assert isinstance(advisory, AdvisoryData) |
| 64 | + assert advisory.advisory_id == "MMSA-2024-001" |
| 65 | + assert advisory.aliases == ["CVE-2024-1234"] |
| 66 | + assert "Test vulnerability" in advisory.summary |
| 67 | + |
| 68 | + assert advisory.affected_packages |
| 69 | + affected = advisory.affected_packages[0] |
| 70 | + |
| 71 | + assert affected.package == PackageURL( |
| 72 | + type="github", |
| 73 | + namespace="mattermost", |
| 74 | + name="mattermost-server", |
| 75 | + ) |
| 76 | + |
| 77 | + assert isinstance(affected.fixed_version_range, GitHubVersionRange) |
| 78 | + assert str(affected.fixed_version_range) == "vers:github/8.1.5|9.0.1" |
| 79 | + |
| 80 | + |
| 81 | +def test_skip_invalid_issue_id(monkeypatch): |
| 82 | + data = [ |
| 83 | + { |
| 84 | + "issue_id": "INVALID-001", |
| 85 | + "platform": "Mattermost Server", |
| 86 | + } |
| 87 | + ] |
| 88 | + |
| 89 | + def mock_fetch_response(url): |
| 90 | + class MockResponse: |
| 91 | + def json(self): |
| 92 | + return data |
| 93 | + |
| 94 | + return MockResponse() |
| 95 | + |
| 96 | + monkeypatch.setattr( |
| 97 | + "vulnerabilities.pipelines.v2_importers.mattermost_importer.fetch_response", |
| 98 | + mock_fetch_response, |
| 99 | + ) |
| 100 | + |
| 101 | + importer = MattermostImporterPipeline() |
| 102 | + advisories = list(importer.collect_advisories()) |
| 103 | + |
| 104 | + assert advisories == [] |
| 105 | + |
| 106 | + |
| 107 | +def test_unknown_platform(monkeypatch): |
| 108 | + data = [ |
| 109 | + { |
| 110 | + "issue_id": "MMSA-2024-002", |
| 111 | + "platform": "Unknown Product", |
| 112 | + "fix_versions": ["1.0.0"], |
| 113 | + } |
| 114 | + ] |
| 115 | + |
| 116 | + def mock_fetch_response(url): |
| 117 | + class MockResponse: |
| 118 | + def json(self): |
| 119 | + return data |
| 120 | + |
| 121 | + return MockResponse() |
| 122 | + |
| 123 | + monkeypatch.setattr( |
| 124 | + "vulnerabilities.pipelines.v2_importers.mattermost_importer.fetch_response", |
| 125 | + mock_fetch_response, |
| 126 | + ) |
| 127 | + |
| 128 | + importer = MattermostImporterPipeline() |
| 129 | + advisories = list(importer.collect_advisories()) |
| 130 | + |
| 131 | + assert len(advisories) == 1 |
| 132 | + assert advisories[0].affected_packages == [] |
| 133 | + |
| 134 | + |
| 135 | +def test_fixed_version_string_normalization(monkeypatch): |
| 136 | + data = [ |
| 137 | + { |
| 138 | + "issue_id": "MMSA-2024-003", |
| 139 | + "platform": "Mattermost Desktop App", |
| 140 | + "fix_versions": "v2.0.0", |
| 141 | + } |
| 142 | + ] |
| 143 | + |
| 144 | + def mock_fetch_response(url): |
| 145 | + class MockResponse: |
| 146 | + def json(self): |
| 147 | + return data |
| 148 | + |
| 149 | + return MockResponse() |
| 150 | + |
| 151 | + monkeypatch.setattr( |
| 152 | + "vulnerabilities.pipelines.v2_importers.mattermost_importer.fetch_response", |
| 153 | + mock_fetch_response, |
| 154 | + ) |
| 155 | + |
| 156 | + importer = MattermostImporterPipeline() |
| 157 | + advisories = list(importer.collect_advisories()) |
| 158 | + |
| 159 | + affected = advisories[0].affected_packages[0] |
| 160 | + assert "2.0.0" in str(affected.fixed_version_range) |
| 161 | + |
| 162 | + |
| 163 | +def test_bad_version_does_not_crash(monkeypatch): |
| 164 | + data = [ |
| 165 | + { |
| 166 | + "issue_id": "MMSA-2024-004", |
| 167 | + "platform": "Mattermost Server", |
| 168 | + "fix_versions": ["not-a-version"], |
| 169 | + } |
| 170 | + ] |
| 171 | + |
| 172 | + def mock_fetch_response(url): |
| 173 | + class MockResponse: |
| 174 | + def json(self): |
| 175 | + return data |
| 176 | + |
| 177 | + return MockResponse() |
| 178 | + |
| 179 | + monkeypatch.setattr( |
| 180 | + "vulnerabilities.pipelines.v2_importers.mattermost_importer.fetch_response", |
| 181 | + mock_fetch_response, |
| 182 | + ) |
| 183 | + |
| 184 | + importer = MattermostImporterPipeline() |
| 185 | + advisories = list(importer.collect_advisories()) |
| 186 | + |
| 187 | + # Advisory should still be yielded, but without affected packages |
| 188 | + assert len(advisories) == 1 |
| 189 | + assert advisories[0].affected_packages == [] |
| 190 | + |
| 191 | + |
| 192 | +def test_fetch_is_cached(monkeypatch): |
| 193 | + call_count = {"count": 0} |
| 194 | + |
| 195 | + def mock_fetch_response(url): |
| 196 | + call_count["count"] += 1 |
| 197 | + |
| 198 | + class MockResponse: |
| 199 | + def json(self): |
| 200 | + return [] |
| 201 | + |
| 202 | + return MockResponse() |
| 203 | + |
| 204 | + monkeypatch.setattr( |
| 205 | + "vulnerabilities.pipelines.v2_importers.mattermost_importer.fetch_response", |
| 206 | + mock_fetch_response, |
| 207 | + ) |
| 208 | + |
| 209 | + importer = MattermostImporterPipeline() |
| 210 | + importer.advisories_count() |
| 211 | + importer.collect_advisories() |
| 212 | + |
| 213 | + assert call_count["count"] == 1 |
0 commit comments