2018-07-19 17:42:25 +01:00
|
|
|
package gosec_test
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-16 01:12:27 +01:00
|
|
|
"go/ast"
|
|
|
|
|
2022-01-03 17:11:35 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2017-07-19 22:17:00 +01:00
|
|
|
. "github.com/onsi/gomega"
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
|
|
|
"github.com/securego/gosec/v2/rules"
|
|
|
|
"github.com/securego/gosec/v2/testutils"
|
2017-07-19 22:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Issue", func() {
|
|
|
|
Context("when creating a new issue", func() {
|
2017-09-16 01:12:27 +01:00
|
|
|
It("should create a code snippet from the specified ast.Node", func() {
|
|
|
|
var target *ast.BasicLit
|
|
|
|
source := `package main
|
|
|
|
const foo = "bar"
|
|
|
|
func main(){
|
|
|
|
println(foo)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo.go", source)
|
|
|
|
ctx := pkg.CreateContext("foo.go")
|
|
|
|
v := testutils.NewMockVisitor()
|
2018-07-19 17:42:25 +01:00
|
|
|
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
|
2017-09-16 01:12:27 +01:00
|
|
|
if node, ok := n.(*ast.BasicLit); ok {
|
|
|
|
target = node
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
v.Context = ctx
|
|
|
|
ast.Walk(v, ctx.Root)
|
|
|
|
Expect(target).ShouldNot(BeNil())
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
issue := gosec.NewIssue(ctx, target, "TEST", "", gosec.High, gosec.High)
|
2017-09-16 01:12:27 +01:00
|
|
|
Expect(issue).ShouldNot(BeNil())
|
|
|
|
Expect(issue.Code).Should(MatchRegexp(`"bar"`))
|
2017-10-01 01:31:39 +01:00
|
|
|
Expect(issue.Line).Should(Equal("2"))
|
2020-01-03 09:56:21 +00:00
|
|
|
Expect(issue.Col).Should(Equal("16"))
|
2021-05-07 15:54:34 +01:00
|
|
|
Expect(issue.Cwe).Should(BeNil())
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should return an error if specific context is not able to be obtained", func() {
|
2017-12-13 06:35:28 +00:00
|
|
|
Skip("Not implemented")
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
2020-04-14 08:50:02 +01:00
|
|
|
It("should construct file path based on line and file information", func() {
|
|
|
|
var target *ast.AssignStmt
|
|
|
|
|
|
|
|
source := `package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
|
|
username := "admin"
|
|
|
|
password := "f62e5bcda4fae4f82370da0c6f20697b8f8447ef"
|
|
|
|
fmt.Println("Doing something with: ", username, password)
|
|
|
|
}`
|
|
|
|
|
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo.go", source)
|
|
|
|
ctx := pkg.CreateContext("foo.go")
|
|
|
|
v := testutils.NewMockVisitor()
|
|
|
|
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
|
|
|
|
if node, ok := n.(*ast.AssignStmt); ok {
|
|
|
|
if id, ok := node.Lhs[0].(*ast.Ident); ok && id.Name == "password" {
|
|
|
|
target = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
v.Context = ctx
|
|
|
|
ast.Walk(v, ctx.Root)
|
|
|
|
Expect(target).ShouldNot(BeNil())
|
|
|
|
|
|
|
|
// Use hardcodeded rule to check assignment
|
|
|
|
cfg := gosec.NewConfig()
|
|
|
|
rule, _ := rules.NewHardcodedCredentials("TEST", cfg)
|
|
|
|
issue, err := rule.Match(target, ctx)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
Expect(issue).ShouldNot(BeNil())
|
|
|
|
Expect(issue.FileLocation()).Should(MatchRegexp("foo.go:5"))
|
|
|
|
})
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
It("should provide accurate line and file information", func() {
|
2017-12-13 06:35:28 +00:00
|
|
|
Skip("Not implemented")
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
2017-09-16 01:12:27 +01:00
|
|
|
It("should provide accurate line and file information for multi-line statements", func() {
|
2020-05-25 13:19:00 +01:00
|
|
|
var target *ast.CallExpr
|
|
|
|
source := `
|
|
|
|
package main
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
func main() {
|
2020-05-26 08:05:24 +01:00
|
|
|
_, _ := net.Listen("tcp",
|
|
|
|
"0.0.0.0:2000")
|
2020-05-25 13:19:00 +01:00
|
|
|
}
|
|
|
|
`
|
2017-09-16 01:12:27 +01:00
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo.go", source)
|
|
|
|
ctx := pkg.CreateContext("foo.go")
|
|
|
|
v := testutils.NewMockVisitor()
|
2018-07-19 17:42:25 +01:00
|
|
|
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
|
2020-05-25 13:19:00 +01:00
|
|
|
if node, ok := n.(*ast.CallExpr); ok {
|
2017-09-16 01:12:27 +01:00
|
|
|
target = node
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
v.Context = ctx
|
|
|
|
ast.Walk(v, ctx.Root)
|
|
|
|
Expect(target).ShouldNot(BeNil())
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
cfg := gosec.NewConfig()
|
2020-05-25 13:19:00 +01:00
|
|
|
rule, _ := rules.NewBindsToAllNetworkInterfaces("TEST", cfg)
|
2017-09-16 01:12:27 +01:00
|
|
|
issue, err := rule.Match(target, ctx)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
Expect(issue).ShouldNot(BeNil())
|
|
|
|
Expect(issue.File).Should(MatchRegexp("foo.go"))
|
2020-05-26 08:05:24 +01:00
|
|
|
Expect(issue.Line).Should(MatchRegexp("7-8"))
|
2020-05-25 13:19:00 +01:00
|
|
|
Expect(issue.Col).Should(Equal("10"))
|
2017-09-16 01:12:27 +01:00
|
|
|
})
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
It("should maintain the provided severity score", func() {
|
2017-12-13 06:35:28 +00:00
|
|
|
Skip("Not implemented")
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should maintain the provided confidence score", func() {
|
2017-12-13 06:35:28 +00:00
|
|
|
Skip("Not implemented")
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|