From 1a5898503d7a77cee1740369f8b5ad574770cfa6 Mon Sep 17 00:00:00 2001 From: Gowri Date: Wed, 25 Nov 2026 11:00:44 +0000 Subject: [PATCH] Expand pessimistic (~>) patched versions in elixir_security_importer_v2 Signed-off-by: Gowri --- .../v2_importers/elixir_security_importer.py | 38 +++++++++++++++++-- .../test_elixir_security_importer_v2.py | 36 ++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py b/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py index 211743a93..447c9d349 100644 --- a/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py +++ b/vulnerabilities/pipelines/v2_importers/elixir_security_importer.py @@ -24,6 +24,33 @@ from vulnerabilities.utils import load_yaml +def expand_pessimistic_version(version: str, version_class) -> list[VersionConstraint]: + """ + Expand an Elixir pessimistic version requirement (e.g. '~> 1.2.3' or '~> 1.2') + into inverted VersionConstraint objects for affected version range calculation. + """ + raw = version[2:].strip() + core = raw.split("-", 1)[0].split("+", 1)[0].strip() + parts = [int(part) for part in core.split(".")] + + if len(parts) >= 3: + lower_str = raw if len(raw.split(".")) >= 3 else f"{raw}.0" + upper_str = f"{parts[0]}.{parts[1] + 1}.0" + elif len(parts) == 2: + lower_str = f"{raw}.0" if "-" not in raw and "+" not in raw else raw + upper_str = f"{parts[0] + 1}.0.0" + else: + raise ValueError(f"Unsupported pessimistic version format: {version!r}") + + lower_constraint = VersionConstraint.from_string( + version_class=version_class, string=f">= {lower_str}" + ).invert() + upper_constraint = VersionConstraint.from_string( + version_class=version_class, string=f"< {upper_str}" + ).invert() + return [lower_constraint, upper_constraint] + + class ElixirSecurityImporterPipeline(VulnerableCodeBaseImporterPipelineV2): """ Elixir Security Advisories Importer Pipeline @@ -113,10 +140,13 @@ def process_file(self, file, base_path) -> Iterable[AdvisoryDataV2]: for version in patched_versions: if version.startswith("~>"): - version = version[2:] - constraints.append( - VersionConstraint.from_string(version_class=vrc, string=version).invert() - ) + constraints.extend( + expand_pessimistic_version(version=version, version_class=vrc) + ) + else: + constraints.append( + VersionConstraint.from_string(version_class=vrc, string=version).invert() + ) affected_packages = [] if pkg_name: 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..7f0d6fedb 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,39 @@ def test_collect_advisories_skips_invalid_cve(mock_fetch_via_vcs, tmp_path): importer.clone() advisories = list(importer.collect_advisories()) assert len(advisories) == 0 + + +def test_pessimistic_patched_versions_expanded(tmp_path): + repo_dir = tmp_path / "pessimistic_repo" + packages_dir = repo_dir / "packages" / "plug" + packages_dir.mkdir(parents=True) + + advisory_file = packages_dir / "2017-02-28.yml" + advisory_file.write_text( + """ + cve: "2017-1000052" + package: "plug" + description: "Test pessimistic version range" + patched_versions: + - ">= 1.3.2" + - "~> 1.2.3" + - "~> 1.1.7" + - "~> 1.0.4" + """ + ) + + 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 + version_range = advisories[0].affected_packages[0].affected_version_range + assert str(version_range) == "vers:hex/<1.0.4|>=1.1.0|<1.1.7|>=1.2.0|<1.2.3|>=1.3.0|<1.3.2"