Skip to content

Commit

Permalink
gosec: add configuration (#1930)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 24, 2021
1 parent db80e16 commit 8f3ad45
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 9 deletions.
24 changes: 24 additions & 0 deletions .golangci.example.yml
Expand Up @@ -334,6 +334,30 @@ linters-settings:
# reason: "testing if blocked version constraint works." # Reason why the version constraint exists. (Optional)
local_replace_directives: false # Set to true to raise lint issues for packages that are loaded from a local path via replace directive

gosec:
# To select a subset of rules to run.
# Available rules: https://github.com/securego/gosec#available-rules
includes:
- G401
- G306
- G101
# To specify a set of rules to explicitly exclude.
# Available rules: https://github.com/securego/gosec#available-rules
excludes:
- G204
# To specify the configuration of rules.
# The configuration of rules is not fully documented by gosec:
# https://github.com/securego/gosec#configuration
# https://github.com/securego/gosec/blob/569328eade2ccbad4ce2d0f21ee158ab5356a5cf/rules/rulelist.go#L60-L102
config:
G306: "0600"
G101:
pattern: "(?i)example"
ignore_entropy: false
entropy_threshold: "80.0"
per_char_threshold: "3.0"
truncate: "32"

govet:
# report about shadowed variables
check-shadowing: true
Expand Down
5 changes: 0 additions & 5 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -103,6 +103,7 @@ type LintersSettings struct {
Gomnd GoMndSettings
GoModDirectives GoModDirectivesSettings
Gomodguard GoModGuardSettings
Gosec GoSecSettings
Govet GovetSettings
Ifshort IfshortSettings
ImportAs ImportAsSettings
Expand Down Expand Up @@ -268,6 +269,12 @@ type GoModGuardSettings struct {
} `mapstructure:"blocked"`
}

type GoSecSettings struct {
Includes []string
Excludes []string
Config map[string]interface{} `mapstructure:"config"`
}

type GovetSettings struct {
CheckShadowing bool `mapstructure:"check-shadowing"`
Settings map[string]map[string]interface{}
Expand Down
37 changes: 34 additions & 3 deletions pkg/golinters/gosec.go
Expand Up @@ -6,26 +6,41 @@ import (
"io/ioutil"
"log"
"strconv"
"strings"
"sync"

"github.com/securego/gosec/v2"
"github.com/securego/gosec/v2/rules"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const gosecName = "gosec"

func NewGosec() *goanalysis.Linter {
func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

gasConfig := gosec.NewConfig()
enabledRules := rules.Generate()

var filters []rules.RuleFilter
if settings != nil {
filters = gosecRuleFilters(settings.Includes, settings.Excludes)

for k, v := range settings.Config {
// Uses ToUpper because the parsing of the map's key change the key to lowercase.
// The value is not impacted by that: the case is respected.
gasConfig.Set(strings.ToUpper(k), v)
}
}

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

logger := log.New(ioutil.Discard, "", 0)

analyzer := &analysis.Analyzer{
Expand All @@ -40,7 +55,8 @@ func NewGosec() *goanalysis.Linter {
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
gosecAnalyzer := gosec.NewAnalyzer(gasConfig, true, logger)
gosecAnalyzer.LoadRules(enabledRules.Builders())
gosecAnalyzer.LoadRules(ruleDefinitions.Builders())

pkg := &packages.Package{
Fset: pass.Fset,
Syntax: pass.Files,
Expand Down Expand Up @@ -95,3 +111,18 @@ func NewGosec() *goanalysis.Linter {
return resIssues
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
}

// based on https://github.com/securego/gosec/blob/569328eade2ccbad4ce2d0f21ee158ab5356a5cf/cmd/gosec/main.go#L170-L188
func gosecRuleFilters(includes, excludes []string) []rules.RuleFilter {
var filters []rules.RuleFilter

if len(includes) > 0 {
filters = append(filters, rules.NewRuleFilter(false, includes...))
}

if len(excludes) > 0 {
filters = append(filters, rules.NewRuleFilter(true, excludes...))
}

return filters
}
4 changes: 3 additions & 1 deletion pkg/lint/lintersdb/manager.go
Expand Up @@ -112,6 +112,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var importAsCfg *config.ImportAsSettings
var goModDirectivesCfg *config.GoModDirectivesSettings
var tagliatelleCfg *config.TagliatelleSettings
var gosecCfg *config.GoSecSettings

if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet
Expand All @@ -127,6 +128,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
importAsCfg = &m.cfg.LintersSettings.ImportAs
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle
gosecCfg = &m.cfg.LintersSettings.Gosec
}

const megacheckName = "megacheck"
Expand Down Expand Up @@ -190,7 +192,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle).
WithURL("https://github.com/dominikh/go-tools/tree/master/stylecheck"),
linter.NewConfig(golinters.NewGosec()).
linter.NewConfig(golinters.NewGosec(gosecCfg)).
WithSince("v1.0.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
Expand Down
13 changes: 13 additions & 0 deletions test/testdata/configs/gosec.yml
@@ -0,0 +1,13 @@
linters-settings:
gosec:
includes:
- G306
- G101
config:
G306: "0666"
G101:
pattern: "(?i)simple"
ignore_entropy: false
entropy_threshold: "80.0"
per_char_threshold: "3.0"
truncate: "32"
12 changes: 12 additions & 0 deletions test/testdata/gosec_rules_config.go
@@ -0,0 +1,12 @@
//args: -Egosec
//config_path: testdata/configs/gosec.yml
package testdata

import "io/ioutil"

const gosecToken = "62ebc7a03d6ca24dca1258fd4b48462f6fed1545"
const gosecSimple = "62ebc7a03d6ca24dca1258fd4b48462f6fed1545" // ERROR "G101: Potential hardcoded credentials"

func gosecCustom() {
ioutil.WriteFile("filename", []byte("test"), 0755) // ERROR "G306: Expect WriteFile permissions to be 0666 or less"
}

0 comments on commit 8f3ad45

Please sign in to comment.