1
0
mirror of https://github.com/securego/gosec.git synced 2025-11-27 22:28:20 +02:00

Switch to valuespec instead of gendecl for hardcoded credential rule (#186)

This commit is contained in:
andyleap
2018-03-08 15:49:49 -08:00
committed by Grant Murphy
parent e76b258456
commit f3c8d59863

View File

@@ -16,7 +16,6 @@ package rules
import ( import (
"go/ast" "go/ast"
"go/token"
"regexp" "regexp"
"strconv" "strconv"
@@ -53,8 +52,8 @@ func (r *credentials) Match(n ast.Node, ctx *gas.Context) (*gas.Issue, error) {
switch node := n.(type) { switch node := n.(type) {
case *ast.AssignStmt: case *ast.AssignStmt:
return r.matchAssign(node, ctx) return r.matchAssign(node, ctx)
case *ast.GenDecl: case *ast.ValueSpec:
return r.matchGenDecl(node, ctx) return r.matchValueSpec(node, ctx)
} }
return nil, nil return nil, nil
} }
@@ -76,12 +75,7 @@ func (r *credentials) matchAssign(assign *ast.AssignStmt, ctx *gas.Context) (*ga
return nil, nil return nil, nil
} }
func (r *credentials) matchGenDecl(decl *ast.GenDecl, ctx *gas.Context) (*gas.Issue, error) { func (r *credentials) matchValueSpec(valueSpec *ast.ValueSpec, ctx *gas.Context) (*gas.Issue, error) {
if decl.Tok != token.CONST && decl.Tok != token.VAR {
return nil, nil
}
for _, spec := range decl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
for index, ident := range valueSpec.Names { for index, ident := range valueSpec.Names {
if r.pattern.MatchString(ident.Name) && valueSpec.Values != nil { if r.pattern.MatchString(ident.Name) && valueSpec.Values != nil {
// const foo, bar = "same value" // const foo, bar = "same value"
@@ -95,8 +89,6 @@ func (r *credentials) matchGenDecl(decl *ast.GenDecl, ctx *gas.Context) (*gas.Is
} }
} }
} }
}
}
return nil, nil return nil, nil
} }
@@ -146,5 +138,5 @@ func NewHardcodedCredentials(conf gas.Config) (gas.Rule, []ast.Node) {
Confidence: gas.Low, Confidence: gas.Low,
Severity: gas.High, Severity: gas.High,
}, },
}, []ast.Node{(*ast.AssignStmt)(nil), (*ast.GenDecl)(nil)} }, []ast.Node{(*ast.AssignStmt)(nil), (*ast.ValueSpec)(nil)}
} }