Skip to content

fix(ruby): keep compound pessimistic patched versions - #2562

Open
sanchit955 wants to merge 1 commit into
aboutcode-org:mainfrom
sanchit955:fix/ruby-patched-compound-pessimistic-range
Open

sanchit955 wants to merge 1 commit into
aboutcode-org:mainfrom
sanchit955:fix/ruby-patched-compound-pessimistic-range

Conversation

@sanchit955

Copy link
Copy Markdown

Fixes #2561

Problem

get_affected_packages (vulnerabilities/pipelines/v2_importers/ruby_importer.py) calls GemVersionRange.from_native(patched_version) + validate_comparators on each patched spec. For a compound spec like "~> 2.0, >= 2.2.6.4", from_native expands ~> 2.0 to >=2.0, <3 and appends >=2.2.6.4, so the combined list holds two >= lower bounds and validate_comparators rejects it. The loop logs the error and skips the entry, so the fixed version for that release line is never recorded.

Evidence

Scanned ruby-advisory-db (1,571 YAMLs, 1 Dec 2026): 159 entries in 99 advisories are exactly this shape ("~> V, >= V", 71 distinct specs).

  • rack CVE-2023-27539, patched ['~> 2.0, >= 2.2.6.4', '>= 3.0.6.1'] → only vers:gem/>=3.0.6.1 recorded pre-fix; post-fix both vers:gem/>=2.2.6.4|<3 and vers:gem/>=3.0.6.1.
  • actionmailbox GHSA-5957-c7qm-m469, patched ['~> 8.0.5, >= 8.0.5.2', '>= 8.1.4.1'] → only >=8.1.4.1 pre-fix.

The loss is partial: no advisory loses all its impacted packages, but fixed versions go missing from the stored data.

Fix

New helper parse_patched_version_range splits the spec on "," and intersects the parts (greatest lower bound, smallest upper bound, != exclusions kept, = handled; empty intersection → ValueError). It also catches InvalidRequirementError (an AttributeError, not a ValueError) and re-raises ValueError so the loop's existing except ValueError works; non-string/empty specs raise ValueError instead of uncaught AttributeError/TypeError.

unaffected_versions is untouched — open #2537 owns that side.

Tests

  • 15 passed in vulnerabilities/tests/pipelines/v2_importers/test_ruby_importer_v2.py (11 new + 4 pre-existing fixture tests). New tests fail pre-fix (advisory-level test verified on pristine main; helper tests fail at collection there since the helper does not exist).
  • Full live re-scan post-fix: all 1,571 files parse, 1,571 advisories, 2,972 affected packages; all 71 distinct compound specs now resolve.
  • black --check clean on both changed files.

Signed-off-by: Sanchit dev.sanchit955@gmail.com

ruby-advisory-db ships patched_versions entries such as
"~> 2.0, >= 2.2.6.4". GemVersionRange.from_native expands the
pessimistic requirement to >=2.0, <3 and appends >=2.2.6.4, so the
combined list holds two >= lower bounds and validate_comparators
rejects it; get_affected_packages logged the error and dropped the
entry, silently losing the fixed version for that release line
(159 entries in 99 real advisories, e.g. rack CVE-2023-27539 kept
only >=3.0.6.1 and lost the 2.x-line fix).

Parse the comma-separated parts and intersect them instead: keep
the greatest lower bound and the smallest upper bound, so the entry
above becomes >=2.2.6.4, <3. Specs that do not intersect, or that
cannot be parsed, still raise ValueError and are skipped as before.

Signed-off-by: Sanchit <dev.sanchit955@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ruby importer drops compound ~> patched_version specs, losing fixed versions

1 participant