2019-06-24 13:10:51 +01:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2019-10-31 08:22:38 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-16 08:05:38 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-06-24 13:10:51 +01:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/securego/gosec"
|
2019-10-31 08:22:38 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-06-24 13:10:51 +01:00
|
|
|
)
|
|
|
|
|
2019-10-31 08:22:38 +00:00
|
|
|
func createIssue(ruleID string, cwe gosec.Cwe) gosec.Issue {
|
|
|
|
return gosec.Issue{
|
|
|
|
File: "/home/src/project/test.go",
|
|
|
|
Line: "1",
|
2020-01-03 09:56:21 +00:00
|
|
|
Col: "1",
|
2019-10-31 08:22:38 +00:00
|
|
|
RuleID: ruleID,
|
|
|
|
What: "test",
|
|
|
|
Confidence: gosec.High,
|
|
|
|
Severity: gosec.High,
|
|
|
|
Code: "testcode",
|
|
|
|
Cwe: cwe,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createReportInfo(rule string, cwe gosec.Cwe) reportInfo {
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
metrics := gosec.Metrics{}
|
|
|
|
return reportInfo{
|
|
|
|
Errors: map[string][]gosec.Error{},
|
|
|
|
Issues: []*gosec.Issue{
|
|
|
|
&issue,
|
|
|
|
},
|
|
|
|
Stats: &metrics,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stripString(str string) string {
|
|
|
|
ret := strings.Replace(str, "\n", "", -1)
|
|
|
|
ret = strings.Replace(ret, " ", "", -1)
|
|
|
|
ret = strings.Replace(ret, "\t", "", -1)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-06-24 13:10:51 +01:00
|
|
|
var _ = Describe("Formatter", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
})
|
|
|
|
Context("when converting to Sonarqube issues", func() {
|
|
|
|
It("it should parse the report info", func() {
|
|
|
|
data := &reportInfo{
|
|
|
|
Errors: map[string][]gosec.Error{},
|
|
|
|
Issues: []*gosec.Issue{
|
|
|
|
&gosec.Issue{
|
|
|
|
Severity: 2,
|
|
|
|
Confidence: 0,
|
|
|
|
RuleID: "test",
|
|
|
|
What: "test",
|
|
|
|
File: "/home/src/project/test.go",
|
|
|
|
Code: "",
|
|
|
|
Line: "1-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stats: &gosec.Metrics{
|
|
|
|
NumFiles: 0,
|
|
|
|
NumLines: 0,
|
|
|
|
NumNosec: 0,
|
|
|
|
NumFound: 0,
|
|
|
|
},
|
|
|
|
}
|
2019-07-09 04:36:09 +01:00
|
|
|
want := &sonarIssues{
|
2019-06-24 13:10:51 +01:00
|
|
|
SonarIssues: []sonarIssue{
|
|
|
|
{
|
|
|
|
EngineID: "gosec",
|
|
|
|
RuleID: "test",
|
|
|
|
PrimaryLocation: location{
|
|
|
|
Message: "test",
|
|
|
|
FilePath: "test.go",
|
|
|
|
TextRange: textRange{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: "VULNERABILITY",
|
|
|
|
Severity: "BLOCKER",
|
|
|
|
EffortMinutes: SonarqubeEffortMinutes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rootPath := "/home/src/project"
|
|
|
|
|
2019-06-24 13:35:11 +01:00
|
|
|
issues, err := convertToSonarIssues([]string{rootPath}, data)
|
2019-06-24 13:10:51 +01:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-07-09 04:36:09 +01:00
|
|
|
Expect(*issues).To(Equal(*want))
|
2019-06-24 13:10:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
It("it should parse the report info with files in subfolders", func() {
|
|
|
|
data := &reportInfo{
|
|
|
|
Errors: map[string][]gosec.Error{},
|
|
|
|
Issues: []*gosec.Issue{
|
|
|
|
&gosec.Issue{
|
|
|
|
Severity: 2,
|
|
|
|
Confidence: 0,
|
|
|
|
RuleID: "test",
|
|
|
|
What: "test",
|
|
|
|
File: "/home/src/project/subfolder/test.go",
|
|
|
|
Code: "",
|
|
|
|
Line: "1-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stats: &gosec.Metrics{
|
|
|
|
NumFiles: 0,
|
|
|
|
NumLines: 0,
|
|
|
|
NumNosec: 0,
|
|
|
|
NumFound: 0,
|
|
|
|
},
|
|
|
|
}
|
2019-07-09 04:36:09 +01:00
|
|
|
want := &sonarIssues{
|
2019-06-24 13:10:51 +01:00
|
|
|
SonarIssues: []sonarIssue{
|
|
|
|
{
|
|
|
|
EngineID: "gosec",
|
|
|
|
RuleID: "test",
|
|
|
|
PrimaryLocation: location{
|
|
|
|
Message: "test",
|
|
|
|
FilePath: "subfolder/test.go",
|
|
|
|
TextRange: textRange{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: "VULNERABILITY",
|
|
|
|
Severity: "BLOCKER",
|
|
|
|
EffortMinutes: SonarqubeEffortMinutes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rootPath := "/home/src/project"
|
|
|
|
|
2019-06-24 13:35:11 +01:00
|
|
|
issues, err := convertToSonarIssues([]string{rootPath}, data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-07-09 04:36:09 +01:00
|
|
|
Expect(*issues).To(Equal(*want))
|
2019-06-24 13:35:11 +01:00
|
|
|
})
|
|
|
|
It("it should not parse the report info for files from other projects", func() {
|
|
|
|
data := &reportInfo{
|
|
|
|
Errors: map[string][]gosec.Error{},
|
|
|
|
Issues: []*gosec.Issue{
|
|
|
|
&gosec.Issue{
|
|
|
|
Severity: 2,
|
|
|
|
Confidence: 0,
|
|
|
|
RuleID: "test",
|
|
|
|
What: "test",
|
|
|
|
File: "/home/src/project1/test.go",
|
|
|
|
Code: "",
|
|
|
|
Line: "1-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stats: &gosec.Metrics{
|
|
|
|
NumFiles: 0,
|
|
|
|
NumLines: 0,
|
|
|
|
NumNosec: 0,
|
|
|
|
NumFound: 0,
|
|
|
|
},
|
|
|
|
}
|
2019-07-09 04:36:09 +01:00
|
|
|
want := &sonarIssues{
|
|
|
|
SonarIssues: []sonarIssue{},
|
2019-06-24 13:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rootPath := "/home/src/project2"
|
|
|
|
|
|
|
|
issues, err := convertToSonarIssues([]string{rootPath}, data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-07-09 04:36:09 +01:00
|
|
|
Expect(*issues).To(Equal(*want))
|
2019-06-24 13:35:11 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
It("it should parse the report info for multiple projects projects", func() {
|
|
|
|
data := &reportInfo{
|
|
|
|
Errors: map[string][]gosec.Error{},
|
|
|
|
Issues: []*gosec.Issue{
|
|
|
|
&gosec.Issue{
|
|
|
|
Severity: 2,
|
|
|
|
Confidence: 0,
|
|
|
|
RuleID: "test",
|
|
|
|
What: "test",
|
|
|
|
File: "/home/src/project1/test-project1.go",
|
|
|
|
Code: "",
|
|
|
|
Line: "1-2",
|
|
|
|
},
|
|
|
|
&gosec.Issue{
|
|
|
|
Severity: 2,
|
|
|
|
Confidence: 0,
|
|
|
|
RuleID: "test",
|
|
|
|
What: "test",
|
|
|
|
File: "/home/src/project2/test-project2.go",
|
|
|
|
Code: "",
|
|
|
|
Line: "1-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stats: &gosec.Metrics{
|
|
|
|
NumFiles: 0,
|
|
|
|
NumLines: 0,
|
|
|
|
NumNosec: 0,
|
|
|
|
NumFound: 0,
|
|
|
|
},
|
|
|
|
}
|
2019-07-09 04:36:09 +01:00
|
|
|
want := &sonarIssues{
|
2019-06-24 13:35:11 +01:00
|
|
|
SonarIssues: []sonarIssue{
|
|
|
|
{
|
|
|
|
EngineID: "gosec",
|
|
|
|
RuleID: "test",
|
|
|
|
PrimaryLocation: location{
|
|
|
|
Message: "test",
|
|
|
|
FilePath: "test-project1.go",
|
|
|
|
TextRange: textRange{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: "VULNERABILITY",
|
|
|
|
Severity: "BLOCKER",
|
|
|
|
EffortMinutes: SonarqubeEffortMinutes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
EngineID: "gosec",
|
|
|
|
RuleID: "test",
|
|
|
|
PrimaryLocation: location{
|
|
|
|
Message: "test",
|
|
|
|
FilePath: "test-project2.go",
|
|
|
|
TextRange: textRange{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: "VULNERABILITY",
|
|
|
|
Severity: "BLOCKER",
|
|
|
|
EffortMinutes: SonarqubeEffortMinutes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rootPaths := []string{"/home/src/project1", "/home/src/project2"}
|
|
|
|
|
|
|
|
issues, err := convertToSonarIssues(rootPaths, data)
|
2019-06-24 13:10:51 +01:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-07-09 04:36:09 +01:00
|
|
|
Expect(*issues).To(Equal(*want))
|
2019-06-24 13:10:51 +01:00
|
|
|
})
|
|
|
|
})
|
2019-10-31 08:22:38 +00:00
|
|
|
Context("When using different report formats", func() {
|
|
|
|
|
|
|
|
grules := []string{"G101", "G102", "G103", "G104", "G106",
|
2020-01-19 19:40:19 +00:00
|
|
|
"G107", "G109", "G110", "G201", "G202", "G203", "G204",
|
|
|
|
"G301", "G302", "G303", "G304", "G305", "G401", "G402",
|
|
|
|
"G403", "G404", "G501", "G502", "G503", "G504", "G505"}
|
2019-10-31 08:22:38 +00:00
|
|
|
|
|
|
|
It("csv formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := CreateReport(buf, "csv", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
pattern := "/home/src/project/test.go,1,test,HIGH,HIGH,testcode,CWE-%s\n"
|
|
|
|
expect := fmt.Sprintf(pattern, cwe.ID)
|
2020-01-16 08:05:38 +00:00
|
|
|
Expect(string(buf.String())).To(Equal(expect))
|
2019-10-31 08:22:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
It("xml formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := CreateReport(buf, "xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{NumFiles: 0, NumLines: 0, NumNosec: 0, NumFound: 0}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
pattern := "Results:\n\n\n[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)\n > testcode\n\n\nSummary:\n Files: 0\n Lines: 0\n Nosec: 0\n Issues: 0\n\n"
|
|
|
|
expect := fmt.Sprintf(pattern, rule, cwe.ID)
|
2020-01-16 08:05:38 +00:00
|
|
|
Expect(string(buf.String())).To(Equal(expect))
|
2019-10-31 08:22:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
It("json formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
data := createReportInfo(rule, cwe)
|
|
|
|
|
|
|
|
expect := new(bytes.Buffer)
|
|
|
|
enc := json.NewEncoder(expect)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := enc.Encode(data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err = CreateReport(buf, "json", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
result := stripString(buf.String())
|
|
|
|
expectation := stripString(expect.String())
|
|
|
|
Expect(result).To(Equal(expectation))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("html formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
data := createReportInfo(rule, cwe)
|
|
|
|
|
|
|
|
expect := new(bytes.Buffer)
|
|
|
|
enc := json.NewEncoder(expect)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := enc.Encode(data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err = CreateReport(buf, "html", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
result := stripString(buf.String())
|
|
|
|
expectation := stripString(expect.String())
|
|
|
|
Expect(result).To(ContainSubstring(expectation))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("yaml formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
data := createReportInfo(rule, cwe)
|
|
|
|
|
|
|
|
expect := new(bytes.Buffer)
|
|
|
|
enc := yaml.NewEncoder(expect)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := enc.Encode(data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err = CreateReport(buf, "yaml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
result := stripString(buf.String())
|
|
|
|
expectation := stripString(expect.String())
|
|
|
|
Expect(result).To(ContainSubstring(expectation))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("junit-xml formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
data := createReportInfo(rule, cwe)
|
|
|
|
|
|
|
|
expect := new(bytes.Buffer)
|
|
|
|
enc := yaml.NewEncoder(expect)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := enc.Encode(data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err = CreateReport(buf, "junit-xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - test (Confidence: 2, Severity: 2, CWE: %s)", cwe.ID))
|
|
|
|
result := stripString(buf.String())
|
|
|
|
Expect(result).To(ContainSubstring(expectation))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("text formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
data := createReportInfo(rule, cwe)
|
|
|
|
|
|
|
|
expect := new(bytes.Buffer)
|
|
|
|
enc := yaml.NewEncoder(expect)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := enc.Encode(data)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err = CreateReport(buf, "text", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)", rule, cwe.ID))
|
|
|
|
result := stripString(buf.String())
|
|
|
|
Expect(result).To(ContainSubstring(expectation))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
It("sonarqube formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := CreateReport(buf, "sonarqube", []string{"/home/src/project"}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
|
|
|
|
2019-10-31 08:22:38 +00:00
|
|
|
result := stripString(buf.String())
|
|
|
|
|
|
|
|
expect := new(bytes.Buffer)
|
|
|
|
enc := json.NewEncoder(expect)
|
2020-01-16 08:05:38 +00:00
|
|
|
err = enc.Encode(cwe)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-10-31 08:22:38 +00:00
|
|
|
|
|
|
|
expectation := stripString(expect.String())
|
|
|
|
Expect(result).To(ContainSubstring(expectation))
|
|
|
|
}
|
|
|
|
})
|
2020-01-03 09:56:21 +00:00
|
|
|
It("golint formatted report should contain the CWE mapping", func() {
|
|
|
|
for _, rule := range grules {
|
|
|
|
cwe := gosec.IssueToCWE[rule]
|
|
|
|
issue := createIssue(rule, cwe)
|
|
|
|
error := map[string][]gosec.Error{}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2020-01-16 08:05:38 +00:00
|
|
|
err := CreateReport(buf, "golint", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error)
|
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2020-01-03 09:56:21 +00:00
|
|
|
pattern := "/home/src/project/test.go:1:1: [CWE-%s] test (Rule:%s, Severity:HIGH, Confidence:HIGH)\n"
|
|
|
|
expect := fmt.Sprintf(pattern, cwe.ID, rule)
|
2020-01-16 08:05:38 +00:00
|
|
|
Expect(string(buf.String())).To(Equal(expect))
|
2020-01-03 09:56:21 +00:00
|
|
|
}
|
|
|
|
})
|
2019-10-31 08:22:38 +00:00
|
|
|
})
|
2019-06-24 13:10:51 +01:00
|
|
|
})
|