Skip to content

Commit e5072f1

Browse files
fix(spdx): save text licenses into otherLicenses without normalize (#8502)
Co-authored-by: Teppei Fukuda <[email protected]>
1 parent a930561 commit e5072f1

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

pkg/sbom/spdx/marshal.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@ func (m *Marshaler) normalizeLicenses(licenses []string) (string, []*spdx.OtherL
416416
var otherLicenses = make(map[string]*spdx.OtherLicense) // licenseID -> OtherLicense
417417

418418
license := strings.Join(lo.Map(licenses, func(license string, index int) string {
419+
// We need to save text licenses before normalization,
420+
// because it is impossible to handle all cases possible in the text.
421+
// as an example, parse a license with 2 consecutive tokens (see https://github.com/aquasecurity/trivy/issues/8465)
422+
if strings.HasPrefix(license, licensing.LicenseTextPrefix) {
423+
license = strings.TrimPrefix(license, licensing.LicenseTextPrefix)
424+
otherLicense := m.newOtherLicense(license, true)
425+
otherLicenses[otherLicense.LicenseIdentifier] = otherLicense
426+
return otherLicense.LicenseIdentifier
427+
}
428+
419429
// e.g. GPL-3.0-with-autoconf-exception
420430
license = strings.ReplaceAll(license, "-with-", " WITH ")
421431
license = strings.ReplaceAll(license, "-WITH-", " WITH ")
@@ -424,13 +434,10 @@ func (m *Marshaler) normalizeLicenses(licenses []string) (string, []*spdx.OtherL
424434

425435
replaceOtherLicenses := func(expr expression.Expression) expression.Expression {
426436
var licenseName string
427-
var textLicense bool
428437
switch e := expr.(type) {
429438
case expression.SimpleExpr:
430-
// Trim `text:--` prefix (expression.NormalizeForSPDX normalized `text://` prefix)
431-
if strings.HasPrefix(e.License, "text:--") {
432-
textLicense = true
433-
e.License = strings.TrimPrefix(e.License, "text:--")
439+
if strings.HasPrefix(e.License, LicenseRefPrefix) {
440+
return e
434441
}
435442

436443
if expression.ValidateSPDXLicense(e.License) || expression.ValidateSPDXException(e.License) {
@@ -454,7 +461,7 @@ func (m *Marshaler) normalizeLicenses(licenses []string) (string, []*spdx.OtherL
454461
licenseName = e.String()
455462
}
456463

457-
l := m.newOtherLicense(licenseName, textLicense)
464+
l := m.newOtherLicense(licenseName, false)
458465
otherLicenses[l.LicenseIdentifier] = l
459466
return expression.SimpleExpr{License: l.LicenseIdentifier}
460467
}

pkg/sbom/spdx/marshal_private_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,21 @@ func TestMarshaler_normalizeLicenses(t *testing.T) {
100100
{
101101
name: "happy path with text of license",
102102
input: []string{
103-
"text://unknown-license",
103+
"text://Redistribution and use in source and binary forms, with or without",
104104
"AFL 2.0",
105105
"unknown-license",
106106
},
107-
wantLicenseName: "LicenseRef-ffca10435cadded4 AND AFL-2.0 AND LicenseRef-a0bb0951a6dfbdbe",
107+
wantLicenseName: "LicenseRef-b5b4cc09bc5f0e16 AND AFL-2.0 AND LicenseRef-a0bb0951a6dfbdbe",
108108
wantOtherLicenses: []*spdx.OtherLicense{
109109
{
110110
LicenseIdentifier: "LicenseRef-a0bb0951a6dfbdbe",
111111
LicenseName: "unknown-license",
112112
ExtractedText: `This component is licensed under "unknown-license"`,
113113
},
114114
{
115-
LicenseIdentifier: "LicenseRef-ffca10435cadded4",
115+
LicenseIdentifier: "LicenseRef-b5b4cc09bc5f0e16",
116116
LicenseName: "NOASSERTION",
117-
ExtractedText: "unknown-license",
117+
ExtractedText: "Redistribution and use in source and binary forms, with or without",
118118
LicenseComment: "The license text represents text found in package metadata and may not represent the full text of the license",
119119
},
120120
},

0 commit comments

Comments
 (0)