Skip to content

Commit 4c82143

Browse files
committed
feat(gosec): add includes and excludes options.
1 parent db80e16 commit 4c82143

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

.golangci.example.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ linters-settings:
334334
# reason: "testing if blocked version constraint works." # Reason why the version constraint exists. (Optional)
335335
local_replace_directives: false # Set to true to raise lint issues for packages that are loaded from a local path via replace directive
336336

337+
gosec:
338+
# To select a subset of rules to run.
339+
# Available rules: https://github.com/securego/gosec#available-rules
340+
includes:
341+
- G401
342+
- G501
343+
- G204
344+
# To specify a set of rules to explicitly exclude.
345+
# Available rules: https://github.com/securego/gosec#available-rules
346+
excludes:
347+
- G204
348+
337349
govet:
338350
# report about shadowed variables
339351
check-shadowing: true

pkg/config/linters_settings.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type LintersSettings struct {
103103
Gomnd GoMndSettings
104104
GoModDirectives GoModDirectivesSettings
105105
Gomodguard GoModGuardSettings
106+
Gosec GoSecSettings
106107
Govet GovetSettings
107108
Ifshort IfshortSettings
108109
ImportAs ImportAsSettings
@@ -268,6 +269,11 @@ type GoModGuardSettings struct {
268269
} `mapstructure:"blocked"`
269270
}
270271

272+
type GoSecSettings struct {
273+
Includes []string
274+
Excludes []string
275+
}
276+
271277
type GovetSettings struct {
272278
CheckShadowing bool `mapstructure:"check-shadowing"`
273279
Settings map[string]map[string]interface{}

pkg/golinters/gosec.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@ import (
1313
"golang.org/x/tools/go/analysis"
1414
"golang.org/x/tools/go/packages"
1515

16+
"github.com/golangci/golangci-lint/pkg/config"
1617
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1718
"github.com/golangci/golangci-lint/pkg/lint/linter"
1819
"github.com/golangci/golangci-lint/pkg/result"
1920
)
2021

2122
const gosecName = "gosec"
2223

23-
func NewGosec() *goanalysis.Linter {
24+
func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
2425
var mu sync.Mutex
2526
var resIssues []goanalysis.Issue
2627

2728
gasConfig := gosec.NewConfig()
28-
enabledRules := rules.Generate()
29+
30+
var filters []rules.RuleFilter
31+
if settings != nil {
32+
filters = gosecRuleFilters(settings.Includes, settings.Excludes)
33+
}
34+
35+
ruleDefinitions := rules.Generate(filters...)
36+
2937
logger := log.New(ioutil.Discard, "", 0)
3038

3139
analyzer := &analysis.Analyzer{
@@ -40,7 +48,8 @@ func NewGosec() *goanalysis.Linter {
4048
).WithContextSetter(func(lintCtx *linter.Context) {
4149
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
4250
gosecAnalyzer := gosec.NewAnalyzer(gasConfig, true, logger)
43-
gosecAnalyzer.LoadRules(enabledRules.Builders())
51+
gosecAnalyzer.LoadRules(ruleDefinitions.Builders())
52+
4453
pkg := &packages.Package{
4554
Fset: pass.Fset,
4655
Syntax: pass.Files,
@@ -95,3 +104,18 @@ func NewGosec() *goanalysis.Linter {
95104
return resIssues
96105
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
97106
}
107+
108+
// based on https://github.com/securego/gosec/blob/569328eade2ccbad4ce2d0f21ee158ab5356a5cf/cmd/gosec/main.go#L170-L188
109+
func gosecRuleFilters(includes, excludes []string) []rules.RuleFilter {
110+
var filters []rules.RuleFilter
111+
112+
if len(includes) > 0 {
113+
filters = append(filters, rules.NewRuleFilter(false, includes...))
114+
}
115+
116+
if len(excludes) > 0 {
117+
filters = append(filters, rules.NewRuleFilter(true, excludes...))
118+
}
119+
120+
return filters
121+
}

pkg/lint/lintersdb/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
112112
var importAsCfg *config.ImportAsSettings
113113
var goModDirectivesCfg *config.GoModDirectivesSettings
114114
var tagliatelleCfg *config.TagliatelleSettings
115+
var gosecCfg *config.GoSecSettings
115116

116117
if m.cfg != nil {
117118
govetCfg = &m.cfg.LintersSettings.Govet
@@ -127,6 +128,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
127128
importAsCfg = &m.cfg.LintersSettings.ImportAs
128129
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
129130
tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle
131+
gosecCfg = &m.cfg.LintersSettings.Gosec
130132
}
131133

132134
const megacheckName = "megacheck"
@@ -190,7 +192,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
190192
WithLoadForGoAnalysis().
191193
WithPresets(linter.PresetStyle).
192194
WithURL("https://github.com/dominikh/go-tools/tree/master/stylecheck"),
193-
linter.NewConfig(golinters.NewGosec()).
195+
linter.NewConfig(golinters.NewGosec(gosecCfg)).
194196
WithSince("v1.0.0").
195197
WithLoadForGoAnalysis().
196198
WithPresets(linter.PresetBugs).

0 commit comments

Comments
 (0)