-
Notifications
You must be signed in to change notification settings - Fork 328
/
main.go
211 lines (181 loc) · 6.52 KB
/
main.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
package main
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/google/osv-scanner/internal/ci"
"github.com/google/osv-scanner/internal/version"
"github.com/google/osv-scanner/pkg/models"
"github.com/google/osv-scanner/pkg/osvscanner"
"github.com/google/osv-scanner/pkg/reporter"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)
var (
// Update this variable when doing a release
commit = "n/a"
date = "n/a"
)
// splitLastArg splits the last argument by new lines and appends the split
// elements onto args and returns it
func splitLastArg(args []string) []string {
lastArg := args[len(args)-1]
lastArgSplits := strings.Split(lastArg, "\n")
args = append(args[:len(args)-1], lastArgSplits...)
return args
}
func run(args []string, stdout, stderr io.Writer) int {
var tableReporter reporter.Reporter
// Allow multiple arguments to be defined by github actions by splitting the last argument
// by new lines.
args = splitLastArg(args)
cli.VersionPrinter = func(ctx *cli.Context) {
// Use the app Writer and ErrWriter since they will be the writers to keep parallel tests consistent
tableReporter = reporter.NewTableReporter(ctx.App.Writer, ctx.App.ErrWriter, reporter.InfoLevel, false, 0)
tableReporter.Infof("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date)
}
app := &cli.App{
Name: "osv-scanner-action-reporter",
Version: version.OSVVersion,
Usage: "(Experimental) generates github action output",
Description: "(Experimental) Used specifically to generate github action output ",
Suggest: true,
Writer: stdout,
ErrWriter: stderr,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "old",
Usage: "the old osv json output",
TakesFile: true,
Required: false,
DefaultText: "",
},
&cli.StringFlag{
Name: "new",
Usage: "the new osv json output",
TakesFile: true,
Required: true,
},
&cli.StringFlag{
Name: "output",
Usage: "saves the SARIF result to the given file path",
TakesFile: true,
},
&cli.BoolFlag{
Name: "gh-annotations",
Usage: "prints github action annotations",
},
&cli.BoolFlag{
Name: "fail-on-vuln",
Usage: "whether to return 1 when vulnerabilities are found",
DefaultText: "true",
},
},
Action: func(context *cli.Context) error {
var termWidth int
var err error
if stdoutAsFile, ok := stdout.(*os.File); ok {
termWidth, _, err = term.GetSize(int(stdoutAsFile.Fd()))
if err != nil { // If output is not a terminal,
termWidth = 0
}
}
if tableReporter, err = reporter.New("table", stdout, stderr, reporter.InfoLevel, termWidth); err != nil {
return err
}
oldPath := context.String("old")
newPath := context.String("new")
oldVulns := models.VulnerabilityResults{}
if oldPath != "" {
oldVulns, err = ci.LoadVulnResults(oldPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open old results at %s: %v - likely because target branch has no lockfiles.\n", oldPath, err)
// Do not return, assume there is no oldVulns (which will display all new vulns).
oldVulns = models.VulnerabilityResults{}
}
}
newVulns, err := ci.LoadVulnResults(newPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open new results at %s: %v - likely because previous step failed.\n", newPath, err)
newVulns = models.VulnerabilityResults{}
// Do not return a non zero error code.
}
var diffVulns models.VulnerabilityResults
diffVulnOccurrences := ci.DiffVulnerabilityResultsByOccurrences(oldVulns, newVulns)
if len(diffVulnOccurrences) == 0 {
// There are actually no new vulns, no need to do full diff
//
// Since `DiffVulnerabilityResultsByUniqueVulnCount` does not account for Source or Package,
// this actually changes the results in some cases, e.g.
//
// When a lockfile is moved, `DiffVulnerabilityResults` will report the moved lockfile as having
// a new vulnerability if the existing lockfile has a vulnerability. However this check will
// report no vulnerabilities. This is desired behavior.
// TODO: This will need to be not empty when we change osv-scanner to report all packages
diffVulns = models.VulnerabilityResults{}
} else {
// TODO: This will need to contain all scanned packages when we change osv-scanner to report all packages
diffVulns = ci.DiffVulnerabilityResults(oldVulns, newVulns)
}
if errPrint := tableReporter.PrintResult(&diffVulns); errPrint != nil {
return fmt.Errorf("failed to write output: %w", errPrint)
}
if context.Bool("gh-annotations") {
var ghAnnotationsReporter reporter.Reporter
if ghAnnotationsReporter, err = reporter.New("gh-annotations", stdout, stderr, reporter.InfoLevel, termWidth); err != nil {
return err
}
if errPrint := ghAnnotationsReporter.PrintResult(&diffVulns); errPrint != nil {
return fmt.Errorf("failed to write output: %w", errPrint)
}
}
outputPath := context.String("output")
if outputPath != "" {
var err error
stdout, err = os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
termWidth = 0
var sarifReporter reporter.Reporter
if sarifReporter, err = reporter.New("sarif", stdout, stderr, reporter.InfoLevel, termWidth); err != nil {
return err
}
if errPrint := sarifReporter.PrintResult(&diffVulns); errPrint != nil {
return fmt.Errorf("failed to write output: %w", errPrint)
}
}
// Default to true, only false when explicitly set to false
failOnVuln := !context.IsSet("fail-on-vuln") || context.Bool("fail-on-vuln")
// if vulnerability exists it should return error
if len(diffVulns.Results) > 0 && failOnVuln {
return osvscanner.VulnerabilitiesFoundErr
}
return nil
},
}
if err := app.Run(args); err != nil {
if tableReporter == nil {
tableReporter = reporter.NewTableReporter(stdout, stderr, reporter.InfoLevel, false, 0)
}
if errors.Is(err, osvscanner.VulnerabilitiesFoundErr) {
return 1
}
if errors.Is(err, osvscanner.NoPackagesFoundErr) {
tableReporter.Errorf("No package sources found, --help for usage information.\n")
return 128
}
tableReporter.Errorf("%v\n", err)
}
// if we've been told to print an error, and not already exited with
// a specific error code, then exit with a generic non-zero code
if tableReporter != nil && tableReporter.HasErrored() {
return 127
}
return 0
}
func main() {
os.Exit(run(os.Args, os.Stdout, os.Stderr))
}