GitLabgitlab-org
cli
main
1package main
3import (
4 "encoding/json"
5 "strings"
6 "testing"
7 "time"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
13const configMsg = `{"config":{"scanner_name":"govulncheck","scanner_version":"v1.8.0","db":"https://vuln.go.dev","go_version":"go1.27.0"}}`
15func osvMsg(id, alias string) string {
16 return `{"osv":{"id":"` + id + `","aliases":["` + alias + `","GHSA-aaaa-bbbb-cccc"],` +
17 `"summary":"Something bad in example.com/mod","details":"Longer description."}}`
18}
20func findingMsg(id, level string) string {
21 frame := `{"module":"example.com/mod","version":"v1.0.0"`
22 switch level {
23 case "symbol":
24 frame += `,"package":"example.com/mod/pkg","function":"Vulnerable","receiver":"*T",` +
25 `"position":{"filename":"pkg/file.go","line":42,"column":3}`
26 case "package":
27 frame += `,"package":"example.com/mod/pkg"`
28 }
29 frame += `}`
30 return `{"finding":{"osv":"` + id + `","fixed_version":"v1.2.3","trace":[` + frame + `]}}`
31}
33func convertString(t *testing.T, input string) (report, []string, error) {
34 t.Helper()
35 msgs, err := decodeStream(strings.NewReader(input))
36 require.NoError(t, err)
37 return convert(msgs, time.Date(2026, 9, 16, 17, 4, 5, 0, time.UTC))
38}
40func TestConvertReportsOnlyReachableFindings(t *testing.T) {
41 t.Parallel()
43 input := strings.Join([]string{
44 configMsg,
45 osvMsg("GO-2026-0001", "CVE-2026-1111"),
46 osvMsg("GO-2026-0002", "CVE-2026-2222"),
47 findingMsg("GO-2026-0001", "module"),
48 findingMsg("GO-2026-0001", "package"),
49 findingMsg("GO-2026-0001", "symbol"),
50 findingMsg("GO-2026-0002", "module"),
51 }, "\n")
53 rep, unignored, err := convertString(t, input)
54 require.NoError(t, err)
56 require.Len(t, rep.Vulnerabilities, 1, "module-only findings must not be reported")
57 assert.Equal(t, []string{"GO-2026-0001"}, unignored)
59 v := rep.Vulnerabilities[0]
60 assert.Equal(t, "Something bad in example.com/mod", v.Name)
61 assert.Equal(t, "Unknown", v.Severity)
62 assert.Equal(t, "Upgrade example.com/mod to v1.2.3.", v.Solution)
63 assert.Equal(t, "go.mod", v.Location.File)
64 assert.Equal(t, "example.com/mod", v.Location.Dependency.Package.Name)
65 assert.Equal(t, "v1.0.0", v.Location.Dependency.Version)
66 assert.Contains(t, v.Description, "example.com/mod/pkg.*T.Vulnerable (pkg/file.go:42)")
67}
69func TestConvertEmitsCVEAndGHSAIdentifiers(t *testing.T) {
70 t.Parallel()
72 rep, _, err := convertString(t, strings.Join([]string{
73 configMsg,
74 osvMsg("GO-2026-0001", "CVE-2026-1111"),
75 findingMsg("GO-2026-0001", "symbol"),
76 }, "\n"))
77 require.NoError(t, err)
78 require.Len(t, rep.Vulnerabilities, 1)
80 types := map[string]string{}
81 for _, id := range rep.Vulnerabilities[0].Identifiers {
82 types[id.Type] = id.Value
83 }
84 // The CVE and GHSA aliases are what let GitLab deduplicate this finding
85 // against the SBOM analyzer's report for the same vulnerability.
86 assert.Equal(t, "GO-2026-0001", types["go-vuln"])
87 assert.Equal(t, "CVE-2026-1111", types["cve"])
88 assert.Equal(t, "GHSA-aaaa-bbbb-cccc", types["ghsa"])
89}
91// Not parallel: the subtests swap the package-level ignore list.
92func TestConvertIgnoreList(t *testing.T) {
93 input := strings.Join([]string{
94 configMsg,
95 osvMsg("GO-2026-0001", "CVE-2026-1111"),
96 findingMsg("GO-2026-0001", "symbol"),
97 }, "\n")
99 msgs, err := decodeStream(strings.NewReader(input))
100 require.NoError(t, err)
102 t.Run("not ignored", func(t *testing.T) {
103 _, unignored, err := convert(msgs, time.Now())
104 require.NoError(t, err)
105 assert.Equal(t, []string{"GO-2026-0001"}, unignored)
106 })
108 t.Run("ignored", func(t *testing.T) {
109 original := ignored
110 t.Cleanup(func() { ignored = original })
111 ignored = map[string]string{"GO-2026-0001": "https://gitlab.com/gitlab-org/cli/-/issues/1"}
113 rep, unignored, err := convert(msgs, time.Now())
114 require.NoError(t, err)
115 assert.Empty(t, unignored, "an ignored finding must not gate the pipeline")
116 assert.Len(t, rep.Vulnerabilities, 1, "an ignored finding is still reported")
117 })
118}
120func TestConvertRejectsInputWithoutConfig(t *testing.T) {
121 t.Parallel()
123 // A crashed govulncheck produces no output. Treating that as a clean scan
124 // would report success for a check that never ran.
125 _, _, err := convertString(t, "")
126 require.Error(t, err)
128 _, _, err = convertString(t, osvMsg("GO-2026-0001", "CVE-2026-1111"))
129 require.Error(t, err)
130}
132func TestConvertScanMetadata(t *testing.T) {
133 t.Parallel()
135 rep, _, err := convertString(t, configMsg)
136 require.NoError(t, err)
138 assert.Empty(t, rep.Vulnerabilities)
139 assert.Equal(t, "dependency_scanning", rep.Scan.Type)
140 assert.Equal(t, "success", rep.Scan.Status)
141 assert.Equal(t, "1.8.0", rep.Scan.Scanner.Version)
142 // The schema's timestamp pattern rejects a timezone suffix.
143 assert.Regexp(t, `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`, rep.Scan.StartTime)
144 assert.Regexp(t, `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`, rep.Scan.EndTime)
145}
147func TestConvertCleanScanMarshalsVulnerabilitiesAsArray(t *testing.T) {
148 t.Parallel()
150 // A clean scan is the usual outcome, and the schema types vulnerabilities
151 // as an array. A nil slice marshals to null, which fails validation, so
152 // assert on the encoded form: assert.Empty passes for nil as well.
153 rep, _, err := convertString(t, configMsg)
154 require.NoError(t, err)
155 require.NotNil(t, rep.Vulnerabilities)
157 out, err := json.Marshal(rep)
158 require.NoError(t, err)
160 var decoded map[string]json.RawMessage
161 require.NoError(t, json.Unmarshal(out, &decoded))
162 assert.JSONEq(t, `[]`, string(decoded["vulnerabilities"]))
163}
165func TestVulnIDIsStable(t *testing.T) {
166 t.Parallel()
168 first := vulnID("GO-2026-0001", "example.com/mod", "v1.0.0")
169 assert.Equal(t, first, vulnID("GO-2026-0001", "example.com/mod", "v1.0.0"))
170 assert.NotEqual(t, first, vulnID("GO-2026-0001", "example.com/mod", "v1.0.1"))
171}
Diagnostics6 git calls · 49 ms
CallStatusSizeTime049 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 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=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 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=4 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=136 git.response.cache=HIT git.response.duration_ms=3 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=603085b158b3e2723be7b292f69a8940e6abb23eOpen pack map
fetch2002.0 KiB · hit39 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=39 git.response.bytes=2005 git.response.cache=HIT git.response.duration_ms=39 git.fetch.wants=4038246605d411dfd0d38b8b2f9b481e5854a3b9 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=2005 git.pkt.payload.max=1978 git.sideband.band1.bytes=1978 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
- a5e25bb96d02224d
- Request
- b38e0dc8…
- 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