Skip to content

Commit 0545d13

Browse files
authored
Merge pull request #109 from GoASTScanner/bugfix
Ensure hardcoded credentials check only considers constant strings
2 parents d4f9b88 + 1e736c8 commit 0545d13

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,36 @@ 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+
}
131+
132+
func TestHardcodedConstString(t *testing.T) {
133+
config := map[string]interface{}{"ignoreNosec": false}
134+
analyzer := gas.NewAnalyzer(config, nil)
135+
analyzer.AddRule(NewHardcodedCredentials(config))
136+
issues := gasTestRunner(`
137+
package main
138+
139+
const (
140+
ATNStateTokenStart = "foo bar"
141+
)
142+
func main() {
143+
println(ATNStateTokenStart)
144+
}`, analyzer)
145+
checkTestResults(t, issues, 1, "Potential hardcoded credentials")
146+
}

0 commit comments

Comments
 (0)