Skip to content

ruby_importer_v2 splits multi-entry unaffected_versions into separate ImpactedPackage rows whose union marks unaffected gem versions as affected #2536

Backlog

Description

@trackoor

When a rubysec/ruby-advisory-db YAML advisory has two or more entries in unaffected_versions (28 advisories in the upstream database, such as gems/activerecord/CVE-2016-6317.yml, gems/activestorage/CVE-2024-26144.yml, and gems/rest-client/CVE-2019-15224.yml), ruby_importer_v2 emits a separate AffectedPackageV2 for the inverse of each entry instead of intersecting them into one GemVersionRange, so unfurl_version_range_v2 takes their union and marks every published gem release on RubyGems.org as affected.

Reproduction

From a migrated vulnerablecode checkout (using gems/activerecord/CVE-2016-6317.yml, which has unaffected_versions: ["< 4.2.0", ">= 5.0.0"] and patched_versions: [">= 4.2.7.1"]; running python manage.py import ruby_importer_v2 against the full upstream repo produces the same ImpactedPackage rows):

mkdir -p /tmp/ruby-adv/gems/activerecord
curl -fsSL https://raw.githubusercontent.com/rubysec/ruby-advisory-db/master/gems/activerecord/CVE-2016-6317.yml \
  -o /tmp/ruby-adv/gems/activerecord/CVE-2016-6317.yml
git -C /tmp/ruby-adv init -q && git -C /tmp/ruby-adv add . && git -C /tmp/ruby-adv -c user.name=t -c user.email=t@e commit -qm init

python manage.py shell -c '
from vulnerabilities.pipelines.v2_importers.ruby_importer import RubyImporterPipeline
class SingleAdvisoryRubyImporter(RubyImporterPipeline):
    repo_url = "git+file:///tmp/ruby-adv"
SingleAdvisoryRubyImporter().execute()
'
python manage.py improve unfurl_version_range_v2
python manage.py improve mark_unfurl_version_range_v2
python manage.py shell -c '
from rest_framework.test import APIClient
from vulnerabilities.models import ImpactedPackage
print("impacts:", list(ImpactedPackage.objects.values_list("base_purl", "affecting_vers", "fixed_vers")))
resp = APIClient(HTTP_HOST="localhost").post(
    "/api/v3/packages",
    {"purls": ["pkg:gem/activerecord@4.1.16", "pkg:gem/activerecord@4.2.7", "pkg:gem/activerecord@5.0.0", "pkg:gem/activerecord@7.1.3"], "details": True},
    format="json",
    HTTP_USER_AGENT="VCIO_API_AGENT",
)
for r in resp.json()["results"]:
    print(r["purl"], [a["advisory_id"] for a in r["affected_by_vulnerabilities"]])
'
rm -rf /tmp/ruby-adv

Actual output

impacts: [('pkg:gem/activerecord', 'vers:gem/>=4.2.0', None), ('pkg:gem/activerecord', 'vers:gem/<5.0.0', None), ('pkg:gem/activerecord', None, 'vers:gem/>=4.2.7.1')]
pkg:gem/activerecord@4.1.16 ['CVE-2016-6317']
pkg:gem/activerecord@4.2.7 ['CVE-2016-6317']
pkg:gem/activerecord@5.0.0 ['CVE-2016-6317']
pkg:gem/activerecord@7.1.3 ['CVE-2016-6317']

All 561 published activerecord releases on RubyGems.org satisfy either >=4.2.0 or <5.0.0, so 4.1.16, 5.0.0, and 7.1.3 are all reported as affected by CVE-2016-6317.

Expected output

Releases matching unaffected_versions (< 4.2.0 or >= 5.0.0) such as pkg:gem/activerecord@4.1.16, pkg:gem/activerecord@5.0.0, and pkg:gem/activerecord@7.1.3 should not include CVE-2016-6317 in affected_by_vulnerabilities (the inverse of < 4.2.0 union >= 5.0.0 is the single range vers:gem/>=4.2.0|<5.0.0).

Versions

  • vulnerablecode: v40.2.0 (910df628cf485e8d58c61b0a7c8c1a64b0964a14) and main (9a9c9b3e8ff7d173bf8d2d04b4587c445a6d96b3)
  • univers: 32.0.1
  • Python 3.12.16 on Linux x86_64

Root cause

In vulnerabilities/pipelines/v2_importers/ruby_importer.py (get_affected_packages, lines 164-176), the loop over unaffected_versions calls .invert() on each entry individually and appends a separate AffectedPackageV2 per entry, which insert_advisory_v2 stores as separate disjunctive ImpactedPackage rows instead of combining the inverted constraints into a single GemVersionRange.

Found with a differential-testing tool; the report was drafted with AI assistance and reviewed and edited by me.

Credit: https://ucsbplse.github.io/

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions