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
16 changes: 15 additions & 1 deletion vulnerabilities/pipelines/v2_importers/gentoo_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """<?xml version="1.0" encoding="UTF-8"?>
<glsa id="209912-99">
<synopsis>Probe advisory</synopsis>
<references>
<uri link="https://example.com/CVE-2099-9999">CVE-2099-9999</uri>
</references>
<affected>
<package name="dev-libs/probe" auto="yes" arch="*">
<vulnerable range="lt">{vulnerable}</vulnerable>
<unaffected range="ge">9.9.9</unaffected>
</package>
</affected>
<impact type="normal"/>
</glsa>
"""


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 <vulnerable> 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(
'<vulnerable range="lt"></vulnerable>', '<vulnerable range="lt"/>'
)
)
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"