Skip to content

Commit 04535b5

Browse files
Damian EDmitriyLewen
andauthored
fix(license): add FilePath to results to allow for license path filtering via trivyignore file (aquasecurity#6215)
Co-authored-by: DmitriyLewen <[email protected]> Co-authored-by: DmitriyLewen <[email protected]>
1 parent 939e34e commit 04535b5

File tree

3 files changed

+129
-6
lines changed

3 files changed

+129
-6
lines changed

docs/docs/configuration/filtering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,4 @@ Please refer to the [VEX documentation](../supply-chain/vex.md) for the details.
494494
495495
496496
[^1]: license name is used as id for `.trivyignore.yaml` files.
497-
[^2]: This doesn't work for package licenses. The `path` field can only be used for license files (licenses obtained using the [--license-full flag](../scanner/license.md#full-scanning)).
497+
[^2]: This doesn't work for os package licenses (e.g. apk, dpkg, rpm). For projects which manage dependencies through a dependency file (e.g. go.mod, yarn.lock) `path` should point to that particular file.

pkg/scanner/local/scan.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ func (s Scanner) scanLicenses(target types.ScanTarget, options types.ScanOptions
298298
Confidence: 1.0,
299299
})
300300
}
301-
302301
}
303302
results = append(results, types.Result{
304303
Target: "OS Packages",
@@ -313,10 +312,13 @@ func (s Scanner) scanLicenses(target types.ScanTarget, options types.ScanOptions
313312
for _, license := range lib.Licenses {
314313
category, severity := scanner.Scan(license)
315314
langLicenses = append(langLicenses, types.DetectedLicense{
316-
Severity: severity,
317-
Category: category,
318-
PkgName: lib.Name,
319-
Name: license,
315+
Severity: severity,
316+
Category: category,
317+
PkgName: lib.Name,
318+
Name: license,
319+
// Lock files use app.FilePath - https://github.com/aquasecurity/trivy/blob/6ccc0a554b07b05fd049f882a1825a0e1e0aabe1/pkg/fanal/types/artifact.go#L245-L246
320+
// Applications use lib.FilePath - https://github.com/aquasecurity/trivy/blob/6ccc0a554b07b05fd049f882a1825a0e1e0aabe1/pkg/fanal/types/artifact.go#L93-L94
321+
FilePath: lo.Ternary(lib.FilePath != "", lib.FilePath, app.FilePath),
320322
Confidence: 1.0,
321323
})
322324
}

pkg/scanner/local/scan_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,127 @@ func TestScanner_Scan(t *testing.T) {
148148
Eosl: true,
149149
},
150150
},
151+
{
152+
name: "happy path license scanner",
153+
args: args{
154+
target: "alpine:latest",
155+
layerIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
156+
options: types.ScanOptions{
157+
Scanners: types.Scanners{types.LicenseScanner},
158+
},
159+
},
160+
fixtures: []string{"testdata/fixtures/happy.yaml"},
161+
applyLayersExpectation: ApplierApplyLayersExpectation{
162+
Args: ApplierApplyLayersArgs{
163+
BlobIDs: []string{"sha256:5216338b40a7b96416b8b9858974bbe4acc3096ee60acbc4dfb1ee02aecceb10"},
164+
},
165+
Returns: ApplierApplyLayersReturns{
166+
Detail: ftypes.ArtifactDetail{
167+
OS: ftypes.OS{
168+
Family: ftypes.Alpine,
169+
Name: "3.11",
170+
},
171+
Packages: []ftypes.Package{
172+
{
173+
Name: "musl",
174+
Version: "1.2.3",
175+
SrcName: "musl",
176+
SrcVersion: "1.2.3",
177+
Licenses: []string{"MIT"},
178+
Layer: ftypes.Layer{
179+
DiffID: "sha256:ebf12965380b39889c99a9c02e82ba465f887b45975b6e389d42e9e6a3857888",
180+
},
181+
},
182+
},
183+
Applications: []ftypes.Application{
184+
{
185+
Type: ftypes.GoModule,
186+
FilePath: "/app/go.mod",
187+
Libraries: []ftypes.Package{
188+
{
189+
Name: "github.com/google/uuid",
190+
Version: "1.6.0",
191+
FilePath: "",
192+
Layer: ftypes.Layer{
193+
DiffID: "sha256:0ea33a93585cf1917ba522b2304634c3073654062d5282c1346322967790ef33",
194+
},
195+
Licenses: []string{"LGPL"},
196+
},
197+
},
198+
},
199+
{
200+
Type: ftypes.PythonPkg,
201+
FilePath: "",
202+
Libraries: []ftypes.Package{
203+
{
204+
Name: "urllib3",
205+
Version: "3.2.1",
206+
FilePath: "/usr/lib/python/site-packages/urllib3-3.2.1/METADATA",
207+
Layer: ftypes.Layer{
208+
DiffID: "sha256:0ea33a93585cf1917ba522b2304634c3073654062d5282c1346322967790ef33",
209+
},
210+
Licenses: []string{"MIT"},
211+
},
212+
},
213+
},
214+
},
215+
},
216+
},
217+
},
218+
wantResults: types.Results{
219+
{
220+
Target: "OS Packages",
221+
Class: types.ClassLicense,
222+
Licenses: []types.DetectedLicense{
223+
{
224+
Severity: "UNKNOWN",
225+
Category: "unknown",
226+
PkgName: "musl",
227+
Name: "MIT",
228+
Confidence: 1,
229+
},
230+
},
231+
},
232+
{
233+
Target: "/app/go.mod",
234+
Class: types.ClassLicense,
235+
Licenses: []types.DetectedLicense{
236+
{
237+
Severity: "UNKNOWN",
238+
Category: "unknown",
239+
PkgName: "github.com/google/uuid",
240+
FilePath: "/app/go.mod",
241+
Name: "LGPL",
242+
Confidence: 1,
243+
Link: "",
244+
},
245+
},
246+
},
247+
{
248+
Target: "Python",
249+
Class: types.ClassLicense,
250+
Licenses: []types.DetectedLicense{
251+
{
252+
Severity: "UNKNOWN",
253+
Category: "unknown",
254+
PkgName: "urllib3",
255+
FilePath: "/usr/lib/python/site-packages/urllib3-3.2.1/METADATA",
256+
Name: "MIT",
257+
Confidence: 1,
258+
},
259+
},
260+
},
261+
{
262+
Target: "Loose File License(s)",
263+
Class: types.ClassLicenseFile,
264+
},
265+
},
266+
wantOS: ftypes.OS{
267+
Family: "alpine",
268+
Name: "3.11",
269+
Eosl: false,
270+
},
271+
},
151272
{
152273
name: "happy path with list all packages",
153274
args: args{

0 commit comments

Comments
 (0)