Skip to content

Commit

Permalink
gosec: handling of global nosec option when it is false (#5228)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Dec 15, 2024
1 parent 7ac2044 commit 87ea9ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/golinters/gosec/gosec.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ func convertGosecGlobals(globalOptionFromConfig any, conf gosec.Config) {
}

for k, v := range globalOptionMap {
conf.SetGlobal(gosec.GlobalOption(k), fmt.Sprintf("%v", v))
option := gosec.GlobalOption(k)

// Set nosec global option only if the value is true
// https://github.com/securego/gosec/blob/v2.21.4/analyzer.go#L572
if option == gosec.Nosec && v == false {
continue
}

conf.SetGlobal(option, fmt.Sprintf("%v", v))
}
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/golinters/gosec/gosec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ func Test_toGosecConfig(t *testing.T) {
},
},
},
{
desc: "with global settings nosec enabled",
settings: &config.GoSecSettings{
Config: map[string]any{
gosec.Globals: map[string]any{
string(gosec.Nosec): false,
string(gosec.Audit): "true",
},
},
},
expected: gosec.Config{
"global": map[gosec.GlobalOption]string{
"audit": "true",
},
},
},
{
desc: "rule specified setting",
settings: &config.GoSecSettings{
Expand Down
14 changes: 14 additions & 0 deletions pkg/golinters/gosec/testdata/gosec_nosec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//golangcitest:args -Egosec
//golangcitest:config_path testdata/gosec_nosec.yml
package testdata

import (
"crypto/md5" // want "G501: Blocklisted import crypto/md5: weak cryptographic primitive"
"log"
)

func Gosec() {
// #nosec G401
h := md5.New()
log.Print(h)
}
5 changes: 5 additions & 0 deletions pkg/golinters/gosec/testdata/gosec_nosec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters-settings:
gosec:
config:
global:
nosec: false

0 comments on commit 87ea9ef

Please sign in to comment.