Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions vulnerabilities/pipelines/v2_importers/elixir_security_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"