Skip to content

Commit d1e67fc

Browse files
committed
Ensure hardcoded credentials only examines strings
The hardcoded credentials test should only consider assignment of const strings. Related to issue #108
1 parent d4f9b88 commit d1e67fc

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

rules/hardcoded_credentials.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (r *Credentials) matchAssign(assign *ast.AssignStmt, ctx *gas.Context) (*ga
4141
if ident, ok := i.(*ast.Ident); ok {
4242
if r.pattern.MatchString(ident.Name) {
4343
for _, e := range assign.Rhs {
44-
if _, ok := e.(*ast.BasicLit); ok {
44+
if rhs, ok := e.(*ast.BasicLit); ok && rhs.Kind == token.STRING {
4545
return gas.NewIssue(ctx, assign, r.What, r.Severity, r.Confidence), nil
4646
}
4747
}
@@ -63,7 +63,7 @@ func (r *Credentials) matchGenDecl(decl *ast.GenDecl, ctx *gas.Context) (*gas.Is
6363
if len(valueSpec.Values) <= index {
6464
index = len(valueSpec.Values) - 1
6565
}
66-
if _, ok := valueSpec.Values[index].(*ast.BasicLit); ok {
66+
if rhs, ok := valueSpec.Values[index].(*ast.BasicLit); ok && rhs.Kind == token.STRING {
6767
return gas.NewIssue(ctx, decl, r.What, r.Severity, r.Confidence), nil
6868
}
6969
}

rules/hardcoded_credentials_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,20 @@ func TestHardecodedVarsNotAssigned(t *testing.T) {
111111
}`, analyzer)
112112
checkTestResults(t, issues, 1, "Potential hardcoded credentials")
113113
}
114+
115+
func TestHardcodedConstInteger(t *testing.T) {
116+
config := map[string]interface{}{"ignoreNosec": false}
117+
analyzer := gas.NewAnalyzer(config, nil)
118+
analyzer.AddRule(NewHardcodedCredentials(config))
119+
issues := gasTestRunner(`
120+
package main
121+
122+
const (
123+
ATNStateSomethingElse = 1,
124+
ATNStateTokenStart = 42,
125+
)
126+
func main() {
127+
println(ATNStateTokenStart)
128+
}`, analyzer)
129+
checkTestResults(t, issues, 0, "Potential hardcoded credentials")
130+
}

0 commit comments

Comments
 (0)