GitLabgitlab-org

cli

main
ply6 git calls39 ms99 KiB5 cachedprotocol v2
355 lines9.7 KiBRaw
1// Command govulncheck-to-gitlab converts `govulncheck -format json` output into
2// a GitLab Dependency Scanning report.
3//
4// Only symbol-reachable findings are reported. govulncheck emits each
5// vulnerability three times, at module, package and symbol level; the first two
6// mean "this version is in our module graph", which is what the SBOM analyzer
7// already tells us. A symbol-level finding means our code has a call path into
8// the vulnerable function, which is the thing worth acting on.
9//
10// The exit code is 3 when a reachable finding is not on the ignore list, so a
11// caller can gate on it, and 1 when the input could not be processed at all.
12// Reporting nothing because govulncheck crashed must not look like success.
13package main
15import (
16 "crypto/sha256"
17 "encoding/json"
18 "errors"
19 "fmt"
20 "io"
21 "os"
22 "sort"
23 "strings"
24 "time"
27const (
28 exitFindings = 3
29 exitError = 1
31 schemaVersion = "15.2.5"
32 reportFile = "go.mod"
34 // The schema's start_time/end_time pattern rejects a timezone suffix, so
35 // RFC3339 cannot be used here.
36 scanTimeLayout = "2006-01-02T15:04:05"
39// Accepted reachable vulnerabilities. Every entry needs an issue recording why
40// the risk was accepted, so that "the pipeline is green" never silently means
41// "somebody added a line here". Modelled on gitlab-org/gitaly's
42// tools/govulncheck-filter.
43var ignored = map[string]string{
44 // "GO-0000-0000": "https://gitlab.com/gitlab-org/cli/-/issues/0",
47type position struct {
48 Filename string `json:"filename"`
49 Line int `json:"line"`
50 Column int `json:"column"`
53type traceFrame struct {
54 Module string `json:"module"`
55 Version string `json:"version"`
56 Package string `json:"package"`
57 Function string `json:"function"`
58 Receiver string `json:"receiver"`
59 Position *position `json:"position"`
62type finding struct {
63 OSV string `json:"osv"`
64 FixedVersion string `json:"fixed_version"`
65 Trace []traceFrame `json:"trace"`
68type osvEntry struct {
69 ID string `json:"id"`
70 Aliases []string `json:"aliases"`
71 Summary string `json:"summary"`
72 Details string `json:"details"`
75type scanConfig struct {
76 ScannerName string `json:"scanner_name"`
77 ScannerVersion string `json:"scanner_version"`
78 DB string `json:"db"`
79 GoVersion string `json:"go_version"`
82type message struct {
83 Config *scanConfig `json:"config"`
84 OSV *osvEntry `json:"osv"`
85 Finding *finding `json:"finding"`
88type identifier struct {
89 Type string `json:"type"`
90 Name string `json:"name"`
91 Value string `json:"value"`
92 URL string `json:"url,omitempty"`
95type pkg struct {
96 Name string `json:"name"`
99type dependency struct {
100 Package pkg `json:"package"`
101 Version string `json:"version"`
104type location struct {
105 File string `json:"file"`
106 Dependency dependency `json:"dependency"`
109type link struct {
110 URL string `json:"url"`
113type vulnerability struct {
114 ID string `json:"id"`
115 Name string `json:"name,omitempty"`
116 Description string `json:"description,omitempty"`
117 Severity string `json:"severity,omitempty"`
118 Solution string `json:"solution,omitempty"`
119 Identifiers []identifier `json:"identifiers"`
120 Links []link `json:"links,omitempty"`
121 Location location `json:"location"`
124type vendor struct {
125 Name string `json:"name"`
128type analyzer struct {
129 ID string `json:"id"`
130 Name string `json:"name"`
131 Version string `json:"version"`
132 Vendor vendor `json:"vendor"`
133 URL string `json:"url,omitempty"`
136type scan struct {
137 Analyzer analyzer `json:"analyzer"`
138 Scanner analyzer `json:"scanner"`
139 Type string `json:"type"`
140 StartTime string `json:"start_time"`
141 EndTime string `json:"end_time"`
142 Status string `json:"status"`
145type report struct {
146 Version string `json:"version"`
147 Scan scan `json:"scan"`
148 Vulnerabilities []vulnerability `json:"vulnerabilities"`
151// decodeStream reads the concatenated JSON objects govulncheck writes. It is a
152// stream of separate values rather than an array, so json.Decoder is used
153// directly instead of unmarshalling the whole input.
154func decodeStream(r io.Reader) ([]message, error) {
155 var msgs []message
156 dec := json.NewDecoder(r)
157 for {
158 var m message
159 if err := dec.Decode(&m); err != nil {
160 if errors.Is(err, io.EOF) {
161 return msgs, nil
162 }
163 return nil, fmt.Errorf("decode: %w", err)
164 }
165 msgs = append(msgs, m)
166 }
169func reachable(f *finding) bool {
170 for _, t := range f.Trace {
171 if t.Function != "" {
172 return true
173 }
174 }
175 return false
178// vulnID derives a stable identifier so re-running the scan does not create a
179// new vulnerability record for an unchanged finding.
180func vulnID(osv, module, version string) string {
181 sum := sha256.Sum256([]byte(osv + "\x00" + module + "\x00" + version))
182 return fmt.Sprintf("%x-%x-%x-%x-%x", sum[0:4], sum[4:6], sum[6:8], sum[8:10], sum[10:16])
185func identifiersFor(o *osvEntry) []identifier {
186 ids := []identifier{{
187 Type: "go-vuln",
188 Name: o.ID,
189 Value: o.ID,
190 URL: "https://pkg.go.dev/vuln/" + o.ID,
191 }}
192 for _, alias := range o.Aliases {
193 switch {
194 case strings.HasPrefix(alias, "CVE-"):
195 ids = append(ids, identifier{Type: "cve", Name: alias, Value: alias})
196 case strings.HasPrefix(alias, "GHSA-"):
197 ids = append(ids, identifier{Type: "ghsa", Name: alias, Value: alias})
198 }
199 }
200 return ids
203func describe(o *osvEntry, f *finding, frame traceFrame) string {
204 var b strings.Builder
205 if o.Details != "" {
206 b.WriteString(o.Details)
207 }
208 b.WriteString("\n\nReachable call path found by govulncheck:\n")
209 for _, t := range f.Trace {
210 if t.Function == "" {
211 continue
212 }
213 name := t.Function
214 if t.Receiver != "" {
215 name = t.Receiver + "." + name
216 }
217 fmt.Fprintf(&b, "- %s.%s", t.Package, name)
218 if t.Position != nil {
219 fmt.Fprintf(&b, " (%s:%d)", t.Position.Filename, t.Position.Line)
220 }
221 b.WriteString("\n")
222 }
223 fmt.Fprintf(&b, "\nFound in %s@%s.", frame.Module, frame.Version)
224 return b.String()
227func convert(msgs []message, now time.Time) (report, []string, error) {
228 var cfg scanConfig
229 sawConfig := false
230 osvs := map[string]*osvEntry{}
231 var findings []*finding
233 for i := range msgs {
234 switch {
235 case msgs[i].Config != nil:
236 cfg = *msgs[i].Config
237 sawConfig = true
238 case msgs[i].OSV != nil:
239 osvs[msgs[i].OSV.ID] = msgs[i].OSV
240 case msgs[i].Finding != nil && reachable(msgs[i].Finding):
241 findings = append(findings, msgs[i].Finding)
242 }
243 }
245 // govulncheck always emits a config message first. Without one the input is
246 // not a scan that ran, and reporting zero vulnerabilities would turn a
247 // crashed scanner into a passing build.
248 if !sawConfig {
249 return report{}, nil, errors.New("no govulncheck config message in input")
250 }
252 seen := map[string]struct{}{}
253 // Empty rather than nil: a clean scan is the expected outcome, and the
254 // schema requires an array, which a nil slice marshals to as null.
255 vulns := []vulnerability{}
256 var unignored []string
258 for _, f := range findings {
259 if _, ok := seen[f.OSV]; ok {
260 continue
261 }
262 seen[f.OSV] = struct{}{}
264 o, ok := osvs[f.OSV]
265 if !ok {
266 o = &osvEntry{ID: f.OSV}
267 }
269 // The first frame is the vulnerable function itself, so it carries the
270 // module and version that need upgrading.
271 var frame traceFrame
272 if len(f.Trace) > 0 {
273 frame = f.Trace[0]
274 }
276 solution := ""
277 if f.FixedVersion != "" {
278 solution = fmt.Sprintf("Upgrade %s to %s.", frame.Module, f.FixedVersion)
279 }
281 vulns = append(vulns, vulnerability{
282 ID: vulnID(f.OSV, frame.Module, frame.Version),
283 Name: o.Summary,
284 // The Go vulnerability database carries no CVSS data, so there is
285 // nothing to map a real severity from.
286 Severity: "Unknown",
287 Description: describe(o, f, frame),
288 Solution: solution,
289 Identifiers: identifiersFor(o),
290 Links: []link{{URL: "https://pkg.go.dev/vuln/" + f.OSV}},
291 Location: location{
292 File: reportFile,
293 Dependency: dependency{Package: pkg{Name: frame.Module}, Version: frame.Version},
294 },
295 })
297 if _, ok := ignored[f.OSV]; !ok {
298 unignored = append(unignored, f.OSV)
299 }
300 }
302 sort.Slice(vulns, func(i, j int) bool { return vulns[i].ID < vulns[j].ID })
303 sort.Strings(unignored)
305 stamp := now.UTC().Format(scanTimeLayout)
306 scanner := analyzer{
307 ID: "govulncheck",
308 Name: "govulncheck",
309 Version: strings.TrimPrefix(cfg.ScannerVersion, "v"),
310 Vendor: vendor{Name: "Go"},
311 URL: "https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck",
312 }
314 return report{
315 Version: schemaVersion,
316 Scan: scan{Analyzer: scanner, Scanner: scanner, Type: "dependency_scanning", StartTime: stamp, EndTime: stamp, Status: "success"},
317 Vulnerabilities: vulns,
318 }, unignored, nil
321func main() {
322 msgs, err := decodeStream(os.Stdin)
323 if err != nil {
324 fmt.Fprintf(os.Stderr, "govulncheck-to-gitlab: %v\n", err)
325 os.Exit(exitError)
326 }
328 rep, unignored, err := convert(msgs, time.Now())
329 if err != nil {
330 fmt.Fprintf(os.Stderr, "govulncheck-to-gitlab: %v\n", err)
331 os.Exit(exitError)
332 }
334 // IOStreams.PrintJSON is for glab commands; this is a standalone build
335 // script whose stdout is a report file consumed by CI.
336 out, err := json.MarshalIndent(rep, "", " ") //nolint:forbidigo
337 if err != nil {
338 fmt.Fprintf(os.Stderr, "govulncheck-to-gitlab: %v\n", err)
339 os.Exit(exitError)
340 }
341 if _, err := os.Stdout.Write(append(out, '\n')); err != nil {
342 fmt.Fprintf(os.Stderr, "govulncheck-to-gitlab: %v\n", err)
343 os.Exit(exitError)
344 }
346 for _, id := range unignored {
347 fmt.Fprintf(os.Stderr, "reachable vulnerability not on the ignore list: %s (https://pkg.go.dev/vuln/%s)\n", id, id)
348 }
349 if len(unignored) > 0 {
350 os.Exit(exitFindings)
351 }
352 for id, issue := range ignored {
353 fmt.Fprintf(os.Stderr, "ignored: %s (%s)\n", id, issue)
354 }
Diagnostics6 git calls · 39 ms
CallStatusSizeTime039 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=7
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=6
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit7 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=7
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0200136 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=25
git.response.bytes=136
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=1215c0a27f1ea813cb3e842ff9a56b59bf0769bb
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=136
git.pkt.payload.max=109
git.sideband.band1.bytes=109
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=109
git.pack.trailer=603085b158b3e2723be7b292f69a8940e6abb23e
Open pack map
fetch2003.6 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3698
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=86527f8cd8b914bc7942ba43f4700052bce4d1b4
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3698
git.pkt.payload.max=3671
git.sideband.band1.bytes=3671
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e28c205daabd4a
Request
c250a8a7…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format