Skip to content

Commit 87ea9ef

Browse files
authored
gosec: handling of global nosec option when it is false (#5228)
1 parent 7ac2044 commit 87ea9ef

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

pkg/golinters/gosec/gosec.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,15 @@ func convertGosecGlobals(globalOptionFromConfig any, conf gosec.Config) {
184184
}
185185

186186
for k, v := range globalOptionMap {
187-
conf.SetGlobal(gosec.GlobalOption(k), fmt.Sprintf("%v", v))
187+
option := gosec.GlobalOption(k)
188+
189+
// Set nosec global option only if the value is true
190+
// https://github.com/securego/gosec/blob/v2.21.4/analyzer.go#L572
191+
if option == gosec.Nosec && v == false {
192+
continue
193+
}
194+
195+
conf.SetGlobal(option, fmt.Sprintf("%v", v))
188196
}
189197
}
190198

pkg/golinters/gosec/gosec_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ func Test_toGosecConfig(t *testing.T) {
3939
},
4040
},
4141
},
42+
{
43+
desc: "with global settings nosec enabled",
44+
settings: &config.GoSecSettings{
45+
Config: map[string]any{
46+
gosec.Globals: map[string]any{
47+
string(gosec.Nosec): false,
48+
string(gosec.Audit): "true",
49+
},
50+
},
51+
},
52+
expected: gosec.Config{
53+
"global": map[gosec.GlobalOption]string{
54+
"audit": "true",
55+
},
56+
},
57+
},
4258
{
4359
desc: "rule specified setting",
4460
settings: &config.GoSecSettings{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//golangcitest:args -Egosec
2+
//golangcitest:config_path testdata/gosec_nosec.yml
3+
package testdata
4+
5+
import (
6+
"crypto/md5" // want "G501: Blocklisted import crypto/md5: weak cryptographic primitive"
7+
"log"
8+
)
9+
10+
func Gosec() {
11+
// #nosec G401
12+
h := md5.New()
13+
log.Print(h)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
gosec:
3+
config:
4+
global:
5+
nosec: false

0 commit comments

Comments
 (0)