Skip to content

Commit

Permalink
filter issues according to the severity and confidence
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Oct 18, 2021
1 parent 680f3e6 commit c1ec2a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ linters-settings:
# Available rules: https://github.com/securego/gosec#available-rules
excludes:
- G204
# Filter out the issues with a lower severity than the given value. Valid options are: low, medium, high.
serveity: "high"
# Filter out the issues with a lower confidence than the given value. Valid options are: low, medium, high.
confidence: "medium"
# To specify the configuration of rules.
# The configuration of rules is not fully documented by gosec:
# https://github.com/securego/gosec#configuration
Expand Down
8 changes: 5 additions & 3 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ type GoModGuardSettings struct {
}

type GoSecSettings struct {
Includes []string
Excludes []string
Config map[string]interface{} `mapstructure:"config"`
Includes []string
Excludes []string
Severity string
Confidence string
Config map[string]interface{} `mapstructure:"config"`
}

type GovetSettings struct {
Expand Down
34 changes: 34 additions & 0 deletions pkg/golinters/gosec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"

"github.com/pkg/errors"
"github.com/securego/gosec/v2"
"github.com/securego/gosec/v2/rules"
"golang.org/x/tools/go/analysis"
Expand Down Expand Up @@ -68,7 +69,16 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
if len(issues) == 0 {
return nil, nil
}
severity, err := convertToScore(settings.Severity)
if err != nil {
lintCtx.Log.Warnf("Provided severity %s, use low instead. Valid options: low, medium, high", err)
}

confidence, err := convertToScore(settings.Confidence)
if err != nil {
lintCtx.Log.Warnf("Provided string %s, use low instead. Valid options: low, medium, high", err)
}
issues = filterIssues(issues, severity, confidence)
res := make([]goanalysis.Issue, 0, len(issues))
for _, i := range issues {
text := fmt.Sprintf("%s: %s", i.RuleID, i.What) // TODO: use severity and confidence
Expand Down Expand Up @@ -126,3 +136,27 @@ func gosecRuleFilters(includes, excludes []string) []rules.RuleFilter {

return filters
}

func convertToScore(str string) (gosec.Score, error) {
str = strings.ToLower(str)
switch str {
case "", "low":
return gosec.Low, nil
case "medium":
return gosec.Medium, nil
case "high":
return gosec.High, nil
default:
return gosec.Low, errors.Errorf("'%s' not valid", str)
}
}

func filterIssues(issues []*gosec.Issue, severity, confidence gosec.Score) []*gosec.Issue {
res := make([]*gosec.Issue, 0)
for _, issue := range issues {
if issue.Severity >= severity && issue.Confidence >= confidence {
res = append(res, issue)
}
}
return res
}
2 changes: 2 additions & 0 deletions test/testdata/configs/gosec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ linters-settings:
includes:
- G306
- G101
serveity: "low"
confidence: "low"
config:
G306: "0666"
G101:
Expand Down

0 comments on commit c1ec2a0

Please sign in to comment.