GitLabgitlab-org
cli
main
1//go:build !integration
3package policy
5import (
6 "encoding/json"
7 "errors"
8 "io"
9 "net/http"
10 "net/http/httptest"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "go.uber.org/mock/gomock"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
20 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
21)
23// restReq builds a Request with a project set, since the REST checker
24// evaluates against Request.ProjectID.
25func restReq(eco, name, version string) Request {
26 r := req(eco, name, version)
27 r.ProjectID = "group/proj"
28 return r
29}
31// restCheckerWithMock returns a checker backed by the client-go mock and the
32// mock recorder to program EvaluatePackage on.
33func restCheckerWithMock(t *testing.T) (Checker, *gitlabtesting.TestClient) {
34 t.Helper()
35 tc := gitlabtesting.NewTestClient(t)
36 return newRESTChecker(tc.Client), tc
37}
39func TestRESTAllowed(t *testing.T) {
40 t.Parallel()
41 c, tc := restCheckerWithMock(t)
42 tc.MockSecurityDependencyFirewall.EXPECT().
43 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
44 Return(&gitlab.PackageEvaluation{Outcome: gitlab.DependencyFirewallOutcomeAllowed}, nil, nil)
46 res, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
47 require.NoError(t, err)
48 assert.Equal(t, Result{}, res)
49}
51func TestRESTWarned(t *testing.T) {
52 t.Parallel()
53 c, tc := restCheckerWithMock(t)
54 tc.MockSecurityDependencyFirewall.EXPECT().
55 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
56 Return(&gitlab.PackageEvaluation{Outcome: gitlab.DependencyFirewallOutcomeWarned, Reason: new("deprecated")}, nil, nil)
58 res, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
59 require.NoError(t, err)
60 assert.Equal(t, verdict.Warning, res.Verdict)
61 assert.Equal(t, "deprecated", res.Reason)
62}
64func TestRESTBlocked(t *testing.T) {
65 t.Parallel()
66 c, tc := restCheckerWithMock(t)
67 tc.MockSecurityDependencyFirewall.EXPECT().
68 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
69 Return(&gitlab.PackageEvaluation{Outcome: gitlab.DependencyFirewallOutcomeBlocked, Reason: new("malware")}, nil, nil)
71 res, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
72 require.NoError(t, err)
73 assert.Equal(t, verdict.Blocked, res.Verdict)
74 assert.Equal(t, "malware", res.Reason)
75}
77func TestRESTUnknownOutcomeFailsClosed(t *testing.T) {
78 t.Parallel()
79 c, tc := restCheckerWithMock(t)
80 tc.MockSecurityDependencyFirewall.EXPECT().
81 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
82 Return(&gitlab.PackageEvaluation{Outcome: "maybe"}, nil, nil)
84 _, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
85 require.Error(t, err)
86}
88func TestRESTNoEvaluationFailsClosed(t *testing.T) {
89 t.Parallel()
90 c, tc := restCheckerWithMock(t)
91 tc.MockSecurityDependencyFirewall.EXPECT().
92 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
93 Return(nil, nil, nil)
95 _, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
96 require.Error(t, err)
97}
99func TestRESTUnsupportedEcosystemFailsClosed(t *testing.T) {
100 t.Parallel()
101 // No EvaluatePackage call is expected: an unsupported ecosystem is
102 // rejected before the request is sent.
103 c, _ := restCheckerWithMock(t)
105 _, err := c.Check(t.Context(), restReq("cocoapods", "Alamofire", "5.9.0"))
106 require.Error(t, err)
107}
109func TestEcosystemValueMapsSupported(t *testing.T) {
110 t.Parallel()
111 tests := []struct {
112 ecosystem string
113 want gitlab.DependencyFirewallEcosystemValue
114 }{
115 {"npm", gitlab.DependencyFirewallEcosystemNPM},
116 {"pypi", gitlab.DependencyFirewallEcosystemPyPI},
117 {"maven", gitlab.DependencyFirewallEcosystemMaven},
118 {"gem", gitlab.DependencyFirewallEcosystemGem},
119 {"nuget", gitlab.DependencyFirewallEcosystemNuGet},
120 {"cargo", gitlab.DependencyFirewallEcosystemCargo},
121 {"golang", gitlab.DependencyFirewallEcosystemGolang},
122 }
123 for _, tt := range tests {
124 t.Run(tt.ecosystem, func(t *testing.T) {
125 t.Parallel()
126 got, err := ecosystemValue(tt.ecosystem)
127 require.NoError(t, err)
128 assert.Equal(t, tt.want, got)
129 })
130 }
131}
133func TestEcosystemValueRejectsUnsupported(t *testing.T) {
134 t.Parallel()
135 _, err := ecosystemValue("cocoapods")
136 require.Error(t, err)
137 assert.Contains(t, err.Error(), "cocoapods")
138}
140// TestRESTStatusOutcomes covers how each API status maps to allow (via the
141// errFirewallNotEvaluating sentinel) or a fail-closed error. gitlab.HasStatusCode
142// reads the code from a *gitlab.ErrorResponse, so the mock returns one directly.
143func TestRESTStatusOutcomes(t *testing.T) {
144 t.Parallel()
145 tests := []struct {
146 name string
147 status int
148 wantSentinel bool
149 }{
150 {"404 not found fails open", http.StatusNotFound, true},
151 {"422 not enforced fails open", http.StatusUnprocessableEntity, true},
152 {"400 bad request fails closed", http.StatusBadRequest, false},
153 {"401 unauthorized fails closed", http.StatusUnauthorized, false},
154 {"403 forbidden fails closed", http.StatusForbidden, false},
155 {"429 too many requests fails closed", http.StatusTooManyRequests, false},
156 {"503 unavailable fails closed", http.StatusServiceUnavailable, false},
157 }
158 for _, tt := range tests {
159 t.Run(tt.name, func(t *testing.T) {
160 t.Parallel()
161 c, tc := restCheckerWithMock(t)
162 tc.MockSecurityDependencyFirewall.EXPECT().
163 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
164 Return(nil, nil, &gitlab.ErrorResponse{StatusCode: tt.status})
166 res, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
167 if tt.wantSentinel {
168 require.ErrorIs(t, err, errFirewallNotEvaluating)
169 assert.Equal(t, Result{}, res)
170 return
171 }
172 require.Error(t, err)
173 assert.NotErrorIs(t, err, errFirewallNotEvaluating)
174 })
175 }
176}
178func TestRESTTransportErrorFailsClosed(t *testing.T) {
179 t.Parallel()
180 c, tc := restCheckerWithMock(t)
181 tc.MockSecurityDependencyFirewall.EXPECT().
182 EvaluatePackage("group/proj", gomock.Any(), gomock.Any()).
183 Return(nil, nil, errors.New("dial tcp: connection refused"))
185 _, err := c.Check(t.Context(), restReq("npm", "left-pad", "1.3.0"))
186 require.Error(t, err)
187 assert.NotErrorIs(t, err, errFirewallNotEvaluating)
188}
190// TestRESTSendsCorrectRequest is the one test that exercises the real HTTP
191// wire format (method, path, JSON body) rather than the mock, to catch a
192// serialization or path-escaping regression the mock cannot see.
193func TestRESTSendsCorrectRequest(t *testing.T) {
194 t.Parallel()
195 type evaluateBody struct {
196 Ecosystem string `json:"ecosystem"`
197 Name string `json:"name"`
198 Version string `json:"version"`
199 }
200 var gotMethod, gotPath string
201 var gotBody evaluateBody
202 var decodeErr error
203 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
204 gotMethod = r.Method
205 gotPath = r.URL.EscapedPath()
206 decodeErr = json.NewDecoder(r.Body).Decode(&gotBody)
207 w.WriteHeader(http.StatusOK)
208 _, _ = io.WriteString(w, `{"outcome":"allowed","reason":null}`)
209 }))
210 t.Cleanup(server.Close)
211 client, err := gitlab.NewClient("", gitlab.WithBaseURL(server.URL+"/api/v4"))
212 require.NoError(t, err)
213 c := newRESTChecker(client)
215 _, err = c.Check(t.Context(), restReq("maven", "org.slf4j:slf4j-api", "2.0.13"))
216 require.NoError(t, err)
217 require.NoError(t, decodeErr)
219 assert.Equal(t, http.MethodPost, gotMethod)
220 assert.Equal(t, "/api/v4/projects/group%2Fproj/dependency_firewall/evaluate", gotPath)
221 assert.Equal(t, evaluateBody{Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "2.0.13"}, gotBody)
222}
Diagnostics8 git calls · 39 ms
CallStatusSizeTime039 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 · 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 · 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=32926 git.response.cache=HIT git.response.duration_ms=4 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
fetchtree:0 · deepen 1200706 B · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=706 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=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=706 git.pkt.payload.max=606 git.sideband.band1.bytes=606 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=606 git.pack.trailer=5b8940d8a309b9f86f08953120610ba63124325eOpen pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=6 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:0200361 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=361 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 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=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200332 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=332 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=cb813f0d675e9dca14f167e8c7788d61eb19627f 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=332 git.pkt.payload.max=305 git.sideband.band1.bytes=305 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=305 git.pack.trailer=5e210c1831ecf78390a6fe6e1784efd898336da0Open pack map
fetch2002.1 KiB · hit25 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=2172 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=9fffe468e3d7519d3ebdfcdee39e37c63f65caa3 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=2172 git.pkt.payload.max=2145 git.sideband.band1.bytes=2145 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
- a5e238f8bc79e9de
- Request
- d7728498…
- 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