Skip to content

Commit fb44007

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Enhance the hardcoded credentials rule to check the equality and non-equality of strings
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent a2a40de commit fb44007

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d
1111
github.com/onsi/ginkgo v1.12.0
1212
github.com/onsi/gomega v1.9.0
13+
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f
1314
github.com/stretchr/testify v1.4.0 // indirect
1415
golang.org/x/text v0.3.2 // indirect
1516
golang.org/x/tools v0.0.0-20200331202046-9d5940d49312

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ github.com/onsi/gomega v1.9.0 h1:R1uwffexN6Pr340GtYRIdZmAiN4J+iw6WG4wog1DUXg=
3131
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
3232
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3333
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34+
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk=
35+
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
3436
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3537
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3638
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=

rules/hardcoded_credentials.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package rules
1616

1717
import (
1818
"go/ast"
19+
"go/token"
1920
"regexp"
2021
"strconv"
2122

@@ -58,6 +59,8 @@ func (r *credentials) Match(n ast.Node, ctx *gosec.Context) (*gosec.Issue, error
5859
return r.matchAssign(node, ctx)
5960
case *ast.ValueSpec:
6061
return r.matchValueSpec(node, ctx)
62+
case *ast.BinaryExpr:
63+
return r.matchEqualityCheck(node, ctx)
6164
}
6265
return nil, nil
6366
}
@@ -96,6 +99,21 @@ func (r *credentials) matchValueSpec(valueSpec *ast.ValueSpec, ctx *gosec.Contex
9699
return nil, nil
97100
}
98101

102+
func (r *credentials) matchEqualityCheck(binaryExpr *ast.BinaryExpr, ctx *gosec.Context) (*gosec.Issue, error) {
103+
if binaryExpr.Op == token.EQL || binaryExpr.Op == token.NEQ {
104+
if ident, ok := binaryExpr.X.(*ast.Ident); ok {
105+
if r.pattern.MatchString(ident.Name) {
106+
if val, err := gosec.GetString(binaryExpr.Y); err == nil {
107+
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
108+
return gosec.NewIssue(ctx, binaryExpr, r.ID(), r.What, r.Severity, r.Confidence), nil
109+
}
110+
}
111+
}
112+
}
113+
}
114+
return nil, nil
115+
}
116+
99117
// NewHardcodedCredentials attempts to find high entropy string constants being
100118
// assigned to variables that appear to be related to credentials.
101119
func NewHardcodedCredentials(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
@@ -151,5 +169,5 @@ func NewHardcodedCredentials(id string, conf gosec.Config) (gosec.Rule, []ast.No
151169
Confidence: gosec.Low,
152170
Severity: gosec.High,
153171
},
154-
}, []ast.Node{(*ast.AssignStmt)(nil), (*ast.ValueSpec)(nil)}
172+
}, []ast.Node{(*ast.AssignStmt)(nil), (*ast.ValueSpec)(nil), (*ast.BinaryExpr)(nil)}
155173
}

testutils/source.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,34 @@ const (
6969
)
7070
func main() {
7171
println(ATNStateTokenStart)
72-
}`}, 1, gosec.NewConfig()}}
72+
}`}, 1, gosec.NewConfig()},
73+
{[]string{`
74+
package main
75+
import "fmt"
76+
func main() {
77+
var password string
78+
if password == "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" {
79+
fmt.Println("password equality")
80+
}
81+
}`}, 1, gosec.NewConfig()},
82+
{[]string{`
83+
package main
84+
import "fmt"
85+
func main() {
86+
var password string
87+
if password != "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" {
88+
fmt.Println("password equality")
89+
}
90+
}`}, 1, gosec.NewConfig()},
91+
{[]string{`
92+
package main
93+
import "fmt"
94+
func main() {
95+
var p string
96+
if p != "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" {
97+
fmt.Println("password equality")
98+
}
99+
}`}, 0, gosec.NewConfig()}}
73100

74101
// SampleCodeG102 code snippets for network binding
75102
SampleCodeG102 = []CodeSample{

0 commit comments

Comments
 (0)