|
| 1 | +package scanners |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// TrivyLicenseOutput represents the JSON output from Trivy license scan |
| 11 | +type TrivyLicenseOutput struct { |
| 12 | + Results []struct { |
| 13 | + Target string `json:"Target"` |
| 14 | + Class string `json:"Class"` |
| 15 | + Licenses []struct { |
| 16 | + Severity string `json:"Severity"` |
| 17 | + Category string `json:"Category"` |
| 18 | + PkgName string `json:"PkgName"` |
| 19 | + FilePath string `json:"FilePath"` |
| 20 | + Name string `json:"Name"` |
| 21 | + Confidence float64 `json:"Confidence"` |
| 22 | + Link string `json:"Link"` |
| 23 | + } `json:"Licenses"` |
| 24 | + } `json:"Results"` |
| 25 | +} |
| 26 | + |
| 27 | +// LicenseConfig holds license scanning configuration |
| 28 | +type LicenseConfig struct { |
| 29 | + Enabled bool |
| 30 | + Deny []string |
| 31 | + Allow []string |
| 32 | +} |
| 33 | + |
| 34 | +// runLicenseScan executes Trivy license scanning |
| 35 | +func (o *Orchestrator) runLicenseScan() (*ScanResult, error) { |
| 36 | + result := &ScanResult{ |
| 37 | + Tool: "licenses", |
| 38 | + Findings: []Finding{}, |
| 39 | + Summary: FindingSummary{}, |
| 40 | + } |
| 41 | + |
| 42 | + // Check if Trivy is installed |
| 43 | + if _, err := exec.LookPath("trivy"); err != nil { |
| 44 | + result.Status = "error" |
| 45 | + result.Error = fmt.Errorf("trivy not installed (required for license scanning)") |
| 46 | + return result, result.Error |
| 47 | + } |
| 48 | + |
| 49 | + // Run trivy with license scanning |
| 50 | + cmd := exec.Command("trivy", "fs", ".", "--scanners", "license", "--format", "json") |
| 51 | + |
| 52 | + // Add skip-dirs for exclusions |
| 53 | + for _, path := range o.options.ExcludePaths { |
| 54 | + cmd.Args = append(cmd.Args, "--skip-dirs", path) |
| 55 | + } |
| 56 | + |
| 57 | + cmd.Dir = o.projectDir |
| 58 | + |
| 59 | + output, err := cmd.CombinedOutput() |
| 60 | + if err != nil { |
| 61 | + if len(output) == 0 || !strings.Contains(string(output), "Results") { |
| 62 | + result.Status = "error" |
| 63 | + result.Error = fmt.Errorf("trivy license scan failed: %w", err) |
| 64 | + return result, result.Error |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Extract JSON |
| 69 | + jsonOutput := extractJSON(output) |
| 70 | + if len(jsonOutput) == 0 { |
| 71 | + result.Status = "success" |
| 72 | + return result, nil |
| 73 | + } |
| 74 | + |
| 75 | + // Parse output |
| 76 | + var licenseOut TrivyLicenseOutput |
| 77 | + if err := json.Unmarshal(jsonOutput, &licenseOut); err != nil { |
| 78 | + result.Status = "error" |
| 79 | + result.Error = fmt.Errorf("failed to parse trivy license output: %w", err) |
| 80 | + return result, result.Error |
| 81 | + } |
| 82 | + |
| 83 | + // Convert to findings, applying deny/allow lists |
| 84 | + for _, scanResult := range licenseOut.Results { |
| 85 | + for _, lic := range scanResult.Licenses { |
| 86 | + // Check if this license is a violation |
| 87 | + if !o.isLicenseViolation(lic.Name) { |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + severity := classifyLicenseSeverity(lic.Name, lic.Category) |
| 92 | + |
| 93 | + finding := Finding{ |
| 94 | + File: scanResult.Target, |
| 95 | + Severity: severity, |
| 96 | + Message: fmt.Sprintf("License violation: %s uses %s license", lic.PkgName, lic.Name), |
| 97 | + RuleID: fmt.Sprintf("license-%s", strings.ToLower(strings.ReplaceAll(lic.Name, " ", "-"))), |
| 98 | + Tool: "licenses", |
| 99 | + } |
| 100 | + |
| 101 | + result.Findings = append(result.Findings, finding) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Update summary |
| 106 | + for _, finding := range result.Findings { |
| 107 | + result.Summary.Total++ |
| 108 | + switch finding.Severity { |
| 109 | + case "CRITICAL": |
| 110 | + result.Summary.Critical++ |
| 111 | + case "HIGH": |
| 112 | + result.Summary.High++ |
| 113 | + case "MEDIUM": |
| 114 | + result.Summary.Medium++ |
| 115 | + case "LOW": |
| 116 | + result.Summary.Low++ |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + result.Status = "success" |
| 121 | + return result, nil |
| 122 | +} |
| 123 | + |
| 124 | +// isLicenseViolation checks if a license should be flagged based on deny/allow lists |
| 125 | +func (o *Orchestrator) isLicenseViolation(licenseName string) bool { |
| 126 | + lower := strings.ToLower(licenseName) |
| 127 | + |
| 128 | + // If deny list is configured, only flag denied licenses |
| 129 | + if len(o.options.LicenseConfig.Deny) > 0 { |
| 130 | + for _, denied := range o.options.LicenseConfig.Deny { |
| 131 | + if matchLicense(lower, strings.ToLower(denied)) { |
| 132 | + return true |
| 133 | + } |
| 134 | + } |
| 135 | + return false |
| 136 | + } |
| 137 | + |
| 138 | + // If allow list is configured, flag anything not allowed |
| 139 | + if len(o.options.LicenseConfig.Allow) > 0 { |
| 140 | + for _, allowed := range o.options.LicenseConfig.Allow { |
| 141 | + if matchLicense(lower, strings.ToLower(allowed)) { |
| 142 | + return false |
| 143 | + } |
| 144 | + } |
| 145 | + return true |
| 146 | + } |
| 147 | + |
| 148 | + // No lists configured: flag known copyleft licenses by default |
| 149 | + copyleftPrefixes := []string{"gpl", "agpl", "lgpl", "sspl", "eupl"} |
| 150 | + for _, prefix := range copyleftPrefixes { |
| 151 | + if strings.Contains(lower, prefix) { |
| 152 | + return true |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return false |
| 157 | +} |
| 158 | + |
| 159 | +// matchLicense checks if a license name matches a pattern (supports trailing wildcard) |
| 160 | +func matchLicense(licenseName, pattern string) bool { |
| 161 | + if strings.HasSuffix(pattern, "*") { |
| 162 | + prefix := strings.TrimSuffix(pattern, "*") |
| 163 | + return strings.HasPrefix(licenseName, prefix) |
| 164 | + } |
| 165 | + return licenseName == pattern |
| 166 | +} |
| 167 | + |
| 168 | +// classifyLicenseSeverity assigns severity based on license type |
| 169 | +func classifyLicenseSeverity(licenseName, category string) string { |
| 170 | + lower := strings.ToLower(licenseName) |
| 171 | + |
| 172 | + // Strong copyleft = HIGH |
| 173 | + if strings.Contains(lower, "agpl") || strings.Contains(lower, "sspl") { |
| 174 | + return "HIGH" |
| 175 | + } |
| 176 | + |
| 177 | + // Weak copyleft = LOW (check before GPL since LGPL contains "gpl") |
| 178 | + if strings.Contains(lower, "lgpl") || strings.Contains(lower, "mpl") { |
| 179 | + return "LOW" |
| 180 | + } |
| 181 | + |
| 182 | + // Copyleft = MEDIUM |
| 183 | + if strings.Contains(lower, "gpl") || strings.Contains(lower, "eupl") { |
| 184 | + return "MEDIUM" |
| 185 | + } |
| 186 | + |
| 187 | + // Restricted category from Trivy |
| 188 | + if strings.ToLower(category) == "restricted" { |
| 189 | + return "HIGH" |
| 190 | + } |
| 191 | + |
| 192 | + return "MEDIUM" |
| 193 | +} |
0 commit comments