-
Notifications
You must be signed in to change notification settings - Fork 363
/
sarif.go
329 lines (267 loc) · 9.64 KB
/
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package output
import (
"fmt"
"io"
"log"
"path/filepath"
"strings"
"text/template"
"github.com/google/osv-scanner/internal/url"
"github.com/google/osv-scanner/internal/utility/results"
"github.com/google/osv-scanner/internal/version"
"github.com/google/osv-scanner/pkg/models"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/owenrumney/go-sarif/v2/sarif"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
type HelpTemplateData struct {
ID string
AffectedPackagesTable string
AffectedPackagePaths []string
AliasedVulns []VulnDescription
HasFixedVersion bool
FixedVersionTable string
}
type FixedPkgTableData struct {
VulnID string
PackageName string
FixedVersion string
}
type VulnDescription struct {
ID string
Details string
}
// Two double-quotes ("") is replaced with a single backtick (`), since we can't embed backticks in raw strings
const SARIFTemplate = `
**Your dependency is vulnerable to [{{.ID}}](https://osv.dev/list?q={{.ID}})**
{{- if gt (len .AliasedVulns) 1 }}
(Also published as: {{range .AliasedVulns -}} {{if ne .ID $.ID}} [{{.ID}}](https://osv.dev/vulnerability/{{.ID}}), {{end}}{{end}})
{{- end}}.
{{range .AliasedVulns}}
## [{{.ID}}](https://osv.dev/vulnerability/{{.ID}})
<details>
<summary>Details</summary>
> {{.Details}}
</details>
{{end}}
---
### Affected Packages
{{.AffectedPackagesTable}}
## Remediation
{{if .HasFixedVersion}}
To fix these vulnerabilities, update the vulnerabilities past the listed fixed versions below.
### Fixed Versions
{{.FixedVersionTable}}
{{end}}
If you believe these vulnerabilities do not affect your code and wish to ignore them, add them to the ignore list in an
""osv-scanner.toml"" file located in the same directory as the lockfile containing the vulnerable dependency.
See the format and more options in our documentation here: https://google.github.io/osv-scanner/configuration/
Add or append these values to the following config files to ignore this vulnerability:
{{range .AffectedPackagePaths}}
""{{.}}/osv-scanner.toml""
""""""
[[IgnoredVulns]]
id = "{{$.ID}}"
reason = "Your reason for ignoring this vulnerability"
""""""
{{end}}
`
// GroupFixedVersions builds the fixed versions for each ID Group, with keys formatted like so:
// `Source:ID`
func GroupFixedVersions(flattened []models.VulnerabilityFlattened) map[string][]string {
groupFixedVersions := map[string][]string{}
// Get the fixed versions indexed by each group of vulnerabilities
// Prepend source path as same vulnerability in two projects should be counted twice
// Remember to sort and compact before displaying later
for _, vf := range flattened {
groupIdx := vf.Source.String() + ":" + vf.GroupInfo.IndexString()
pkg := models.Package{
Ecosystem: models.Ecosystem(vf.Package.Ecosystem),
Name: vf.Package.Name,
}
groupFixedVersions[groupIdx] =
append(groupFixedVersions[groupIdx], vf.Vulnerability.FixedVersions()[pkg]...)
}
// Remove duplicates
for k := range groupFixedVersions {
fixedVersions := groupFixedVersions[k]
slices.Sort(fixedVersions)
groupFixedVersions[k] = slices.Compact(fixedVersions)
}
return groupFixedVersions
}
// createSARIFAffectedPkgTable creates a vulnerability table which includes the affected versions for a specific source file
func createSARIFAffectedPkgTable(pkgWithSrc []pkgWithSource) table.Writer {
helpTable := table.NewWriter()
helpTable.AppendHeader(table.Row{"Source", "Package Name", "Package Version"})
for _, ps := range pkgWithSrc {
version := ps.Package.Version
if ps.Package.Commit != "" {
version = ps.Package.Commit
}
helpTable.AppendRow(table.Row{
ps.Source.String(),
ps.Package.Name,
version,
})
}
return helpTable
}
// createSARIFFixedPkgTable creates a vulnerability table which includes the fixed versions for a specific source file
func createSARIFFixedPkgTable(fixedPkgTableData []FixedPkgTableData) table.Writer {
helpTable := table.NewWriter()
helpTable.AppendHeader(table.Row{"Vulnerability ID", "Package Name", "Fixed Version"})
slices.SortFunc(fixedPkgTableData, func(a, b FixedPkgTableData) int {
return strings.Compare(a.VulnID, b.VulnID)
})
for _, data := range fixedPkgTableData {
helpTable.AppendRow(table.Row{
data.VulnID,
data.PackageName,
data.FixedVersion,
})
}
return helpTable
}
// stripGitHubWorkspace strips /github/workspace/ from the given path.
func stripGitHubWorkspace(path string) string {
return strings.TrimPrefix(path, "/github/workspace/")
}
// createSARIFHelpText returns the text for SARIF rule's help field
func createSARIFHelpText(gv *groupedSARIFFinding) string {
backtickSARIFTemplate := strings.ReplaceAll(SARIFTemplate, `""`, "`")
helpTextTemplate, err := template.New("helpText").Parse(backtickSARIFTemplate)
if err != nil {
log.Panicf("failed to parse sarif help text template: %v", err)
}
vulnDescriptions := []VulnDescription{}
fixedPkgTableData := []FixedPkgTableData{}
hasFixedVersion := false
for _, v := range gv.AliasedVulns {
for p, v2 := range v.FixedVersions() {
slices.Sort(v2)
fixedPkgTableData = append(fixedPkgTableData, FixedPkgTableData{
PackageName: p.Name,
FixedVersion: strings.Join(slices.Compact(v2), ", "),
VulnID: v.ID,
})
hasFixedVersion = true
}
vulnDescriptions = append(vulnDescriptions, VulnDescription{
ID: v.ID,
Details: strings.ReplaceAll(v.Details, "\n", "\n> "),
})
}
slices.SortFunc(vulnDescriptions, func(a, b VulnDescription) int { return idSortFunc(a.ID, b.ID) })
helpText := strings.Builder{}
pkgWithSrcKeys := maps.Keys(gv.PkgSource)
slices.SortFunc(pkgWithSrcKeys, func(a, b pkgWithSource) int {
// This doesn't take into account multiple packages within the same source file
// which will still be non deterministic. But since that is a rare edge case,
// no need to add significant extra logic here to make it deterministic.
return strings.Compare(a.Source.Path, b.Source.Path)
})
affectedPackagePaths := []string{}
for _, pws := range pkgWithSrcKeys {
affectedPackagePaths = append(affectedPackagePaths, stripGitHubWorkspace(filepath.Dir(pws.Source.Path)))
}
// Compact to remove duplicates
// (which should already be next to each other since it's sorted in the previous step)
affectedPackagePaths = slices.Compact(affectedPackagePaths)
err = helpTextTemplate.Execute(&helpText, HelpTemplateData{
ID: gv.DisplayID,
AffectedPackagesTable: createSARIFAffectedPkgTable(pkgWithSrcKeys).RenderMarkdown(),
AliasedVulns: vulnDescriptions,
HasFixedVersion: hasFixedVersion,
FixedVersionTable: createSARIFFixedPkgTable(fixedPkgTableData).RenderMarkdown(),
AffectedPackagePaths: affectedPackagePaths,
})
if err != nil {
log.Panicf("failed to execute sarif help text template")
}
return helpText.String()
}
// PrintSARIFReport prints SARIF output to outputWriter
func PrintSARIFReport(vulnResult *models.VulnerabilityResults, outputWriter io.Writer) error {
report, err := sarif.New(sarif.Version210)
if err != nil {
return err
}
run := sarif.NewRunWithInformationURI("osv-scanner", "https://github.com/google/osv-scanner")
run.Tool.Driver.WithVersion(version.OSVVersion)
vulnIDMap := mapIDsToGroupedSARIFFinding(vulnResult)
// Sort the IDs to have deterministic loop of vulnIDMap
vulnIDs := []string{}
for vulnID := range vulnIDMap {
vulnIDs = append(vulnIDs, vulnID)
}
slices.Sort(vulnIDs)
for _, vulnID := range vulnIDs {
gv := vulnIDMap[vulnID]
helpText := createSARIFHelpText(gv)
// Pick the "best" description from the alias group based on the source.
// Set short description to the first entry with a non empty summary
// Set long description to the same entry as short description
// or use a random long description.
var shortDescription, longDescription string
ids := slices.Clone(gv.AliasedIDList)
slices.SortFunc(ids, idSortFuncForDescription)
for _, id := range ids {
v := gv.AliasedVulns[id]
longDescription = v.Details
if v.Summary != "" {
shortDescription = fmt.Sprintf("%s: %s", gv.DisplayID, v.Summary)
break
}
}
// If no advisory for this vulnerability has a summary field,
// just show the ID in the shortDescription
if shortDescription == "" {
shortDescription = gv.DisplayID
}
rule := run.AddRule(gv.DisplayID).
WithName(gv.DisplayID).
WithShortDescription(sarif.NewMultiformatMessageString(shortDescription)).
WithFullDescription(sarif.NewMultiformatMessageString(longDescription).WithMarkdown(longDescription)).
WithMarkdownHelp(helpText).
WithTextHelp(helpText)
rule.DeprecatedIds = gv.AliasedIDList
for pws := range gv.PkgSource {
artifactPath := stripGitHubWorkspace(pws.Source.Path)
if filepath.IsAbs(artifactPath) {
// this only errors if the file path is not absolute,
// which we've already confirmed is not the case
p, _ := url.FromFilePath(artifactPath)
artifactPath = p.String()
}
run.AddDistinctArtifact(artifactPath)
alsoKnownAsStr := ""
if len(gv.AliasedIDList) > 1 {
alsoKnownAsStr = fmt.Sprintf(" (also known as '%s')", strings.Join(gv.AliasedIDList[1:], "', '"))
}
run.CreateResultForRule(gv.DisplayID).
WithLevel("warning").
WithMessage(
sarif.NewTextMessage(
fmt.Sprintf(
"Package '%s' is vulnerable to '%s'%s.",
results.PkgToString(pws.Package),
gv.DisplayID,
alsoKnownAsStr,
))).
AddLocation(
sarif.NewLocationWithPhysicalLocation(
sarif.NewPhysicalLocation().
WithArtifactLocation(sarif.NewSimpleArtifactLocation(artifactPath)),
))
}
}
report.AddRun(run)
err = report.PrettyWrite(outputWriter)
if err != nil {
return err
}
fmt.Fprintln(outputWriter)
return nil
}