Skip to content

Commit c1ec2a0

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

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

Diff for: .golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ linters-settings:
369369
# Available rules: https://github.com/securego/gosec#available-rules
370370
excludes:
371371
- G204
372+
# Filter out the issues with a lower severity than the given value. Valid options are: low, medium, high.
373+
serveity: "high"
374+
# Filter out the issues with a lower confidence than the given value. Valid options are: low, medium, high.
375+
confidence: "medium"
372376
# To specify the configuration of rules.
373377
# The configuration of rules is not fully documented by gosec:
374378
# https://github.com/securego/gosec#configuration

Diff for: pkg/config/linters_settings.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,11 @@ type GoModGuardSettings struct {
294294
}
295295

296296
type GoSecSettings struct {
297-
Includes []string
298-
Excludes []string
299-
Config map[string]interface{} `mapstructure:"config"`
297+
Includes []string
298+
Excludes []string
299+
Severity string
300+
Confidence string
301+
Config map[string]interface{} `mapstructure:"config"`
300302
}
301303

302304
type GovetSettings struct {

Diff for: 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+
}

Diff for: 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)