mirror of
https://github.com/securego/gosec.git
synced 2025-03-01 04:33:29 +00:00
address review comments
This commit is contained in:
parent
af25ac1f6e
commit
25d74c6b20
4 changed files with 41 additions and 5 deletions
|
@ -101,7 +101,7 @@ func (gas *Analyzer) Process(packagePath string) error {
|
|||
}
|
||||
|
||||
packageConfig := loader.Config{Build: &build.Default, ParserMode: parser.ParseComments}
|
||||
packageFiles := make([]string, 0)
|
||||
var packageFiles []string
|
||||
for _, filename := range basePackage.GoFiles {
|
||||
packageFiles = append(packageFiles, path.Join(packagePath, filename))
|
||||
}
|
||||
|
|
|
@ -52,6 +52,27 @@ var _ = Describe("Analyzer", func() {
|
|||
|
||||
})
|
||||
|
||||
It("should be able to analyze mulitple Go files", func() {
|
||||
analyzer.LoadRules(rules.Generate().Builders()...)
|
||||
pkg := testutils.NewTestPackage()
|
||||
defer pkg.Close()
|
||||
pkg.AddFile("foo.go", `
|
||||
package main
|
||||
func main(){
|
||||
bar()
|
||||
}`)
|
||||
pkg.AddFile("bar.go", `
|
||||
package main
|
||||
func bar(){
|
||||
println("package has two files!")
|
||||
}`)
|
||||
pkg.Build()
|
||||
err := analyzer.Process(pkg.Path)
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
_, metrics := analyzer.Report()
|
||||
Expect(metrics.NumFiles).To(Equal(2))
|
||||
})
|
||||
|
||||
It("should find errors when nosec is not in use", func() {
|
||||
|
||||
// Rule for MD5 weak crypto usage
|
||||
|
|
|
@ -91,7 +91,7 @@ func usage() {
|
|||
flag.PrintDefaults()
|
||||
fmt.Fprint(os.Stderr, "\n\nRULES:\n\n")
|
||||
|
||||
// sorted rule list for eas of reading
|
||||
// sorted rule list for ease of reading
|
||||
rl := rules.Generate()
|
||||
keys := make([]string, 0, len(rl))
|
||||
for key := range rl {
|
||||
|
@ -126,13 +126,19 @@ func loadConfig(configFile string) (gas.Config, error) {
|
|||
func loadRules(include, exclude string) rules.RuleList {
|
||||
var filters []rules.RuleFilter
|
||||
if include != "" {
|
||||
log.Printf("including rules: %s", include)
|
||||
including := strings.Split(include, ",")
|
||||
filters = append(filters, rules.NewRuleFilter(false, including...))
|
||||
} else {
|
||||
log.Println("including rules: default")
|
||||
}
|
||||
|
||||
if exclude != "" {
|
||||
log.Printf("excluding rules: %s", exclude)
|
||||
excluding := strings.Split(exclude, ",")
|
||||
filters = append(filters, rules.NewRuleFilter(true, excluding...))
|
||||
} else {
|
||||
log.Println("excluding rules: default")
|
||||
}
|
||||
return rules.Generate(filters...)
|
||||
}
|
||||
|
@ -186,6 +192,9 @@ func main() {
|
|||
|
||||
// Load enabled rule definitions
|
||||
ruleDefinitions := loadRules(*flagRulesInclude, *flagRulesExclude)
|
||||
if len(ruleDefinitions) <= 0 {
|
||||
log.Fatal("cannot continue: no rules are configured.")
|
||||
}
|
||||
|
||||
// Create the analyzer
|
||||
analyzer := gas.NewAnalyzer(config, logger)
|
||||
|
|
12
config.go
12
config.go
|
@ -8,6 +8,12 @@ import (
|
|||
"io/ioutil"
|
||||
)
|
||||
|
||||
const (
|
||||
// Globals are applicable to all rules and used for general
|
||||
// configuration settings for gas.
|
||||
Globals = "global"
|
||||
)
|
||||
|
||||
// Config is used to provide configuration and customization to each of the rules.
|
||||
type Config map[string]interface{}
|
||||
|
||||
|
@ -16,7 +22,7 @@ type Config map[string]interface{}
|
|||
// or from a *os.File.
|
||||
func NewConfig() Config {
|
||||
cfg := make(Config)
|
||||
cfg["global"] = make(map[string]string)
|
||||
cfg[Globals] = make(map[string]string)
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
@ -60,7 +66,7 @@ func (c Config) Set(section string, value interface{}) {
|
|||
|
||||
// GetGlobal returns value associated with global configuration option
|
||||
func (c Config) GetGlobal(option string) (string, error) {
|
||||
if globals, ok := c["global"]; ok {
|
||||
if globals, ok := c[Globals]; ok {
|
||||
if settings, ok := globals.(map[string]string); ok {
|
||||
if value, ok := settings[option]; ok {
|
||||
return value, nil
|
||||
|
@ -74,7 +80,7 @@ func (c Config) GetGlobal(option string) (string, error) {
|
|||
|
||||
// SetGlobal associates a value with a global configuration ooption
|
||||
func (c Config) SetGlobal(option, value string) {
|
||||
if globals, ok := c["global"]; ok {
|
||||
if globals, ok := c[Globals]; ok {
|
||||
if settings, ok := globals.(map[string]string); ok {
|
||||
settings[option] = value
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue