@@ -16,7 +16,6 @@ package rules
16
16
17
17
import (
18
18
"go/ast"
19
- "go/token"
20
19
"regexp"
21
20
"strconv"
22
21
@@ -53,8 +52,8 @@ func (r *credentials) Match(n ast.Node, ctx *gas.Context) (*gas.Issue, error) {
53
52
switch node := n .(type ) {
54
53
case * ast.AssignStmt :
55
54
return r .matchAssign (node , ctx )
56
- case * ast.GenDecl :
57
- return r .matchGenDecl (node , ctx )
55
+ case * ast.ValueSpec :
56
+ return r .matchValueSpec (node , ctx )
58
57
}
59
58
return nil , nil
60
59
}
@@ -76,23 +75,16 @@ func (r *credentials) matchAssign(assign *ast.AssignStmt, ctx *gas.Context) (*ga
76
75
return nil , nil
77
76
}
78
77
79
- func (r * credentials ) matchGenDecl (decl * ast.GenDecl , ctx * gas.Context ) (* gas.Issue , error ) {
80
- if decl .Tok != token .CONST && decl .Tok != token .VAR {
81
- return nil , nil
82
- }
83
- for _ , spec := range decl .Specs {
84
- if valueSpec , ok := spec .(* ast.ValueSpec ); ok {
85
- for index , ident := range valueSpec .Names {
86
- if r .pattern .MatchString (ident .Name ) && valueSpec .Values != nil {
87
- // const foo, bar = "same value"
88
- if len (valueSpec .Values ) <= index {
89
- index = len (valueSpec .Values ) - 1
90
- }
91
- if val , err := gas .GetString (valueSpec .Values [index ]); err == nil {
92
- if r .ignoreEntropy || (! r .ignoreEntropy && r .isHighEntropyString (val )) {
93
- return gas .NewIssue (ctx , valueSpec , r .What , r .Severity , r .Confidence ), nil
94
- }
95
- }
78
+ func (r * credentials ) matchValueSpec (valueSpec * ast.ValueSpec , ctx * gas.Context ) (* gas.Issue , error ) {
79
+ for index , ident := range valueSpec .Names {
80
+ if r .pattern .MatchString (ident .Name ) && valueSpec .Values != nil {
81
+ // const foo, bar = "same value"
82
+ if len (valueSpec .Values ) <= index {
83
+ index = len (valueSpec .Values ) - 1
84
+ }
85
+ if val , err := gas .GetString (valueSpec .Values [index ]); err == nil {
86
+ if r .ignoreEntropy || (! r .ignoreEntropy && r .isHighEntropyString (val )) {
87
+ return gas .NewIssue (ctx , valueSpec , r .What , r .Severity , r .Confidence ), nil
96
88
}
97
89
}
98
90
}
@@ -146,5 +138,5 @@ func NewHardcodedCredentials(conf gas.Config) (gas.Rule, []ast.Node) {
146
138
Confidence : gas .Low ,
147
139
Severity : gas .High ,
148
140
},
149
- }, []ast.Node {(* ast .AssignStmt )(nil ), (* ast .GenDecl )(nil )}
141
+ }, []ast.Node {(* ast .AssignStmt )(nil ), (* ast .ValueSpec )(nil )}
150
142
}
0 commit comments