From 952329ea8065749996c6aa514b2104e3f5c76736 Mon Sep 17 00:00:00 2001 From: Dezhen Lu Date: Fri, 20 Nov 2026 09:10:50 +0100 Subject: [PATCH] fix: skip GIT ranges and handle last_affected in OSV version matching OSVDevService.is_version_affected compared the commit hashes of GIT ranges as versions. They failed to parse, so the GIT range matched every version, including fixed ones. last_affected events were ignored, so a range ending in last_affected matched all later versions too, and only the last introduced/fixed pair of an events list was used. Skip GIT ranges, read the events of each range as a sequence of intervals that end at fixed (exclusive) or last_affected (inclusive), and raise APIAccessError if no non-GIT range is left. Refs #1498 Signed-off-by: Dezhen Lu --- .../slsa_analyzer/package_registry/osv_dev.py | 128 ++++++--- .../package_registry/test_osv_dev.py | 250 ++++++++++++++++++ 2 files changed, 343 insertions(+), 35 deletions(-) diff --git a/src/macaron/slsa_analyzer/package_registry/osv_dev.py b/src/macaron/slsa_analyzer/package_registry/osv_dev.py index eda4e7683..2e1134dc9 100644 --- a/src/macaron/slsa_analyzer/package_registry/osv_dev.py +++ b/src/macaron/slsa_analyzer/package_registry/osv_dev.py @@ -358,46 +358,25 @@ def is_version_affected( if not affected_ranges: raise APIAccessError(f"Failed to extract affected versions for {pkg_name}@{pkg_version}.") + try: + parsed_version = version.Version(pkg_version) + except version.InvalidVersion as error: + raise APIAccessError(f"Failed to parse version string {pkg_version}.") from error + + checked_range = False for affected_range in affected_ranges: + # GIT ranges use commit hashes for their events, which cannot be compared with a version. + # The ECOSYSTEM range of the same record describes the affected versions. + if json_extract(affected_range, ["type"], str) == "GIT": + continue + events = json_extract(affected_range, ["events"], list) if not events: raise APIAccessError(f"Failed to extract affected versions for {pkg_name}@{pkg_version}.") - introduced = None - fixed = None - for e in events: - if "introduced" in e: - introduced = e["introduced"] - if "fixed" in e: - fixed = e["fixed"] - - # TODO: convert commit to tag & version - parsed_introduced = version.Version("0") - if introduced: - try: - parsed_introduced = version.Version(introduced) - except version.InvalidVersion as error: - logger.debug(error) - - parsed_fix = None - if fixed: - try: - parsed_fix = version.Version(fixed) - except version.InvalidVersion as error: - logger.debug(error) - + checked_range = True try: - parsed_version = version.Version(pkg_version) - except version.InvalidVersion as error: - raise APIAccessError(f"Failed to parse version string {pkg_version}.") from error - - try: - if parsed_version > parsed_introduced or parsed_version == parsed_introduced: - if parsed_fix is not None: - if parsed_version == parsed_fix or parsed_version > parsed_fix: - continue - - # If a fixed version does not exist, the current version is affected. + if OSVDevService._is_version_in_events(parsed_version, events): return True # We should not get this error, but if we do, we avoid false positives and continue with the next # version range. @@ -405,5 +384,84 @@ def is_version_affected( logger.debug(error) continue - # If current version is smaller than the introduced version, it is not affected. + if not checked_range: + raise APIAccessError(f"Failed to find a version range for {pkg_name}@{pkg_version}.") + + # The version is not in any of the affected version ranges. return False + + @staticmethod + def _is_version_in_events(parsed_version: version.Version, events: list) -> bool: + """Check whether a version is in one of the intervals described by OSV range events. + + The events are read in order. Each ``introduced`` event starts an interval, which ends at the next + ``fixed`` event (exclusive) or ``last_affected`` event (inclusive). An interval without an end + contains all later versions. + + Parameters + ---------- + parsed_version : version.Version + The version to check. + events : list + The ``events`` list of an OSV ``ECOSYSTEM`` or ``SEMVER`` range. + + Returns + ------- + bool + True if the version is in one of the intervals, otherwise False. + """ + introduced: version.Version | None = None + open_interval = False + saw_end = False + for event in events: + if not isinstance(event, dict): + continue + if "introduced" in event: + introduced = OSVDevService._parse_event_version(event["introduced"]) or version.Version("0") + open_interval = True + continue + + if "fixed" in event: + end, inclusive = event["fixed"], False + elif "last_affected" in event: + end, inclusive = event["last_affected"], True + else: + continue + + saw_end = True + start = introduced or version.Version("0") + parsed_end = OSVDevService._parse_event_version(end) + if parsed_version >= start: + # If the end of the interval cannot be parsed, the version is considered affected. + if parsed_end is None or parsed_version < parsed_end or (inclusive and parsed_version == parsed_end): + return True + introduced = None + open_interval = False + + if open_interval or not saw_end: + # The last interval has no end, so all versions from its start are affected. + return parsed_version >= (introduced or version.Version("0")) + + return False + + @staticmethod + def _parse_event_version(value: object) -> version.Version | None: + """Parse the version of an OSV range event. + + Parameters + ---------- + value : object + The value of the event. + + Returns + ------- + version.Version | None + The parsed version, or None if the value is missing or not a valid version. + """ + if not value or not isinstance(value, str): + return None + try: + return version.Version(value) + except version.InvalidVersion as error: + logger.debug(error) + return None diff --git a/tests/slsa_analyzer/package_registry/test_osv_dev.py b/tests/slsa_analyzer/package_registry/test_osv_dev.py index 8bb277b51..ea673aba8 100644 --- a/tests/slsa_analyzer/package_registry/test_osv_dev.py +++ b/tests/slsa_analyzer/package_registry/test_osv_dev.py @@ -104,6 +104,28 @@ def test_is_affected_version_invalid_response() -> None: "tj-actions/changed-files", id="Test missing events", ), + pytest.param( + { + "id": "GHSA-6f4g-xm8v-pgv6", + "affected": [ + { + "package": {"name": "tmatens/compose-lint", "ecosystem": "GitHub Actions"}, + "ranges": [ + { + "type": "GIT", + "repo": "https://github.com/tmatens/compose-lint", + "events": [ + {"introduced": "60c091c7cc8f86c6b0c5954031097364fea0a201"}, + {"fixed": "a6a7a76736de1c72abfa96c41ff8af556ffdc55f"}, + ], + }, + ], + } + ], + }, + "tmatens/compose-lint", + id="Test only GIT ranges", + ), ], ) def test_is_affected_version_invalid_osv_vulns(vuln: dict, workflow: str) -> None: @@ -214,6 +236,234 @@ def test_is_affected_version_invalid_osv_vulns(vuln: dict, workflow: str) -> Non False, id="Test fix version", ), + pytest.param( + { + "id": "GHSA-5xjx-3w67-w9x9", + "affected": [ + { + "package": {"name": "pypa/gh-action-pypi-publish", "ecosystem": "GitHub Actions"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"fixed": "1.14.3"}]}, + { + "type": "GIT", + "repo": "https://github.com/pypa/gh-action-pypi-publish", + "events": [{"introduced": "0"}, {"fixed": "7245906fb61fa33a13554f9d5fb856fe61758086"}], + }, + ], + } + ], + }, + "pypa/gh-action-pypi-publish", + "v1.14.3", + False, + id="Test fixed version with a GIT range", + ), + pytest.param( + { + "id": "GHSA-5xjx-3w67-w9x9", + "affected": [ + { + "package": {"name": "pypa/gh-action-pypi-publish", "ecosystem": "GitHub Actions"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"fixed": "1.14.3"}]}, + { + "type": "GIT", + "repo": "https://github.com/pypa/gh-action-pypi-publish", + "events": [{"introduced": "0"}, {"fixed": "7245906fb61fa33a13554f9d5fb856fe61758086"}], + }, + ], + } + ], + }, + "pypa/gh-action-pypi-publish", + "v1.14.2", + True, + id="Test affected version with a GIT range", + ), + pytest.param( + { + "id": "GHSA-6f4g-xm8v-pgv6", + "affected": [ + { + "package": {"name": "tmatens/compose-lint", "ecosystem": "GitHub Actions"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0.2.0"}, {"fixed": "0.31.0"}]}, + { + "type": "GIT", + "repo": "https://github.com/tmatens/compose-lint", + "events": [ + {"introduced": "60c091c7cc8f86c6b0c5954031097364fea0a201"}, + {"fixed": "a6a7a76736de1c72abfa96c41ff8af556ffdc55f"}, + ], + }, + ], + } + ], + }, + "tmatens/compose-lint", + "0.1.0", + False, + id="Test version before introduced with a GIT range", + ), + pytest.param( + { + "id": "GHSA-6f4g-xm8v-pgv6", + "affected": [ + { + "package": {"name": "tmatens/compose-lint", "ecosystem": "GitHub Actions"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0.2.0"}, {"fixed": "0.31.0"}]}, + { + "type": "GIT", + "repo": "https://github.com/tmatens/compose-lint", + "events": [ + {"introduced": "60c091c7cc8f86c6b0c5954031097364fea0a201"}, + {"fixed": "a6a7a76736de1c72abfa96c41ff8af556ffdc55f"}, + ], + }, + ], + } + ], + }, + "tmatens/compose-lint", + "0.31.0", + False, + id="Test fix version with a GIT range", + ), + pytest.param( + { + "id": "GHSA-r4fj-r33x-8v88", + "affected": [ + { + "package": {"name": "njzjz/wenxian", "ecosystem": "GitHub Actions"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"last_affected": "0.3.1"}]}, + ], + } + ], + }, + "njzjz/wenxian", + "0.3.1", + True, + id="Test last_affected version", + ), + pytest.param( + { + "id": "GHSA-r4fj-r33x-8v88", + "affected": [ + { + "package": {"name": "njzjz/wenxian", "ecosystem": "GitHub Actions"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"last_affected": "0.3.1"}]}, + ], + } + ], + }, + "njzjz/wenxian", + "0.3.2", + False, + id="Test version after last_affected", + ), + pytest.param( + { + "id": "GHSA-test-multiple-intervals", + "affected": [ + { + "package": {"name": "rlespinasse/github-slug-action", "ecosystem": "GitHub Actions"}, + "ranges": [ + { + "type": "ECOSYSTEM", + "events": [ + {"introduced": "0"}, + {"fixed": "1.1.1"}, + {"introduced": "2.0.0"}, + {"fixed": "2.1.1"}, + ], + }, + ], + } + ], + }, + "rlespinasse/github-slug-action", + "1.0.0", + True, + id="Test version in the first interval", + ), + pytest.param( + { + "id": "GHSA-test-multiple-intervals", + "affected": [ + { + "package": {"name": "rlespinasse/github-slug-action", "ecosystem": "GitHub Actions"}, + "ranges": [ + { + "type": "ECOSYSTEM", + "events": [ + {"introduced": "0"}, + {"fixed": "1.1.1"}, + {"introduced": "2.0.0"}, + {"fixed": "2.1.1"}, + ], + }, + ], + } + ], + }, + "rlespinasse/github-slug-action", + "1.2.0", + False, + id="Test version between two intervals", + ), + pytest.param( + { + "id": "GHSA-test-multiple-intervals", + "affected": [ + { + "package": {"name": "rlespinasse/github-slug-action", "ecosystem": "GitHub Actions"}, + "ranges": [ + { + "type": "ECOSYSTEM", + "events": [ + {"introduced": "0"}, + {"fixed": "1.1.1"}, + {"introduced": "2.0.0"}, + {"fixed": "2.1.1"}, + ], + }, + ], + } + ], + }, + "rlespinasse/github-slug-action", + "2.0.0", + True, + id="Test version in the second interval", + ), + pytest.param( + { + "id": "GHSA-test-multiple-intervals", + "affected": [ + { + "package": {"name": "rlespinasse/github-slug-action", "ecosystem": "GitHub Actions"}, + "ranges": [ + { + "type": "ECOSYSTEM", + "events": [ + {"introduced": "0"}, + {"fixed": "1.1.1"}, + {"introduced": "2.0.0"}, + {"fixed": "2.1.1"}, + ], + }, + ], + } + ], + }, + "rlespinasse/github-slug-action", + "2.1.1", + False, + id="Test fix version of the second interval", + ), ], ) def test_is_affected_version_ranges(vuln: dict, workflow: str, version: str, expected: bool) -> None: