|
9 | 9 | "strings"
|
10 | 10 | "sync"
|
11 | 11 |
|
| 12 | + "github.com/pkg/errors" |
12 | 13 | "github.com/securego/gosec/v2"
|
13 | 14 | "github.com/securego/gosec/v2/rules"
|
14 | 15 | "golang.org/x/tools/go/analysis"
|
@@ -68,7 +69,16 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
|
68 | 69 | if len(issues) == 0 {
|
69 | 70 | return nil, nil
|
70 | 71 | }
|
| 72 | + severity, err := convertToScore(settings.Severity) |
| 73 | + if err != nil { |
| 74 | + lintCtx.Log.Warnf("Provided severity %s, use low instead. Valid options: low, medium, high", err) |
| 75 | + } |
71 | 76 |
|
| 77 | + confidence, err := convertToScore(settings.Confidence) |
| 78 | + if err != nil { |
| 79 | + lintCtx.Log.Warnf("Provided string %s, use low instead. Valid options: low, medium, high", err) |
| 80 | + } |
| 81 | + issues = filterIssues(issues, severity, confidence) |
72 | 82 | res := make([]goanalysis.Issue, 0, len(issues))
|
73 | 83 | for _, i := range issues {
|
74 | 84 | text := fmt.Sprintf("%s: %s", i.RuleID, i.What) // TODO: use severity and confidence
|
@@ -126,3 +136,27 @@ func gosecRuleFilters(includes, excludes []string) []rules.RuleFilter {
|
126 | 136 |
|
127 | 137 | return filters
|
128 | 138 | }
|
| 139 | + |
| 140 | +func convertToScore(str string) (gosec.Score, error) { |
| 141 | + str = strings.ToLower(str) |
| 142 | + switch str { |
| 143 | + case "", "low": |
| 144 | + return gosec.Low, nil |
| 145 | + case "medium": |
| 146 | + return gosec.Medium, nil |
| 147 | + case "high": |
| 148 | + return gosec.High, nil |
| 149 | + default: |
| 150 | + return gosec.Low, errors.Errorf("'%s' not valid", str) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func filterIssues(issues []*gosec.Issue, severity, confidence gosec.Score) []*gosec.Issue { |
| 155 | + res := make([]*gosec.Issue, 0) |
| 156 | + for _, issue := range issues { |
| 157 | + if issue.Severity >= severity && issue.Confidence >= confidence { |
| 158 | + res = append(res, issue) |
| 159 | + } |
| 160 | + } |
| 161 | + return res |
| 162 | +} |
0 commit comments