|
| 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 json |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | +from types import SimpleNamespace |
| 14 | + |
| 15 | +import pytest |
| 16 | +from packageurl import PackageURL |
| 17 | +from univers.version_constraint import VersionConstraint |
| 18 | +from univers.version_range import NpmVersionRange |
| 19 | +from univers.versions import SemverVersion |
| 20 | + |
| 21 | +from vulnerabilities.importer import AffectedPackageV2 |
| 22 | +from vulnerabilities.pipelines.v2_importers.npm_live_importer import NpmLiveImporterPipeline |
| 23 | + |
| 24 | +TEST_DATA = Path(__file__).parent.parent.parent / "test_data" / "npm" |
| 25 | + |
| 26 | + |
| 27 | +def test_package_first_mode_valid_npm_package(tmp_path): |
| 28 | + vuln_dir = tmp_path / "vuln" / "npm" |
| 29 | + vuln_dir.mkdir(parents=True) |
| 30 | + |
| 31 | + npm_sample_file = os.path.join(TEST_DATA, "npm_sample.json") |
| 32 | + with open(npm_sample_file) as f: |
| 33 | + sample_data = json.load(f) |
| 34 | + |
| 35 | + advisory_file = vuln_dir / "152.json" |
| 36 | + advisory_file.write_text(json.dumps(sample_data)) |
| 37 | + |
| 38 | + mock_vcs_response = SimpleNamespace(dest_dir=str(tmp_path), delete=lambda: None) |
| 39 | + |
| 40 | + purl = PackageURL(type="npm", name="npm", version="1.2.0") |
| 41 | + pipeline = NpmLiveImporterPipeline(purl=purl) |
| 42 | + pipeline.vcs_response = mock_vcs_response |
| 43 | + |
| 44 | + pipeline.get_purl_inputs() |
| 45 | + advisories = list(pipeline.collect_advisories()) |
| 46 | + |
| 47 | + assert len(advisories) == 1 |
| 48 | + assert advisories[0].aliases == ["CVE-2013-4116"] |
| 49 | + assert len(advisories[0].affected_packages) == 1 |
| 50 | + assert advisories[0].affected_packages[0].package.name == "npm" |
| 51 | + |
| 52 | + |
| 53 | +def test_package_first_mode_unaffected_version(tmp_path): |
| 54 | + vuln_dir = tmp_path / "vuln" / "npm" |
| 55 | + vuln_dir.mkdir(parents=True) |
| 56 | + |
| 57 | + npm_sample_file = os.path.join(TEST_DATA, "npm_sample.json") |
| 58 | + with open(npm_sample_file) as f: |
| 59 | + sample_data = json.load(f) |
| 60 | + |
| 61 | + advisory_file = vuln_dir / "152.json" |
| 62 | + advisory_file.write_text(json.dumps(sample_data)) |
| 63 | + |
| 64 | + mock_vcs_response = SimpleNamespace(dest_dir=str(tmp_path), delete=lambda: None) |
| 65 | + |
| 66 | + purl = PackageURL(type="npm", name="npm", version="1.4.0") |
| 67 | + pipeline = NpmLiveImporterPipeline(purl=purl) |
| 68 | + pipeline.vcs_response = mock_vcs_response |
| 69 | + |
| 70 | + pipeline.get_purl_inputs() |
| 71 | + advisories = list(pipeline.collect_advisories()) |
| 72 | + |
| 73 | + assert len(advisories) == 0 |
| 74 | + |
| 75 | + |
| 76 | +def test_package_first_mode_invalid_package_type(tmp_path): |
| 77 | + vuln_dir = tmp_path / "vuln" / "npm" |
| 78 | + vuln_dir.mkdir(parents=True) |
| 79 | + |
| 80 | + mock_vcs_response = SimpleNamespace(dest_dir=str(tmp_path), delete=lambda: None) |
| 81 | + |
| 82 | + purl = PackageURL(type="pypi", name="django", version="3.0.0") |
| 83 | + pipeline = NpmLiveImporterPipeline(purl=purl) |
| 84 | + pipeline.vcs_response = mock_vcs_response |
| 85 | + |
| 86 | + with pytest.raises(ValueError): |
| 87 | + pipeline.get_purl_inputs() |
| 88 | + |
| 89 | + |
| 90 | +def test_package_first_mode_package_not_found(tmp_path): |
| 91 | + vuln_dir = tmp_path / "vuln" / "npm" |
| 92 | + vuln_dir.mkdir(parents=True) |
| 93 | + |
| 94 | + npm_sample_file = os.path.join(TEST_DATA, "npm_sample.json") |
| 95 | + with open(npm_sample_file) as f: |
| 96 | + sample_data = json.load(f) |
| 97 | + |
| 98 | + sample_data["module_name"] = "some-other-package" |
| 99 | + |
| 100 | + advisory_file = vuln_dir / "152.json" |
| 101 | + advisory_file.write_text(json.dumps(sample_data)) |
| 102 | + |
| 103 | + mock_vcs_response = SimpleNamespace(dest_dir=str(tmp_path), delete=lambda: None) |
| 104 | + |
| 105 | + purl = PackageURL(type="npm", name="nonexistent-package", version="1.0.0") |
| 106 | + pipeline = NpmLiveImporterPipeline(purl=purl) |
| 107 | + pipeline.vcs_response = mock_vcs_response |
| 108 | + |
| 109 | + pipeline.get_purl_inputs() |
| 110 | + advisories = list(pipeline.collect_advisories()) |
| 111 | + |
| 112 | + assert len(advisories) == 0 |
| 113 | + |
| 114 | + |
| 115 | +def test_version_is_affected(): |
| 116 | + purl = PackageURL(type="npm", name="npm", version="1.2.0") |
| 117 | + pipeline = NpmLiveImporterPipeline(purl=purl) |
| 118 | + pipeline.get_purl_inputs() |
| 119 | + |
| 120 | + affected_package = AffectedPackageV2( |
| 121 | + package=PackageURL(type="npm", name="npm"), |
| 122 | + affected_version_range=NpmVersionRange( |
| 123 | + constraints=(VersionConstraint(comparator="<", version=SemverVersion(string="1.3.3")),) |
| 124 | + ), |
| 125 | + ) |
| 126 | + |
| 127 | + assert pipeline._version_is_affected(affected_package) == True |
| 128 | + |
| 129 | + pipeline.purl = PackageURL(type="npm", name="npm", version="1.4.0") |
| 130 | + assert pipeline._version_is_affected(affected_package) == False |
| 131 | + |
| 132 | + pipeline.purl = PackageURL(type="npm", name="npm") |
| 133 | + assert pipeline._version_is_affected(affected_package) == True |
0 commit comments