Skip to content

Commit

Permalink
Added template fucntion to escape string before output (#583)
Browse files Browse the repository at this point in the history
* Added template fucntion to escape string before output

* Fixed tests
  • Loading branch information
rahul2393 committed Jul 31, 2020
1 parent add65f2 commit 52feff2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions contrib/sarif.tpl
Expand Up @@ -20,10 +20,10 @@
"id": "[{{ .Vulnerability.Severity }}] {{ .VulnerabilityID }}",
"name": "dockerfile_scan",
"shortDescription": {
"text": "{{ .VulnerabilityID }} Package: {{ .PkgName }}"
"text": "{{ .VulnerabilityID }} Package: {{ .PkgName }}."
},
"fullDescription": {
"text": "{{ endWithPeriod .Title }}"
"text": "{{ endWithPeriod (escapeString .Title) }}"
},
"help": {
"text": "Vulnerability {{ .VulnerabilityID }}\nSeverity: {{ .Vulnerability.Severity }}\nPackage: {{ .PkgName }}\nInstalled Version: {{ .InstalledVersion }}\nFixed Version: {{ .FixedVersion }}\nLink: [{{ .VulnerabilityID }}](https://nvd.nist.gov/vuln/detail/{{ .VulnerabilityID | toLower }})",
Expand Down Expand Up @@ -57,7 +57,7 @@
"ruleIndex": {{ $index }},
"level": "error",
"message": {
"text": {{ endWithPeriod $vulnerability.Description | printf "%q" }}
"text": {{ endWithPeriod (escapeString $vulnerability.Description) | printf "%q" }}
},
"locations": [{
"physicalLocation": {
Expand Down
8 changes: 4 additions & 4 deletions integration/testdata/alpine-310.sarif.golden
Expand Up @@ -12,7 +12,7 @@
"id": "[MEDIUM] CVE-2019-1549",
"name": "dockerfile_scan",
"shortDescription": {
"text": "CVE-2019-1549 Package: openssl"
"text": "CVE-2019-1549 Package: openssl."
},
"fullDescription": {
"text": "openssl: information disclosure in fork()."
Expand All @@ -34,7 +34,7 @@
"id": "[MEDIUM] CVE-2019-1551",
"name": "dockerfile_scan",
"shortDescription": {
"text": "CVE-2019-1551 Package: openssl"
"text": "CVE-2019-1551 Package: openssl."
},
"fullDescription": {
"text": "openssl: Integer overflow in RSAZ modular exponentiation on x86_64."
Expand All @@ -56,7 +56,7 @@
"id": "[MEDIUM] CVE-2019-1563",
"name": "dockerfile_scan",
"shortDescription": {
"text": "CVE-2019-1563 Package: openssl"
"text": "CVE-2019-1563 Package: openssl."
},
"fullDescription": {
"text": "openssl: information disclosure in PKCS7_dataDecode and CMS_decrypt_set1_pkey."
Expand All @@ -78,7 +78,7 @@
"id": "[LOW] CVE-2019-1547",
"name": "dockerfile_scan",
"shortDescription": {
"text": "CVE-2019-1547 Package: openssl"
"text": "CVE-2019-1547 Package: openssl."
},
"fullDescription": {
"text": "openssl: side-channel weak encryption vulnerability."
Expand Down
4 changes: 4 additions & 0 deletions pkg/report/writer.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"html"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -62,6 +63,9 @@ func WriteResults(format string, output io.Writer, severities []dbTypes.Severity
"toLower": func(input string) string {
return strings.ToLower(input)
},
"escapeString": func(input string) string {
return html.EscapeString(input)
},
}).Parse(outputTemplate)
if err != nil {
return xerrors.Errorf("error parsing template: %w", err)
Expand Down
11 changes: 9 additions & 2 deletions pkg/report/writer_test.go
Expand Up @@ -292,9 +292,16 @@ func TestReportWriter_Template(t *testing.T) {
Description: "with period.",
},
},
{
VulnerabilityID: "CVE-2019-0000",
PkgName: "bar",
Vulnerability: dbTypes.Vulnerability{
Description: `with period and unescaped string curl: Use-after-free when closing 'easy' handle in Curl_close().`,
},
},
},
template: `{{ range . }}{{ range .Vulnerabilities}}{{.VulnerabilityID}} {{ endWithPeriod .Description | printf "%q" }}{{ end }}{{ end }}`,
expected: `CVE-2019-0000 "without period."CVE-2019-0000 "with period."`,
template: `{{ range . }}{{ range .Vulnerabilities}}{{.VulnerabilityID}} {{ endWithPeriod (escapeString .Description) | printf "%q" }}{{ end }}{{ end }}`,
expected: `CVE-2019-0000 "without period."CVE-2019-0000 "with period."CVE-2019-0000 "with period and unescaped string curl: Use-after-free when closing 'easy' handle in Curl_close()."`,
},
}
for _, tc := range testCases {
Expand Down

0 comments on commit 52feff2

Please sign in to comment.