Skip to content

Commit 057648f

Browse files
committed
feat(gosec): support configuration.
1 parent 4c82143 commit 057648f

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

.golangci.example.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,24 @@ linters-settings:
339339
# Available rules: https://github.com/securego/gosec#available-rules
340340
includes:
341341
- G401
342-
- G501
343-
- G204
342+
- G306
343+
- G101
344344
# To specify a set of rules to explicitly exclude.
345345
# Available rules: https://github.com/securego/gosec#available-rules
346346
excludes:
347347
- G204
348+
# To specify the configuration of rules.
349+
# The configuration of rules is not fully documented by gosec:
350+
# https://github.com/securego/gosec#configuration
351+
# https://github.com/securego/gosec/blob/569328eade2ccbad4ce2d0f21ee158ab5356a5cf/rules/rulelist.go#L60-L102
352+
config:
353+
G306: "0600"
354+
G101:
355+
pattern: "(?i)example"
356+
ignore_entropy: false
357+
entropy_threshold: "80.0"
358+
per_char_threshold: "3.0"
359+
truncate: "32"
348360

349361
govet:
350362
# report about shadowed variables

pkg/config/linters_settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ type GoModGuardSettings struct {
272272
type GoSecSettings struct {
273273
Includes []string
274274
Excludes []string
275+
Config map[string]interface{} `mapstructure:"config"`
275276
}
276277

277278
type GovetSettings struct {

pkg/golinters/gosec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"log"
88
"strconv"
9+
"strings"
910
"sync"
1011

1112
"github.com/securego/gosec/v2"
@@ -30,6 +31,12 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
3031
var filters []rules.RuleFilter
3132
if settings != nil {
3233
filters = gosecRuleFilters(settings.Includes, settings.Excludes)
34+
35+
for k, v := range settings.Config {
36+
// Uses ToUpper because the parsing of the map's key change the key to lowercase.
37+
// The value is not impacted by that: the case is respected.
38+
gasConfig.Set(strings.ToUpper(k), v)
39+
}
3340
}
3441

3542
ruleDefinitions := rules.Generate(filters...)

test/testdata/configs/gosec.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
linters-settings:
2+
gosec:
3+
includes:
4+
- G306
5+
- G101
6+
config:
7+
G306: "0666"
8+
G101:
9+
pattern: "(?i)simple"
10+
ignore_entropy: false
11+
entropy_threshold: "80.0"
12+
per_char_threshold: "3.0"
13+
truncate: "32"

test/testdata/gosec_rules_config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//args: -Egosec
2+
//config_path: testdata/configs/gosec.yml
3+
package testdata
4+
5+
import "io/ioutil"
6+
7+
const gosecToken = "62ebc7a03d6ca24dca1258fd4b48462f6fed1545"
8+
const gosecSimple = "62ebc7a03d6ca24dca1258fd4b48462f6fed1545" // ERROR "G101: Potential hardcoded credentials"
9+
10+
func gosecCustom() {
11+
ioutil.WriteFile("filename", []byte("test"), 0755) // ERROR "G306: Expect WriteFile permissions to be 0666 or less"
12+
}

0 commit comments

Comments
 (0)