|
| 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 os |
| 11 | +from typing import List |
| 12 | + |
| 13 | +import pytest |
| 14 | +from packageurl import PackageURL |
| 15 | + |
| 16 | +from vulntotal.datasources.vulnerablecode_local import LocalVulnerableCodeDataSource |
| 17 | + |
| 18 | + |
| 19 | +class FakeResponse: |
| 20 | + def __init__(self, status_code=200, json_data=None, text=""): |
| 21 | + self.status_code = status_code |
| 22 | + self._json = json_data |
| 23 | + self.text = text or ("" if json_data is None else str(json_data)) |
| 24 | + |
| 25 | + def json(self): |
| 26 | + return self._json |
| 27 | + |
| 28 | + |
| 29 | +def make_v2_advisories_response( |
| 30 | + pkg_purl: str, advisory_id: str, aliases: List[str], fixes: List[str] |
| 31 | +): |
| 32 | + return { |
| 33 | + "packages": [ |
| 34 | + { |
| 35 | + "purl": pkg_purl, |
| 36 | + "affected_by_vulnerabilities": { |
| 37 | + "live_v2_importer_name/" |
| 38 | + + advisory_id: { |
| 39 | + "advisory_id": "live_v2_importer_name/" + advisory_id, |
| 40 | + "fixed_by_packages": fixes, |
| 41 | + "code_fixes": [], |
| 42 | + } |
| 43 | + }, |
| 44 | + } |
| 45 | + ], |
| 46 | + "advisories": { |
| 47 | + advisory_id: { |
| 48 | + "advisory_id": "live_v2_importer_name/" + advisory_id, |
| 49 | + "aliases": aliases, |
| 50 | + } |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +def test_local_vulnerablecode_v2_bulk_search_and_vendor_data(monkeypatch): |
| 56 | + monkeypatch.setenv("VCIO_HOST", "localhost") |
| 57 | + monkeypatch.setenv("VCIO_PORT", "1234") |
| 58 | + monkeypatch.setenv("ENABLE_LIVE_EVAL", "0") |
| 59 | + |
| 60 | + calls = [] |
| 61 | + |
| 62 | + def fake_post(url, json=None, **kwargs): |
| 63 | + calls.append((url, json)) |
| 64 | + if url.endswith("/api/v2/advisories-packages/bulk_search/"): |
| 65 | + return FakeResponse( |
| 66 | + 200, |
| 67 | + make_v2_advisories_response( |
| 68 | + pkg_purl="pkg:pypi/demo@1.2.3", |
| 69 | + advisory_id="ADV-123", |
| 70 | + aliases=["CVE-2024-0001", "GHSA-foo"], |
| 71 | + fixes=["pkg:pypi/demo@1.2.4", "pkg:pypi/demo@1.3.0"], |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + return FakeResponse(404, {"detail": "not found"}) |
| 76 | + |
| 77 | + monkeypatch.setattr("vulntotal.datasources.vulnerablecode_local.requests.post", fake_post) |
| 78 | + |
| 79 | + ds = LocalVulnerableCodeDataSource() |
| 80 | + purl = PackageURL.from_string("pkg:pypi/demo@1.2.3") |
| 81 | + |
| 82 | + results = list(ds.datasource_advisory(purl)) |
| 83 | + |
| 84 | + assert any( |
| 85 | + "/api/v2/advisories-packages/bulk_search/" in url for url, _ in calls |
| 86 | + ), "v2 advisories bulk_search should be called" |
| 87 | + |
| 88 | + assert not any( |
| 89 | + "/api/v2/live-evaluation/evaluate" in url for url, _ in calls |
| 90 | + ), "live evaluation should not be called when disabled" |
| 91 | + |
| 92 | + assert len(results) == 1 |
| 93 | + vd = results[0].to_dict() |
| 94 | + assert vd["purl"] == "pkg:pypi/demo" |
| 95 | + assert vd["aliases"] == ["CVE-2024-0001", "GHSA-foo"] |
| 96 | + assert vd["affected_versions"] == ["1.2.3"] |
| 97 | + assert sorted(vd["fixed_versions"]) == ["1.2.4", "1.3.0"] |
| 98 | + |
| 99 | + |
| 100 | +def test_local_vulnerablecode_triggers_live_evaluation_when_enabled(monkeypatch): |
| 101 | + monkeypatch.setenv("VCIO_HOST", "localhost") |
| 102 | + monkeypatch.setenv("VCIO_PORT", "1234") |
| 103 | + monkeypatch.setenv("ENABLE_LIVE_EVAL", "1") |
| 104 | + |
| 105 | + calls = [] |
| 106 | + |
| 107 | + def fake_post(url, json=None, **kwargs): # noqa: A002 (shadowing builtins) |
| 108 | + calls.append((url, json)) |
| 109 | + if url.endswith("/api/v2/live-evaluation/evaluate"): |
| 110 | + return FakeResponse(202, {"status": "accepted"}) |
| 111 | + if url.endswith("/api/v2/advisories-packages/bulk_search/"): |
| 112 | + return FakeResponse( |
| 113 | + 200, |
| 114 | + make_v2_advisories_response( |
| 115 | + pkg_purl="pkg:pypi/demo@1.2.3", |
| 116 | + advisory_id="ADV-999", |
| 117 | + aliases=["CVE-2025-1111"], |
| 118 | + fixes=["pkg:pypi/demo@1.2.5"], |
| 119 | + ), |
| 120 | + ) |
| 121 | + return FakeResponse(404, {"detail": "not found"}) |
| 122 | + |
| 123 | + monkeypatch.setattr("vulntotal.datasources.vulnerablecode_local.requests.post", fake_post) |
| 124 | + |
| 125 | + ds = LocalVulnerableCodeDataSource() |
| 126 | + purl = PackageURL.from_string("pkg:pypi/demo@1.2.3") |
| 127 | + |
| 128 | + results = list(ds.datasource_advisory(purl)) |
| 129 | + |
| 130 | + urls = [u for u, _ in calls] |
| 131 | + assert any( |
| 132 | + "/api/v2/live-evaluation/evaluate" in url for url in urls |
| 133 | + ), "live evaluation endpoint should be called when enabled" |
| 134 | + assert any( |
| 135 | + "/api/v2/advisories-packages/bulk_search/" in url for url in urls |
| 136 | + ), "v2 advisories bulk_search should be called" |
| 137 | + |
| 138 | + assert len(results) == 1 |
| 139 | + vd = results[0].to_dict() |
| 140 | + assert vd["aliases"] == ["CVE-2025-1111"] |
| 141 | + assert vd["affected_versions"] == ["1.2.3"] |
| 142 | + assert vd["fixed_versions"] == ["1.2.5"] |
0 commit comments