Skip to content

Commit 6ef59ba

Browse files
authored
Merge pull request #86 from GoASTScanner/experimental
Handle inbalanced declaration of constants
2 parents 9301684 + c7bb2dd commit 6ef59ba

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

rules/hardcoded_credentials.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func (r *Credentials) matchGenDecl(decl *ast.GenDecl, ctx *gas.Context) (*gas.Is
5858
for _, spec := range decl.Specs {
5959
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
6060
for index, ident := range valueSpec.Names {
61-
if r.pattern.MatchString(ident.Name) {
61+
if r.pattern.MatchString(ident.Name) && valueSpec.Values != nil {
62+
// const foo, bar = "same value"
63+
if len(valueSpec.Values) <= index {
64+
index = len(valueSpec.Values) - 1
65+
}
6266
if _, ok := valueSpec.Values[index].(*ast.BasicLit); ok {
6367
return gas.NewIssue(ctx, decl, r.What, r.Severity, r.Confidence), nil
6468
}

rules/hardcoded_credentials_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,35 @@ func TestHardcodedConstant(t *testing.T) {
7979

8080
checkTestResults(t, issues, 1, "Potential hardcoded credentials")
8181
}
82+
83+
func TestHardcodedConstantMulti(t *testing.T) {
84+
config := map[string]interface{}{"ignoreNosec": false}
85+
analyzer := gas.NewAnalyzer(config, nil)
86+
analyzer.AddRule(NewHardcodedCredentials(config))
87+
88+
issues := gasTestRunner(`
89+
package samples
90+
91+
import "fmt"
92+
93+
const username, password = "secret"
94+
95+
func main() {
96+
fmt.Println("Doing something with: ", username, password)
97+
}`, analyzer)
98+
99+
checkTestResults(t, issues, 1, "Potential hardcoded credentials")
100+
}
101+
102+
func TestHardecodedVarsNotAssigned(t *testing.T) {
103+
config := map[string]interface{}{"ignoreNosec": false}
104+
analyzer := gas.NewAnalyzer(config, nil)
105+
analyzer.AddRule(NewHardcodedCredentials(config))
106+
issues := gasTestRunner(`
107+
package main
108+
var password string
109+
func init() {
110+
password = "this is a secret string"
111+
}`, analyzer)
112+
checkTestResults(t, issues, 1, "Potential hardcoded credentials")
113+
}

0 commit comments

Comments
 (0)