Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gosec: handling of global nosec option when it is false #5228

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading