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
128 changes: 93 additions & 35 deletions src/macaron/slsa_analyzer/package_registry/osv_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,52 +358,110 @@ 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.
except ValueError as error:
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
Loading