Repository navigation
add license all #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package output | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "reflect" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| const maxLicenseCellLen = 40 | ||
|
|
||
| // licenseCell renders a component's license, coloured by production risk. | ||
| func licenseCell(c colors, comp reflect.Value) string { | ||
| label := stringField(comp, "License") | ||
| if len(label) > maxLicenseCellLen { | ||
| label = label[:maxLicenseCellLen-3] + "..." | ||
| } | ||
| return c.license(label, stringField(comp, "LicenseRisk")) | ||
| } | ||
|
|
||
| func licenseRiskLabel(c colors, risk string) string { | ||
| switch risk { | ||
| case "high": | ||
| return c.crit("HIGH") | ||
| case "medium": | ||
| return c.med("MEDIUM") | ||
| } | ||
| return risk | ||
| } | ||
|
|
||
| // renderLicenseRisks lists every component whose license is risky in | ||
| // production (strong/network copyleft, non-commercial = red; weak copyleft = | ||
| // yellow). Image scans hide clean components from Findings, so this section is | ||
| // the only place such packages are guaranteed to show up. | ||
| func renderLicenseRisks(w io.Writer, c colors, components reflect.Value) { | ||
| if !components.IsValid() || components.Kind() != reflect.Slice { | ||
| return | ||
| } | ||
| type row struct { | ||
| pkg, eco, license, risk string | ||
| rank int | ||
| } | ||
| var rows []row | ||
| seen := map[string]bool{} | ||
| for i := 0; i < components.Len(); i++ { | ||
| comp := components.Index(i) | ||
| risk := stringField(comp, "LicenseRisk") | ||
| rank := 0 | ||
| switch risk { | ||
| case "high": | ||
| rank = 2 | ||
| case "medium": | ||
| rank = 1 | ||
| default: | ||
| continue | ||
| } | ||
| pkg := fmt.Sprintf("%s@%s", stringField(comp, "Name"), stringField(comp, "Version")) | ||
| if seen[pkg] { | ||
| continue | ||
| } | ||
| seen[pkg] = true | ||
| rows = append(rows, row{pkg: pkg, eco: strings.ToLower(stringField(comp, "System")), | ||
| license: stringField(comp, "License"), risk: risk, rank: rank}) | ||
| } | ||
| if len(rows) == 0 { | ||
| return | ||
| } | ||
| sort.SliceStable(rows, func(i, j int) bool { | ||
| if rows[i].rank != rows[j].rank { | ||
| return rows[i].rank > rows[j].rank | ||
| } | ||
| return rows[i].pkg < rows[j].pkg | ||
| }) | ||
|
|
||
| fmt.Fprintln(w, c.bold("License risks")+c.low(" (red = copyleft / non-commercial, yellow = weak copyleft)")) | ||
| g := &grid{} | ||
| g.add("RISK", "PACKAGE", "ECO", "LICENSE") | ||
| const maxRows = 200 | ||
| for i, r := range rows { | ||
| if i >= maxRows { | ||
| g.add(fmt.Sprintf("... (%d more - use --format json for the full list)", len(rows)-i)) | ||
| break | ||
| } | ||
| g.add(licenseRiskLabel(c, r.risk), r.pkg, r.eco, c.license(r.license, r.risk)) | ||
| } | ||
| g.render(w) | ||
| fmt.Fprintln(w) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package output | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func licenseReport(source, layer string) *tReport { | ||
| return &tReport{ | ||
| Source: source, | ||
| Totals: tTotals{Components: 3, Scanned: 3, WithVulns: 1, HIGH: 1, LicenseHigh: 1, LicenseMedium: 1}, | ||
| Components: []tComponent{ | ||
| {PURL: "pkg:npm/[email protected]", System: "NPM", Name: "gpl-lib", Version: "1.0.0", LayerDigest: layer, | ||
| License: "AGPL-3.0-only", LicenseRisk: "high"}, | ||
| {PURL: "pkg:npm/[email protected]", System: "NPM", Name: "mpl-lib", Version: "2.0.0", LayerDigest: layer, | ||
| License: "MPL-2.0", LicenseRisk: "medium", TopSeverity: "HIGH", VulnCount: 1, | ||
| Vulnerabilities: []tVuln{{Severity: "HIGH", ID: "CVE-2024-1", CVE: "CVE-2024-1"}}}, | ||
| {PURL: "pkg:npm/[email protected]", System: "NPM", Name: "mit-lib", Version: "3.0.0", LayerDigest: layer, | ||
| License: "MIT", LicenseRisk: "low"}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestTable_Render_LicenseColumnAndRisks(t *testing.T) { | ||
| for _, tc := range []struct{ name, source, layer string }{ | ||
| {"sbom", "sbom:app.cdx.json", ""}, | ||
| {"image", "image:app:latest", "sha256:layer0"}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| if err := (Table{NoColor: true}).Render(&buf, licenseReport(tc.source, tc.layer)); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| out := buf.String() | ||
| for _, want := range []string{"LICENSE", "MPL-2.0", "Risky licenses", "Weak-copyleft licenses", "License risks"} { | ||
| if !strings.Contains(out, want) { | ||
| t.Errorf("missing %q:\n%s", want, out) | ||
| } | ||
| } | ||
| // Image scans hide clean components from Findings, but the GPL lib | ||
| // must still surface in the License risks section. | ||
| risks := out[strings.Index(out, "License risks"):] | ||
| if !strings.Contains(risks, "[email protected]") || !strings.Contains(risks, "AGPL-3.0-only") { | ||
| t.Errorf("high-risk license missing from License risks:\n%s", risks) | ||
| } | ||
| if strings.Contains(risks, "mit-lib") { | ||
| t.Errorf("permissive license must not be listed as a risk:\n%s", risks) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTable_Render_HighRiskLicenseIsRed(t *testing.T) { | ||
| t.Setenv("NO_COLOR", "") | ||
| var buf bytes.Buffer | ||
| if err := (Table{}).Render(&buf, licenseReport("sbom:app.cdx.json", "")); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| out := buf.String() | ||
| if !strings.Contains(out, "\x1b[1;31mAGPL-3.0-only\x1b[0m") { | ||
| t.Errorf("high-risk license should be bold red:\n%q", out) | ||
| } | ||
| if !strings.Contains(out, "\x1b[33mMPL-2.0\x1b[0m") { | ||
| t.Errorf("weak-copyleft license should be yellow:\n%q", out) | ||
| } | ||
| if strings.Contains(out, "\x1b[1;31mMIT\x1b[0m") { | ||
| t.Errorf("permissive license must not be red:\n%q", out) | ||
| } | ||
| } | ||
|
|
||
| func TestSARIF_Render_Licenses(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| if err := (SARIF{}).Render(&buf, licenseReport("sbom:app.cdx.json", "")); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var doc sarifDoc | ||
| if err := json.Unmarshal(buf.Bytes(), &doc); err != nil { | ||
| t.Fatalf("SARIF not valid JSON: %v", err) | ||
| } | ||
| levels := map[string]string{} | ||
| var vulnLicense any | ||
| for _, res := range doc.Runs[0].Results { | ||
| levels[res.RuleID] = res.Level | ||
| if res.RuleID == "CVE-2024-1" { | ||
| vulnLicense = res.Properties["license"] | ||
| } | ||
| } | ||
| if levels["WOLFEE-LICENSE-HIGH-RISK"] != "error" { | ||
| t.Errorf("high-risk license result missing or not error: %v", levels) | ||
| } | ||
| if levels["WOLFEE-LICENSE-WEAK-COPYLEFT"] != "warning" { | ||
| t.Errorf("weak-copyleft license result missing or not warning: %v", levels) | ||
| } | ||
| if vulnLicense != "MPL-2.0" { | ||
| t.Errorf("vulnerability result must carry the package license, got %v", vulnLicense) | ||
| } | ||
| for id := range levels { | ||
| if strings.Contains(id, "LICENSE") && strings.Contains(buf.String(), "mit-lib is licensed") { | ||
| t.Errorf("permissive license must not produce a SARIF result") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestFixPlan_Render_LicenseRisksWithoutPlan(t *testing.T) { | ||
| type report struct { | ||
| Source string | ||
| FixPlan *tFixPlan | ||
| Components []tComponent | ||
| } | ||
| r := report{Components: []tComponent{ | ||
| {Name: "gpl-lib", Version: "1.0.0", System: "NPM", License: "GPL-3.0-only", LicenseRisk: "high"}, | ||
| }} | ||
| var buf bytes.Buffer | ||
| if err := (FixPlan{NoColor: true}).Render(&buf, r); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| out := buf.String() | ||
| if !strings.Contains(out, "No remediation plan available.") { | ||
| t.Errorf("expected the no-plan notice:\n%s", out) | ||
| } | ||
| if !strings.Contains(out, "License risks") || !strings.Contains(out, "[email protected]") { | ||
| t.Errorf("license risks must be rendered even without a remediation plan:\n%s", out) | ||
| } | ||
| } | ||
|
|
||
| func TestFixPlan_Render_License(t *testing.T) { | ||
| t.Setenv("NO_COLOR", "") | ||
| var buf bytes.Buffer | ||
| report := tFixPlanReport{FixPlan: &tFixPlan{Groups: []tFixGroup{{ | ||
| Direct: "express", CurrentVersion: "4.18.2", FixVersion: "4.21.2", | ||
| Packages: []tFixPackage{{Package: "[email protected]", License: "GPL-3.0", LicenseRisk: "high", | ||
| Vulnerabilities: []tFixVulnerability{{CVE: "CVE-1", Severity: "HIGH"}}}}, | ||
| }}}} | ||
| if err := (FixPlan{}).Render(&buf, report); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !strings.Contains(buf.String(), "LICENSE") || !strings.Contains(buf.String(), "\x1b[1;31mGPL-3.0\x1b[0m") { | ||
| t.Errorf("fix-plan must show the license in red:\n%q", buf.String()) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a report contains risky licenses but no vulnerabilities,
BuildFixPlanreturns nil and the earlier!plan.IsValid()branch exits before this new call executes. Consequently,--format fix-planreports only “No remediation plan available” and omits every license risk precisely for license-only findings; render the license section before that return or otherwise handle a nil remediation plan.Useful? React with 👍 / 👎.