Repository navigation
License listed in trivy.yaml is classified at random between the configured category and its built-in one #11275
Replies: 3 comments
Root causeThere are two layers to this. 1. How the category lists are assembled. licenseCategories[types.CategoryForbidden] = f.LicenseForbidden.Value()
licenseCategories[types.CategoryRestricted] = f.LicenseRestricted.Value()
...
licenseCategories[types.CategoryNotice] = f.LicenseNotice.Value()Each flag's 2. How a category is picked. for category, names := range s.categories {
if normalizedNames.Intersection(names).Size() > 0 {
return category
}
}Go randomises map iteration order, so when a license is in two categories the winner differs per call. Confirmed at the source level: building a A second problem: the documented workaround
But the generated license:
confidencelevel: "0.9"
full: false
ignored: []So there's no supported way to produce a config with every category set, and setting just one category — the natural thing to do — is exactly what triggers the bug. Proposed fixI have a patch, built and tested locally (75 insertions, 9 deletions across three files), in two separable parts. Part 1 (determinism). Add an ordered list of categories, most severe first, to Part 2 (make configuration actually move a license). In Results with the patched binary vs. stock 0.74.0, counting categories across five MIT packages:
Questions
|
|
Opened #11276 with Part 1 only, so the change stays one thing. Part 2 and the |
|
Thanks for the detailed report. The classification problem is tracked in #11736, and the missing lists in Until #11736 is fixed, write the built-in category of the license in |
Uh oh!
There was an error while loading. Please reload this page.
Description
Naming a license in a category in
trivy.yaml(for example underlicense.forbidden) adds it to that category but does not remove it from the built-in category it already belongs to. The license then belongs to two categories at once, and Trivy picks one of them per package, differently on each run. The same input, scanned with the same command, produces a different classification and a different severity every time.This matters because a license policy is commonly used as a gate in CI: a classification that changes between runs makes the build pass or fail at random on unchanged code.
Desired Behavior
A license named in a category in the configuration file should be classified as that category, deterministically. If overriding a category is meant to be additive rather than a move, the documentation should say so, and a license that ends up in more than one category should resolve by a defined rule rather than at random.
Actual Behavior
MIT is in Trivy's built-in
noticecategory. Atrivy.yamlcontaining:does not move MIT out of
notice. Scanning apackage-lock.jsonwhose five packages are all MIT then reports some of them asforbidden/CRITICAL and the rest asnotice/LOW, with the split changing on every run.Five consecutive runs of one identical command on one unchanged directory, counting categories across the five packages:
Table output for a single package across three runs:
Control experiments, four runs each:
license.restricted: [MIT]: same instability, alternating betweenrestrictedand the built-innotice.license.notice: [MIT]: stable, alwaysnotice. MIT is already in that category, so there is no second category to choose from.license.forbidden: [GPL-3.0]: stable, MIT alwaysnotice. No package carries GPL-3.0, so nothing is in two categories.license.forbidden: [MIT]together withlicense.notice: []: stable, all five packagesforbiddenon every run. This is the workaround: also override the category the license currently sits in, so it ends up in exactly one.--parallel 1andGOMAXPROCS=1make no difference, so this does not appear to be about the parallelism of the file walk. The alternation looks consistent with map iteration order somewhere in category resolution, but that is a guess.Reproduction Steps
Create a directory with the two files below and run the scan five times against the unchanged directory:
The
integrityvalues in the lock file are placeholders; Trivy does not verify them. Licenses are read directly out of the lock file, so nonpm installis required to reproduce this.Target
Filesystem
Scanner
License
Output Format
JSON and Table
Mode
Standalone
Debug Output
License-related lines from
trivy fs --debug --scanners license .:Operating System
Ubuntu Linux 6.8.0-138-generic, Trivy run in the official Docker image
Version
Checklist
trivy clean --allAll reactions