/
formatter_sarif.go
244 lines (203 loc) · 6.29 KB
/
formatter_sarif.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package formatter
import (
"encoding/json"
"fmt"
"github.com/Legit-Labs/legitify/internal/analyzers"
"strings"
"github.com/owenrumney/go-sarif/v2/sarif"
"github.com/Legit-Labs/legitify/internal/common/severity"
"github.com/Legit-Labs/legitify/internal/outputer/scheme"
)
type sarifFormatter struct {
colorizer sarifColorizer
}
func newSarifFormatter() OutputFormatter {
return &sarifFormatter{
colorizer: sarifColorizer{},
}
}
func (f *sarifFormatter) URIFromLink(link string) (base, uri string) {
const (
httpsPrefix = "https://"
httpPrefix = "http://"
)
if strings.HasPrefix(link, httpsPrefix) {
return httpsPrefix, link[len(httpsPrefix):]
} else if strings.HasPrefix(link, httpPrefix) {
return httpPrefix, link[len(httpPrefix):]
} else {
return "", link
}
}
func (f *sarifFormatter) Format(s scheme.Scheme, failedOnly bool) ([]byte, error) {
report, err := sarif.New(sarif.Version210)
if err != nil {
return nil, err
}
typedOutput, ok := s.(*scheme.Flattened)
if !ok {
return nil, UnsupportedScheme{s}
}
run := sarif.NewRunWithInformationURI("legitify", "https://legitify.dev/")
for _, policyName := range s.AsOrderedMap().Keys() {
data := typedOutput.GetPolicyData(policyName)
policyInfo := data.PolicyInfo
pb := sarif.NewPropertyBag()
pb.Add("impact", policyInfo.Threat)
pb.Add("resolution", policyInfo.RemediationSteps)
pb.Add("precision", "high")
pb.Add("problem.severity", sarifProblemSeverity(policyInfo.Severity))
pb.Add("security-severity", sarifSecuritySeverity(policyInfo.Severity))
run.AddRule(policyInfo.FullyQualifiedPolicyName).
WithDescription(policyInfo.Description).
WithShortDescription(sarif.NewMultiformatMessageString(policyInfo.Title)).
WithProperties(pb.Properties).
WithTextHelp(getPlaintextPolicySummary(typedOutput, policyName)).
WithMarkdownHelp(getMarkdownPolicySummary(typedOutput, policyName))
// Tools like legitify don't fit perfectly into the SARIF model, so we're going to follow the
// lead of OpenSSF's scorecard output as a starting point.
// https://github.com/ossf/scorecard/blob/273dccda33590b7b46e98e19a9154f9da5400521/pkg/testdata/check6.sarif
for _, violation := range data.Violations {
if violation.Status != analyzers.PolicyFailed {
continue
}
base, uri := f.URIFromLink(violation.CanonicalLink)
run.AddDistinctArtifact(violation.ViolationEntityType)
run.CreateResultForRule(policyInfo.FullyQualifiedPolicyName).
WithLevel(sarifSeverity(policyInfo.Severity)).
WithMessage(sarif.NewTextMessage(getViolationMessage(&violation, &policyInfo))).
WithHostedViewerUri(violation.CanonicalLink).
AddLocation(
sarif.NewLocationWithPhysicalLocation(
sarif.NewPhysicalLocation().
WithArtifactLocation(
sarif.NewArtifactLocation().
WithUri(uri).WithUriBaseId(base),
),
),
)
}
}
report.AddRun(run)
bytes, err := json.MarshalIndent(report, "", DefaultOutputIndent)
if err != nil {
return nil, err
}
return bytes, nil
}
func (f *sarifFormatter) IsSchemeSupported(schemeType string) bool {
return true
}
func getViolationMessage(violation *scheme.Violation, policyInfo *scheme.PolicyInfo) string {
pc := getSarifContent()
return strings.TrimSpace(pc.pf.FormatText(0, policyInfo.Description)) +
pc.pf.Linebreak() +
string(pc.FormatViolation(violation))
}
// See https://github.com/github/docs/issues/21221
func sarifSeverity(s severity.Severity) string {
switch s {
case severity.Critical:
return "error"
case severity.High:
return "error"
case severity.Medium:
return "warning"
case severity.Low:
return "note"
default:
return "none"
}
}
func sarifProblemSeverity(s severity.Severity) string {
switch s {
case severity.Critical:
return "error"
case severity.High:
return "error"
case severity.Medium:
return "warning"
case severity.Low:
return "recommendation"
default:
return "recommendation"
}
}
func sarifSecuritySeverity(s severity.Severity) string {
switch s {
case severity.Critical:
return "9.0"
case severity.High:
return "7.0"
case severity.Medium:
return "4.0"
case severity.Low:
return "1.0"
default:
return "1.0"
}
}
func getSarifContent() *policiesContent {
sFormatter := newSarifFormatter()
typedFormatter := sFormatter.(*sarifFormatter)
pf := newSarifPolicyFormatter()
pc := newPoliciesContent(pf, typedFormatter.colorizer)
return pc
}
func getMarkdownContent() *policiesContent {
sFormatter := newMarkdownFormatter()
typedFormatter := sFormatter.(*markdownFormatter)
pf := newMarkdownPolicyFormatter()
pc := newPoliciesContent(pf, typedFormatter.colorizer)
return pc
}
func getPlaintextPolicySummary(output *scheme.Flattened, policyName string) string {
return string(getSarifContent().FormatPolicy(output, policyName))
}
func getMarkdownPolicySummary(output *scheme.Flattened, policyName string) string {
pc := getMarkdownContent()
return string(pc.FormatPolicy(output, policyName))
}
type sarifColorizer struct {
}
func (sc sarifColorizer) colorize(tColor themeColor, text interface{}) string {
return fmt.Sprintf("%v", text)
}
// plaintext policy formatting
type sarifPolicyFormatter struct {
colorizer sarifColorizer
}
func newSarifPolicyFormatter() sarifPolicyFormatter {
return sarifPolicyFormatter{colorizer: sarifColorizer{}}
}
func (sp sarifPolicyFormatter) FormatTitle(title string, severity severity.Severity) string {
color := severityToThemeColor(severity)
title = sp.colorizer.colorize(color, title)
return title
}
func (sp sarifPolicyFormatter) FormatSubtitle(title string) string {
return title
}
func (sp sarifPolicyFormatter) FormatText(depth int, format string, args ...interface{}) string {
return indentMultilineSpecial(depth, fmt.Sprintf(format, args...), sp.Indent(1), sp.Linebreak())
}
func (sp sarifPolicyFormatter) FormatList(depth int, title string, list []string, ordered bool) string {
if len(list) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString(sp.FormatText(depth, "%s%s", title, sp.Linebreak()))
for _, step := range list {
sb.WriteString(sp.FormatText(depth, "%s%s", step, sp.Linebreak()))
}
return sb.String()
}
func (sp sarifPolicyFormatter) Linebreak() string {
return " \n"
}
func (sp sarifPolicyFormatter) Separator() string {
return "---"
}
func (sp sarifPolicyFormatter) Indent(depth int) string {
return strings.Repeat(" ", depth)
}