Skip to content

Commit ce52b08

Browse files
committed
update: sanitize HTML tag and new line breaks in desc
1 parent 5dc00dd commit ce52b08

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

cmd/format/sarif.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"html"
77
"io"
8+
"regexp"
89
"strings"
910

1011
"github.com/xmirrorsecurity/opensca-cli/v3/cmd/detail"
@@ -154,8 +155,8 @@ func formatDesc(v *detail.VulnInfo) string {
154155
{"| cnvd | %s |", v.Cnvd},
155156
{"| cwe | %s |", v.Cwe},
156157
{"| level | %s |", v.SecurityLevel()},
157-
{"| desc | %s |", v.Description},
158-
{"| suggestion | %s |", v.Suggestion},
158+
{"| desc | %s |", sanitizeString(v.Description)},
159+
{"| suggestion | %s |", sanitizeString(v.Suggestion)},
159160
}
160161
var lines []string
161162
for _, line := range table {
@@ -168,9 +169,20 @@ func formatDesc(v *detail.VulnInfo) string {
168169
lines = append(lines, fmt.Sprintf(line.fmt, line.val))
169170
}
170171
}
172+
171173
return html.EscapeString(strings.Join(lines, "\n"))
172174
}
173175

176+
func sanitizeString(s string) string {
177+
re := regexp.MustCompile("<[^>]*>")
178+
s = re.ReplaceAllString(s, "")
179+
180+
s = strings.ReplaceAll(s, "\r", "")
181+
s = strings.ReplaceAll(s, "\n", "")
182+
183+
return s
184+
}
185+
174186
func formatTags(v *detail.VulnInfo) []string {
175187
tags := []string{"security", "Use-Vulnerable-and-Outdated-Components", v.Cve, v.Cwe, v.AttackType, v.Language}
176188
for i := 0; i < len(tags); {

0 commit comments

Comments
 (0)