Skip to content

Commit

Permalink
update: sanitize HTML tag and new line breaks in desc
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperChen-CC committed Jan 9, 2024
1 parent 5dc00dd commit ce52b08
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cmd/format/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html"
"io"
"regexp"
"strings"

"github.com/xmirrorsecurity/opensca-cli/v3/cmd/detail"
Expand Down Expand Up @@ -154,8 +155,8 @@ func formatDesc(v *detail.VulnInfo) string {
{"| cnvd | %s |", v.Cnvd},
{"| cwe | %s |", v.Cwe},
{"| level | %s |", v.SecurityLevel()},
{"| desc | %s |", v.Description},
{"| suggestion | %s |", v.Suggestion},
{"| desc | %s |", sanitizeString(v.Description)},
{"| suggestion | %s |", sanitizeString(v.Suggestion)},
}
var lines []string
for _, line := range table {
Expand All @@ -168,9 +169,20 @@ func formatDesc(v *detail.VulnInfo) string {
lines = append(lines, fmt.Sprintf(line.fmt, line.val))
}
}

return html.EscapeString(strings.Join(lines, "\n"))
}

func sanitizeString(s string) string {
re := regexp.MustCompile("<[^>]*>")
s = re.ReplaceAllString(s, "")

s = strings.ReplaceAll(s, "\r", "")
s = strings.ReplaceAll(s, "\n", "")

return s
}

func formatTags(v *detail.VulnInfo) []string {
tags := []string{"security", "Use-Vulnerable-and-Outdated-Components", v.Cve, v.Cwe, v.AttackType, v.Language}
for i := 0; i < len(tags); {
Expand Down

0 comments on commit ce52b08

Please sign in to comment.