2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2016-07-28 04:55:09 +01:00
|
|
|
"encoding/csv"
|
2016-07-26 00:39:55 +01:00
|
|
|
"encoding/json"
|
2018-01-26 03:16:49 +00:00
|
|
|
"encoding/xml"
|
2016-10-18 06:36:35 +01:00
|
|
|
htmlTemplate "html/template"
|
2016-07-20 11:02:01 +01:00
|
|
|
"io"
|
2016-10-18 06:36:35 +01:00
|
|
|
plainTemplate "text/template"
|
2016-07-20 11:02:01 +01:00
|
|
|
|
2017-04-26 16:08:46 +01:00
|
|
|
"github.com/GoASTScanner/gas"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// ReportFormat enumrates the output format for reported issues
|
2016-07-20 11:02:01 +01:00
|
|
|
type ReportFormat int
|
|
|
|
|
|
|
|
const (
|
2017-12-13 12:35:47 +00:00
|
|
|
// ReportText is the default format that writes to stdout
|
2016-07-20 11:02:01 +01:00
|
|
|
ReportText ReportFormat = iota // Plain text format
|
2017-12-13 12:35:47 +00:00
|
|
|
|
|
|
|
// ReportJSON set the output format to json
|
|
|
|
ReportJSON // Json format
|
|
|
|
|
|
|
|
// ReportCSV set the output format to csv
|
|
|
|
ReportCSV // CSV format
|
2018-01-26 03:16:49 +00:00
|
|
|
|
|
|
|
// ReportXML set the output format to junit xml
|
|
|
|
ReportXML // JUnit XML format
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var text = `Results:
|
|
|
|
{{ range $index, $issue := .Issues }}
|
|
|
|
[{{ $issue.File }}:{{ $issue.Line }}] - {{ $issue.What }} (Confidence: {{ $issue.Confidence}}, Severity: {{ $issue.Severity }})
|
|
|
|
> {{ $issue.Code }}
|
|
|
|
|
|
|
|
{{ end }}
|
|
|
|
Summary:
|
|
|
|
Files: {{.Stats.NumFiles}}
|
|
|
|
Lines: {{.Stats.NumLines}}
|
|
|
|
Nosec: {{.Stats.NumNosec}}
|
|
|
|
Issues: {{.Stats.NumFound}}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
type reportInfo struct {
|
|
|
|
Issues []*gas.Issue
|
|
|
|
Stats *gas.Metrics
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:16:49 +00:00
|
|
|
type XMLReport struct {
|
|
|
|
XMLName xml.Name `xml:"testsuites"`
|
|
|
|
Testsuites []Testsuite `xml:"testsuite"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Testsuite struct {
|
|
|
|
XMLName xml.Name `xml:"testsuite"`
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Tests int `xml:"tests,attr"`
|
|
|
|
Testcases []Testcase `xml:"testcase"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Testcase struct {
|
|
|
|
XMLName xml.Name `xml:"testcase"`
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Failure Failure `xml:"failure"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Failure struct {
|
|
|
|
XMLName xml.Name `xml:"failure"`
|
|
|
|
Message string `xml:"message,attr"`
|
|
|
|
Text string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// CreateReport generates a report based for the supplied issues and metrics given
|
|
|
|
// the specified format. The formats currently accepted are: json, csv, html and text.
|
2017-04-26 01:57:12 +01:00
|
|
|
func CreateReport(w io.Writer, format string, issues []*gas.Issue, metrics *gas.Metrics) error {
|
|
|
|
data := &reportInfo{
|
|
|
|
Issues: issues,
|
|
|
|
Stats: metrics,
|
|
|
|
}
|
2016-07-26 00:39:55 +01:00
|
|
|
var err error
|
2016-07-20 11:02:01 +01:00
|
|
|
switch format {
|
|
|
|
case "json":
|
2016-07-26 00:39:55 +01:00
|
|
|
err = reportJSON(w, data)
|
|
|
|
case "csv":
|
2016-07-28 04:55:09 +01:00
|
|
|
err = reportCSV(w, data)
|
2018-01-26 03:16:49 +00:00
|
|
|
case "xml":
|
|
|
|
err = reportXML(w, data)
|
2016-10-18 06:36:35 +01:00
|
|
|
case "html":
|
|
|
|
err = reportFromHTMLTemplate(w, html, data)
|
2016-07-20 11:02:01 +01:00
|
|
|
case "text":
|
2016-10-18 06:36:35 +01:00
|
|
|
err = reportFromPlaintextTemplate(w, text, data)
|
2016-07-20 11:02:01 +01:00
|
|
|
default:
|
2016-10-18 06:36:35 +01:00
|
|
|
err = reportFromPlaintextTemplate(w, text, data)
|
2016-07-26 00:39:55 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportJSON(w io.Writer, data *reportInfo) error {
|
2016-07-26 00:39:55 +01:00
|
|
|
raw, err := json.MarshalIndent(data, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-07-26 00:39:55 +01:00
|
|
|
_, err = w.Write(raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportCSV(w io.Writer, data *reportInfo) error {
|
2016-07-28 04:55:09 +01:00
|
|
|
out := csv.NewWriter(w)
|
|
|
|
defer out.Flush()
|
|
|
|
for _, issue := range data.Issues {
|
|
|
|
err := out.Write([]string{
|
|
|
|
issue.File,
|
2017-10-01 01:31:39 +01:00
|
|
|
issue.Line,
|
2016-07-28 04:55:09 +01:00
|
|
|
issue.What,
|
|
|
|
issue.Severity.String(),
|
|
|
|
issue.Confidence.String(),
|
|
|
|
issue.Code,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:16:49 +00:00
|
|
|
func reportXML(w io.Writer, data *reportInfo) error {
|
|
|
|
testsuites := make(map[string][]Testcase)
|
|
|
|
for _, issue := range data.Issues {
|
|
|
|
stacktrace, err := json.MarshalIndent(issue, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
testcase := Testcase{
|
|
|
|
Name: issue.File,
|
|
|
|
Failure: Failure{
|
|
|
|
Message: "Found 1 vulnerability. See stacktrace for details.",
|
|
|
|
Text: string(stacktrace),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, ok := testsuites[issue.What]; ok {
|
|
|
|
testsuites[issue.What] = append(testsuites[issue.What], testcase)
|
|
|
|
} else {
|
|
|
|
testsuites[issue.What] = []Testcase{testcase}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var xmlReport XMLReport
|
|
|
|
for what, testcases := range testsuites {
|
|
|
|
testsuite := Testsuite{
|
|
|
|
Name: what,
|
|
|
|
Tests: len(testcases),
|
|
|
|
}
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
testsuite.Testcases = append(testsuite.Testcases, testcase)
|
|
|
|
}
|
|
|
|
xmlReport.Testsuites = append(xmlReport.Testsuites, testsuite)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := xml.Marshal(xmlReport)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-27 03:49:58 +00:00
|
|
|
raw = append([]byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"), raw...)
|
2018-01-26 03:16:49 +00:00
|
|
|
_, err = w.Write(raw)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportFromPlaintextTemplate(w io.Writer, reportTemplate string, data *reportInfo) error {
|
2016-10-18 06:36:35 +01:00
|
|
|
t, e := plainTemplate.New("gas").Parse(reportTemplate)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Execute(w, data)
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:57:12 +01:00
|
|
|
func reportFromHTMLTemplate(w io.Writer, reportTemplate string, data *reportInfo) error {
|
2016-10-18 06:36:35 +01:00
|
|
|
t, e := htmlTemplate.New("gas").Parse(reportTemplate)
|
2016-07-20 11:02:01 +01:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Execute(w, data)
|
|
|
|
}
|