Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc262bb

Browse files
authoredOct 27, 2021
gosec: filter issues according to the severity and confidence (#2295)
1 parent f500e4c commit cc262bb

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-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+
severity: "low"
376+
# Filter out the issues with a lower confidence than the given value. Valid options are: low, medium, high.
377+
confidence: "low"
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
@@ -297,6 +297,8 @@ type GoModGuardSettings struct {
297297
type GoSecSettings struct {
298298
Includes []string
299299
Excludes []string
300+
Severity string
301+
Confidence string
300302
ExcludeGenerated bool `mapstructure:"exclude-generated"`
301303
Config map[string]interface{} `mapstructure:"config"`
302304
}

‎pkg/golinters/gosec.go

+36
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("The provided severity %v", err)
75+
}
7176

77+
confidence, err := convertToScore(settings.Confidence)
78+
if err != nil {
79+
lintCtx.Log.Warnf("The provided confidence %v", 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,29 @@ func gosecRuleFilters(includes, excludes []string) []rules.RuleFilter {
126136

127137
return filters
128138
}
139+
140+
// code borrowed from https://github.com/securego/gosec/blob/69213955dacfd560562e780f723486ef1ca6d486/cmd/gosec/main.go#L250-L262
141+
func convertToScore(str string) (gosec.Score, error) {
142+
str = strings.ToLower(str)
143+
switch str {
144+
case "", "low":
145+
return gosec.Low, nil
146+
case "medium":
147+
return gosec.Medium, nil
148+
case "high":
149+
return gosec.High, nil
150+
default:
151+
return gosec.Low, errors.Errorf("'%s' is invalid, use low instead. Valid options: low, medium, high", str)
152+
}
153+
}
154+
155+
// code borrowed from https://github.com/securego/gosec/blob/69213955dacfd560562e780f723486ef1ca6d486/cmd/gosec/main.go#L264-L276
156+
func filterIssues(issues []*gosec.Issue, severity, confidence gosec.Score) []*gosec.Issue {
157+
res := make([]*gosec.Issue, 0)
158+
for _, issue := range issues {
159+
if issue.Severity >= severity && issue.Confidence >= confidence {
160+
res = append(res, issue)
161+
}
162+
}
163+
return res
164+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters-settings:
2+
gosec:
3+
severity: "medium"
4+
confidence: "medium"
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//args: -Egosec
2+
//config_path: testdata/configs/gosec_severity_confidence.yml
3+
package testdata
4+
5+
import (
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
)
10+
11+
var url string = "https://www.abcdefghijk.com"
12+
13+
func gosecVariableURL() {
14+
resp, err := http.Get(url) // ERROR "G107: Potential HTTP request made with variable url"
15+
if err != nil {
16+
panic(err)
17+
}
18+
defer resp.Body.Close()
19+
body, err := ioutil.ReadAll(resp.Body)
20+
if err != nil {
21+
panic(err)
22+
}
23+
fmt.Printf("%s", body)
24+
}
25+
26+
func gosecHardcodedCredentials() {
27+
username := "admin"
28+
var password = "f62e5bcda4fae4f82370da0c6f20697b8f8447ef"
29+
30+
fmt.Println("Doing something with: ", username, password)
31+
}

0 commit comments

Comments
 (0)
Please sign in to comment.