2018-07-19 17:42:25 +01:00
|
|
|
package gosec_test
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
import (
|
2019-04-30 15:57:32 +01:00
|
|
|
"errors"
|
2017-07-19 22:17:00 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
|
|
|
"github.com/securego/gosec/v2/rules"
|
2019-04-30 12:53:22 +01:00
|
|
|
"golang.org/x/tools/go/packages"
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2/testutils"
|
2017-07-19 22:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Analyzer", func() {
|
|
|
|
|
|
|
|
var (
|
2018-07-19 17:42:25 +01:00
|
|
|
analyzer *gosec.Analyzer
|
2018-04-20 00:45:04 +01:00
|
|
|
logger *log.Logger
|
|
|
|
buildTags []string
|
2019-04-28 18:33:50 +01:00
|
|
|
tests bool
|
2017-07-19 22:17:00 +01:00
|
|
|
)
|
|
|
|
BeforeEach(func() {
|
2018-01-29 23:32:04 +00:00
|
|
|
logger, _ = testutils.NewLogger()
|
2019-04-28 18:33:50 +01:00
|
|
|
analyzer = gosec.NewAnalyzer(nil, tests, logger)
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("when processing a package", func() {
|
|
|
|
|
2019-04-30 15:57:32 +01:00
|
|
|
It("should not report an error if the package contains no Go files", func() {
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
2017-07-19 22:17:00 +01:00
|
|
|
dir, err := ioutil.TempDir("", "empty")
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2018-04-20 00:45:04 +01:00
|
|
|
err = analyzer.Process(buildTags, dir)
|
2019-04-30 15:57:32 +01:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(0))
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
2019-04-30 15:57:32 +01:00
|
|
|
It("should report an error if the package fails to build", func() {
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
2017-07-19 22:17:00 +01:00
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("wonky.go", `func main(){ println("forgot the package")}`)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := pkg.Build()
|
|
|
|
Expect(err).Should(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, pkg.Path)
|
2019-04-30 15:57:32 +01:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(1))
|
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|
|
|
|
|
2018-10-11 13:45:31 +01:00
|
|
|
It("should be able to analyze multiple Go files", func() {
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
2017-12-14 00:04:22 +00:00
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo.go", `
|
|
|
|
package main
|
|
|
|
func main(){
|
|
|
|
bar()
|
|
|
|
}`)
|
|
|
|
pkg.AddFile("bar.go", `
|
|
|
|
package main
|
|
|
|
func bar(){
|
|
|
|
println("package has two files!")
|
|
|
|
}`)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := pkg.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, pkg.Path)
|
2017-12-14 00:04:22 +00:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
_, metrics, _ := analyzer.Report()
|
2017-12-14 00:04:22 +00:00
|
|
|
Expect(metrics.NumFiles).To(Equal(2))
|
2018-01-07 23:02:33 +00:00
|
|
|
})
|
|
|
|
|
2018-10-11 13:45:31 +01:00
|
|
|
It("should be able to analyze multiple Go packages", func() {
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
2018-01-07 23:02:33 +00:00
|
|
|
pkg1 := testutils.NewTestPackage()
|
|
|
|
pkg2 := testutils.NewTestPackage()
|
|
|
|
defer pkg1.Close()
|
|
|
|
defer pkg2.Close()
|
|
|
|
pkg1.AddFile("foo.go", `
|
|
|
|
package main
|
|
|
|
func main(){
|
|
|
|
}`)
|
|
|
|
pkg2.AddFile("bar.go", `
|
|
|
|
package main
|
|
|
|
func bar(){
|
|
|
|
}`)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := pkg1.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = pkg2.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, pkg1.Path, pkg2.Path)
|
2018-01-07 23:02:33 +00:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
_, metrics, _ := analyzer.Report()
|
2018-01-07 23:02:33 +00:00
|
|
|
Expect(metrics.NumFiles).To(Equal(2))
|
2017-12-14 00:04:22 +00:00
|
|
|
})
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
It("should find errors when nosec is not in use", func() {
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
2018-09-28 09:42:25 +01:00
|
|
|
source := sample.Code[0]
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
controlPackage := testutils.NewTestPackage()
|
|
|
|
defer controlPackage.Close()
|
|
|
|
controlPackage.AddFile("md5.go", source)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := controlPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, controlPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
controlIssues, _, _ := analyzer.Report()
|
2017-07-19 22:17:00 +01:00
|
|
|
Expect(controlIssues).Should(HaveLen(sample.Errors))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2019-04-30 11:17:44 +01:00
|
|
|
It("should report Go build errors and invalid files", func() {
|
2019-02-26 22:24:06 +00:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo.go", `
|
|
|
|
package main
|
|
|
|
func main()
|
|
|
|
}`)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := pkg.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, pkg.Path)
|
2019-02-26 22:24:06 +00:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-04-30 11:17:44 +01:00
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(1))
|
|
|
|
Expect(ferr[0].Line).To(Equal(4))
|
|
|
|
Expect(ferr[0].Column).To(Equal(5))
|
|
|
|
Expect(ferr[0].Err).Should(MatchRegexp(`expected declaration, found '}'`))
|
2019-02-26 22:24:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
It("should not report errors when a nosec comment is present", func() {
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
2018-09-28 09:42:25 +01:00
|
|
|
source := sample.Code[0]
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
2017-07-19 22:17:00 +01:00
|
|
|
|
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec", 1)
|
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
nosecIssues, _, _ := analyzer.Report()
|
2017-07-19 22:17:00 +01:00
|
|
|
Expect(nosecIssues).Should(BeEmpty())
|
|
|
|
})
|
2017-10-05 22:32:03 +01:00
|
|
|
|
|
|
|
It("should not report errors when an exclude comment is present for the correct rule", func() {
|
|
|
|
// Rule for MD5 weak crypto usage
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
2018-09-28 09:42:25 +01:00
|
|
|
source := sample.Code[0]
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
|
|
|
|
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
2018-03-08 19:01:00 +00:00
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec G401", 1)
|
2017-10-05 22:32:03 +01:00
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
nosecIssues, _, _ := analyzer.Report()
|
2017-10-05 22:32:03 +01:00
|
|
|
Expect(nosecIssues).Should(BeEmpty())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should report errors when an exclude comment is present for a different rule", func() {
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
2018-09-28 09:42:25 +01:00
|
|
|
source := sample.Code[0]
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
|
|
|
|
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
2018-03-08 19:01:00 +00:00
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec G301", 1)
|
2017-10-05 22:32:03 +01:00
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
nosecIssues, _, _ := analyzer.Report()
|
2017-10-05 22:32:03 +01:00
|
|
|
Expect(nosecIssues).Should(HaveLen(sample.Errors))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should not report errors when an exclude comment is present for multiple rules, including the correct rule", func() {
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
2018-09-28 09:42:25 +01:00
|
|
|
source := sample.Code[0]
|
2017-10-05 22:32:03 +01:00
|
|
|
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
|
|
|
|
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
2018-03-08 19:01:00 +00:00
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec G301 G401", 1)
|
2017-10-05 22:32:03 +01:00
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
2019-04-28 21:30:08 +01:00
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-02-26 22:24:06 +00:00
|
|
|
nosecIssues, _, _ := analyzer.Report()
|
2017-10-05 22:32:03 +01:00
|
|
|
Expect(nosecIssues).Should(BeEmpty())
|
|
|
|
})
|
2018-04-20 00:45:04 +01:00
|
|
|
|
|
|
|
It("should pass the build tags", func() {
|
2020-05-20 16:17:44 +01:00
|
|
|
sample := testutils.SampleCodeBuildTag[0]
|
2018-09-28 09:42:25 +01:00
|
|
|
source := sample.Code[0]
|
2018-04-20 00:45:04 +01:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("tags.go", source)
|
2019-04-30 15:57:32 +01:00
|
|
|
tags := []string{"tag"}
|
|
|
|
err := analyzer.Process(tags, pkg.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2018-04-20 00:45:04 +01:00
|
|
|
})
|
2019-04-30 09:21:16 +01:00
|
|
|
|
2019-04-30 10:09:57 +01:00
|
|
|
It("should process an empty package with test file", func() {
|
2019-04-30 09:21:16 +01:00
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo_test.go", `
|
2019-04-30 16:14:26 +01:00
|
|
|
package tests
|
2019-04-30 09:21:16 +01:00
|
|
|
import "testing"
|
|
|
|
func TestFoo(t *testing.T){
|
|
|
|
}`)
|
|
|
|
err := pkg.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, pkg.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
})
|
2019-04-30 12:53:22 +01:00
|
|
|
|
|
|
|
It("should be possible to overwrite nosec comments, and report issues", func() {
|
|
|
|
// Rule for MD5 weak crypto usage
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
|
|
|
source := sample.Code[0]
|
2017-07-19 22:17:00 +01:00
|
|
|
|
2019-04-30 12:53:22 +01:00
|
|
|
// overwrite nosec option
|
|
|
|
nosecIgnoreConfig := gosec.NewConfig()
|
|
|
|
nosecIgnoreConfig.SetGlobal(gosec.Nosec, "true")
|
|
|
|
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, logger)
|
|
|
|
customAnalyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
2019-04-28 21:30:08 +01:00
|
|
|
|
2019-04-30 12:53:22 +01:00
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec", 1)
|
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = customAnalyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
nosecIssues, _, _ := customAnalyzer.Report()
|
|
|
|
Expect(nosecIssues).Should(HaveLen(sample.Errors))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2020-01-06 08:47:28 +00:00
|
|
|
It("should be possible to use an alternative nosec tag", func() {
|
2019-09-04 09:20:43 +01:00
|
|
|
// Rule for MD5 weak crypto usage
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
|
|
|
source := sample.Code[0]
|
|
|
|
|
|
|
|
// overwrite nosec option
|
|
|
|
nosecIgnoreConfig := gosec.NewConfig()
|
|
|
|
nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "#falsePositive")
|
|
|
|
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, logger)
|
|
|
|
customAnalyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
|
|
|
|
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #falsePositive", 1)
|
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = customAnalyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
nosecIssues, _, _ := customAnalyzer.Report()
|
|
|
|
Expect(nosecIssues).Should(HaveLen(0))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2020-01-06 08:47:28 +00:00
|
|
|
It("should ignore vulnerabilities when the default tag is found", func() {
|
2019-09-04 09:20:43 +01:00
|
|
|
// Rule for MD5 weak crypto usage
|
|
|
|
sample := testutils.SampleCodeG401[0]
|
|
|
|
source := sample.Code[0]
|
|
|
|
|
|
|
|
// overwrite nosec option
|
|
|
|
nosecIgnoreConfig := gosec.NewConfig()
|
|
|
|
nosecIgnoreConfig.SetGlobal(gosec.NoSecAlternative, "#falsePositive")
|
|
|
|
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, logger)
|
|
|
|
customAnalyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
|
|
|
|
|
|
|
|
nosecPackage := testutils.NewTestPackage()
|
|
|
|
defer nosecPackage.Close()
|
|
|
|
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #nosec", 1)
|
|
|
|
nosecPackage.AddFile("md5.go", nosecSource)
|
|
|
|
err := nosecPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = customAnalyzer.Process(buildTags, nosecPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
nosecIssues, _, _ := customAnalyzer.Report()
|
2020-01-06 08:47:28 +00:00
|
|
|
Expect(nosecIssues).Should(HaveLen(0))
|
2019-09-04 09:20:43 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2019-04-30 12:53:22 +01:00
|
|
|
It("should be able to analyze Go test package", func() {
|
|
|
|
customAnalyzer := gosec.NewAnalyzer(nil, true, logger)
|
|
|
|
customAnalyzer.LoadRules(rules.Generate().Builders())
|
|
|
|
pkg := testutils.NewTestPackage()
|
|
|
|
defer pkg.Close()
|
|
|
|
pkg.AddFile("foo.go", `
|
2019-04-30 16:14:26 +01:00
|
|
|
package foo
|
|
|
|
func foo(){
|
|
|
|
}`)
|
2019-04-30 12:53:22 +01:00
|
|
|
pkg.AddFile("foo_test.go", `
|
2019-04-30 16:14:26 +01:00
|
|
|
package foo_test
|
|
|
|
import "testing"
|
2019-05-17 14:35:46 +01:00
|
|
|
func test() error {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-30 16:14:26 +01:00
|
|
|
func TestFoo(t *testing.T){
|
2019-05-17 14:35:46 +01:00
|
|
|
test()
|
2019-04-30 16:14:26 +01:00
|
|
|
}`)
|
2019-04-30 12:53:22 +01:00
|
|
|
err := pkg.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = customAnalyzer.Process(buildTags, pkg.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-05-17 14:35:46 +01:00
|
|
|
issues, _, _ := customAnalyzer.Report()
|
|
|
|
Expect(issues).Should(HaveLen(1))
|
2019-04-30 12:53:22 +01:00
|
|
|
})
|
|
|
|
})
|
2020-01-15 15:56:50 +00:00
|
|
|
It("should be able to analyze Cgo files", func() {
|
|
|
|
analyzer.LoadRules(rules.Generate().Builders())
|
|
|
|
sample := testutils.SampleCodeCgo[0]
|
|
|
|
source := sample.Code[0]
|
|
|
|
|
|
|
|
testPackage := testutils.NewTestPackage()
|
|
|
|
defer testPackage.Close()
|
|
|
|
testPackage.AddFile("main.go", source)
|
|
|
|
err := testPackage.Build()
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
err = analyzer.Process(buildTags, testPackage.Path)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
issues, _, _ := analyzer.Report()
|
|
|
|
Expect(issues).Should(HaveLen(0))
|
|
|
|
})
|
2019-04-30 12:53:22 +01:00
|
|
|
|
|
|
|
Context("when parsing errors from a package", func() {
|
|
|
|
|
|
|
|
It("should return no error when the error list is empty", func() {
|
|
|
|
pkg := &packages.Package{}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should properly parse the errors", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file:1:2",
|
|
|
|
Msg: "build error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(1))
|
|
|
|
Expect(ferr[0].Line).To(Equal(1))
|
|
|
|
Expect(ferr[0].Column).To(Equal(2))
|
|
|
|
Expect(ferr[0].Err).Should(MatchRegexp(`build error`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should properly parse the errors without line and column", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file",
|
|
|
|
Msg: "build error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(1))
|
|
|
|
Expect(ferr[0].Line).To(Equal(0))
|
|
|
|
Expect(ferr[0].Column).To(Equal(0))
|
|
|
|
Expect(ferr[0].Err).Should(MatchRegexp(`build error`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should properly parse the errors without column", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file",
|
|
|
|
Msg: "build error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(1))
|
|
|
|
Expect(ferr[0].Line).To(Equal(0))
|
|
|
|
Expect(ferr[0].Column).To(Equal(0))
|
|
|
|
Expect(ferr[0].Err).Should(MatchRegexp(`build error`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should return error when line cannot be parsed", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file:line",
|
|
|
|
Msg: "build error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).Should(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should return error when column cannot be parsed", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file:1:column",
|
|
|
|
Msg: "build error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).Should(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should append error to the same file", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file:1:2",
|
|
|
|
Msg: "error1",
|
|
|
|
},
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file:3:4",
|
|
|
|
Msg: "error2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(2))
|
|
|
|
Expect(ferr[0].Line).To(Equal(1))
|
|
|
|
Expect(ferr[0].Column).To(Equal(2))
|
|
|
|
Expect(ferr[0].Err).Should(MatchRegexp(`error1`))
|
|
|
|
Expect(ferr[1].Line).To(Equal(3))
|
|
|
|
Expect(ferr[1].Column).To(Equal(4))
|
|
|
|
Expect(ferr[1].Err).Should(MatchRegexp(`error2`))
|
|
|
|
}
|
|
|
|
})
|
2019-06-25 10:56:26 +01:00
|
|
|
|
|
|
|
It("should set the config", func() {
|
|
|
|
config := gosec.NewConfig()
|
|
|
|
config["test"] = "test"
|
|
|
|
analyzer.SetConfig(config)
|
|
|
|
found := analyzer.Config()
|
|
|
|
Expect(config).To(Equal(found))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should reset the analyzer", func() {
|
|
|
|
analyzer.Reset()
|
|
|
|
issues, metrics, errors := analyzer.Report()
|
|
|
|
Expect(issues).To(BeEmpty())
|
|
|
|
Expect(*metrics).To(Equal(gosec.Metrics{}))
|
|
|
|
Expect(errors).To(BeEmpty())
|
|
|
|
})
|
2019-04-28 21:30:08 +01:00
|
|
|
})
|
2019-04-30 15:57:32 +01:00
|
|
|
|
|
|
|
Context("when appending errors", func() {
|
|
|
|
It("should skip error for non-buildable packages", func() {
|
|
|
|
analyzer.AppendError("test", errors.New(`loading file from package "pkg/test": no buildable Go source files in pkg/test`))
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(0))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should add a new error", func() {
|
|
|
|
pkg := &packages.Package{
|
|
|
|
Errors: []packages.Error{
|
|
|
|
packages.Error{
|
|
|
|
Pos: "file:1:2",
|
|
|
|
Msg: "build error",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := analyzer.ParseErrors(pkg)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
analyzer.AppendError("file", errors.New("file build error"))
|
|
|
|
_, _, errors := analyzer.Report()
|
|
|
|
Expect(len(errors)).To(Equal(1))
|
|
|
|
for _, ferr := range errors {
|
|
|
|
Expect(len(ferr)).To(Equal(2))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-07-19 22:17:00 +01:00
|
|
|
})
|