Skip to content

Commit ccd9b2d

Browse files
authored
Added function to escape string in failure message title and descriptions (#551)
* Added function to escape string in failure message title and descriptions * updated template to use xml.EscapeText * Renamed template function
1 parent ec770cd commit ccd9b2d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

contrib/junit.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{{- end -}}
1111
{{ range .Vulnerabilities }}
1212
<testcase classname="{{ .PkgName }}-{{ .InstalledVersion }}" name="[{{ .Vulnerability.Severity }}] {{ .VulnerabilityID }}" time="">
13-
<failure message={{ .Title | printf "%q" }} type="description">{{ .Description | printf "%q" }}</failure>
13+
<failure message={{escapeXML .Title | printf "%q" }} type="description">{{escapeXML .Description | printf "%q" }}</failure>
1414
</testcase>
1515
{{- end }}
1616
</testsuite>

pkg/report/writer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package report
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"encoding/xml"
57
"fmt"
68
"io"
79
"io/ioutil"
@@ -41,7 +43,16 @@ func WriteResults(format string, output io.Writer, results Results, outputTempla
4143
case "json":
4244
writer = &JsonWriter{Output: output}
4345
case "template":
44-
tmpl, err := template.New("output template").Parse(outputTemplate)
46+
tmpl, err := template.New("output template").Funcs(template.FuncMap{
47+
"escapeXML": func(input string) string {
48+
escaped := &bytes.Buffer{}
49+
if err := xml.EscapeText(escaped, []byte(input)); err != nil {
50+
fmt.Printf("error while escapeString to XML: %v", err.Error())
51+
return input
52+
}
53+
return escaped.String()
54+
},
55+
}).Parse(outputTemplate)
4556
if err != nil {
4657
return xerrors.Errorf("error parsing template: %w", err)
4758
}

pkg/report/writer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ func TestReportWriter_Template(t *testing.T) {
239239
InstalledVersion: "1.2.3",
240240
FixedVersion: "3.4.5",
241241
Vulnerability: dbTypes.Vulnerability{
242-
Title: "foobar",
243-
Description: "baz",
242+
Title: `gcc: POWER9 "DARN" RNG intrinsic produces repeated output`,
243+
Description: `curl version curl 7.20.0 to and including curl 7.59.0 contains a CWE-126: Buffer Over-read vulnerability in denial of service that can result in curl can be tricked into reading data beyond the end of a heap based buffer used to store downloaded RTSP content.. This vulnerability appears to have been fixed in curl < 7.20.0 and curl >= 7.60.0.`,
244244
Severity: "HIGH",
245245
},
246246
},
@@ -257,7 +257,7 @@ func TestReportWriter_Template(t *testing.T) {
257257
{{- end -}}
258258
{{ range .Vulnerabilities }}
259259
<testcase classname="{{ .PkgName }}-{{ .InstalledVersion }}" name="[{{ .Vulnerability.Severity }}] {{ .VulnerabilityID }}" time="">
260-
<failure message={{ .Title | printf "%q" }} type="description">{{ .Description | printf "%q" }}</failure>
260+
<failure message={{escapeXML .Title | printf "%q" }} type="description">{{escapeXML .Description | printf "%q" }}</failure>
261261
</testcase>
262262
{{- end }}
263263
</testsuite>
@@ -270,7 +270,7 @@ func TestReportWriter_Template(t *testing.T) {
270270
<property name="type" value="test"></property>
271271
</properties>
272272
<testcase classname="foo-1.2.3" name="[HIGH] 123" time="">
273-
<failure message="foobar" type="description">"baz"</failure>
273+
<failure message="gcc: POWER9 &#34;DARN&#34; RNG intrinsic produces repeated output" type="description">"curl version curl 7.20.0 to and including curl 7.59.0 contains a CWE-126: Buffer Over-read vulnerability in denial of service that can result in curl can be tricked into reading data beyond the end of a heap based buffer used to store downloaded RTSP content.. This vulnerability appears to have been fixed in curl &lt; 7.20.0 and curl &gt;= 7.60.0."</failure>
274274
</testcase>
275275
</testsuite>
276276
</testsuites>`,

0 commit comments

Comments
 (0)