Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge indirect matches with direct matches #2241

Merged
merged 2 commits into from
Nov 7, 2024
Merged

Merge indirect matches with direct matches #2241

merged 2 commits into from
Nov 7, 2024

Conversation

wagoodman
Copy link
Contributor

@wagoodman wagoodman commented Nov 6, 2024

Today if a vulnerability is matched for an artifact both directly and indirectly (using a source/origin package instead of the downstream/original package) then we raise up both matches separately:

$ grype 'pkg:rpm/ol/perl-Errno@1.28-417.el8_3?arch=x86_64&epoch=0&upstream=perl-5.26.3-417.el8_3.src.rpm&distro=ol-8.3' -q -o json | jq '.matches[] | select(.artifact.name == "perl-Errno" and .vulnerability.id == "ELSA-2021-1678") | .matchDetails'
[
  {
    "type": "exact-direct-match",
    "matcher": "rpm-matcher",
    "searchedBy": {
      "distro": {
        "type": "oraclelinux",
        "version": "8.3"
      },
      "namespace": "oracle:distro:oraclelinux:8",
      "package": {
        "name": "perl-Errno",
        "version": "0:1.28-417.el8_3"
      }
    },
    "found": {
      "versionConstraint": "< 0:1.28-419.el8 (rpm)",
      "vulnerabilityID": "ELSA-2021-1678"
    }
  }
]
[
  {
    "type": "exact-indirect-match",
    "matcher": "rpm-matcher",
    "searchedBy": {
      "distro": {
        "type": "oraclelinux",
        "version": "8.3"
      },
      "namespace": "oracle:distro:oraclelinux:8",
      "package": {
        "name": "perl",
        "version": "5.26.3-417.el8_3"
      }
    },
    "found": {
      "versionConstraint": "< 4:5.26.3-419.el8 (rpm)",
      "vulnerabilityID": "ELSA-2021-1678"
    }
  }
]

This PR changes this behavior such that any direct match for a package-vuln pairing and similar indirect match for that same package-vuln are merged into a single match, preserving the indirect .matchDetails on the direct match (running the same command):

[
  {
    "type": "exact-direct-match",
    "matcher": "rpm-matcher",
    "searchedBy": {
      "distro": {
        "type": "oraclelinux",
        "version": "8.3"
      },
      "namespace": "oracle:distro:oraclelinux:8",
      "package": {
        "name": "perl-Errno",
        "version": "0:1.28-417.el8_3"
      }
    },
    "found": {
      "versionConstraint": "< 0:1.28-419.el8 (rpm)",
      "vulnerabilityID": "ELSA-2021-1678"
    }
  },
  {
    "type": "exact-indirect-match",
    "matcher": "rpm-matcher",
    "searchedBy": {
      "distro": {
        "type": "oraclelinux",
        "version": "8.3"
      },
      "namespace": "oracle:distro:oraclelinux:8",
      "package": {
        "name": "perl",
        "version": "5.26.3-417.el8_3"
      }
    },
    "found": {
      "versionConstraint": "< 4:5.26.3-419.el8 (rpm)",
      "vulnerabilityID": "ELSA-2021-1678"
    }
  }
]

(note the same match details but in a single array entry instead of two)

Along the way additional tests were added to existing code that was previously untested (and some code, such as sorting, was either tweaked or refactored for either additional functionality or to satisfy linting rules).

Fixes #1931

@wagoodman wagoodman requested a review from a team November 6, 2024 02:42
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Copy link
Contributor

@kzantow kzantow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

grype/match/details.go Outdated Show resolved Hide resolved
test/integration/match_by_image_test.go Outdated Show resolved Hide resolved
@wagoodman wagoodman enabled auto-merge (squash) November 7, 2024 15:29
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman wagoodman merged commit 787aae1 into main Nov 7, 2024
10 checks passed
@wagoodman wagoodman deleted the grype-1931 branch November 7, 2024 16:22
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.

Prefer direct match information over indirect matches
2 participants