Skip to content

Commit c7bb2dd

Browse files
committed
Fix additional crash condition
A var GenDecl may not have a value assigned. This error case must be handled.
1 parent 5012c34 commit c7bb2dd

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

rules/hardcoded_credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ 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 {
6262
// const foo, bar = "same value"
6363
if len(valueSpec.Values) <= index {
6464
index = len(valueSpec.Values) - 1

rules/hardcoded_credentials_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,16 @@ func TestHardcodedConstantMulti(t *testing.T) {
9898

9999
checkTestResults(t, issues, 1, "Potential hardcoded credentials")
100100
}
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)