|
| 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 shutil |
| 11 | +from pathlib import Path |
| 12 | +from unittest.mock import MagicMock |
| 13 | +from unittest.mock import patch |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from vulnerabilities.importer import AdvisoryData |
| 18 | +from vulnerabilities.pipelines.v2_importers.elixir_security_importer import ( |
| 19 | + ElixirSecurityImporterPipeline, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def mock_vcs_response(tmp_path): |
| 25 | + repo_dir = tmp_path / "repo" |
| 26 | + repo_dir.mkdir() |
| 27 | + packages_dir = repo_dir / "packages" / "some_package" |
| 28 | + packages_dir.mkdir(parents=True) |
| 29 | + |
| 30 | + advisory_file = packages_dir / "CVE-2022-9999.yml" |
| 31 | + advisory_file.write_text( |
| 32 | + """ |
| 33 | + cve: "2022-9999" |
| 34 | + package: "plug" |
| 35 | + description: "Cross-site scripting vulnerability in plug < 1.11.1" |
| 36 | + patched_versions: |
| 37 | + - ">= 1.11.1" |
| 38 | + unaffected_versions: |
| 39 | + - "< 1.0.0" |
| 40 | + disclosure_date: "2022-12-01" |
| 41 | + link: "https://github.com/plug/plug/security/advisories/GHSA-xxxx-yyyy" |
| 42 | + """ |
| 43 | + ) |
| 44 | + |
| 45 | + mock = MagicMock() |
| 46 | + mock.dest_dir = str(repo_dir) |
| 47 | + mock.delete = MagicMock() |
| 48 | + return mock |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def mock_fetch_via_vcs(mock_vcs_response): |
| 53 | + with patch( |
| 54 | + "vulnerabilities.pipelines.v2_importers.elixir_security_importer.fetch_via_vcs" |
| 55 | + ) as mock: |
| 56 | + mock.return_value = mock_vcs_response |
| 57 | + yield mock |
| 58 | + |
| 59 | + |
| 60 | +def test_advisories_count(mock_fetch_via_vcs, mock_vcs_response): |
| 61 | + importer = ElixirSecurityImporterPipeline() |
| 62 | + importer.clone() |
| 63 | + count = importer.advisories_count() |
| 64 | + assert count == 1 |
| 65 | + |
| 66 | + |
| 67 | +def test_collect_advisories(mock_fetch_via_vcs, mock_vcs_response): |
| 68 | + importer = ElixirSecurityImporterPipeline() |
| 69 | + importer.clone() |
| 70 | + advisories = list(importer.collect_advisories()) |
| 71 | + |
| 72 | + assert len(advisories) == 1 |
| 73 | + |
| 74 | + advisory: AdvisoryData = advisories[0] |
| 75 | + assert advisory.advisory_id == "CVE-2022-9999" |
| 76 | + assert advisory.summary.startswith("Cross-site scripting vulnerability") |
| 77 | + assert advisory.affected_packages[0].package.name == "plug" |
| 78 | + assert advisory.affected_packages[0].package.type == "hex" |
| 79 | + assert ( |
| 80 | + advisory.references_v2[0].url |
| 81 | + == "https://github.com/plug/plug/security/advisories/GHSA-xxxx-yyyy" |
| 82 | + ) |
| 83 | + assert advisory.date_published.isoformat().startswith("2022-12-01") |
| 84 | + |
| 85 | + |
| 86 | +def test_collect_advisories_skips_invalid_cve(mock_fetch_via_vcs, tmp_path): |
| 87 | + repo_dir = tmp_path / "repo" |
| 88 | + packages_dir = repo_dir / "packages" |
| 89 | + |
| 90 | + if packages_dir.exists(): |
| 91 | + shutil.rmtree(packages_dir) |
| 92 | + packages_dir.mkdir(parents=True, exist_ok=True) |
| 93 | + |
| 94 | + advisory_file = packages_dir / "bad_advisory.yml" |
| 95 | + advisory_file.write_text("cve: BAD-ID\npackage: x\n") |
| 96 | + |
| 97 | + mock_response = MagicMock() |
| 98 | + mock_response.dest_dir = str(repo_dir) |
| 99 | + mock_response.delete = MagicMock() |
| 100 | + |
| 101 | + with patch( |
| 102 | + "vulnerabilities.pipelines.v2_importers.elixir_security_importer.fetch_via_vcs" |
| 103 | + ) as mock: |
| 104 | + mock.return_value = mock_response |
| 105 | + importer = ElixirSecurityImporterPipeline() |
| 106 | + importer.clone() |
| 107 | + advisories = list(importer.collect_advisories()) |
| 108 | + assert len(advisories) == 0 # Confirm it skipped the invalid CVE |
0 commit comments