GitLabgitlab-org

cli

main
ply7 git calls504 ms102 KiB4 cachedprotocol v2
660 lines26 KiBRaw
1//go:build !integration
3package proxy
5import (
6 "context"
7 "errors"
8 "fmt"
9 "net/http"
10 "net/http/httptest"
11 "slices"
12 "strings"
13 "sync"
14 "sync/atomic"
15 "testing"
16 "time"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
21 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
22 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
25// mirrorHost is the host used to simulate a mirror or proxy repository in
26// these tests, via mvn's host parameter (defined in mavenmatch_test.go).
27const mirrorHost = "https://mirror.example.com"
29// denyChecker blocks exactly one coordinate name and allows everything else,
30// standing in for a policy rule written against the real package.
31type denyChecker struct {
32 name string
33 mu sync.Mutex
34 seen []string
35 ops []policy.Operation
38func (d *denyChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
39 d.mu.Lock()
40 defer d.mu.Unlock()
41 d.seen = append(d.seen, r.Coordinate.Name)
42 d.ops = append(d.ops, r.Operation)
43 if r.Coordinate.Name == d.name {
44 return policy.Result{Verdict: verdict.Blocked, Reason: "denied by test policy"}, nil
45 }
46 return policy.Result{}, nil
49// verdictChecker returns a canned verdict per coordinate name.
50type verdictChecker map[string]policy.Result
52func (v verdictChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
53 return v[r.Coordinate.Name], nil
56// errChecker fails the check for one coordinate and allows the rest, standing
57// in for a transient API failure part-way through the candidates.
58type errChecker struct{ on string }
60func (e errChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
61 if r.Coordinate.Name == e.on {
62 return policy.Result{}, errors.New("boom")
63 }
64 return policy.Result{}, nil
67// deadlineRecorder allows everything and records the deadline each check ran
68// under, so a test can tell one shared budget from one budget per candidate.
69type deadlineRecorder struct {
70 mu sync.Mutex
71 deadlines []time.Time
74func (d *deadlineRecorder) Check(ctx context.Context, _ policy.Request) (policy.Result, error) {
75 deadline, _ := ctx.Deadline()
76 d.mu.Lock()
77 defer d.mu.Unlock()
78 d.deadlines = append(d.deadlines, deadline)
79 return policy.Result{}, nil
82// panicChecker panics on one coordinate and allows the rest.
83type panicChecker struct{ on string }
85func (c panicChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
86 if r.Coordinate.Name == c.on {
87 panic("checker bug")
88 }
89 return policy.Result{}, nil
92// lateErrChecker fails the check for slow after a delay and blocks fast at
93// once, so a later candidate's verdict arrives before an earlier one's.
94type lateErrChecker struct{ slow, fast string }
96func (l lateErrChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
97 switch r.Coordinate.Name {
98 case l.slow:
99 time.Sleep(50 * time.Millisecond)
100 return policy.Result{}, errors.New("boom")
101 case l.fast:
102 return policy.Result{Verdict: verdict.Blocked, Reason: "denied"}, nil
103 }
104 return policy.Result{}, nil
107// overlapRecorder allows everything and records the most checks in flight at
108// once. Each check stays in flight long enough for checks run together to overlap.
109type overlapRecorder struct {
110 mu sync.Mutex
111 calls, inFlight, most int
114func (o *overlapRecorder) Check(context.Context, policy.Request) (policy.Result, error) {
115 o.mu.Lock()
116 o.calls++
117 o.inFlight++
118 o.most = max(o.most, o.inFlight)
119 o.mu.Unlock()
120 time.Sleep(50 * time.Millisecond)
121 o.mu.Lock()
122 o.inFlight--
123 o.mu.Unlock()
124 return policy.Result{}, nil
127// seenErrChecker fails the check for on, allows the rest, and records every
128// coordinate it checks.
129type seenErrChecker struct {
130 on string
131 mu sync.Mutex
132 seen []string
135func (e *seenErrChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
136 e.mu.Lock()
137 defer e.mu.Unlock()
138 e.seen = append(e.seen, r.Coordinate.Name)
139 if r.Coordinate.Name == e.on {
140 return policy.Result{}, errors.New("boom")
141 }
142 return policy.Result{}, nil
145// batchErrChecker allows the primary, fails the check for on and blocks every
146// other alternate, each once a full batch of alternates is in flight.
147type batchErrChecker struct {
148 primary, on string
149 calls atomic.Int64
150 alternates atomic.Int64
151 batchIn chan struct{}
154func (b *batchErrChecker) Check(_ context.Context, r policy.Request) (policy.Result, error) {
155 b.calls.Add(1)
156 if r.Coordinate.Name == b.primary {
157 return policy.Result{}, nil
158 }
159 if b.alternates.Add(1) == maxConcurrentChecks {
160 close(b.batchIn)
161 }
162 <-b.batchIn
163 if r.Coordinate.Name == b.on {
164 return policy.Result{}, errors.New("boom")
165 }
166 return policy.Result{Verdict: verdict.Blocked, Reason: "denied"}, nil
169func candidateNames(m Match) []string {
170 names := []string{m.Coordinate.Name}
171 for _, c := range m.Alternates {
172 names = append(names, c.Name)
173 }
174 return names
177func TestMavenMirrorCandidates(t *testing.T) {
178 t.Parallel()
180 t.Run("when the path has no mirror prefix", func(t *testing.T) {
181 t.Parallel()
182 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/maven2/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar"))
184 require.True(t, m.Matched)
185 assert.Equal(t, "org.slf4j:slf4j-api", m.Coordinate.Name,
186 "the unprefixed parse must stay the primary coordinate")
187 assert.Equal(t, "2.0.9", m.Coordinate.Version)
188 })
190 t.Run("when a mirror adds path segments", func(t *testing.T) {
191 t.Parallel()
192 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
194 require.True(t, m.Matched)
195 assert.Equal(t, "repository.maven-central.org.slf4j:slf4j-api", m.Coordinate.Name)
196 assert.Equal(t, []policy.Coordinate{
197 {Ecosystem: "maven", Name: "slf4j:slf4j-api", Version: "2.0.9"},
198 {Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "2.0.9"},
199 {Ecosystem: "maven", Name: "maven-central.org.slf4j:slf4j-api", Version: "2.0.9"},
200 }, m.Alternates, "the real coordinate must be among the candidates whatever the mirror prefixes it with")
201 })
203 t.Run("when the mirror is the GitLab virtual registry", func(t *testing.T) {
204 t.Parallel()
205 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/api/v4/virtual_registries/packages/maven/7/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
207 require.True(t, m.Matched)
208 assert.Contains(t, candidateNames(m), "org.slf4j:slf4j-api")
209 })
211 t.Run("when the group has more segments than the cap", func(t *testing.T) {
212 t.Parallel()
213 // Uncapped, 25 segments give 24 readings; the cap keeps the ten shortest
214 // and the ten that drop fewest leading segments.
215 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/pkg/1.0/pkg-1.0.jar", mirrorHost))
217 require.True(t, m.Matched)
218 assert.Len(t, m.Alternates, 2*mavenMaxGroupDepth,
219 "a pathologically deep group must not generate unbounded policy calls")
220 assert.Equal(t, "y:pkg", m.Alternates[0].Name,
221 "the shortest reading -- the one a real rule is most likely written against -- must survive the cap")
222 assert.Contains(t, candidateNames(m), "b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y:pkg",
223 "dropping one leading segment must survive the cap however long the reading is")
224 })
226 t.Run("when a long group sits behind a mirror prefix", func(t *testing.T) {
227 t.Parallel()
228 // The publisher picks the groupId's depth, so an 11-segment group must
229 // not slip past a rule written for it.
230 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/io/github/evil/a/b/c/d/e/f/g/h/payload/1.0/payload-1.0.jar", mirrorHost))
232 require.True(t, m.Matched)
233 assert.Contains(t, candidateNames(m), "io.github.evil.a.b.c.d.e.f.g.h:payload")
234 })
236 t.Run("when the path is the exact Central layout", func(t *testing.T) {
237 t.Parallel()
238 // Under a leading maven2/ the coordinate is exact, and dropping segments
239 // would only collide with relocated groups like dom4j:dom4j.
240 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/maven2/org/dom4j/dom4j/2.1.4/dom4j-2.1.4.jar"))
242 require.True(t, m.Matched)
243 assert.Equal(t, "org.dom4j:dom4j", m.Coordinate.Name)
244 assert.Empty(t, m.Alternates)
245 })
247 t.Run("when the path has a doubled slash", func(t *testing.T) {
248 t.Parallel()
249 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository//org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
251 require.True(t, m.Matched)
252 assert.Equal(t, []string{
253 "repository..org.slf4j:slf4j-api",
254 "slf4j:slf4j-api",
255 "org.slf4j:slf4j-api",
256 }, candidateNames(m), "readings with an empty group segment are not coordinates and cost a wasted call")
257 })
259 t.Run("the candidate list for a mirrored path", func(t *testing.T) {
260 t.Parallel()
261 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/central/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.jar", mirrorHost))
263 require.True(t, m.Matched)
264 // Pinned so the cost of the fan-out cannot change without showing up
265 // in a diff: one policy call per entry when the package is allowed.
266 assert.Equal(t, []string{
267 "repository.central.org.apache.maven.plugins:maven-compiler-plugin",
268 "plugins:maven-compiler-plugin",
269 "maven.plugins:maven-compiler-plugin",
270 "apache.maven.plugins:maven-compiler-plugin",
271 "org.apache.maven.plugins:maven-compiler-plugin",
272 "central.org.apache.maven.plugins:maven-compiler-plugin",
273 }, candidateNames(m))
274 })
276 t.Run("when the mirror is a deep GitLab virtual registry layout, for a plugin artifact", func(t *testing.T) {
277 t.Parallel()
278 // A plugin resolved via <pluginRepositories> must be covered the
279 // same as a dependency via <repositories>: the matcher can't tell
280 // which section caused the request.
281 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/api/v4/virtual_registries/packages/maven/42/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.jar", mirrorHost))
283 require.True(t, m.Matched)
284 assert.Contains(t, candidateNames(m), "org.apache.maven.plugins:maven-compiler-plugin")
285 })
287 t.Run("when the mirror is a shallow corporate Nexus-style layout", func(t *testing.T) {
288 t.Parallel()
289 // A 2-segment mirror path, the shape a Nexus proxy typically uses.
290 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/public/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
292 require.True(t, m.Matched)
293 assert.Contains(t, candidateNames(m), "org.slf4j:slf4j-api",
294 "must cover a shallow corporate mirror path, not only the 'maven-central' name used in other fixtures")
295 })
298func TestMavenMirrorPolicyMatching(t *testing.T) {
299 t.Parallel()
301 t.Run("when a rule names the real package behind a mirror", func(t *testing.T) {
302 t.Parallel()
303 checker := &denyChecker{name: "org.slf4j:slf4j-api"}
304 p, err := New(MavenMatcher{}, checker, "group/proj")
305 require.NoError(t, err)
307 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
308 res, coord := p.checkPolicy(t.Context(), m)
310 assert.True(t, res.Blocked(), "a mirror prefix must not hide a package from its rule")
311 assert.Equal(t, "org.slf4j:slf4j-api", coord.Name,
312 "the blocked entry must name the real package, not the prefixed form")
313 })
315 t.Run("when no rule matches any candidate", func(t *testing.T) {
316 t.Parallel()
317 checker := &denyChecker{name: "com.example:nothing"}
318 p, err := New(MavenMatcher{}, checker, "group/proj")
319 require.NoError(t, err)
321 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
322 res, _ := p.checkPolicy(t.Context(), m)
324 assert.False(t, res.Blocked())
325 })
327 t.Run("when an earlier candidate warns and a later one is blocked", func(t *testing.T) {
328 t.Parallel()
329 checker := verdictChecker{
330 "repository.maven-central.org.slf4j:slf4j-api": policy.Result{Verdict: verdict.Warning, Reason: "warned"},
331 "org.slf4j:slf4j-api": policy.Result{Verdict: verdict.Blocked, Reason: "denied"},
332 }
333 p, err := New(MavenMatcher{}, checker, "group/proj")
334 require.NoError(t, err)
336 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
337 res, coord := p.checkPolicy(t.Context(), m)
339 assert.True(t, res.Blocked(), "a warn on one reading must not mask a block on another")
340 assert.Equal(t, "org.slf4j:slf4j-api", coord.Name)
341 })
343 t.Run("when a candidate check errors", func(t *testing.T) {
344 t.Parallel()
345 checker := errChecker{on: "org.slf4j:slf4j-api"}
346 p, err := New(MavenMatcher{}, checker, "group/proj")
347 require.NoError(t, err)
349 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
350 res, coord := p.checkPolicy(t.Context(), m)
352 assert.True(t, res.Blocked(), "an undecidable candidate must fail closed")
353 assert.Equal(t, m.Coordinate.Name, coord.Name,
354 "the report must name the request, not the invented candidate that errored")
355 })
357 t.Run("when there is no mirror prefix", func(t *testing.T) {
358 t.Parallel()
359 checker := &denyChecker{name: "org.slf4j:slf4j-api"}
360 p, err := New(MavenMatcher{}, checker, "group/proj")
361 require.NoError(t, err)
363 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/maven2/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar"))
364 res, _ := p.checkPolicy(t.Context(), m)
366 require.True(t, res.Blocked())
367 assert.Equal(t, []string{"org.slf4j:slf4j-api"}, checker.seen,
368 "a blocked primary coordinate decides immediately, costing one call")
369 })
371 t.Run("when an unmirrored package is allowed", func(t *testing.T) {
372 t.Parallel()
373 checker := &denyChecker{name: "com.example:nothing"}
374 p, err := New(MavenMatcher{}, checker, "group/proj")
375 require.NoError(t, err)
377 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.jar"))
378 res, _ := p.checkPolicy(t.Context(), m)
380 assert.False(t, res.Blocked())
381 // The exact Central layout needs no candidates, so the common case
382 // costs one call, as it did before enumeration existed.
383 assert.Equal(t, []string{"org.apache.maven.plugins:maven-compiler-plugin"}, checker.seen)
384 })
386 t.Run("when a rule names a relocated single-segment group on Central", func(t *testing.T) {
387 t.Parallel()
388 // dom4j:dom4j was relocated to org.dom4j:dom4j; a rule on the old name
389 // must not reach the new one from Central, which main allows.
390 checker := &denyChecker{name: "dom4j:dom4j"}
391 p, err := New(MavenMatcher{}, checker, "group/proj")
392 require.NoError(t, err)
394 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/maven2/org/dom4j/dom4j/2.1.4/dom4j-2.1.4.jar"))
395 res, _ := p.checkPolicy(t.Context(), m)
397 assert.False(t, res.Blocked())
398 assert.Equal(t, []string{"org.dom4j:dom4j"}, checker.seen)
399 })
401 t.Run("when a check fails behind the caching checker", func(t *testing.T) {
402 t.Parallel()
403 // Production's CachingChecker turns an error into a block, which must
404 // still name the request rather than the invented candidate.
405 checker := policy.NewCachingChecker(errChecker{on: "slf4j:slf4j-api"}, nil)
406 p, err := New(MavenMatcher{}, checker, "group/proj")
407 require.NoError(t, err)
409 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
410 res, coord := p.checkPolicy(t.Context(), m)
412 assert.True(t, res.Blocked(), "an undecidable candidate must fail closed")
413 assert.Equal(t, m.Coordinate.Name, coord.Name)
414 })
416 t.Run("when only alternates warn", func(t *testing.T) {
417 t.Parallel()
418 checker := verdictChecker{
419 "slf4j:slf4j-api": {Verdict: verdict.Warning, Reason: "first"},
420 "org.slf4j:slf4j-api": {Verdict: verdict.Warning, Reason: "second"},
421 }
422 p, err := New(MavenMatcher{}, checker, "group/proj")
423 require.NoError(t, err)
425 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
426 res, coord := p.checkPolicy(t.Context(), m)
428 assert.Equal(t, verdict.Warning, res.Verdict)
429 assert.Equal(t, "slf4j:slf4j-api", coord.Name, "the first warning in candidate order decides")
430 assert.Equal(t, "first", res.Reason)
431 })
433 t.Run("when candidates share one deadline", func(t *testing.T) {
434 t.Parallel()
435 // One policy.CheckTimeout covers the whole candidate list, so a slow
436 // backend bounds a request's total wait rather than one per candidate.
437 checker := &deadlineRecorder{}
438 p, err := New(MavenMatcher{}, checker, "group/proj")
439 require.NoError(t, err)
441 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
442 _, _ = p.checkPolicy(t.Context(), m)
444 require.Greater(t, len(checker.deadlines), 1)
445 require.False(t, checker.deadlines[0].IsZero(), "the candidates must run under a deadline")
446 assert.Equal(t, slices.Repeat([]time.Time{checker.deadlines[0]}, len(checker.deadlines)), checker.deadlines,
447 "every candidate must share one deadline")
448 })
450 t.Run("when there are more candidates than run at once", func(t *testing.T) {
451 t.Parallel()
452 // Sequential checks under the shared deadline turn a slow backend into
453 // a spurious block; a few at a time keeps the endpoint's load bounded.
454 checker := &overlapRecorder{}
455 p, err := New(MavenMatcher{}, checker, "group/proj")
456 require.NoError(t, err)
458 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/nexus/content/repositories/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
459 require.Greater(t, 1+len(m.Alternates), maxConcurrentChecks)
460 res, _ := p.checkPolicy(t.Context(), m)
462 assert.False(t, res.Blocked())
463 assert.Equal(t, 1+len(m.Alternates), checker.calls, "every candidate is checked")
464 assert.GreaterOrEqual(t, checker.most, 2, "candidates must not wait for each other")
465 assert.LessOrEqual(t, checker.most, maxConcurrentChecks)
466 })
468 t.Run("when the primary coordinate is blocked", func(t *testing.T) {
469 t.Parallel()
470 checker := &denyChecker{name: "repository.maven-central.org.slf4j:slf4j-api"}
471 p, err := New(MavenMatcher{}, checker, "group/proj")
472 require.NoError(t, err)
474 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
475 require.NotEmpty(t, m.Alternates)
476 res, _ := p.checkPolicy(t.Context(), m)
478 require.True(t, res.Blocked())
479 assert.Equal(t, []string{"repository.maven-central.org.slf4j:slf4j-api"}, checker.seen,
480 "a decided primary costs one call, so a failing backend isn't sent every alternate")
481 })
483 t.Run("when the primary coordinate's check fails", func(t *testing.T) {
484 t.Parallel()
485 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
486 require.NotEmpty(t, m.Alternates)
487 checker := &seenErrChecker{on: m.Coordinate.Name}
488 p, err := New(MavenMatcher{}, checker, "group/proj")
489 require.NoError(t, err)
491 res, coord := p.checkPolicy(t.Context(), m)
493 assert.True(t, res.CheckFailed)
494 assert.Equal(t, m.Coordinate.Name, coord.Name)
495 assert.Equal(t, []string{m.Coordinate.Name}, checker.seen, "a failing primary costs one call")
496 })
498 t.Run("when the first alternate's check fails", func(t *testing.T) {
499 t.Parallel()
500 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/nexus/content/repositories/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
501 require.Greater(t, len(m.Alternates), maxConcurrentChecks)
502 checker := &batchErrChecker{primary: m.Coordinate.Name, on: m.Alternates[0].Name, batchIn: make(chan struct{})}
503 p, err := New(MavenMatcher{}, checker, "group/proj")
504 require.NoError(t, err)
506 res, coord := p.checkPolicy(t.Context(), m)
508 assert.True(t, res.CheckFailed, "the first alternate's failure decides, not the later rule blocks")
509 assert.Equal(t, m.Coordinate.Name, coord.Name)
510 assert.Equal(t, int64(1+maxConcurrentChecks), checker.calls.Load(),
511 "the loop starts no alternate once it sees one has decided the verdict")
512 })
514 assertPanicBlocks := func(t *testing.T, on string) {
515 t.Helper()
516 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
517 p, err := New(MavenMatcher{}, panicChecker{on: on}, "group/proj")
518 require.NoError(t, err)
520 res, coord := p.checkPolicy(t.Context(), m)
522 assert.True(t, res.CheckFailed, "a panicking check fails closed instead of crashing glab")
523 assert.True(t, res.Internal, "a glab panic is told apart from a policy service failure")
524 assert.Contains(t, res.Reason, "glab hit an internal error while checking "+m.Coordinate.Name+"@"+m.Coordinate.Version)
525 assert.Equal(t, m.Coordinate.Name, coord.Name)
526 assert.NotContains(t, res.Reason, "checker bug", "the panic value can hold anything, so it stays out of the block reason")
527 }
529 t.Run("when the primary coordinate's check panics", func(t *testing.T) {
530 t.Parallel()
531 assertPanicBlocks(t, "repository.maven-central.org.slf4j:slf4j-api")
532 })
534 t.Run("when an alternate's check panics", func(t *testing.T) {
535 t.Parallel()
536 assertPanicBlocks(t, "org.slf4j:slf4j-api")
537 })
539 t.Run("when an earlier candidate's check fails after a later one is blocked", func(t *testing.T) {
540 t.Parallel()
541 checker := lateErrChecker{slow: "slf4j:slf4j-api", fast: "org.slf4j:slf4j-api"}
542 p, err := New(MavenMatcher{}, checker, "group/proj")
543 require.NoError(t, err)
545 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
546 res, coord := p.checkPolicy(t.Context(), m)
548 assert.True(t, res.CheckFailed, "candidate order decides, not which check returns first")
549 assert.Equal(t, m.Coordinate.Name, coord.Name)
550 })
552 t.Run("when the request is an upload to a deploy repository under a path prefix", func(t *testing.T) {
553 t.Parallel()
554 // Maven applies mirrors only to resolution, so a deploy repository
555 // whose URL has a prefix is what puts an upload on a prefixed path.
556 checker := &denyChecker{name: "org.slf4j:slf4j-api"}
557 p, err := New(MavenMatcher{}, checker, "group/proj")
558 require.NoError(t, err)
560 m := MavenMatcher{}.Match(mvn(http.MethodPut, "/repository/releases/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
561 require.True(t, m.Matched)
563 res, coord := p.checkPolicy(t.Context(), m)
565 assert.True(t, res.Blocked(), "an upload under a prefix must be covered the same way as a download")
566 assert.Equal(t, "org.slf4j:slf4j-api", coord.Name)
567 require.NotEmpty(t, checker.ops)
568 assert.Equal(t, slices.Repeat([]policy.Operation{policy.Upload}, len(checker.ops)), checker.ops,
569 "every candidate must be checked as an upload")
570 })
573// TestMavenMirrorThroughTunnel drives a request through the proxy, since only
574// that path records the verdict entry naming the real package.
575func TestMavenMirrorThroughTunnel(t *testing.T) {
576 t.Parallel()
577 const path = "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar"
579 get := func(t *testing.T, checker policy.Checker) (int, *Proxy) {
580 t.Helper()
581 upstream := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
582 t.Cleanup(upstream.Close)
583 p := startProxyWithMatcher(t, upstream, MavenMatcher{}, checker)
584 resp, err := newClient(t, p).Get(upstream.URL + path)
585 require.NoError(t, err)
586 defer resp.Body.Close()
587 return resp.StatusCode, p
588 }
590 t.Run("when a rule names the real package", func(t *testing.T) {
591 t.Parallel()
592 status, p := get(t, policy.NewCachingChecker(&denyChecker{name: "org.slf4j:slf4j-api"}, nil))
594 assert.Equal(t, http.StatusForbidden, status)
595 verdicts := p.Verdicts()
596 require.Len(t, verdicts, 1)
597 assert.Equal(t, "org.slf4j:slf4j-api", verdicts[0].Package)
598 })
600 t.Run("when a warn rule names the real package", func(t *testing.T) {
601 t.Parallel()
602 status, p := get(t, policy.NewCachingChecker(verdictChecker{"org.slf4j:slf4j-api": {Verdict: verdict.Warning, Reason: "warned"}}, nil))
604 assert.Equal(t, http.StatusOK, status)
605 verdicts := p.Verdicts()
606 require.Len(t, verdicts, 1)
607 assert.Equal(t, verdict.Warning, verdicts[0].Verdict)
608 assert.Equal(t, "org.slf4j:slf4j-api", verdicts[0].Package)
609 })
611 t.Run("when a check fails behind the caching checker", func(t *testing.T) {
612 t.Parallel()
613 status, p := get(t, policy.NewCachingChecker(errChecker{on: "slf4j:slf4j-api"}, nil))
615 assert.Equal(t, http.StatusForbidden, status)
616 verdicts := p.Verdicts()
617 require.Len(t, verdicts, 1)
618 assert.Equal(t, "repository.maven-central.org.slf4j:slf4j-api", verdicts[0].Package,
619 "a failed check is reported under the request's own coordinate, not an invented candidate")
620 })
623func TestPanickingCheckBlockResponseNamesTheInternalError(t *testing.T) {
624 t.Parallel()
625 p, err := New(MavenMatcher{}, panicChecker{on: "org.slf4j:slf4j-api"}, "group/proj")
626 require.NoError(t, err)
627 r := mvn(http.MethodGet, "/maven2/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar")
629 _, reason, checkFailed, ok := p.admit(t.Context(), r)
631 require.False(t, ok)
632 assert.True(t, checkFailed)
633 assert.Equal(t, "Blocked: glab internal error while checking org.slf4j:slf4j-api@2.0.9", reason)
634 assert.Contains(t, blockResponse("maven", reason, checkFailed), "HTTP/1.1 403 Blocked: glab internal error while checking org.slf4j:slf4j-api@2.0.9\r\n")
637func TestPanickingAlternateNamesTheRequest(t *testing.T) {
638 t.Parallel()
639 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/repository/maven-central/org/slf4j/slf4j-api/2.0.9/slf4j-api-2.0.9.jar", mirrorHost))
640 require.NotEmpty(t, m.Alternates)
641 var warned strings.Builder
642 var warnedMu sync.Mutex
643 logf := func(format string, a ...any) {
644 warnedMu.Lock()
645 defer warnedMu.Unlock()
646 fmt.Fprintf(&warned, format, a...)
647 }
648 p, err := New(MavenMatcher{}, policy.NewCachingChecker(panicChecker{on: "org.slf4j:slf4j-api"}, logf), "group/proj")
649 require.NoError(t, err)
651 res, coord := p.checkPolicy(t.Context(), m)
653 assert.True(t, res.Internal)
654 assert.Equal(t, m.Coordinate.Name, coord.Name)
655 assert.Contains(t, res.Reason, "while checking "+m.Coordinate.Name+"@"+m.Coordinate.Version, "the reason names the same package as the CI log row")
656 warnedMu.Lock()
657 defer warnedMu.Unlock()
658 assert.Equal(t, "warning: glab hit an internal error while checking org.slf4j:slf4j-api@2.0.9. Rerun to retry, and report this with the output of GLAB_DEBUG=1.\n", warned.String(),
659 "the warning names the alternate it checked and leaves the verdict to the 403 and CI log")
Diagnostics7 git calls · 504 ms
CallStatusSizeTime0504 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 KiB190 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=183
git.response.bytes=59849
git.response.cache=EXPIRED
git.response.duration_ms=190
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 · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=5
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 · 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=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.0 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=1051
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=tree:0
git.fetch.wants=7a2a90605315ba5b37f63c08726e6b2caa26e48d
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=1051
git.pkt.payload.max=1024
git.sideband.band1.bytes=1024
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1024
git.pack.trailer=7503ef36a75d3595b473035c69e639fd0ce41598
Open pack map
fetch2005.5 KiB300 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=300
git.response.bytes=5651
git.response.cache=MISS
git.response.duration_ms=300
git.fetch.wants=1c8eab02e297472c1d636a88980fb659fbbf27d8
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=5651
git.pkt.payload.max=5624
git.sideband.band1.bytes=5624
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
a5e1825efaa93014
Request
d247e2eb…
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