Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions rules/hardcoded_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ func (r *credentials) matchValueSpec(valueSpec *ast.ValueSpec, ctx *gosec.Contex

func (r *credentials) matchEqualityCheck(binaryExpr *ast.BinaryExpr, ctx *gosec.Context) (*gosec.Issue, error) {
if binaryExpr.Op == token.EQL || binaryExpr.Op == token.NEQ {
if ident, ok := binaryExpr.X.(*ast.Ident); ok {
if r.pattern.MatchString(ident.Name) {
if val, err := gosec.GetString(binaryExpr.Y); err == nil {
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
return gosec.NewIssue(ctx, binaryExpr, r.ID(), r.What, r.Severity, r.Confidence), nil
}
ident, ok := binaryExpr.X.(*ast.Ident)
if !ok {
ident, _ = binaryExpr.Y.(*ast.Ident)
}

if ident != nil && r.pattern.MatchString(ident.Name) {
valueNode := binaryExpr.Y
if !ok {
valueNode = binaryExpr.X
}
if val, err := gosec.GetString(valueNode); err == nil {
if r.ignoreEntropy || (!r.ignoreEntropy && r.isHighEntropyString(val)) {
return gosec.NewIssue(ctx, binaryExpr, r.ID(), r.What, r.Severity, r.Confidence), nil
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions testutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ package main

import "fmt"

func main() {
var password string
if "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" == password {
fmt.Println("password equality")
}
}`}, 1, gosec.NewConfig()},
{[]string{`
package main

import "fmt"

func main() {
var password string
if password != "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" {
Expand All @@ -124,6 +135,17 @@ package main

import "fmt"

func main() {
var password string
if "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" != password {
fmt.Println("password equality")
}
}`}, 1, gosec.NewConfig()},
{[]string{`
package main

import "fmt"

func main() {
var p string
if p != "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" {
Expand All @@ -135,6 +157,17 @@ package main

import "fmt"

func main() {
var p string
if "f62e5bcda4fae4f82370da0c6f20697b8f8447ef" != p {
fmt.Println("password equality")
}
}`}, 0, gosec.NewConfig()},
{[]string{`
package main

import "fmt"

const (
pw = "KjasdlkjapoIKLlka98098sdf012U/rL2sLdBqOHQUlt5Z6kCgKGDyCFA=="
)
Expand Down