Skip to content

Commit e1374f7

Browse files
authored
fix: update license content filtering default case to be 'none' for no content returned
--------- Signed-off-by: Christopher Phillips <[email protected]>
1 parent 9458938 commit e1374f7

File tree

9 files changed

+52
-53
lines changed

9 files changed

+52
-53
lines changed

cmd/syft/internal/options/license.go

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ import (
44
"fmt"
55

66
"github.com/anchore/clio"
7+
"github.com/anchore/syft/internal"
78
"github.com/anchore/syft/syft/cataloging"
89
)
910

1011
type licenseConfig struct {
11-
Content cataloging.LicenseContent `yaml:"content" json:"content" mapstructure:"content"`
12-
// Deprecated: please use include-license-content instead
13-
IncludeUnknownLicenseContent *bool `yaml:"-" json:"-" mapstructure:"include-unknown-license-content"`
14-
15-
Coverage float64 `yaml:"coverage" json:"coverage" mapstructure:"coverage"`
16-
// Deprecated: please use coverage instead
17-
LicenseCoverage *float64 `yaml:"license-coverage" json:"license-coverage" mapstructure:"license-coverage"`
18-
12+
Content cataloging.LicenseContent `yaml:"content" json:"content" mapstructure:"content"`
13+
Coverage float64 `yaml:"coverage" json:"coverage" mapstructure:"coverage"`
1914
AvailableLicenseContent []cataloging.LicenseContent `yaml:"-" json:"-" mapstructure:"-"`
2015
}
2116

@@ -25,43 +20,16 @@ var _ interface {
2520

2621
func (o *licenseConfig) DescribeFields(descriptions clio.FieldDescriptionSet) {
2722
descriptions.Add(&o.Content, fmt.Sprintf("include the content of licenses in the SBOM for a given syft scan; valid values are: %s", o.AvailableLicenseContent))
28-
descriptions.Add(&o.IncludeUnknownLicenseContent, `deprecated: please use 'license-content' instead`)
2923

3024
descriptions.Add(&o.Coverage, `adjust the percent as a fraction of the total text, in normalized words, that
3125
matches any valid license for the given inputs, expressed as a percentage across all of the licenses matched.`)
32-
descriptions.Add(&o.LicenseCoverage, `deprecated: please use 'coverage' instead`)
3326
}
3427

3528
func (o *licenseConfig) PostLoad() error {
36-
cfg := cataloging.DefaultLicenseConfig()
37-
defaultContent := cfg.IncludeContent
38-
defaultCoverage := cfg.Coverage
39-
40-
// if both legacy and new fields are specified, error out
41-
if o.IncludeUnknownLicenseContent != nil && o.Content != defaultContent {
42-
return fmt.Errorf("both 'include-unknown-license-content' and 'content' are set, please use only 'content'")
43-
}
44-
45-
if o.LicenseCoverage != nil && o.Coverage != defaultCoverage {
46-
return fmt.Errorf("both 'license-coverage' and 'coverage' are set, please use only 'coverage'")
29+
validContent := internal.NewSet(o.AvailableLicenseContent...)
30+
if !validContent.Contains(o.Content) {
31+
return fmt.Errorf("could not use %q as license content option; valid values are: %v", o.Content, validContent.ToSlice())
4732
}
48-
49-
// finalize the license content value
50-
if o.IncludeUnknownLicenseContent != nil {
51-
// convert 'include-unknown-license-content' -> 'license-content'
52-
v := cataloging.LicenseContentExcludeAll
53-
if *o.IncludeUnknownLicenseContent {
54-
v = cataloging.LicenseContentIncludeUnknown
55-
}
56-
o.Content = v
57-
}
58-
59-
// finalize the coverage value
60-
if o.LicenseCoverage != nil {
61-
// convert 'license-coverage' -> 'coverage'
62-
o.Coverage = *o.LicenseCoverage
63-
}
64-
6533
return nil
6634
}
6735

internal/task/package_task_factory.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,16 @@ func applyLicenseContentRules(p *pkg.Package, cfg cataloging.LicenseConfig) {
278278
l := &licenses[i]
279279
switch cfg.IncludeContent {
280280
case cataloging.LicenseContentIncludeUnknown:
281-
// we don't have an SPDX expression, which means we didn't find an SPDX license
282-
// include the unknown licenses content in the final SBOM
281+
// we have an SPDX expression, which means this is NOT an unknown license
282+
// remove the content, we are only including content for unknown licenses by default
283283
if l.SPDXExpression != "" {
284284
licenses[i].Contents = ""
285285
}
286-
case cataloging.LicenseContentExcludeAll:
287-
// clear it all out
288-
licenses[i].Contents = ""
289286
case cataloging.LicenseContentIncludeAll:
290287
// always include the content
288+
default:
289+
// clear it all out
290+
licenses[i].Contents = ""
291291
}
292292
}
293293

internal/task/package_task_factory_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestApplyLicenseContentRules(t *testing.T) {
174174
},
175175
},
176176
{
177-
name: "IncludeLicenseContentDefault",
177+
name: "LicenseContentIncludeAll",
178178
inputLicenses: []pkg.License{
179179
licenseWithSPDX,
180180
licenseWithoutSPDX,
@@ -193,6 +193,41 @@ func TestApplyLicenseContentRules(t *testing.T) {
193193
},
194194
},
195195
},
196+
{
197+
name: "default license config should be LicenseContentExcludeAll",
198+
inputLicenses: []pkg.License{
199+
licenseWithSPDX,
200+
licenseWithoutSPDX,
201+
},
202+
cfg: cataloging.DefaultLicenseConfig(),
203+
expectedLicenses: []pkg.License{
204+
{
205+
SPDXExpression: "MIT",
206+
},
207+
{
208+
Value: "License-Not-A-SPDX-Expression",
209+
},
210+
},
211+
},
212+
{
213+
name: "invalid license content cataloging config results in the default case",
214+
inputLicenses: []pkg.License{
215+
licenseWithSPDX,
216+
licenseWithoutSPDX,
217+
},
218+
cfg: cataloging.LicenseConfig{
219+
IncludeContent: cataloging.LicenseContent("invalid"),
220+
},
221+
expectedLicenses: []pkg.License{
222+
{
223+
SPDXExpression: "MIT",
224+
},
225+
{
226+
Value: "License-Not-A-SPDX-Expression",
227+
Contents: "", // content all removed
228+
},
229+
},
230+
},
196231
{
197232
name: "Empty licenses",
198233
inputLicenses: []pkg.License{},

syft/cataloging/license.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ const (
1414
)
1515

1616
type LicenseConfig struct {
17-
// IncludeUnknownLicenseContent controls whether the content of a license should be included in the SBOM when the license ID cannot be determined.
18-
// Deprecated: use IncludeContent instead
19-
IncludeUnknownLicenseContent bool `json:"-" yaml:"-" mapstructure:"-"`
20-
2117
// IncludeContent controls whether license copy discovered should be included in the SBOM.
2218
IncludeContent LicenseContent `json:"include-content" yaml:"include-content" mapstructure:"include-content"`
2319

syft/format/common/spdxhelpers/to_format_model_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ func Test_otherLicenses(t *testing.T) {
958958
{
959959
LicenseIdentifier: "LicenseRef-3f17782eef51ae86f18fdd6832f5918e2b40f688b52c9adc07ba6ec1024ef408",
960960
// Carries through the syft-json license value when we shasum large texts
961-
LicenseName: "LicenseRef-sha256:3f17782eef51ae86f18fdd6832f5918e2b40f688b52c9adc07ba6ec1024ef408",
961+
LicenseName: "sha256:3f17782eef51ae86f18fdd6832f5918e2b40f688b52c9adc07ba6ec1024ef408",
962962
ExtractedText: strings.TrimSpace(bigText),
963963
},
964964
},

syft/format/internal/spdxutil/helpers/license.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ParseLicenses(raw []pkg.License) (concluded, declared []SPDXLicense, otherL
7373
for _, l := range raw {
7474
candidate := createSPDXLicense(l)
7575

76-
// isCustomLicense determines if the candidate falls under https://spdx.github.io/spdx-spec/v2.3/other-licensing-information-detected/#
76+
// this determines if the candidate falls under https://spdx.github.io/spdx-spec/v2.3/other-licensing-information-detected/#
7777
// of the SPDX spec, where:
7878
// - we should not have a complex SPDX expression
7979
// - if a single license, it should not be a known license (on the SPDX license list)

syft/pkg/license.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func (b *licenseBuilder) licensesFromEvidenceAndContent(evidence []licenses.Evid
365365

366366
func (b *licenseBuilder) licenseFromContentHash(content string) License {
367367
hash := sha256HexFromString(content)
368-
value := "LicenseRef-sha256:" + hash
368+
value := "sha256:" + hash
369369

370370
return License{
371371
Value: value,

syft/pkg/license_set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestLicenseSet_Add(t *testing.T) {
149149
Locations: file.NewLocationSet(),
150150
},
151151
{
152-
Value: "LicenseRef-sha256:eebcea3ab1d1a28e671de90119ffcfb35fe86951e4af1b17af52b7a82fcf7d0a",
152+
Value: "sha256:eebcea3ab1d1a28e671de90119ffcfb35fe86951e4af1b17af52b7a82fcf7d0a",
153153
Contents: readFileAsString("../../internal/licenses/test-fixtures/nvidia-software-and-cuda-supplement"),
154154
Type: license.Declared,
155155
},

syft/pkg/license_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func TestFullText(t *testing.T) {
255255
name: "Full Text field is populated with the correct full text and contents are given a sha256 as value",
256256
value: fullText,
257257
want: License{
258-
Value: "LicenseRef-sha256:108067fa71229a2b98b9696af0ce21cd11d9639634c8bc94bda70ebedf291e5a",
258+
Value: "sha256:108067fa71229a2b98b9696af0ce21cd11d9639634c8bc94bda70ebedf291e5a",
259259
Type: license.Declared,
260260
Contents: fullText,
261261
},

0 commit comments

Comments
 (0)