|
| 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 | +# |
| 6 | + |
| 7 | +import pytest |
| 8 | +import requests |
| 9 | +from packageurl import PackageURL |
| 10 | + |
| 11 | +from vulnerabilities.importer import AdvisoryData |
| 12 | +from vulnerabilities.pipelines.v2_importers.postgresql_live_importer import ( |
| 13 | + PostgreSQLLiveImporterPipeline, |
| 14 | +) |
| 15 | + |
| 16 | +HTML_BASE = """ |
| 17 | +<html> |
| 18 | + <body> |
| 19 | + <table> |
| 20 | + <tbody> |
| 21 | + <tr> |
| 22 | + <td> |
| 23 | + <span class="nobr"><a href="/support/security/CVE-2022-1234/">CVE-2022-1234</a></span><br> |
| 24 | + <a href="/about/news/postgresql-175-169-1513-1418-and-1321-released-3072/">Announcement</a><br> |
| 25 | + </td> |
| 26 | + <td>{affected}</td> |
| 27 | + <td>{fixed}</td> |
| 28 | + <td><a href="/vector?vector=CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H">9.8</a></td> |
| 29 | + <td>{summary}</td> |
| 30 | + </tr> |
| 31 | + </tbody> |
| 32 | + </table> |
| 33 | + </body> |
| 34 | +</html> |
| 35 | +""" |
| 36 | + |
| 37 | + |
| 38 | +class DummyResponse: |
| 39 | + def __init__(self, content): |
| 40 | + self.content = content.encode("utf-8") |
| 41 | + |
| 42 | + |
| 43 | +def test_affected_version(monkeypatch): |
| 44 | + html = HTML_BASE.format(affected="10.0, 10.1", fixed="10.2", summary="Issue affects all") |
| 45 | + monkeypatch.setattr(requests, "get", lambda url: DummyResponse(html)) |
| 46 | + |
| 47 | + purl = PackageURL(type="generic", name="postgresql", version="10.1") |
| 48 | + pipeline = PostgreSQLLiveImporterPipeline(purl=purl) |
| 49 | + pipeline.get_purl_inputs() |
| 50 | + advisories = list(pipeline.collect_advisories()) |
| 51 | + |
| 52 | + assert len(advisories) == 1 |
| 53 | + adv = advisories[0] |
| 54 | + assert isinstance(adv, AdvisoryData) |
| 55 | + assert adv.advisory_id == "CVE-2022-1234" |
| 56 | + |
| 57 | + |
| 58 | +def test_unaffected_version(monkeypatch): |
| 59 | + html = HTML_BASE.format(affected="10.0, 10.1", fixed="10.2", summary="Issue affects all") |
| 60 | + monkeypatch.setattr(requests, "get", lambda url: DummyResponse(html)) |
| 61 | + |
| 62 | + purl = PackageURL(type="generic", name="postgresql", version="10.2") |
| 63 | + pipeline = PostgreSQLLiveImporterPipeline(purl=purl) |
| 64 | + pipeline.get_purl_inputs() |
| 65 | + advisories = list(pipeline.collect_advisories()) |
| 66 | + |
| 67 | + assert len(advisories) == 0 |
| 68 | + |
| 69 | + |
| 70 | +def test_qualifier_filtering(monkeypatch): |
| 71 | + html = HTML_BASE.format(affected="12.0, 12.1", fixed="12.2", summary="Windows-specific issue") |
| 72 | + monkeypatch.setattr(requests, "get", lambda url: DummyResponse(html)) |
| 73 | + |
| 74 | + purl = PackageURL( |
| 75 | + type="generic", name="postgresql", version="12.1", qualifiers={"os": "windows"} |
| 76 | + ) |
| 77 | + pipeline = PostgreSQLLiveImporterPipeline(purl=purl) |
| 78 | + pipeline.get_purl_inputs() |
| 79 | + advisories = list(pipeline.collect_advisories()) |
| 80 | + assert len(advisories) == 1 |
| 81 | + |
| 82 | + purl = PackageURL(type="generic", name="postgresql", version="12.1", qualifiers={"os": "linux"}) |
| 83 | + pipeline = PostgreSQLLiveImporterPipeline(purl=purl) |
| 84 | + pipeline.get_purl_inputs() |
| 85 | + advisories = list(pipeline.collect_advisories()) |
| 86 | + assert len(advisories) == 0 |
| 87 | + |
| 88 | + |
| 89 | +def test_invalid_purl(): |
| 90 | + pipeline = PostgreSQLLiveImporterPipeline() |
| 91 | + |
| 92 | + pipeline. inputs = { "purl": "pkg:pypi/[email protected]"} |
| 93 | + with pytest.raises(ValueError): |
| 94 | + pipeline.get_purl_inputs() |
| 95 | + |
| 96 | + pipeline. inputs = { "purl": "pkg:generic/[email protected]"} |
| 97 | + with pytest.raises(ValueError): |
| 98 | + pipeline.get_purl_inputs() |
| 99 | + |
| 100 | + pipeline.inputs = {"purl": "pkg:generic/postgresql"} |
| 101 | + with pytest.raises(ValueError): |
| 102 | + pipeline.get_purl_inputs() |
0 commit comments