Skip to content

Commit 52f7aa5

Browse files
authored
fix(license): return license separation using separators ,, or, etc. (#6916)
1 parent d77d9ce commit 52f7aa5

File tree

6 files changed

+80
-43
lines changed

6 files changed

+80
-43
lines changed

pkg/dependency/parser/conda/meta/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package meta
33
import (
44
"encoding/json"
55

6-
"github.com/samber/lo"
76
"golang.org/x/xerrors"
87

98
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
9+
"github.com/aquasecurity/trivy/pkg/licensing"
1010
xio "github.com/aquasecurity/trivy/pkg/x/io"
1111
)
1212

@@ -40,7 +40,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
4040
{
4141
Name: data.Name,
4242
Version: data.Version,
43-
Licenses: lo.Ternary(data.License != "", []string{data.License}, nil),
43+
Licenses: licensing.SplitLicenses(data.License),
4444
},
4545
}, nil, nil
4646
}

pkg/dependency/parser/php/composer/parse.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/aquasecurity/trivy/pkg/dependency"
1313
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
14+
"github.com/aquasecurity/trivy/pkg/licensing"
1415
"github.com/aquasecurity/trivy/pkg/log"
1516
xio "github.com/aquasecurity/trivy/pkg/x/io"
1617
)
@@ -22,7 +23,7 @@ type packageInfo struct {
2223
Name string `json:"name"`
2324
Version string `json:"version"`
2425
Require map[string]string `json:"require"`
25-
License []string `json:"license"`
26+
License any `json:"license"`
2627
StartLine int
2728
EndLine int
2829
}
@@ -55,7 +56,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
5556
Name: lpkg.Name,
5657
Version: lpkg.Version,
5758
Relationship: ftypes.RelationshipUnknown, // composer.lock file doesn't have info about direct/indirect dependencies
58-
Licenses: lpkg.License,
59+
Licenses: licenses(lpkg.License),
5960
Locations: []ftypes.Location{
6061
{
6162
StartLine: lpkg.StartLine,
@@ -114,3 +115,23 @@ func (t *packageInfo) UnmarshalJSONWithMetadata(node jfather.Node) error {
114115
t.EndLine = node.Range().End.Line
115116
return nil
116117
}
118+
119+
// licenses returns slice of licenses from string, string with separators (`or`, `and`, etc.) or string array
120+
// cf. https://getcomposer.org/doc/04-schema.md#license
121+
func licenses(val any) []string {
122+
switch v := val.(type) {
123+
case string:
124+
if v != "" {
125+
return licensing.SplitLicenses(v)
126+
}
127+
case []any:
128+
var lics []string
129+
for _, l := range v {
130+
if lic, ok := l.(string); ok {
131+
lics = append(lics, lic)
132+
}
133+
}
134+
return lics
135+
}
136+
return nil
137+
}

pkg/dependency/parser/php/composer/parse_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ var (
9898
Locations: []ftypes.Location{
9999
{
100100
StartLine: 502,
101-
EndLine: 585,
101+
EndLine: 583,
102102
},
103103
},
104104
},
105105
{
106106
ID: "symfony/polyfill-php72@v1.27.0",
107107
Name: "symfony/polyfill-php72",
108108
Version: "v1.27.0",
109-
Licenses: []string{"MIT"},
109+
Licenses: []string{"MIT", "BSD-2-Clause"},
110110
Locations: []ftypes.Location{
111111
{
112-
StartLine: 586,
113-
EndLine: 661,
112+
StartLine: 584,
113+
EndLine: 657,
114114
},
115115
},
116116
},

pkg/dependency/parser/php/composer/testdata/composer_happy.lock

+2-6
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,7 @@
541541
]
542542
},
543543
"notification-url": "https://packagist.org/downloads/",
544-
"license": [
545-
"MIT"
546-
],
544+
"license": "MIT",
547545
"authors": [
548546
{
549547
"name": "Nicolas Grekas",
@@ -619,9 +617,7 @@
619617
}
620618
},
621619
"notification-url": "https://packagist.org/downloads/",
622-
"license": [
623-
"MIT"
624-
],
620+
"license": "MIT or BSD-2-Clause",
625621
"authors": [
626622
{
627623
"name": "Nicolas Grekas",

pkg/dependency/parser/python/packaging/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"net/textproto"
88
"strings"
99

10-
"github.com/samber/lo"
1110
"golang.org/x/xerrors"
1211

1312
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
13+
"github.com/aquasecurity/trivy/pkg/licensing"
1414
"github.com/aquasecurity/trivy/pkg/log"
1515
xio "github.com/aquasecurity/trivy/pkg/x/io"
1616
)
@@ -87,7 +87,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
8787
{
8888
Name: name,
8989
Version: version,
90-
Licenses: lo.Ternary(license != "", []string{license}, nil),
90+
Licenses: licensing.SplitLicenses(license),
9191
},
9292
}, nil, nil
9393
}

pkg/dependency/parser/python/packaging/parse_test.go

+47-27
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ func TestParse(t *testing.T) {
3535
// tr "\n" "\t" | awk -F "\t" '{printf("\{\""$1"\", \""$2"\", \""$3"\"\}\n")}'
3636
want: []ftypes.Package{
3737
{
38-
Name: "setuptools",
39-
Version: "51.3.3",
40-
Licenses: []string{"UNKNOWN"},
38+
Name: "setuptools",
39+
Version: "51.3.3",
40+
Licenses: []string{
41+
"UNKNOWN",
42+
},
4143
},
4244
},
4345
},
@@ -46,9 +48,11 @@ func TestParse(t *testing.T) {
4648
input: "testdata/unidecode-egg-info.PKG-INFO",
4749
want: []ftypes.Package{
4850
{
49-
Name: "Unidecode",
50-
Version: "0.4.1",
51-
Licenses: []string{"UNKNOWN"},
51+
Name: "Unidecode",
52+
Version: "0.4.1",
53+
Licenses: []string{
54+
"UNKNOWN",
55+
},
5256
},
5357
},
5458
},
@@ -63,9 +67,11 @@ func TestParse(t *testing.T) {
6367
// tr "\n" "\t" | awk -F "\t" '{printf("\{\""$1"\", \""$2"\", \""$3"\"\}\n")}'
6468
want: []ftypes.Package{
6569
{
66-
Name: "distlib",
67-
Version: "0.3.1",
68-
Licenses: []string{"Python license"},
70+
Name: "distlib",
71+
Version: "0.3.1",
72+
Licenses: []string{
73+
"Python license",
74+
},
6975
},
7076
},
7177
},
@@ -96,9 +102,11 @@ func TestParse(t *testing.T) {
96102
input: "testdata/distlib-0.3.1.METADATA",
97103
want: []ftypes.Package{
98104
{
99-
Name: "distlib",
100-
Version: "0.3.1",
101-
Licenses: []string{"Python Software Foundation License"},
105+
Name: "distlib",
106+
Version: "0.3.1",
107+
Licenses: []string{
108+
"Python Software Foundation License",
109+
},
102110
},
103111
},
104112
},
@@ -109,9 +117,11 @@ func TestParse(t *testing.T) {
109117

110118
want: []ftypes.Package{
111119
{
112-
Name: "asyncssh",
113-
Version: "2.14.2",
114-
Licenses: []string{"Eclipse Public License v2.0"},
120+
Name: "asyncssh",
121+
Version: "2.14.2",
122+
Licenses: []string{
123+
"Eclipse Public License v2.0",
124+
},
115125
},
116126
},
117127
},
@@ -122,9 +132,13 @@ func TestParse(t *testing.T) {
122132

123133
want: []ftypes.Package{
124134
{
125-
Name: "pyphen",
126-
Version: "0.14.0",
127-
Licenses: []string{"GNU General Public License v2 or later (GPLv2+), GNU Lesser General Public License v2 or later (LGPLv2+), Mozilla Public License 1.1 (MPL 1.1)"},
135+
Name: "pyphen",
136+
Version: "0.14.0",
137+
Licenses: []string{
138+
"GNU General Public License v2 or later (GPLv2+)",
139+
"GNU Lesser General Public License v2 or later (LGPLv2+)",
140+
"Mozilla Public License 1.1 (MPL 1.1)",
141+
},
128142
},
129143
},
130144
},
@@ -138,9 +152,11 @@ func TestParse(t *testing.T) {
138152
input: "testdata/iniconfig-2.0.0.METADATA",
139153
want: []ftypes.Package{
140154
{
141-
Name: "iniconfig",
142-
Version: "2.0.0",
143-
Licenses: []string{"MIT"},
155+
Name: "iniconfig",
156+
Version: "2.0.0",
157+
Licenses: []string{
158+
"MIT",
159+
},
144160
},
145161
},
146162
},
@@ -149,9 +165,11 @@ func TestParse(t *testing.T) {
149165
input: "testdata/zipp-3.12.1.METADATA",
150166
want: []ftypes.Package{
151167
{
152-
Name: "zipp",
153-
Version: "3.12.1",
154-
Licenses: []string{"MIT License"},
168+
Name: "zipp",
169+
Version: "3.12.1",
170+
Licenses: []string{
171+
"MIT License",
172+
},
155173
},
156174
},
157175
},
@@ -160,9 +178,11 @@ func TestParse(t *testing.T) {
160178
input: "testdata/networkx-3.0.METADATA",
161179
want: []ftypes.Package{
162180
{
163-
Name: "networkx",
164-
Version: "3.0",
165-
Licenses: []string{"file://LICENSE.txt"},
181+
Name: "networkx",
182+
Version: "3.0",
183+
Licenses: []string{
184+
"file://LICENSE.txt",
185+
},
166186
},
167187
},
168188
},

0 commit comments

Comments
 (0)