Repository navigation
Cyclone dx java - matching #2057
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Package java matches Java packages recorded under a Red Hat product CPE. | ||
| package java | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/internal/maven" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| "github.com/quay/claircore/rhel" | ||
| "github.com/quay/claircore/toolkit/types/cpe" | ||
| ) | ||
|
|
||
| // RedHatMatcher matches Java packages recorded under a Red Hat product CPE. | ||
| type RedHatMatcher struct{} | ||
|
|
||
| var _ driver.Matcher = (*RedHatMatcher)(nil) | ||
|
|
||
| // Name implements [driver.Matcher]. | ||
| func (*RedHatMatcher) Name() string { return "redhat-java" } | ||
|
|
||
| // Filter implements [driver.Matcher]. | ||
| func (*RedHatMatcher) Filter(record *claircore.IndexRecord) bool { | ||
| return record.Repository != nil && record.Repository.Key == RepositoryKey | ||
| } | ||
|
|
||
| // Query implements [driver.Matcher]. | ||
| func (*RedHatMatcher) Query() []driver.MatchConstraint { | ||
| return []driver.MatchConstraint{driver.RepositoryKey} | ||
| } | ||
|
|
||
| // Vulnerable implements [driver.Matcher]. | ||
| // | ||
| // The vulnerability repository name is a normalized CPE. It must match | ||
| // the indexed CPE, including the Red Hat version-stream rule. An empty | ||
| // FixedInVersion matches any installed version. An empty installed version | ||
| // is not compared when a fix exists: a Maven null version is older than | ||
| // every real fix. Otherwise the installed Maven version is vulnerable when | ||
| // it is strictly older. | ||
| func (*RedHatMatcher) Vulnerable(ctx context.Context, record *claircore.IndexRecord, vuln *claircore.Vulnerability) (bool, error) { | ||
| if vuln.Repo == nil || record.Repository == nil || vuln.Repo.Key != RepositoryKey { | ||
| return false, nil | ||
| } | ||
| var err error | ||
| // Vulnerability repositories do not persist CPE. The name is the normalized form. | ||
| vuln.Repo.CPE, err = cpe.Unbind(vuln.Repo.Name) | ||
| if err != nil { | ||
| slog.WarnContext(ctx, "unable to unbind repo CPE", "reason", err, "vulnerability name", vuln.Name) | ||
| return false, nil | ||
| } | ||
| if !rhel.IsCPESubstringMatch(record.Repository.CPE, vuln.Repo.CPE) { | ||
| return false, nil | ||
| } | ||
| if vuln.FixedInVersion == "" { | ||
| return true, nil | ||
| } | ||
| // An empty version parses as a Maven null version, which compares older | ||
| // than every real fix. There is no installed version to compare. | ||
| if record.Package == nil || record.Package.Version == "" { | ||
| slog.WarnContext(ctx, "empty package version", "vulnerability name", vuln.Name) | ||
| return false, nil | ||
| } | ||
| installed, err := maven.ParseVersion(record.Package.Version) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| fixed, err := maven.ParseVersion(vuln.FixedInVersion) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return installed.Compare(fixed) < 0, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package java | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| "github.com/quay/claircore/test" | ||
| "github.com/quay/claircore/toolkit/types/cpe" | ||
| ) | ||
|
|
||
| func TestRedHatMatcherVulnerable(t *testing.T) { | ||
| ctx := test.Logging(t) | ||
| m := &RedHatMatcher{} | ||
| quarkus := cpe.MustUnbind("cpe:/a:redhat:quarkus:3.33") | ||
| quarkusEl := cpe.MustUnbind("cpe:/a:redhat:quarkus:3.33::el8") | ||
| quarkusMajor := cpe.MustUnbind("cpe:/a:redhat:quarkus:3") | ||
| eap := cpe.MustUnbind("cpe:/a:redhat:jboss_enterprise_application_platform:8.1") | ||
| eapMajor := cpe.MustUnbind("cpe:/a:redhat:jboss_enterprise_application_platform:8") | ||
|
|
||
| record := func(version string, w cpe.WFN) *claircore.IndexRecord { | ||
| return &claircore.IndexRecord{ | ||
| Package: &claircore.Package{Name: "io.netty:netty-codec", Version: version}, | ||
| Repository: &claircore.Repository{ | ||
| Name: w.String(), | ||
| Key: RepositoryKey, | ||
| CPE: w, | ||
| }, | ||
| } | ||
| } | ||
| vuln := func(fixed string, name string) *claircore.Vulnerability { | ||
| return &claircore.Vulnerability{ | ||
| Name: "CVE-2026-59889", | ||
| FixedInVersion: fixed, | ||
| Repo: &claircore.Repository{ | ||
| Name: name, | ||
| Key: RepositoryKey, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| record *claircore.IndexRecord | ||
| vuln *claircore.Vulnerability | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "older than fixed", | ||
| record: record("4.1.100.Final-redhat-00001", quarkus), | ||
| vuln: vuln("4.1.115.Final-redhat-00002", quarkus.String()), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "equal to fixed", | ||
| record: record("4.1.115.Final-redhat-00002", quarkus), | ||
| vuln: vuln("4.1.115.Final-redhat-00002", quarkus.String()), | ||
| }, | ||
| { | ||
| name: "newer than fixed", | ||
| record: record("4.1.120.Final-redhat-00001", quarkus), | ||
| vuln: vuln("4.1.115.Final-redhat-00002", quarkus.String()), | ||
| }, | ||
| { | ||
| name: "unversioned known_affected", | ||
| record: record("9.9.9", eap), | ||
| vuln: vuln("", eapMajor.String()), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "major CPE pattern", | ||
| record: record("1.0", quarkus), | ||
| vuln: vuln("", quarkusMajor.String()), | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "edition makes the advisory narrower", | ||
| record: record("1.0", quarkus), | ||
| vuln: vuln("", quarkusEl.String()), | ||
| }, | ||
| { | ||
| name: "different product", | ||
| record: record("1.0", quarkus), | ||
| vuln: vuln("", eapMajor.String()), | ||
| }, | ||
| { | ||
| name: "empty package version", | ||
| record: record("", quarkus), | ||
| vuln: vuln("4.1.115.Final-redhat-00002", quarkus.String()), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := m.Vulnerable(ctx, tt.record, tt.vuln) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tt.want { | ||
| t.Fatalf("Vulnerable() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if !m.Filter(record("1.0", quarkus)) { | ||
| t.Fatal("product CPE record was filtered out") | ||
| } | ||
| central := &claircore.IndexRecord{Repository: &claircore.Repository{Name: "maven"}} | ||
| if m.Filter(central) { | ||
| t.Fatal("Maven Central record was accepted") | ||
| } | ||
| q := m.Query() | ||
| if len(q) != 1 || q[0] != driver.RepositoryKey { | ||
| t.Fatalf("Query() = %v", q) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package java | ||
|
|
||
| // RepositoryKey is the repository key for a Red Hat product CPE attributed | ||
| // to a Java component. It is distinct from the Maven Central repository and | ||
| // from the RPM CPE repository. | ||
| const RepositoryKey = "redhat-java-cpe-repository" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may be possible to have an empty (
"")record.Package.VersionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will check and return not vulnerable if the
record.Package.Versionis emptyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/quay/claircore/compare/c2f47eaca59973db4543d7b01d3ac6bc003d2902..868672d32b3f4c967ee1d4d71dae6637107910e6