gosec/output/junit_xml_format.go

70 lines
1.7 KiB
Go
Raw Normal View History

2018-01-27 04:14:35 +00:00
package output
import (
"encoding/xml"
2018-02-01 04:30:47 +00:00
htmlLib "html"
"strconv"
2018-01-27 04:14:35 +00:00
"github.com/securego/gosec/v2"
2018-01-27 04:14:35 +00:00
)
type junitXMLReport struct {
2018-01-27 04:14:35 +00:00
XMLName xml.Name `xml:"testsuites"`
Testsuites []testsuite `xml:"testsuite"`
2018-01-27 04:14:35 +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"`
Testcases []testcase `xml:"testcase"`
2018-01-27 04:14:35 +00:00
}
type testcase struct {
2018-01-27 04:14:35 +00:00
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Failure failure `xml:"failure"`
2018-01-27 04:14:35 +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"`
}
func generatePlaintext(issue *gosec.Issue) string {
return "Results:\n" +
"[" + issue.File + ":" + issue.Line + "] - " +
issue.What + " (Confidence: " + strconv.Itoa(int(issue.Confidence)) +
", Severity: " + strconv.Itoa(int(issue.Severity)) +
", CWE: " + issue.Cwe.ID + ")\n" + "> " + htmlLib.EscapeString(issue.Code)
}
func createJUnitXMLStruct(data *reportInfo) junitXMLReport {
var xmlReport junitXMLReport
testsuites := map[string]int{}
for _, issue := range data.Issues {
index, ok := testsuites[issue.What]
if !ok {
xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite{
Name: issue.What,
})
index = len(xmlReport.Testsuites) - 1
testsuites[issue.What] = index
2018-01-27 04:14:35 +00:00
}
testcase := testcase{
Name: issue.File,
Failure: failure{
Message: "Found 1 vulnerability. See stacktrace for details.",
Text: generatePlaintext(issue),
},
2018-01-27 04:14:35 +00:00
}
xmlReport.Testsuites[index].Testcases = append(xmlReport.Testsuites[index].Testcases, testcase)
xmlReport.Testsuites[index].Tests++
2018-01-27 04:14:35 +00:00
}
2018-01-27 04:14:35 +00:00
return xmlReport
}