From 2d8cc94c024d94a6afe4329cf2050e0a14c39fef Mon Sep 17 00:00:00 2001 From: Yuriy Tumanov <57435526+Eljees@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:11:54 +0100 Subject: [PATCH] fix(java): preserve Eclipse Classpath license expressions (#5080) * fix(java): preserve Eclipse Classpath license expressions Signed-off-by: Eljees * chore: use tagged switch for quote handling in splitUnquoted Signed-off-by: Eljees <3.14hell@gmail.com> * Fix unquoted Apache license names in Java manifests Recognize the FreeMarker Bundle-License name as Apache-2.0 while preserving neighboring license clauses and parameters. Assisted-by: OpenAI Codex Signed-off-by: Eljees <3.14hell@gmail.com> --------- Signed-off-by: Eljees Signed-off-by: Eljees <3.14hell@gmail.com> --- internal/spdxlicense/license.go | 12 ++ internal/spdxlicense/license_url_test.go | 18 +++ syft/pkg/cataloger/java/archive_parser.go | 120 +++++++++++++++++- .../pkg/cataloger/java/archive_parser_test.go | 77 +++++++++++ .../pkg/cataloger/java/parse_java_manifest.go | 60 ++++++++- .../java/parse_java_manifest_test.go | 89 +++++++++++++ 6 files changed, 372 insertions(+), 4 deletions(-) diff --git a/internal/spdxlicense/license.go b/internal/spdxlicense/license.go index d23491e5c..792b1eed7 100644 --- a/internal/spdxlicense/license.go +++ b/internal/spdxlicense/license.go @@ -14,8 +14,15 @@ import ( const ( LicenseRefPrefix = "LicenseRef-" // prefix for non-standard licenses + epl2LicenseID = "EPL-2.0" ) +var licenseExpressionsByURL = map[string]string{ + "projects.eclipse.org/license/secondary-gpl-2.0-cp": "GPL-2.0-only WITH Classpath-exception-2.0", + "www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt": epl2LicenseID, + "www.gnu.org/software/classpath/license.html": "GPL-2.0-only WITH Classpath-exception-2.0", +} + //go:generate go run ./generate // ID returns the canonical license ID for the given license ID @@ -53,6 +60,11 @@ func LicenseByURL(url string) (LicenseInfo, bool) { ID: id, }, true } + if expression, exists := licenseExpressionsByURL[url]; exists { + return LicenseInfo{ + ID: expression, + }, true + } if normalized, ok := normalizeOpenSourceOrgURL(url); ok { if id, exists := openSourceOrgURLToLicense[normalized]; exists { return LicenseInfo{ diff --git a/internal/spdxlicense/license_url_test.go b/internal/spdxlicense/license_url_test.go index 490976de3..fcc9585fa 100644 --- a/internal/spdxlicense/license_url_test.go +++ b/internal/spdxlicense/license_url_test.go @@ -41,6 +41,24 @@ func TestLicenseByURL(t *testing.T) { wantID: "BSD-3-Clause", wantFound: true, }, + { + name: "GNU Classpath Exception URL", + url: "https://www.gnu.org/software/classpath/license.html", + wantID: "GPL-2.0-only WITH Classpath-exception-2.0", + wantFound: true, + }, + { + name: "Eclipse secondary GPL Classpath URL", + url: "https://projects.eclipse.org/license/secondary-gpl-2.0-cp", + wantID: "GPL-2.0-only WITH Classpath-exception-2.0", + wantFound: true, + }, + { + name: "Eclipse Public License legacy URL", + url: "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt", + wantID: "EPL-2.0", + wantFound: true, + }, { name: "URL with trailing whitespace", url: " http://opensource.org/licenses/MIT ", diff --git a/syft/pkg/cataloger/java/archive_parser.go b/syft/pkg/cataloger/java/archive_parser.go index 6827bda66..280b3632b 100644 --- a/syft/pkg/cataloger/java/archive_parser.go +++ b/syft/pkg/cataloger/java/archive_parser.go @@ -20,6 +20,7 @@ import ( "github.com/anchore/syft/internal/archive" intFile "github.com/anchore/syft/internal/file" "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/internal/spdxlicense" "github.com/anchore/syft/internal/tmpdir" "github.com/anchore/syft/internal/unknown" "github.com/anchore/syft/syft/artifact" @@ -53,6 +54,8 @@ var archiveFormatGlobs = []string{ "**/*.zap", // ZAP add-ons https://github.com/zaproxy/zaproxy/wiki/ZapAddOns } +const eclipseClasspathDualLicense = "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0" + // javaArchiveHashes are all the current hash algorithms used to calculate archive digests var javaArchiveHashes = []crypto.Hash{ crypto.SHA1, @@ -320,7 +323,7 @@ func (j *archiveParser) discoverMainPackage(ctx context.Context) (*pkg.Package, func (j *archiveParser) discoverNameVersionLicense(ctx context.Context, manifest *pkg.JavaManifest) (string, string, []pkg.License, *parsedPomProject) { // we use j.location because we want to associate the license declaration with where we discovered the contents in the manifest // TODO: when we support locations of paths within archives we should start passing the specific manifest location object instead of the top jar - lics := pkg.NewLicensesFromLocationWithContext(ctx, j.location, selectLicenses(manifest)...) + lics := combineEclipseClasspathAlternatives(pkg.NewLicensesFromLocationWithContext(ctx, j.location, selectLicenses(manifest)...)) /* We should name and version from, in this order: 1. pom.properties if we find exactly 1 @@ -412,9 +415,90 @@ func toPkgLicenses(ctx context.Context, location *file.Location, licenses []mave if name == "" && url == "" { continue } - out = append(out, pkg.NewLicenseFromFieldsWithContext(ctx, name, url, location)) + candidate := pkg.NewLicenseFromFieldsWithContext(ctx, name, url, location) + out = append(out, candidate) + } + return combineMavenLicenseAlternatives(out) +} + +func combineMavenLicenseAlternatives(licenses []pkg.License) []pkg.License { + if len(licenses) < 2 { + return licenses + } + + enriched := slices.Clone(licenses) + for i := range enriched { + if enriched[i].SPDXExpression == "" && len(enriched[i].URLs) > 0 { + if info, found := spdxlicense.LicenseByURL(enriched[i].URLs[0]); found { + enriched[i].SPDXExpression = info.ID + } + } + } + + return combineLicenseAlternatives(enriched) +} + +func combineEclipseClasspathAlternatives(licenses []pkg.License) []pkg.License { + if len(licenses) != 2 { + return licenses + } + + hasEPL := false + hasGPLClasspath := false + for _, candidate := range licenses { + switch candidate.SPDXExpression { + case "EPL-2.0": + hasEPL = true + case "GPL-2.0-only WITH Classpath-exception-2.0": + hasGPLClasspath = true + } + } + if !hasEPL || !hasGPLClasspath { + return licenses + } + + return combineLicenseAlternatives(licenses) +} + +func combineLicenseAlternatives(licenses []pkg.License) []pkg.License { + if len(licenses) < 2 { + return licenses + } + + var values []string + var expressions []string + var locations []file.Location + urls := strset.New() + seenExpressions := strset.New() + + for _, candidate := range licenses { + if candidate.SPDXExpression == "" { + return licenses + } + + if candidate.Value != "" { + values = append(values, candidate.Value) + } + if !seenExpressions.Has(candidate.SPDXExpression) { + expressions = append(expressions, candidate.SPDXExpression) + seenExpressions.Add(candidate.SPDXExpression) + } + urls.Add(candidate.URLs...) + locations = append(locations, candidate.Locations.ToSlice()...) + } + + combinedURLs := urls.List() + slices.Sort(combinedURLs) + + return []pkg.License{ + { + Value: strings.Join(values, " OR "), + SPDXExpression: strings.Join(expressions, " OR "), + Type: licenses[0].Type, + URLs: combinedURLs, + Locations: file.NewLocationSet(locations...), + }, } - return out } type parsedPomProject struct { @@ -623,6 +707,7 @@ func (j *archiveParser) getLicenseFromFileInArchive(ctx context.Context) []pkg.L licenseContents := contents[licenseMatch] r := strings.NewReader(licenseContents) foundLicenses := pkg.NewLicensesFromReadCloserWithContext(ctx, file.NewLocationReadCloser(j.location, io.NopCloser(r))) + foundLicenses = normalizeClasspathExceptionLicenses(foundLicenses, licenseContents) for _, l := range foundLicenses { if l.SPDXExpression != "" { identified = append(identified, l) @@ -684,6 +769,35 @@ func (j *archiveParser) versionFromPropertiesFile(ctx context.Context, manifest return "" } +func normalizeClasspathExceptionLicenses(found []pkg.License, contents string) []pkg.License { + if len(found) != 2 || !strings.Contains(strings.ToLower(contents), "classpath exception") { + return found + } + + var epl *pkg.License + var gpl *pkg.License + for i := range found { + switch found[i].SPDXExpression { + case "EPL-2.0": + epl = &found[i] + case "GPL-2.0-only": + gpl = &found[i] + } + } + if epl == nil || gpl == nil { + return found + } + + return []pkg.License{ + { + Value: eclipseClasspathDualLicense, + SPDXExpression: eclipseClasspathDualLicense, + Type: epl.Type, + Locations: file.NewLocationSet(append(epl.Locations.ToSlice(), gpl.Locations.ToSlice()...)...), + }, + } +} + // discoverPkgsFromNestedArchives recurses into the java archives inside this one, which needs an // archive file to unzip; an already-extracted archive's nested archives are the archive cataloger // task's to find. diff --git a/syft/pkg/cataloger/java/archive_parser_test.go b/syft/pkg/cataloger/java/archive_parser_test.go index c586a421e..44450c588 100644 --- a/syft/pkg/cataloger/java/archive_parser_test.go +++ b/syft/pkg/cataloger/java/archive_parser_test.go @@ -91,6 +91,83 @@ func TestSearchMavenForLicenses(t *testing.T) { } } +func TestToPkgLicensesCombinesMavenAlternatives(t *testing.T) { + const eplName = "Eclipse Public License 2.0" + const eplURL = "https://projects.eclipse.org/license/epl-2.0" + const gplName = "GNU General Public License, version 2 with the GNU Classpath Exception" + const gplURL = "https://projects.eclipse.org/license/secondary-gpl-2.0-cp" + + actual := toPkgLicenses(pkgtest.Context(t), nil, []maven.License{ + {Name: ptr(eplName), URL: ptr(eplURL)}, + {Name: ptr(gplName), URL: ptr(gplURL)}, + }) + + expected := []pkg.License{ + { + Value: eplName + " OR " + gplName, + SPDXExpression: "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0", + Type: license.Declared, + URLs: []string{eplURL, gplURL}, + }, + } + assert.Equal(t, expected, actual) +} + +func TestCombineEclipseClasspathAlternatives(t *testing.T) { + alternatives := []pkg.License{ + {Value: "EPL", SPDXExpression: "EPL-2.0", Type: license.Declared}, + {Value: "GPL plus exception", SPDXExpression: "GPL-2.0-only WITH Classpath-exception-2.0", Type: license.Declared}, + } + + actual := combineEclipseClasspathAlternatives(alternatives) + require.Len(t, actual, 1) + assert.Equal(t, eclipseClasspathDualLicense, actual[0].SPDXExpression) + + unrelated := []pkg.License{ + {SPDXExpression: "Apache-2.0"}, + {SPDXExpression: "MIT"}, + } + assert.Equal(t, unrelated, combineEclipseClasspathAlternatives(unrelated)) +} + +func TestNormalizeClasspathExceptionLicenses(t *testing.T) { + location := file.NewLocation("META-INF/LICENSE.md") + actual := normalizeClasspathExceptionLicenses([]pkg.License{ + { + Value: "EPL-2.0", + SPDXExpression: "EPL-2.0", + Type: license.Concluded, + Locations: file.NewLocationSet(location), + }, + { + Value: "GPL-2.0-only", + SPDXExpression: "GPL-2.0-only", + Type: license.Concluded, + Locations: file.NewLocationSet(location), + }, + }, "## CLASSPATH EXCEPTION\n\nAs a special exception...") + + expected := []pkg.License{ + { + Value: eclipseClasspathDualLicense, + SPDXExpression: eclipseClasspathDualLicense, + Type: license.Concluded, + Locations: file.NewLocationSet(location), + }, + } + assert.Equal(t, expected, actual) +} + +func TestNormalizeClasspathExceptionLicensesPreservesOtherLicenses(t *testing.T) { + found := []pkg.License{ + {SPDXExpression: "EPL-2.0"}, + {SPDXExpression: "GPL-2.0-only"}, + {SPDXExpression: "MIT"}, + } + + assert.Equal(t, found, normalizeClasspathExceptionLicenses(found, "## CLASSPATH EXCEPTION")) +} + func TestParseJar(t *testing.T) { ctx := pkgtest.Context(t) tests := []struct { diff --git a/syft/pkg/cataloger/java/parse_java_manifest.go b/syft/pkg/cataloger/java/parse_java_manifest.go index 0c1e3129a..d433f0c20 100644 --- a/syft/pkg/cataloger/java/parse_java_manifest.go +++ b/syft/pkg/cataloger/java/parse_java_manifest.go @@ -261,13 +261,71 @@ func selectLicenses(manifest *pkg.JavaManifest) []string { for _, fieldName := range fieldNames { if v := fieldValueFromManifest(*manifest, fieldName); v != "" { - result = append(result, v) + if fieldName != "Bundle-License" { + result = append(result, v) + continue + } + + clauses := splitUnquoted(v, ',') + for i := 0; i < len(clauses); i++ { + identifier := strings.TrimSpace(splitUnquoted(clauses[i], ';')[0]) + // Some bundles, including FreeMarker, leave this comma-containing name unquoted. + if strings.EqualFold(strings.TrimSpace(clauses[i]), "Apache License") && i+1 < len(clauses) && + strings.EqualFold(strings.TrimSpace(splitUnquoted(clauses[i+1], ';')[0]), "Version 2.0") { + identifier += ", Version 2.0" + i++ + } + if strings.EqualFold(strings.Trim(identifier, `"'`), "Apache License, Version 2.0") { + identifier = "Apache-2.0" + } + if identifier != "" { + result = append(result, identifier) + } + } } } return result } +func splitUnquoted(value string, delimiter rune) []string { + var parts []string + var current strings.Builder + var quote rune + escaped := false + + for _, r := range value { + if escaped { + current.WriteRune(r) + escaped = false + continue + } + if quote != 0 && r == '\\' { + current.WriteRune(r) + escaped = true + continue + } + if r == '\'' || r == '"' { + switch quote { + case 0: + quote = r + case r: + quote = 0 + } + current.WriteRune(r) + continue + } + if quote == 0 && r == delimiter { + parts = append(parts, current.String()) + current.Reset() + continue + } + current.WriteRune(r) + } + + return append(parts, current.String()) +} + func fieldValueFromManifest(manifest pkg.JavaManifest, fieldName string) string { if value := manifest.Main.MustGet(fieldName); value != "" { return value diff --git a/syft/pkg/cataloger/java/parse_java_manifest_test.go b/syft/pkg/cataloger/java/parse_java_manifest_test.go index 7673fab62..24ad2e010 100644 --- a/syft/pkg/cataloger/java/parse_java_manifest_test.go +++ b/syft/pkg/cataloger/java/parse_java_manifest_test.go @@ -194,6 +194,95 @@ func TestParseJavaManifest(t *testing.T) { } } +func TestSelectLicensesSplitsBundleLicenseClauses(t *testing.T) { + manifest := &pkg.JavaManifest{ + Main: pkg.KeyValues{ + { + Key: "Bundle-License", + Value: "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt;description=\"EPL, version 2\", " + + "https://www.gnu.org/software/classpath/license.html", + }, + }, + } + + assert.Equal(t, []string{ + "https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt", + "https://www.gnu.org/software/classpath/license.html", + }, selectLicenses(manifest)) +} + +func TestSelectLicensesApacheName(t *testing.T) { + tests := []struct { + name string + value string + expected []string + }{ + { + name: "freemarker unquoted name with see parameter", + value: "Apache License, Version 2.0; see: http://www.apache.org/licenses/LICENSE-2.0.txt", + expected: []string{"Apache-2.0"}, + }, + { + name: "unquoted name", + value: "Apache License, Version 2.0", + expected: []string{"Apache-2.0"}, + }, + { + name: "quoted name", + value: `"Apache License, Version 2.0";description="Apache, version 2"`, + expected: []string{"Apache-2.0"}, + }, + { + name: "single quoted name", + value: "'Apache License, Version 2.0'", + expected: []string{"Apache-2.0"}, + }, + { + name: "other licenses before and after", + value: "MIT, Apache License, Version 2.0;description=Apache, BSD-3-Clause", + expected: []string{"MIT", "Apache-2.0", "BSD-3-Clause"}, + }, + { + name: "case and whitespace", + value: " apache license , version 2.0 ;description=Apache ", + expected: []string{"Apache-2.0"}, + }, + { + name: "different version", + value: "Apache License, Version 1.0", + expected: []string{"Apache License", "Version 1.0"}, + }, + { + name: "different license name", + value: "Custom License, Version 2.0", + expected: []string{"Custom License", "Version 2.0"}, + }, + { + name: "comma in description", + value: `MIT;description="Apache License, Version 2.0", BSD-3-Clause`, + expected: []string{"MIT", "BSD-3-Clause"}, + }, + { + name: "parameter separates the two clauses", + value: "Apache License;description=custom, Version 2.0", + expected: []string{"Apache License", "Version 2.0"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + manifest := &pkg.JavaManifest{Main: pkg.KeyValues{{Key: "Bundle-License", Value: test.value}}} + actual := selectLicenses(manifest) + assert.Equal(t, test.expected, actual) + for _, license := range pkg.NewLicensesFromValuesWithContext(t.Context(), actual...) { + if license.Value == "Apache-2.0" { + assert.Equal(t, "Apache-2.0", license.SPDXExpression) + } + } + }) + } +} + func TestSelectName(t *testing.T) { tests := []struct { desc string