|
| 1 | +import json |
| 2 | +from io import BytesIO |
| 3 | +from unittest.mock import patch |
| 4 | +from zipfile import ZipFile |
| 5 | + |
| 6 | +import pytest |
| 7 | +from packageurl import PackageURL |
| 8 | + |
| 9 | +from vulnerabilities.importer import AdvisoryData |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def mock_zip_data(): |
| 14 | + # Create a zip with two advisories for the same package with different versions |
| 15 | + zip_buffer = BytesIO() |
| 16 | + with ZipFile(zip_buffer, mode="w") as zip_file: |
| 17 | + advisory1 = { |
| 18 | + "advisory_id": "PYSEC-1001", |
| 19 | + "summary": "Vuln in foo", |
| 20 | + "affected": [ |
| 21 | + { |
| 22 | + "package": {"name": "foo", "ecosystem": "PyPI"}, |
| 23 | + "ranges": [ |
| 24 | + { |
| 25 | + "type": "ECOSYSTEM", |
| 26 | + "events": [{"introduced": "1.0.0"}, {"fixed": "2.0.0"}], |
| 27 | + } |
| 28 | + ], |
| 29 | + } |
| 30 | + ], |
| 31 | + } |
| 32 | + advisory2 = { |
| 33 | + "advisory_id": "PYSEC-1002", |
| 34 | + "summary": "Vuln in foo, later version", |
| 35 | + "affected": [ |
| 36 | + { |
| 37 | + "package": {"name": "foo", "ecosystem": "PyPI"}, |
| 38 | + "ranges": [ |
| 39 | + { |
| 40 | + "type": "ECOSYSTEM", |
| 41 | + "events": [{"introduced": "2.5.0"}, {"fixed": "3.0.0"}], |
| 42 | + } |
| 43 | + ], |
| 44 | + } |
| 45 | + ], |
| 46 | + } |
| 47 | + advisory3 = { |
| 48 | + "advisory_id": "PYSEC-2000", |
| 49 | + "summary": "Vuln in bar", |
| 50 | + "affected": [ |
| 51 | + { |
| 52 | + "package": {"name": "bar", "ecosystem": "PyPI"}, |
| 53 | + "ranges": [ |
| 54 | + { |
| 55 | + "type": "ECOSYSTEM", |
| 56 | + "events": [{"introduced": "0.1.0"}, {"fixed": "0.2.0"}], |
| 57 | + } |
| 58 | + ], |
| 59 | + } |
| 60 | + ], |
| 61 | + } |
| 62 | + zip_file.writestr("PYSEC-1001.json", json.dumps(advisory1)) |
| 63 | + zip_file.writestr("PYSEC-1002.json", json.dumps(advisory2)) |
| 64 | + zip_file.writestr("PYSEC-2000.json", json.dumps(advisory3)) |
| 65 | + zip_buffer.seek(0) |
| 66 | + return zip_buffer |
| 67 | + |
| 68 | + |
| 69 | +def test_package_with_version_affected(mock_zip_data): |
| 70 | + from vulnerabilities.pipelines.v2_importers.pysec_live_importer import PySecLiveImporterPipeline |
| 71 | + |
| 72 | + purl = PackageURL(type="pypi", name="foo", version="1.5.0") |
| 73 | + |
| 74 | + with patch("requests.get") as mock_get: |
| 75 | + mock_get.return_value.content = mock_zip_data.read() |
| 76 | + |
| 77 | + with patch("vulnerabilities.importers.osv.parse_advisory_data_v2") as mock_parse: |
| 78 | + |
| 79 | + def parse_side_effect(raw_data, supported_ecosystems, advisory_url, advisory_text): |
| 80 | + return AdvisoryData( |
| 81 | + advisory_id=raw_data["advisory_id"], |
| 82 | + summary=raw_data["summary"], |
| 83 | + references_v2=[{"url": advisory_url}], |
| 84 | + affected_packages=[], |
| 85 | + weaknesses=[], |
| 86 | + url=advisory_url, |
| 87 | + ) |
| 88 | + |
| 89 | + mock_parse.side_effect = parse_side_effect |
| 90 | + |
| 91 | + pipeline = PySecLiveImporterPipeline(purl=purl) |
| 92 | + pipeline.get_purl_inputs() |
| 93 | + pipeline.fetch_zip() |
| 94 | + advisories = list(pipeline.collect_advisories()) |
| 95 | + |
| 96 | + # Only PYSEC-1001 should match |
| 97 | + assert len(advisories) == 1 |
| 98 | + assert advisories[0].advisory_id == "PYSEC-1001" |
| 99 | + |
| 100 | + |
| 101 | +def test_package_with_version_not_affected(mock_zip_data): |
| 102 | + from vulnerabilities.pipelines.v2_importers.pysec_live_importer import PySecLiveImporterPipeline |
| 103 | + |
| 104 | + purl = PackageURL(type="pypi", name="foo", version="2.2.0") |
| 105 | + |
| 106 | + with patch("requests.get") as mock_get: |
| 107 | + mock_get.return_value.content = mock_zip_data.read() |
| 108 | + |
| 109 | + with patch("vulnerabilities.importers.osv.parse_advisory_data_v2") as mock_parse: |
| 110 | + mock_parse.return_value = AdvisoryData( |
| 111 | + advisory_id="PYSEC-1002", |
| 112 | + summary="Vuln in foo, later version", |
| 113 | + references_v2=[{"url": "dummy"}], |
| 114 | + affected_packages=[], |
| 115 | + weaknesses=[], |
| 116 | + url="dummy", |
| 117 | + ) |
| 118 | + |
| 119 | + pipeline = PySecLiveImporterPipeline(purl=purl) |
| 120 | + pipeline.get_purl_inputs() |
| 121 | + pipeline.fetch_zip() |
| 122 | + advisories = list(pipeline.collect_advisories()) |
| 123 | + |
| 124 | + # No advisories should match |
| 125 | + assert len(advisories) == 0 |
| 126 | + |
| 127 | + |
| 128 | +def test_nonexistent_package(mock_zip_data): |
| 129 | + from vulnerabilities.pipelines.v2_importers.pysec_live_importer import PySecLiveImporterPipeline |
| 130 | + |
| 131 | + purl = PackageURL(type="pypi", name="baz", version="1.0.0") |
| 132 | + |
| 133 | + with patch("requests.get") as mock_get: |
| 134 | + mock_get.return_value.content = mock_zip_data.read() |
| 135 | + |
| 136 | + pipeline = PySecLiveImporterPipeline(purl=purl) |
| 137 | + pipeline.get_purl_inputs() |
| 138 | + pipeline.fetch_zip() |
| 139 | + advisories = list(pipeline.collect_advisories()) |
| 140 | + |
| 141 | + assert len(advisories) == 0 |
0 commit comments