2018-01-27 04:14:35 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2018-02-01 04:30:47 +00:00
|
|
|
htmlLib "html"
|
2018-01-27 04:43:08 +00:00
|
|
|
"strconv"
|
2018-01-27 04:14:35 +00:00
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
"github.com/securego/gosec"
|
2018-01-27 04:14:35 +00:00
|
|
|
)
|
|
|
|
|
2018-01-27 06:45:04 +00:00
|
|
|
type junitXMLReport struct {
|
2018-01-27 04:14:35 +00:00
|
|
|
XMLName xml.Name `xml:"testsuites"`
|
2018-01-27 06:45:04 +00:00
|
|
|
Testsuites []testsuite `xml:"testsuite"`
|
2018-01-27 04:14:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 06:45:04 +00:00
|
|
|
type testsuite struct {
|
2018-01-27 04:14:35 +00:00
|
|
|
XMLName xml.Name `xml:"testsuite"`
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Tests int `xml:"tests,attr"`
|
2018-01-27 06:45:04 +00:00
|
|
|
Testcases []testcase `xml:"testcase"`
|
2018-01-27 04:14:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 06:45:04 +00:00
|
|
|
type testcase struct {
|
2018-01-27 04:14:35 +00:00
|
|
|
XMLName xml.Name `xml:"testcase"`
|
|
|
|
Name string `xml:"name,attr"`
|
2018-01-27 06:45:04 +00:00
|
|
|
Failure failure `xml:"failure"`
|
2018-01-27 04:14:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 06:45:04 +00:00
|
|
|
type failure struct {
|
2018-01-27 04:14:35 +00:00
|
|
|
XMLName xml.Name `xml:"failure"`
|
|
|
|
Message string `xml:"message,attr"`
|
|
|
|
Text string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
func generatePlaintext(issue *gosec.Issue) string {
|
2018-01-27 06:45:04 +00:00
|
|
|
return "Results:\n" +
|
|
|
|
"[" + issue.File + ":" + issue.Line + "] - " +
|
|
|
|
issue.What + " (Confidence: " + strconv.Itoa(int(issue.Confidence)) +
|
2019-10-31 08:22:38 +00:00
|
|
|
", Severity: " + strconv.Itoa(int(issue.Severity)) +
|
|
|
|
", CWE: " + issue.Cwe.ID + ")\n" + "> " + htmlLib.EscapeString(issue.Code)
|
2018-01-27 06:45:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
func groupDataByRules(data *reportInfo) map[string][]*gosec.Issue {
|
|
|
|
groupedData := make(map[string][]*gosec.Issue)
|
2018-01-27 04:14:35 +00:00
|
|
|
for _, issue := range data.Issues {
|
|
|
|
if _, ok := groupedData[issue.What]; ok {
|
|
|
|
groupedData[issue.What] = append(groupedData[issue.What], issue)
|
|
|
|
} else {
|
2018-07-19 17:42:25 +01:00
|
|
|
groupedData[issue.What] = []*gosec.Issue{issue}
|
2018-01-27 04:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return groupedData
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:42:25 +01:00
|
|
|
func createJUnitXMLStruct(groupedData map[string][]*gosec.Issue) junitXMLReport {
|
2018-01-27 06:45:04 +00:00
|
|
|
var xmlReport junitXMLReport
|
2018-01-27 04:14:35 +00:00
|
|
|
for what, issues := range groupedData {
|
2018-01-27 06:45:04 +00:00
|
|
|
testsuite := testsuite{
|
2018-01-27 04:14:35 +00:00
|
|
|
Name: what,
|
|
|
|
Tests: len(issues),
|
|
|
|
}
|
|
|
|
for _, issue := range issues {
|
2018-01-27 06:45:04 +00:00
|
|
|
testcase := testcase{
|
2018-01-27 04:14:35 +00:00
|
|
|
Name: issue.File,
|
2018-01-27 06:45:04 +00:00
|
|
|
Failure: failure{
|
2018-01-27 04:14:35 +00:00
|
|
|
Message: "Found 1 vulnerability. See stacktrace for details.",
|
2018-01-27 14:23:07 +00:00
|
|
|
Text: generatePlaintext(issue),
|
2018-01-27 04:14:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
testsuite.Testcases = append(testsuite.Testcases, testcase)
|
|
|
|
}
|
|
|
|
xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite)
|
|
|
|
}
|
|
|
|
return xmlReport
|
|
|
|
}
|