Skip to content

Commit 6b9bd1f

Browse files
committed
filter issues according to the severity and confidence
Signed-off-by: Ryan Leung <[email protected]>
1 parent 6edca92 commit 6b9bd1f

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

.golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ linters-settings:
371371
- G204
372372
# Exclude generated files
373373
exclude-generated: true
374+
# Filter out the issues with a lower severity than the given value. Valid options are: low, medium, high.
375+
serveity: "high"
376+
# Filter out the issues with a lower confidence than the given value. Valid options are: low, medium, high.
377+
confidence: "medium"
374378
# To specify the configuration of rules.
375379
# The configuration of rules is not fully documented by gosec:
376380
# https://github.com/securego/gosec#configuration

pkg/config/linters_settings.go

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ type GoModGuardSettings struct {
296296
type GoSecSettings struct {
297297
Includes []string
298298
Excludes []string
299+
Severity string
300+
Confidence string
299301
ExcludeGenerated bool `mapstructure:"exclude-generated"`
300302
Config map[string]interface{} `mapstructure:"config"`
301303
}

pkg/golinters/gosec.go

+34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"sync"
1111

12+
"github.com/pkg/errors"
1213
"github.com/securego/gosec/v2"
1314
"github.com/securego/gosec/v2/rules"
1415
"golang.org/x/tools/go/analysis"
@@ -68,7 +69,16 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
6869
if len(issues) == 0 {
6970
return nil, nil
7071
}
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+
}
7176

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)
7282
res := make([]goanalysis.Issue, 0, len(issues))
7383
for _, i := range issues {
7484
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 {
126136

127137
return filters
128138
}
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+
}

test/testdata/configs/gosec.yml

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ linters-settings:
33
includes:
44
- G306
55
- G101
6+
serveity: "low"
7+
confidence: "low"
68
config:
79
G306: "0666"
810
G101:

0 commit comments

Comments
 (0)