From a07fe7d3b715a4f2f08a0bdffa694d33f39b9627 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Fri, 13 Sep 2024 16:59:26 -0400 Subject: [PATCH] add jvm version comparison Signed-off-by: Alex Goodman --- .gitignore | 4 +- cmd/grype/cli/commands/root.go | 4 + cmd/grype/cli/options/match.go | 2 + grype/internal/packagemetadata/names.go | 11 +- grype/match/matcher_type.go | 2 + grype/matcher/jvm/matcher.go | 51 ++++++++ grype/matcher/matchers.go | 3 + grype/matcher/stock/matcher.go | 8 ++ grype/pkg/java_metadata.go | 31 +++++ grype/pkg/package.go | 17 +++ grype/search/cpe.go | 17 ++- grype/version/constraint.go | 2 + grype/version/format.go | 59 +++++---- grype/version/format_test.go | 74 ++++++++++-- grype/version/jvm_constraint.go | 43 +++++++ grype/version/jvm_constraint_test.go | 70 +++++++++++ grype/version/jvm_version.go | 151 +++++++++++++++++++++++ grype/version/jvm_version_test.go | 152 ++++++++++++++++++++++++ grype/version/version.go | 9 +- internal/regex_helpers.go | 45 +++++++ internal/regex_helpers_test.go | 70 +++++++++++ 21 files changed, 779 insertions(+), 46 deletions(-) create mode 100644 grype/matcher/jvm/matcher.go create mode 100644 grype/version/jvm_constraint.go create mode 100644 grype/version/jvm_constraint_test.go create mode 100644 grype/version/jvm_version.go create mode 100644 grype/version/jvm_version_test.go create mode 100644 internal/regex_helpers.go create mode 100644 internal/regex_helpers_test.go diff --git a/.gitignore b/.gitignore index af1ef36b559..f1e1f81bd2c 100644 --- a/.gitignore +++ b/.gitignore @@ -18,8 +18,8 @@ bin/ /.task # changelog generation -CHANGELOG.md -VERSION +/CHANGELOG.md +/VERSION # IDE configuration .vscode/ diff --git a/cmd/grype/cli/commands/root.go b/cmd/grype/cli/commands/root.go index f23382db24a..1fe2f6db1c8 100644 --- a/cmd/grype/cli/commands/root.go +++ b/cmd/grype/cli/commands/root.go @@ -22,6 +22,7 @@ import ( "github.com/anchore/grype/grype/matcher/golang" "github.com/anchore/grype/grype/matcher/java" "github.com/anchore/grype/grype/matcher/javascript" + "github.com/anchore/grype/grype/matcher/jvm" "github.com/anchore/grype/grype/matcher/python" "github.com/anchore/grype/grype/matcher/ruby" "github.com/anchore/grype/grype/matcher/stock" @@ -285,6 +286,9 @@ func getMatchers(opts *options.Grype) []matcher.Matcher { ExternalSearchConfig: opts.ExternalSources.ToJavaMatcherConfig(), UseCPEs: opts.Match.Java.UseCPEs, }, + JVM: jvm.MatcherConfig{ + UseCPEs: opts.Match.JVM.UseCPEs, + }, Ruby: ruby.MatcherConfig(opts.Match.Ruby), Python: python.MatcherConfig(opts.Match.Python), Dotnet: dotnet.MatcherConfig(opts.Match.Dotnet), diff --git a/cmd/grype/cli/options/match.go b/cmd/grype/cli/options/match.go index f789a5b4689..b24df165b29 100644 --- a/cmd/grype/cli/options/match.go +++ b/cmd/grype/cli/options/match.go @@ -5,6 +5,7 @@ import "github.com/anchore/clio" // matchConfig contains all matching-related configuration options available to the user via the application config. type matchConfig struct { Java matcherConfig `yaml:"java" json:"java" mapstructure:"java"` // settings for the java matcher + JVM matcherConfig `yaml:"jvm" json:"jvm" mapstructure:"jvm"` // settings for the jvm matcher Dotnet matcherConfig `yaml:"dotnet" json:"dotnet" mapstructure:"dotnet"` // settings for the dotnet matcher Golang golangConfig `yaml:"golang" json:"golang" mapstructure:"golang"` // settings for the golang matcher Javascript matcherConfig `yaml:"javascript" json:"javascript" mapstructure:"javascript"` // settings for the javascript matcher @@ -43,6 +44,7 @@ func defaultMatchConfig() matchConfig { dontUseCpe := matcherConfig{UseCPEs: false} return matchConfig{ Java: dontUseCpe, + JVM: useCpe, Dotnet: dontUseCpe, Golang: defaultGolangConfig(), Javascript: dontUseCpe, diff --git a/grype/internal/packagemetadata/names.go b/grype/internal/packagemetadata/names.go index caf79c0465b..75e01a0da10 100644 --- a/grype/internal/packagemetadata/names.go +++ b/grype/internal/packagemetadata/names.go @@ -13,11 +13,12 @@ import ( // not the same it may be important to select different names. This design decision has been deferred, for now // the same metadata types that have been used in the past should be used here. var jsonNameFromType = map[reflect.Type][]string{ - reflect.TypeOf(pkg.ApkMetadata{}): nameList("ApkMetadata"), - reflect.TypeOf(pkg.GolangBinMetadata{}): nameList("GolangBinMetadata"), - reflect.TypeOf(pkg.GolangModMetadata{}): nameList("GolangModMetadata"), - reflect.TypeOf(pkg.JavaMetadata{}): nameList("JavaMetadata"), - reflect.TypeOf(pkg.RpmMetadata{}): nameList("RpmMetadata"), + reflect.TypeOf(pkg.ApkMetadata{}): nameList("ApkMetadata"), + reflect.TypeOf(pkg.GolangBinMetadata{}): nameList("GolangBinMetadata"), + reflect.TypeOf(pkg.GolangModMetadata{}): nameList("GolangModMetadata"), + reflect.TypeOf(pkg.JavaMetadata{}): nameList("JavaMetadata"), + reflect.TypeOf(pkg.RpmMetadata{}): nameList("RpmMetadata"), + reflect.TypeOf(pkg.JavaVMInstallationMetadata{}): nameList("JavaVMInstallationMetadata"), } //nolint:unparam diff --git a/grype/match/matcher_type.go b/grype/match/matcher_type.go index ad547c6d94c..29f6f9255de 100644 --- a/grype/match/matcher_type.go +++ b/grype/match/matcher_type.go @@ -16,6 +16,7 @@ const ( GoModuleMatcher MatcherType = "go-module-matcher" OpenVexMatcher MatcherType = "openvex-matcher" RustMatcher MatcherType = "rust-matcher" + JVMMatcher MatcherType = "jvm-matcher" ) var AllMatcherTypes = []MatcherType{ @@ -32,6 +33,7 @@ var AllMatcherTypes = []MatcherType{ GoModuleMatcher, OpenVexMatcher, RustMatcher, + JVMMatcher, } type MatcherType string diff --git a/grype/matcher/jvm/matcher.go b/grype/matcher/jvm/matcher.go new file mode 100644 index 00000000000..e26b1b39752 --- /dev/null +++ b/grype/matcher/jvm/matcher.go @@ -0,0 +1,51 @@ +package jvm + +import ( + "fmt" + + "github.com/anchore/grype/grype/distro" + "github.com/anchore/grype/grype/match" + "github.com/anchore/grype/grype/pkg" + "github.com/anchore/grype/grype/search" + "github.com/anchore/grype/grype/vulnerability" + syftPkg "github.com/anchore/syft/syft/pkg" +) + +type MatcherConfig struct { + UseCPEs bool +} + +type Matcher struct { + cfg MatcherConfig +} + +func NewJVMMatcher(cfg MatcherConfig) *Matcher { + return &Matcher{ + cfg: cfg, + } +} + +func (m *Matcher) PackageTypes() []syftPkg.Type { + return []syftPkg.Type{syftPkg.BinaryPkg} +} + +func (m *Matcher) Type() match.MatcherType { + return match.JVMMatcher +} + +func (m *Matcher) Match(store vulnerability.Provider, d *distro.Distro, p pkg.Package) ([]match.Match, error) { + if !pkg.IsJvmPackage(p) { + return nil, nil + } + + criteria := search.CommonCriteria + if m.cfg.UseCPEs { + criteria = append(criteria, search.ByCPE) + } + matches, err := search.ByCriteria(store, d, p, m.Type(), criteria...) + if err != nil { + return nil, fmt.Errorf("failed to match by exact package: %w", err) + } + + return matches, nil +} diff --git a/grype/matcher/matchers.go b/grype/matcher/matchers.go index 72778eb6292..8c538433b5b 100644 --- a/grype/matcher/matchers.go +++ b/grype/matcher/matchers.go @@ -7,6 +7,7 @@ import ( "github.com/anchore/grype/grype/matcher/golang" "github.com/anchore/grype/grype/matcher/java" "github.com/anchore/grype/grype/matcher/javascript" + "github.com/anchore/grype/grype/matcher/jvm" "github.com/anchore/grype/grype/matcher/msrc" "github.com/anchore/grype/grype/matcher/portage" "github.com/anchore/grype/grype/matcher/python" @@ -19,6 +20,7 @@ import ( // Config contains values used by individual matcher structs for advanced configuration type Config struct { Java java.MatcherConfig + JVM jvm.MatcherConfig Ruby ruby.MatcherConfig Python python.MatcherConfig Dotnet dotnet.MatcherConfig @@ -36,6 +38,7 @@ func NewDefaultMatchers(mc Config) []Matcher { dotnet.NewDotnetMatcher(mc.Dotnet), &rpm.Matcher{}, java.NewJavaMatcher(mc.Java), + jvm.NewJVMMatcher(mc.JVM), javascript.NewJavascriptMatcher(mc.Javascript), &apk.Matcher{}, golang.NewGolangMatcher(mc.Golang), diff --git a/grype/matcher/stock/matcher.go b/grype/matcher/stock/matcher.go index 7f30a52df9a..661f4b67604 100644 --- a/grype/matcher/stock/matcher.go +++ b/grype/matcher/stock/matcher.go @@ -32,9 +32,17 @@ func (m *Matcher) Type() match.MatcherType { } func (m *Matcher) Match(store vulnerability.Provider, d *distro.Distro, p pkg.Package) ([]match.Match, error) { + if !inboundsForMatcher(p) { + return nil, nil + } + criteria := search.CommonCriteria if m.cfg.UseCPEs { criteria = append(criteria, search.ByCPE) } return search.ByCriteria(store, d, p, m.Type(), criteria...) } + +func inboundsForMatcher(p pkg.Package) bool { + return !pkg.IsJvmPackage(p) +} diff --git a/grype/pkg/java_metadata.go b/grype/pkg/java_metadata.go index 24ba9371787..8b2b2ac08b8 100644 --- a/grype/pkg/java_metadata.go +++ b/grype/pkg/java_metadata.go @@ -1,5 +1,11 @@ package pkg +import ( + "strings" + + "github.com/anchore/syft/syft/pkg" +) + type JavaMetadata struct { VirtualPath string `json:"virtualPath"` PomArtifactID string `json:"pomArtifactID"` @@ -12,3 +18,28 @@ type Digest struct { Algorithm string `json:"algorithm"` Value string `json:"value"` } + +type JavaVMInstallationMetadata struct { + Release JavaVMReleaseMetadata `json:"release,omitempty"` +} + +type JavaVMReleaseMetadata struct { + JavaRuntimeVersion string `json:"javaRuntimeVersion,omitempty"` + JavaVersion string `json:"javaVersion,omitempty"` + FullVersion string `json:"fullVersion,omitempty"` + SemanticVersion string `json:"semanticVersion,omitempty"` +} + +func IsJvmPackage(p Package) bool { + if p.Type == pkg.BinaryPkg { + if strings.Contains(p.Name, "jdk") || strings.Contains(p.Name, "jre") || strings.Contains(p.Name, "java") { + return true + } + } + + if _, ok := p.Metadata.(JavaVMInstallationMetadata); ok { + return true + } + + return false +} diff --git a/grype/pkg/package.go b/grype/pkg/package.go index e5b6399ac88..89cbd9208d1 100644 --- a/grype/pkg/package.go +++ b/grype/pkg/package.go @@ -214,10 +214,27 @@ func dataFromPkg(p pkg.Package) (interface{}, []UpstreamPackage) { case pkg.ApkDBEntry: metadata = apkMetadataFromPkg(p) upstreams = apkDataFromPkg(p) + case pkg.JavaVMInstallation: + metadata = javaVMDataFromPkg(p) } return metadata, upstreams } +func javaVMDataFromPkg(p pkg.Package) any { + if value, ok := p.Metadata.(pkg.JavaVMInstallation); ok { + return JavaVMInstallationMetadata{ + Release: JavaVMReleaseMetadata{ + JavaRuntimeVersion: value.Release.JavaRuntimeVersion, + JavaVersion: value.Release.JavaVersion, + FullVersion: value.Release.FullVersion, + SemanticVersion: value.Release.SemanticVersion, + }, + } + } + + return nil +} + func apkMetadataFromPkg(p pkg.Package) interface{} { if m, ok := p.Metadata.(pkg.ApkDBEntry); ok { metadata := ApkMetadata{} diff --git a/grype/search/cpe.go b/grype/search/cpe.go index c6aaa2ea5b3..83ef8266639 100644 --- a/grype/search/cpe.go +++ b/grype/search/cpe.go @@ -105,7 +105,14 @@ func ByPackageCPE(store vulnerability.ProviderByCPE, d *distro.Distro, p pkg.Pac if searchVersion == wfn.NA || searchVersion == wfn.Any { searchVersion = p.Version } - verObj, err := version.NewVersion(searchVersion, version.FormatFromPkgType(p.Type)) + + format := version.FormatFromPkg(p) + + if format == version.JVMFormat { + searchVersion = specificLegacyJvmVersion(searchVersion, c.Attributes.Update) + } + + verObj, err := version.NewVersion(searchVersion, format) if err != nil { return nil, fmt.Errorf("matcher failed to parse version pkg=%q ver=%q: %w", p.Name, p.Version, err) } @@ -140,6 +147,14 @@ func ByPackageCPE(store vulnerability.ProviderByCPE, d *distro.Distro, p pkg.Pac return toMatches(matchesByFingerprint), nil } +func specificLegacyJvmVersion(searchVersion, updateCpeField string) string { + // we should take into consideration the CPE update field for JVM packages + if strings.HasPrefix(searchVersion, "1.") && !strings.Contains(searchVersion, "_") && updateCpeField != wfn.NA && updateCpeField != wfn.Any { + searchVersion = fmt.Sprintf("%s_%s", searchVersion, strings.TrimPrefix(updateCpeField, "update")) + } + return searchVersion +} + func addNewMatch(matchesByFingerprint map[match.Fingerprint]match.Match, vuln vulnerability.Vulnerability, p pkg.Package, searchVersion version.Version, upstreamMatcher match.MatcherType, searchedByCPE cpe.CPE) { candidateMatch := match.Match{ diff --git a/grype/version/constraint.go b/grype/version/constraint.go index ec463ae53ef..00a83ce56e0 100644 --- a/grype/version/constraint.go +++ b/grype/version/constraint.go @@ -29,6 +29,8 @@ func GetConstraint(constStr string, format Format) (Constraint, error) { return newKBConstraint(constStr) case PortageFormat: return newPortageConstraint(constStr) + case JVMFormat: + return newJvmConstraint(constStr) case UnknownFormat: return newFuzzyConstraint(constStr, "unknown") } diff --git a/grype/version/format.go b/grype/version/format.go index 9164456e75a..ab7f7de8c93 100644 --- a/grype/version/format.go +++ b/grype/version/format.go @@ -3,7 +3,8 @@ package version import ( "strings" - "github.com/anchore/syft/syft/pkg" + "github.com/anchore/grype/grype/pkg" + syftPkg "github.com/anchore/syft/syft/pkg" ) const ( @@ -18,6 +19,7 @@ const ( GemFormat PortageFormat GolangFormat + JVMFormat ) type Format int @@ -34,6 +36,7 @@ var formatStr = []string{ "Gem", "Portage", "Go", + "JVM", } var Formats = []Format{ @@ -46,6 +49,8 @@ var Formats = []Format{ KBFormat, GemFormat, PortageFormat, + GolangFormat, + JVMFormat, } func ParseFormat(userStr string) Format { @@ -70,35 +75,39 @@ func ParseFormat(userStr string) Format { return GemFormat case strings.ToLower(PortageFormat.String()), "portage": return PortageFormat + case strings.ToLower(JVMFormat.String()), "jvm", "jre", "jdk", "openjdk", "jep223": + return JVMFormat } return UnknownFormat } -func FormatFromPkgType(t pkg.Type) Format { - var format Format - switch t { - case pkg.ApkPkg: - format = ApkFormat - case pkg.DebPkg: - format = DebFormat - case pkg.JavaPkg: - format = MavenFormat - case pkg.RpmPkg: - format = RpmFormat - case pkg.GemPkg: - format = GemFormat - case pkg.PythonPkg: - format = PythonFormat - case pkg.KbPkg: - format = KBFormat - case pkg.PortagePkg: - format = PortageFormat - case pkg.GoModulePkg: - format = GolangFormat - default: - format = UnknownFormat +func FormatFromPkg(p pkg.Package) Format { + switch p.Type { + case syftPkg.ApkPkg: + return ApkFormat + case syftPkg.DebPkg: + return DebFormat + case syftPkg.JavaPkg: + return MavenFormat + case syftPkg.RpmPkg: + return RpmFormat + case syftPkg.GemPkg: + return GemFormat + case syftPkg.PythonPkg: + return PythonFormat + case syftPkg.KbPkg: + return KBFormat + case syftPkg.PortagePkg: + return PortageFormat + case syftPkg.GoModulePkg: + return GolangFormat + } + + if pkg.IsJvmPackage(p) { + return JVMFormat } - return format + + return UnknownFormat } func (f Format) String() string { diff --git a/grype/version/format_test.go b/grype/version/format_test.go index ecc5647ba64..5471ebaa159 100644 --- a/grype/version/format_test.go +++ b/grype/version/format_test.go @@ -4,7 +4,8 @@ import ( "fmt" "testing" - "github.com/anchore/syft/syft/pkg" + "github.com/anchore/grype/grype/pkg" + syftPkg "github.com/anchore/syft/syft/pkg" ) func TestParseFormat(t *testing.T) { @@ -51,29 +52,78 @@ func TestParseFormat(t *testing.T) { func TestFormatFromPkgType(t *testing.T) { tests := []struct { - pkgType pkg.Type - format Format + name string + p pkg.Package + format Format }{ { - pkgType: pkg.DebPkg, - format: DebFormat, + name: "deb", + p: pkg.Package{ + Type: syftPkg.DebPkg, + }, + format: DebFormat, + }, + { + name: "java jar", + p: pkg.Package{ + Type: syftPkg.JavaPkg, + }, + format: MavenFormat, + }, + { + name: "gem", + p: pkg.Package{ + Type: syftPkg.GemPkg, + }, + format: GemFormat, + }, + { + name: "jvm by metadata", + p: pkg.Package{ + Metadata: pkg.JavaVMInstallationMetadata{}, + }, + format: JVMFormat, + }, + { + name: "jvm by type and name (jdk)", + p: pkg.Package{ + Type: syftPkg.BinaryPkg, + Name: "jdk", + }, + format: JVMFormat, + }, + { + name: "jvm by type and name (openjdk)", + p: pkg.Package{ + Type: syftPkg.BinaryPkg, + Name: "openjdk", + }, + format: JVMFormat, }, { - pkgType: pkg.JavaPkg, - format: MavenFormat, + name: "jvm by type and name (jre)", + p: pkg.Package{ + Type: syftPkg.BinaryPkg, + Name: "jre", + }, + format: JVMFormat, }, { - pkgType: pkg.GemPkg, - format: GemFormat, + name: "jvm by type and name (java_se)", + p: pkg.Package{ + Type: syftPkg.BinaryPkg, + Name: "java_se", + }, + format: JVMFormat, }, } for _, test := range tests { - name := fmt.Sprintf("pkgType[%s]->format[%s]", test.pkgType, test.format) + name := fmt.Sprintf("pkgType[%s]->format[%s]", test.p.Type, test.format) t.Run(name, func(t *testing.T) { - actual := FormatFromPkgType(test.pkgType) + actual := FormatFromPkg(test.p) if actual != test.format { - t.Errorf("mismatched pkgType->format mapping, pkgType='%s': '%s'!='%s'", test.pkgType, test.format, actual) + t.Errorf("mismatched pkgType->format mapping, pkgType='%s': '%s'!='%s'", test.p.Type, test.format, actual) } }) } diff --git a/grype/version/jvm_constraint.go b/grype/version/jvm_constraint.go new file mode 100644 index 00000000000..bae6e06436e --- /dev/null +++ b/grype/version/jvm_constraint.go @@ -0,0 +1,43 @@ +package version + +import "fmt" + +var _ Constraint = (*jvmConstraint)(nil) + +type jvmConstraint struct { + raw string + expression constraintExpression +} + +func newJvmConstraint(raw string) (jvmConstraint, error) { + constraints, err := newConstraintExpression(raw, newJvmComparator) + if err != nil { + return jvmConstraint{}, err + } + return jvmConstraint{ + expression: constraints, + raw: raw, + }, nil +} + +func (g jvmConstraint) String() string { + if g.raw == "" { + return "none (jvm)" + } + return fmt.Sprintf("%s (jvm)", g.raw) +} + +func (g jvmConstraint) Satisfied(version *Version) (bool, error) { + if g.raw == "" { + return true, nil // the empty constraint is always satisfied + } + return g.expression.satisfied(version) +} + +func newJvmComparator(unit constraintUnit) (Comparator, error) { + ver, err := newJvmVersion(unit.version) + if err != nil { + return nil, fmt.Errorf("unable to parse constraint version (%s): %w", unit.version, err) + } + return ver, nil +} diff --git a/grype/version/jvm_constraint_test.go b/grype/version/jvm_constraint_test.go new file mode 100644 index 00000000000..b587662826c --- /dev/null +++ b/grype/version/jvm_constraint_test.go @@ -0,0 +1,70 @@ +package version + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestVersionConstraintJVM(t *testing.T) { + tests := []testCase{ + // pre jep 223 versions + {version: "1.7.0_80", constraint: "< 1.8.0", satisfied: true}, + {version: "1.8.0_131", constraint: "> 1.8.0", satisfied: true}, + {version: "1.8.0_131", constraint: "< 1.8.0_132", satisfied: true}, + {version: "1.8.0_131-b11", constraint: "< 1.8.0_132", satisfied: true}, + + {version: "1.7.0_80", constraint: "> 1.8.0", satisfied: false}, + {version: "1.8.0_131", constraint: "< 1.8.0", satisfied: false}, + {version: "1.8.0_131", constraint: "> 1.8.0_132", satisfied: false}, + {version: "1.8.0_131-b11", constraint: "> 1.8.0_132", satisfied: false}, + + {version: "1.7.0_80", constraint: "= 1.8.0", satisfied: false}, + {version: "1.8.0_131", constraint: "= 1.8.0", satisfied: false}, + {version: "1.8.0_131", constraint: "= 1.8.0_132", satisfied: false}, + {version: "1.8.0_131-b11", constraint: "= 1.8.0_132", satisfied: false}, + + {version: "1.8.0_80", constraint: "= 1.8.0_80", satisfied: true}, + {version: "1.8.0_131", constraint: ">= 1.8.0_131", satisfied: true}, + {version: "1.8.0_131", constraint: "= 1.8.0_131-b001", satisfied: true}, // builds should not matter + {version: "1.8.0_131-ea-b11", constraint: "= 1.8.0_131-ea", satisfied: true}, + + // jep 223 versions + {version: "8.0.4", constraint: "> 8.0.3", satisfied: true}, + {version: "8.0.4", constraint: "< 8.0.5", satisfied: true}, + {version: "9.0.0", constraint: "> 8.0.5", satisfied: true}, + {version: "9.0.0", constraint: "< 9.1.0", satisfied: true}, + {version: "11.0.4", constraint: "<= 11.0.4", satisfied: true}, + {version: "11.0.5", constraint: "> 11.0.4", satisfied: true}, + + {version: "8.0.4", constraint: "< 8.0.3", satisfied: false}, + {version: "8.0.4", constraint: "> 8.0.5", satisfied: false}, + {version: "9.0.0", constraint: "< 8.0.5", satisfied: false}, + {version: "9.0.0", constraint: "> 9.1.0", satisfied: false}, + {version: "11.0.4", constraint: "> 11.0.4", satisfied: false}, + {version: "11.0.5", constraint: "< 11.0.4", satisfied: false}, + + // mixed versions + {version: "1.8.0_131", constraint: "< 9.0.0", satisfied: true}, // 1.8.0_131 -> 8.0.131 + {version: "9.0.0", constraint: "> 1.8.0_131", satisfied: true}, // 1.8.0_131 -> 8.0.131 + {version: "1.8.0_131", constraint: "<= 8.0.131", satisfied: true}, + {version: "1.8.0_131", constraint: "> 7.0.79", satisfied: true}, + {version: "1.8.0_131", constraint: "= 8.0.131", satisfied: true}, + {version: "1.8.0_131", constraint: ">= 9.0.0", satisfied: false}, + {version: "9.0.1", constraint: "< 8.0.131", satisfied: false}, + + // pre-release versions + {version: "1.8.0_131-ea", constraint: "< 1.8.0_131", satisfied: true}, + {version: "1.8.0_131", constraint: "> 1.8.0_131-ea", satisfied: true}, + {version: "9.0.0-ea", constraint: "< 9.0.0", satisfied: true}, + {version: "9.0.0-ea", constraint: "> 1.8.0_131", satisfied: true}, + } + + for _, test := range tests { + t.Run(test.version+"_constraint_"+test.constraint, func(t *testing.T) { + constraint, err := newJvmConstraint(test.constraint) + require.NoError(t, err) + test.assertVersionConstraint(t, JVMFormat, constraint) + }) + } +} diff --git a/grype/version/jvm_version.go b/grype/version/jvm_version.go new file mode 100644 index 00000000000..f07a15d13e7 --- /dev/null +++ b/grype/version/jvm_version.go @@ -0,0 +1,151 @@ +package version + +import ( + "fmt" + "regexp" + "strings" + + hashiVer "github.com/anchore/go-version" + "github.com/anchore/grype/internal" + "github.com/anchore/grype/internal/log" +) + +var _ Comparator = (*jvmVersion)(nil) + +var ( + preJep223VersionPattern = regexp.MustCompile(`^1\.(?P\d+)(\.(?P\d+)([_-](update)?(_)?(?P\d+))?(-(?P[^b][^-]+))?(-b(?P\d+))?)?`) + nonCompliantSemverIsh = regexp.MustCompile(`^(?P\d+)(\.(?P\d+)(\.(?P\d+))?([_-](update)?(_)?(?P\d+))?(-(?P[^b][^-]+))?(-b(?P\d+))?)?`) +) + +type jvmVersion struct { + isPreJep223 bool + semVer *hashiVer.Version +} + +func newJvmVersion(raw string) (*jvmVersion, error) { + isPreJep233 := strings.HasPrefix(raw, "1.") + + if isPreJep233 { + // convert the pre-JEP 223 version to semver + raw = convertPreJep223Version(raw) + } else { + raw = convertNonCompliantSemver(raw) + } + verObj, err := hashiVer.NewVersion(raw) + if err != nil { + return nil, fmt.Errorf("unable to create semver obj for JVM version: %w", err) + } + + return &jvmVersion{ + isPreJep223: isPreJep233, + semVer: verObj, + }, nil +} + +func (v *jvmVersion) Compare(other *Version) (int, error) { + if other.Format == JVMFormat { + if other.rich.jvmVersion == nil { + return -1, fmt.Errorf("given empty jvmVersion object") + } + return other.rich.jvmVersion.compare(*v), nil + } + + if other.Format == SemanticFormat { + if other.rich.semVer == nil { + return -1, fmt.Errorf("given empty semVer object") + } + return other.rich.semVer.verObj.Compare(v.semVer), nil + } + + return -1, fmt.Errorf("unable to compare JVM to given format: %s", other.Format) +} + +func (v jvmVersion) compare(other jvmVersion) int { + return v.semVer.Compare(other.semVer) +} + +func convertNonCompliantSemver(version string) string { + // if there is -update as a prerelease, and the patch version is missing or 0, then we should parse the prerelease + // info that has the update value and extract the version. This should be used as the patch version. + + // 8.0-update302 --> 8.0.302 + // 8.0-update302-b08 --> 8.0.302+8 + // 8.0-update_302-b08 --> 8.0.302+8 + + matches := internal.MatchNamedCaptureGroups(nonCompliantSemverIsh, version) + if len(matches) == 0 { + log.WithFields("version", version).Trace("unable to convert pre-JEP 223 JVM version") + return version + } + + // extract relevant parts from the matches + majorVersion := trim0sFromLeft(matches["major"]) + minorVersion := trim0sFromLeft(matches["minor"]) + patchVersion := trim0sFromLeft(matches["patch"]) + update := trim0sFromLeft(matches["update"]) + preRelease := trim0sFromLeft(matches["prerelease"]) + build := trim0sFromLeft(matches["build"]) + + if (patchVersion == "" || patchVersion == "0") && update != "" { + patchVersion = update + } + + return buildSemVer(majorVersion, minorVersion, patchVersion, preRelease, build) +} + +func convertPreJep223Version(version string) string { + // convert the following pre JEP 223 version strings to semvers + // 1.8.0_302-b08 --> 8.0.302+8 + // 1.9.0-ea-b19 --> 9.0.0-ea+19 + // NOTE: this makes an assumption that the old update field is the patch version in semver... + // this is NOT strictly in the spec, but for 1.8 this tends to be true (especially for temurin-based builds) + version = strings.TrimSpace(version) + + matches := internal.MatchNamedCaptureGroups(preJep223VersionPattern, version) + if len(matches) == 0 { + log.WithFields("version", version).Trace("unable to convert pre-JEP 223 JVM version") + return version + } + + // extract relevant parts from the matches + majorVersion := trim0sFromLeft(matches["major"]) + minorVersion := trim0sFromLeft(matches["minor"]) + patchVersion := trim0sFromLeft(matches["patch"]) + preRelease := trim0sFromLeft(matches["prerelease"]) + build := trim0sFromLeft(matches["build"]) + + if patchVersion == "" { + patchVersion = "0" + } + + return buildSemVer(majorVersion, minorVersion, patchVersion, preRelease, build) +} +func buildSemVer(majorVersion, minorVersion, patchVersion, preRelease, build string) string { + if minorVersion == "" { + minorVersion = "0" + } + + segs := []string{majorVersion, minorVersion} + if patchVersion != "" { + segs = append(segs, patchVersion) + } + + var semver strings.Builder + semver.WriteString(strings.Join(segs, ".")) + + if preRelease != "" { + semver.WriteString(fmt.Sprintf("-%s", preRelease)) + } + if build != "" { + semver.WriteString(fmt.Sprintf("+%s", build)) + } + + return semver.String() +} + +func trim0sFromLeft(v string) string { + if v == "0" { + return v + } + return strings.TrimLeft(v, "0") +} diff --git a/grype/version/jvm_version_test.go b/grype/version/jvm_version_test.go new file mode 100644 index 00000000000..26b3a94c885 --- /dev/null +++ b/grype/version/jvm_version_test.go @@ -0,0 +1,152 @@ +package version + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestVersionJVM(t *testing.T) { + tests := []struct { + v1 string + v2 string + expected int + }{ + // pre jep223 versions + {"1.8", "1.8.0", 0}, + {"1.8.0", "1.8.0_0", 0}, + {"1.8.0", "1.8.0", 0}, + {"1.7.0", "1.8.0", -1}, + {"1.8.0_131", "1.8.0_131", 0}, + {"1.8.0_131", "1.8.0_132", -1}, + + // builds should not matter + {"1.8.0_131", "1.8.0_130", 1}, + {"1.8.0_131", "1.8.0_132-b11", -1}, + {"1.8.0_131-b11", "1.8.0_132-b11", -1}, + {"1.8.0_131-b11", "1.8.0_131-b12", 0}, + {"1.8.0_131-b11", "1.8.0_131-b10", 0}, + {"1.8.0_131-b11", "1.8.0_131", 0}, + {"1.8.0_131-b11", "1.8.0_131-b11", 0}, + + // jep223 versions (semver) + {"8.0.4", "8.0.4", 0}, + {"8.0.4", "8.0.5", -1}, + {"8.0.4", "8.0.3", 1}, + {"8.0.4", "8.0.4+b1", 0}, + + // mix comparison + {"1.8.0_131", "8.0.4", 1}, // 1.8.0_131 --> 8.0.131 + {"8.0.4", "1.8.0_131", -1}, // doesn't matter which side the comparison is on + {"1.8.0_131-b002", "8.0.131+b2", 0}, // builds should not matter + {"1.8.0_131-b002", "8.0.131+b1", 0}, // builds should not matter + {"1.6.0", "8.0.1", -1}, // 1.6.0 --> 6.0.0 + + // prerelease + {"1.8.0_13-ea-b002", "1.8.0_13-ea-b001", 0}, + {"1.8.0_13-ea", "1.8.0_13-ea-b001", 0}, + {"1.8.0_13-ea-b002", "8.0.13-ea+b2", 0}, + {"1.8.0_13-ea-b002", "8.0.13+b2", -1}, + {"1.8.0_13-b002", "8.0.13-ea+b2", 1}, + + // pre 1.8 (when the jep 223 was introduced) + {"1.7.0", "7.0.0", 0}, // there is no v7 of the JVM, but we want to honor this comparison since it may be someone mistakenly using the wrong version format + + // invalid but we should work with these + {"1.8.0_131", "1.8.0-update131-b02", 0}, + {"1.8.0_131", "1.8.0-update_131-b02", 0}, + } + + for _, test := range tests { + name := test.v1 + "_vs_" + test.v2 + t.Run(name, func(t *testing.T) { + v1, err := newJvmVersion(test.v1) + require.NotNil(t, v1) + require.NoError(t, err) + + v2, err := newJvmVersion(test.v2) + require.NotNil(t, v2) + require.NoError(t, err) + + actual := v1.compare(*v2) + assert.Equal(t, test.expected, actual) + }) + } +} + +func TestConvertNonCompliantSemver(t *testing.T) { + tests := []struct { + name string + input string + expected string + }{ + { + name: "simple update", + input: "8.0-update302", + expected: "8.0.302", + }, + { + name: "update with build", + input: "8.0-update302-b08", + expected: "8.0.302+8", + }, + { + name: "update with underscore and build", + input: "8.0-update_302-b08", + expected: "8.0.302+8", + }, + { + name: "version without patch and prerelease", + input: "8.0.0", + expected: "8.0.0", + }, + { + name: "version with patch, no update", + input: "8.0.100", + expected: "8.0.100", + }, + { + name: "version with patch and prerelease", + input: "8.0.0-rc1", + expected: "8.0.0-rc1", + }, + { + name: "invalid update format, no update keyword", + input: "8.0-foo302", + expected: "8.0-foo302", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := convertNonCompliantSemver(tt.input) + assert.Equal(t, tt.expected, result) + }) + } +} + +func TestVersionJVM_invalid(t *testing.T) { + tests := []struct { + name string + version string + wantErr require.ErrorAssertionFunc + }{ + { + name: "invalid version", + version: "1.a", + wantErr: require.Error, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.wantErr == nil { + tt.wantErr = require.NoError + } + v, err := newJvmVersion(tt.version) + assert.Nil(t, v) + tt.wantErr(t, err) + }) + } +} diff --git a/grype/version/version.go b/grype/version/version.go index 3819ecb22d8..6173ccf678d 100644 --- a/grype/version/version.go +++ b/grype/version/version.go @@ -28,6 +28,7 @@ type rich struct { kbVer *kbVersion portVer *portageVersion pep440version *pep440Version + jvmVersion *jvmVersion } func NewVersion(raw string, format Format) (*Version, error) { @@ -45,7 +46,9 @@ func NewVersion(raw string, format Format) (*Version, error) { } func NewVersionFromPkg(p pkg.Package) (*Version, error) { - ver, err := NewVersion(p.Version, FormatFromPkgType(p.Type)) + format := FormatFromPkg(p) + + ver, err := NewVersion(p.Version, format) if err != nil { return nil, err } @@ -96,6 +99,10 @@ func (v *Version) populate() error { ver := newPortageVersion(v.Raw) v.rich.portVer = &ver return nil + case JVMFormat: + ver, err := newJvmVersion(v.Raw) + v.rich.jvmVersion = ver + return err case UnknownFormat: // use the raw string + fuzzy constraint return nil diff --git a/internal/regex_helpers.go b/internal/regex_helpers.go new file mode 100644 index 00000000000..7130f21a89c --- /dev/null +++ b/internal/regex_helpers.go @@ -0,0 +1,45 @@ +package internal + +import "regexp" + +// MatchNamedCaptureGroups takes a regular expression and string and returns all of the named capture group results in a map. +// This is only for the first match in the regex. Callers shouldn't be providing regexes with multiple capture groups with the same name. +func MatchNamedCaptureGroups(regEx *regexp.Regexp, content string) map[string]string { + // note: we are looking across all matches and stopping on the first non-empty match. Why? Take the following example: + // input: "cool something to match against" pattern: `((?Pmatch) (?Pagainst))?`. Since the pattern is + // encapsulated in an optional capture group, there will be results for each character, but the results will match + // on nothing. The only "true" match will be at the end ("match against"). + allMatches := regEx.FindAllStringSubmatch(content, -1) + var results map[string]string + for _, match := range allMatches { + // fill a candidate results map with named capture group results, accepting empty values, but not groups with + // no names + for nameIdx, name := range regEx.SubexpNames() { + if nameIdx > len(match) || len(name) == 0 { + continue + } + if results == nil { + results = make(map[string]string) + } + results[name] = match[nameIdx] + } + // note: since we are looking for the first best potential match we should stop when we find the first one + // with non-empty results. + if !isEmptyMap(results) { + break + } + } + return results +} + +func isEmptyMap(m map[string]string) bool { + if len(m) == 0 { + return true + } + for _, value := range m { + if value != "" { + return false + } + } + return true +} diff --git a/internal/regex_helpers_test.go b/internal/regex_helpers_test.go new file mode 100644 index 00000000000..1c483775309 --- /dev/null +++ b/internal/regex_helpers_test.go @@ -0,0 +1,70 @@ +package internal + +import ( + "regexp" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMatchCaptureGroups(t *testing.T) { + tests := []struct { + name string + input string + pattern string + expected map[string]string + }{ + { + name: "go-case", + input: "match this thing", + pattern: `(?Pmatch).*(?Pthing)`, + expected: map[string]string{ + "name": "match", + "version": "thing", + }, + }, + { + name: "only matches the first instance", + input: "match this thing batch another think", + pattern: `(?P[mb]atch).*?(?Pthin[gk])`, + expected: map[string]string{ + "name": "match", + "version": "thing", + }, + }, + { + name: "nested capture groups", + input: "cool something to match against", + pattern: `((?Pmatch) (?Pagainst))`, + expected: map[string]string{ + "name": "match", + "version": "against", + }, + }, + { + name: "nested optional capture groups", + input: "cool something to match against", + pattern: `((?Pmatch) (?Pagainst))?`, + expected: map[string]string{ + "name": "match", + "version": "against", + }, + }, + { + name: "nested optional capture groups with larger match", + input: "cool something to match against match never", + pattern: `.*?((?Pmatch) (?P(against|never)))?`, + expected: map[string]string{ + "name": "match", + "version": "against", + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actual := MatchNamedCaptureGroups(regexp.MustCompile(test.pattern), test.input) + assert.Equal(t, test.expected, actual) + }) + } +}