From d08fce5296c063b666b45f3cc6b0aff8c37cf32f Mon Sep 17 00:00:00 2001 From: Sanchit Date: Tue, 24 Nov 2026 05:43:54 +0000 Subject: [PATCH] fix(elixir): skip entries with no usable version range An advisory with no patched or unaffected versions listed produced a HexVersionRange with no constraints, which serializes as 'vers:hex/'. VersionRange.from_string rejects that string, so the affected package was dropped on round-trip. Skip the entry instead, mirroring the gentoo/apache_httpd fix for #2387. Signed-off-by: Sanchit --- .../v2_importers/elixir_security_importer.py | 19 +++-- .../test_elixir_security_importer_v2.py | 80 +++++++++++++++++++ 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py b/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py index 211743a93..3bf3cc684 100644 --- a/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py +++ b/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py @@ -120,12 +120,21 @@ def process_file(self, file, base_path) -> Iterable[AdvisoryDataV2]: affected_packages = [] if pkg_name: - affected_packages.append( - AffectedPackageV2( - package=PackageURL(type="hex", name=pkg_name), - affected_version_range=HexVersionRange(constraints=constraints), + # Do not build a version range with empty constraints: its + # string form ("vers:hex/") is rejected by + # VersionRange.from_string and the affected package is then + # dropped on round-trip. Skip the entry instead, mirroring + # the gentoo/apache_httpd fix for #2387. + version_range = HexVersionRange(constraints=constraints) if constraints else None + if version_range is None: + self.log(f"Skipping package {pkg_name} with no usable version range") + else: + affected_packages.append( + AffectedPackageV2( + package=PackageURL(type="hex", name=pkg_name), + affected_version_range=version_range, + ) ) - ) date_published = None if yaml_file.get("disclosure_date"): diff --git a/vulnerabilities/tests/pipelines/v2_importers/test_elixir_security_importer_v2.py b/vulnerabilities/tests/pipelines/v2_importers/test_elixir_security_importer_v2.py index e47d11623..3d95a380e 100644 --- a/vulnerabilities/tests/pipelines/v2_importers/test_elixir_security_importer_v2.py +++ b/vulnerabilities/tests/pipelines/v2_importers/test_elixir_security_importer_v2.py @@ -106,3 +106,83 @@ def test_collect_advisories_skips_invalid_cve(mock_fetch_via_vcs, tmp_path): importer.clone() advisories = list(importer.collect_advisories()) assert len(advisories) == 0 + + +@pytest.fixture +def mock_vcs_response_no_versions(tmp_path): + repo_dir = tmp_path / "repo" + repo_dir.mkdir() + packages_dir = repo_dir / "packages" / "some_package" + packages_dir.mkdir(parents=True) + + advisory_file = packages_dir / "CVE-2022-9999.yml" + advisory_file.write_text( + """ + cve: "2022-9999" + package: "plug" + description: "Vulnerability with no patched or unaffected versions listed" + disclosure_date: "2022-12-01" + link: "https://example.com/advisory" + """ + ) + + mock = MagicMock() + mock.dest_dir = str(repo_dir) + mock.delete = MagicMock() + return mock + + +def test_collect_advisories_skips_empty_version_range( + mock_vcs_response_no_versions, +): + """An advisory with no patched/unaffected versions must not mint an + empty range: 'vers:hex/' is rejected by VersionRange.from_string and + the affected package would be dropped on round-trip.""" + with patch( + "vulnerabilities.pipelines.v2_importers.elixir_security_importer.fetch_via_vcs" + ) as mock: + mock.return_value = mock_vcs_response_no_versions + importer = ElixirSecurityImporterPipeline() + importer.clone() + advisories = list(importer.collect_advisories()) + + assert len(advisories) == 1 + assert advisories[0].affected_packages == [] + # The advisory itself is still recorded, with no invalid range string + assert "vers:hex/" not in str(advisories[0].to_dict()) + + +def test_collect_advisories_keeps_one_sided_versions(tmp_path): + """Only one version list present still builds a usable range.""" + repo_dir = tmp_path / "repo" + packages_dir = repo_dir / "packages" / "some_package" + packages_dir.mkdir(parents=True) + + advisory_file = packages_dir / "CVE-2022-9999.yml" + advisory_file.write_text( + """ + cve: "2022-9999" + package: "plug" + description: "Vulnerability with only a patched version listed" + patched_versions: + - ">= 1.11.1" + disclosure_date: "2022-12-01" + link: "https://example.com/advisory" + """ + ) + + mock_response = MagicMock() + mock_response.dest_dir = str(repo_dir) + mock_response.delete = MagicMock() + + with patch( + "vulnerabilities.pipelines.v2_importers.elixir_security_importer.fetch_via_vcs" + ) as mock: + mock.return_value = mock_response + importer = ElixirSecurityImporterPipeline() + importer.clone() + advisories = list(importer.collect_advisories()) + + assert len(advisories) == 1 + assert len(advisories[0].affected_packages) == 1 + assert str(advisories[0].affected_packages[0].affected_version_range) != "vers:hex/"