diff --git a/vulnerabilities/pipelines/v2_importers/gentoo_importer.py b/vulnerabilities/pipelines/v2_importers/gentoo_importer.py
index 91f1d85c3..039d842b5 100644
--- a/vulnerabilities/pipelines/v2_importers/gentoo_importer.py
+++ b/vulnerabilities/pipelines/v2_importers/gentoo_importer.py
@@ -86,7 +86,18 @@ def process_file(self, file):
child, logger=self.log
):
constraints = build_constraints([constraints], logger=self.log)
- version_range = EbuildVersionRange(constraints=constraints)
+ # Do not build a version range with empty constraints:
+ # its string form (e.g. "vers:ebuild/") is rejected by
+ # VersionRange.from_string and the affected package is
+ # then dropped on round-trip. Skip the entry instead,
+ # mirroring the apache_httpd importer fix for #2387.
+ version_range = (
+ EbuildVersionRange(constraints=constraints) if constraints else None
+ )
+
+ if version_range is None:
+ self.log(f"Skipping package {purl} with no usable version range")
+ continue
if is_unaffected:
affected_package = AffectedPackageV2(
@@ -142,6 +153,9 @@ def build_constraints(constraint_pairs, logger):
"""
constraints = []
for comparator, version in constraint_pairs:
+ if not version:
+ logger(f"Missing version for constraint with comparator: {comparator}")
+ continue
try:
constraint = VersionConstraint(version=GentooVersion(version), comparator=comparator)
constraints.append(constraint)
diff --git a/vulnerabilities/tests/pipelines/v2_importers/test_gentoo_importer_v2.py b/vulnerabilities/tests/pipelines/v2_importers/test_gentoo_importer_v2.py
index 796a87e1a..0a4c35031 100644
--- a/vulnerabilities/tests/pipelines/v2_importers/test_gentoo_importer_v2.py
+++ b/vulnerabilities/tests/pipelines/v2_importers/test_gentoo_importer_v2.py
@@ -39,3 +39,63 @@ def test_gentoo_advisories_per_file(xml_file):
expected_file = xml_file.with_name(xml_file.stem + "-expected.json")
util_tests.check_results_against_json(results, expected_file)
+
+
+GLSA_TEMPLATE = """
+
+ Probe advisory
+
+ CVE-2099-9999
+
+
+
+ {vulnerable}
+ 9.9.9
+
+
+
+
+"""
+
+
+def _process_glsa(tmp_path, vulnerable):
+ xml_file = tmp_path / "glsa-probe.xml"
+ xml_file.write_text(GLSA_TEMPLATE.format(vulnerable=vulnerable))
+ pipeline = GentooImporterPipeline()
+ return list(pipeline.process_file(xml_file))
+
+
+@pytest.mark.django_db
+def test_gentoo_invalid_version_does_not_build_empty_range(tmp_path):
+ """An unparsable GLSA version must not produce an empty version range.
+
+ A range built from no constraints serializes as "vers:ebuild/", which
+ VersionRange.from_string rejects, so the affected package is dropped
+ on round-trip. The entry is skipped instead, mirroring the
+ apache_httpd importer fix for #2387.
+ """
+ advisories = _process_glsa(tmp_path, "1.0*")
+ assert len(advisories) == 1
+ affected_packages = advisories[0].affected_packages
+ # Only the unaffected entry survives; the vulnerable entry is skipped.
+ assert len(affected_packages) == 1
+ assert affected_packages[0].affected_version_range is None
+ assert str(affected_packages[0].fixed_version_range) == "vers:ebuild/>=9.9.9"
+
+
+@pytest.mark.django_db
+def test_gentoo_empty_version_element_does_not_crash(tmp_path):
+ """An empty element has no version text and must not crash
+ the import: the entry is skipped, the unaffected entry is kept."""
+ xml_file = tmp_path / "glsa-probe.xml"
+ xml_file.write_text(
+ GLSA_TEMPLATE.format(vulnerable="").replace(
+ '', ''
+ )
+ )
+ pipeline = GentooImporterPipeline()
+ advisories = list(pipeline.process_file(xml_file))
+ assert len(advisories) == 1
+ affected_packages = advisories[0].affected_packages
+ assert len(affected_packages) == 1
+ assert str(affected_packages[0].fixed_version_range) == "vers:ebuild/>=9.9.9"