|
| 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 unittest import TestCase |
| 13 | +from unittest.mock import MagicMock |
| 14 | +from unittest.mock import patch |
| 15 | + |
| 16 | +from vulnerabilities.pipelines.v2_importers.libreoffice_importer import LibreOfficeImporterPipeline |
| 17 | +from vulnerabilities.pipelines.v2_importers.libreoffice_importer import parse_cve_advisory |
| 18 | +from vulnerabilities.pipelines.v2_importers.libreoffice_importer import parse_cve_ids |
| 19 | + |
| 20 | +TEST_DATA = os.path.join(os.path.dirname(__file__), "..", "..", "test_data", "libreoffice") |
| 21 | + |
| 22 | + |
| 23 | +def load_json(filename): |
| 24 | + with open(os.path.join(TEST_DATA, filename), encoding="utf-8") as f: |
| 25 | + return json.load(f) |
| 26 | + |
| 27 | + |
| 28 | +def load_html(filename): |
| 29 | + with open(os.path.join(TEST_DATA, filename), encoding="utf-8") as f: |
| 30 | + return f.read() |
| 31 | + |
| 32 | + |
| 33 | +class TestParseCveIds(TestCase): |
| 34 | + def test_extracts_cve_ids_from_html(self): |
| 35 | + html = load_html("advisories.html") |
| 36 | + cve_ids = parse_cve_ids(html) |
| 37 | + self.assertIn("CVE-2025-1080", cve_ids) |
| 38 | + self.assertIn("CVE-2023-2255", cve_ids) |
| 39 | + self.assertIn("CVE-2023-4863", cve_ids) |
| 40 | + |
| 41 | + def test_deduplicates_repeated_ids(self): |
| 42 | + html = "<a>CVE-2025-1080</a> ... <a>CVE-2025-1080</a>" |
| 43 | + self.assertEqual(parse_cve_ids(html), ["CVE-2025-1080"]) |
| 44 | + |
| 45 | + def test_empty_html_returns_empty_list(self): |
| 46 | + self.assertEqual(parse_cve_ids("<html></html>"), []) |
| 47 | + |
| 48 | + |
| 49 | +class TestParseCveAdvisory(TestCase): |
| 50 | + def test_cvss4_and_cwe(self): |
| 51 | + data = load_json("cve_2025_1080.json") |
| 52 | + advisory = parse_cve_advisory(data, "CVE-2025-1080") |
| 53 | + self.assertIsNotNone(advisory) |
| 54 | + self.assertEqual(advisory.advisory_id, "CVE-2025-1080") |
| 55 | + self.assertEqual(advisory.aliases, []) |
| 56 | + self.assertIn("macro", advisory.summary.lower()) |
| 57 | + self.assertEqual(len(advisory.severities), 1) |
| 58 | + self.assertEqual(advisory.severities[0].value, "7.2") |
| 59 | + self.assertIn("CVSS:4.0/", advisory.severities[0].scoring_elements) |
| 60 | + self.assertEqual(advisory.weaknesses, [20]) |
| 61 | + self.assertIsNotNone(advisory.date_published) |
| 62 | + self.assertIn("cve-2025-1080", advisory.url) |
| 63 | + |
| 64 | + def test_no_cvss_has_empty_severities(self): |
| 65 | + data = load_json("cve_2023_2255.json") |
| 66 | + advisory = parse_cve_advisory(data, "CVE-2023-2255") |
| 67 | + self.assertIsNotNone(advisory) |
| 68 | + self.assertEqual(advisory.severities, []) |
| 69 | + |
| 70 | + def test_cwe_264_extracted(self): |
| 71 | + data = load_json("cve_2023_2255.json") |
| 72 | + advisory = parse_cve_advisory(data, "CVE-2023-2255") |
| 73 | + self.assertEqual(advisory.weaknesses, [264]) |
| 74 | + |
| 75 | + def test_references_from_cna(self): |
| 76 | + data = load_json("cve_2023_2255.json") |
| 77 | + advisory = parse_cve_advisory(data, "CVE-2023-2255") |
| 78 | + urls = [r.url for r in advisory.references] |
| 79 | + self.assertIn("https://www.debian.org/security/2023/dsa-5415", urls) |
| 80 | + self.assertIn("https://security.gentoo.org/glsa/202311-15", urls) |
| 81 | + |
| 82 | + def test_missing_cve_id_returns_none(self): |
| 83 | + advisory = parse_cve_advisory({"cveMetadata": {"cveId": ""}, "containers": {}}, "") |
| 84 | + self.assertIsNone(advisory) |
| 85 | + |
| 86 | + def test_original_advisory_text_is_json(self): |
| 87 | + data = load_json("cve_2025_1080.json") |
| 88 | + advisory = parse_cve_advisory(data, "CVE-2025-1080") |
| 89 | + parsed = json.loads(advisory.original_advisory_text) |
| 90 | + self.assertEqual(parsed["cveMetadata"]["cveId"], "CVE-2025-1080") |
| 91 | + |
| 92 | + def test_malformed_cwe_skipped(self): |
| 93 | + data = load_json("cve_2025_1080.json") |
| 94 | + data = json.loads(json.dumps(data)) |
| 95 | + data["containers"]["cna"]["problemTypes"] = [ |
| 96 | + {"descriptions": [{"cweId": "CWE-INVALID", "lang": "en", "type": "CWE"}]} |
| 97 | + ] |
| 98 | + advisory = parse_cve_advisory(data, "CVE-2025-1080") |
| 99 | + self.assertEqual(advisory.weaknesses, []) |
| 100 | + |
| 101 | + |
| 102 | +class TestLibreOfficeImporterPipeline(TestCase): |
| 103 | + def _make_resp(self, data, status=200): |
| 104 | + resp = MagicMock() |
| 105 | + resp.json.return_value = data |
| 106 | + resp.text = json.dumps(data) |
| 107 | + resp.raise_for_status.return_value = None |
| 108 | + resp.status_code = status |
| 109 | + return resp |
| 110 | + |
| 111 | + @patch("vulnerabilities.pipelines.v2_importers.libreoffice_importer.requests.get") |
| 112 | + def test_fetch_stores_cve_ids(self, mock_get): |
| 113 | + html = load_html("advisories.html") |
| 114 | + mock_get.return_value = MagicMock(text=html, raise_for_status=MagicMock()) |
| 115 | + pipeline = LibreOfficeImporterPipeline() |
| 116 | + pipeline.fetch() |
| 117 | + self.assertIn("CVE-2025-1080", pipeline.cve_ids) |
| 118 | + self.assertIn("CVE-2023-2255", pipeline.cve_ids) |
| 119 | + |
| 120 | + @patch("vulnerabilities.pipelines.v2_importers.libreoffice_importer.requests.get") |
| 121 | + def test_collect_advisories_yields_advisory(self, mock_get): |
| 122 | + cve_data = load_json("cve_2025_1080.json") |
| 123 | + pipeline = LibreOfficeImporterPipeline() |
| 124 | + pipeline.cve_ids = ["CVE-2025-1080"] |
| 125 | + mock_get.return_value = self._make_resp(cve_data) |
| 126 | + advisories = list(pipeline.collect_advisories()) |
| 127 | + self.assertEqual(len(advisories), 1) |
| 128 | + self.assertEqual(advisories[0].advisory_id, "CVE-2025-1080") |
| 129 | + |
| 130 | + @patch("vulnerabilities.pipelines.v2_importers.libreoffice_importer.requests.get") |
| 131 | + def test_collect_advisories_skips_on_http_error(self, mock_get): |
| 132 | + pipeline = LibreOfficeImporterPipeline() |
| 133 | + pipeline.cve_ids = ["CVE-2025-1080"] |
| 134 | + mock_get.side_effect = Exception("timeout") |
| 135 | + logger_name = "vulnerabilities.pipelines.v2_importers.libreoffice_importer" |
| 136 | + with self.assertLogs(logger_name, level="ERROR") as cm: |
| 137 | + advisories = list(pipeline.collect_advisories()) |
| 138 | + self.assertEqual(advisories, []) |
| 139 | + self.assertTrue(any("CVE-2025-1080" in msg for msg in cm.output)) |
| 140 | + |
| 141 | + def test_advisories_count(self): |
| 142 | + pipeline = LibreOfficeImporterPipeline() |
| 143 | + pipeline.cve_ids = ["CVE-2025-1080", "CVE-2023-2255"] |
| 144 | + self.assertEqual(pipeline.advisories_count(), 2) |
0 commit comments