Skip to content

Commit c321fdf

Browse files
authored
feat(license): improve work with custom classification of licenses from config file (#8861)
1 parent 69a5fa1 commit c321fdf

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

pkg/licensing/scanner.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package licensing
22

33
import (
4-
"slices"
5-
64
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
75
"github.com/aquasecurity/trivy/pkg/fanal/types"
86
"github.com/aquasecurity/trivy/pkg/licensing/expression"
7+
"github.com/aquasecurity/trivy/pkg/set"
98
)
109

1110
type ScannerOption struct {
@@ -22,17 +21,14 @@ func NewScanner(categories map[types.LicenseCategory][]string) Scanner {
2221
}
2322

2423
func (s *Scanner) Scan(licenseName string) (types.LicenseCategory, string) {
25-
normalized := NormalizeLicense(expression.SimpleExpr{License: licenseName})
26-
var normalizedName string
27-
switch normalized := normalized.(type) {
28-
case expression.SimpleExpr:
29-
normalizedName = normalized.License
30-
case expression.CompoundExpr:
31-
normalizedName = normalized.String()
24+
expr := NormalizeLicense(expression.SimpleExpr{License: licenseName})
25+
normalizedNames := set.New(expr.String()) // The license name with suffix (e.g. AGPL-1.0-or-later)
26+
if se, ok := expr.(expression.SimpleExpr); ok {
27+
normalizedNames.Append(se.License) // Also accept the license name without suffix (e.g. AGPL-1.0)
3228
}
3329

3430
for category, names := range s.categories {
35-
if slices.Contains(names, normalizedName) {
31+
if normalizedNames.Intersection(set.New(names...)).Size() > 0 {
3632
return category, categoryToSeverity(category).String()
3733
}
3834
}

pkg/licensing/scanner_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ func TestScanner_Scan(t *testing.T) {
4242
wantCategory: types.CategoryForbidden,
4343
wantSeverity: "CRITICAL",
4444
},
45+
{
46+
name: "`categories` contains license with suffix",
47+
categories: map[types.LicenseCategory][]string{
48+
types.CategoryNotice: {
49+
"LGPL-2.0-only",
50+
},
51+
},
52+
licenseName: "LGPL-2.0-only",
53+
wantCategory: types.CategoryNotice,
54+
wantSeverity: "LOW",
55+
},
4556
{
4657
name: "restricted",
4758
categories: map[types.LicenseCategory][]string{

0 commit comments

Comments
 (0)