GitLabgitlab-org

cli

main
ply7 git calls348 ms97 KiB4 cachedprotocol v2
126 lines4.2 KiBRaw
1//go:build !integration
3package gomodproxy
5import (
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10 "unicode"
11 "unicode/utf8"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
17func TestWriteBlockedResponseShape(t *testing.T) {
18 t.Parallel()
19 rec := httptest.NewRecorder()
20 writeBlocked(rec, "github.com/pkg/errors", "v0.9.1", "known vulnerability CVE-2024-0001")
22 res := rec.Result()
23 defer res.Body.Close()
25 assert.Equal(t, http.StatusForbidden, res.StatusCode)
26 assert.Equal(t, "text/plain; charset=utf-8", res.Header.Get("Content-Type"))
27 assert.Equal(t, blockedValue, res.Header.Get("X-Gitlab-Dependency-Firewall"))
28 assert.Equal(t, "no-store", res.Header.Get("Cache-Control"))
30 body := rec.Body.String()
31 assert.Contains(t, body, "github.com/pkg/errors")
32 assert.Contains(t, body, "v0.9.1")
33 assert.Contains(t, body, "known vulnerability CVE-2024-0001")
36func TestBlockBodyHonorsGoErrorDetailLimits(t *testing.T) {
37 t.Parallel()
38 hostile := "\x1b[31mred\x1b[0m\n\nblank line above\x00control\u200bzero\u202eright\ufeffbom\u00adsoft\n" + strings.Repeat("long ", 500)
39 body := blockBody("example.com/m", "v1.0.0", hostile)
41 assert.True(t, utf8.ValidString(body), "body must be valid UTF-8")
42 assert.LessOrEqual(t, len(body), maxDetailBytes)
43 assert.LessOrEqual(t, strings.Count(body, "\n")+1, maxDetailLines)
45 for line := range strings.SplitSeq(body, "\n") {
46 assert.NotEmpty(t, strings.TrimSpace(line), "a blank line truncates the detail")
47 }
48 for _, r := range body {
49 if r == '\n' {
50 continue
51 }
52 assert.True(t, unicode.IsGraphic(r) || unicode.IsSpace(r), "go voids the whole detail on rune %U", r)
53 }
54 assert.NotContains(t, body, "\x1b")
57// goErrorDetail renders body the way cmd/go shows a proxy error body to the
58// user, copied from the loop in cmd/go/internal/web/api.go (go1.26): lines are
59// joined with "\n\t", and a line that would push the detail past
60// 8*81 bytes is dropped whole, not cut.
61func goErrorDetail(body string) string {
62 const maxLines, maxBytes = 8, 8 * 81
63 var detail strings.Builder
64 for i, line := range strings.Split(body, "\n") {
65 if strings.TrimSpace(line) == "" {
66 break
67 }
68 if i > 0 {
69 detail.WriteString("\n\t")
70 }
71 if i >= maxLines {
72 detail.WriteString("[Truncated: too many lines.]")
73 break
74 }
75 if detail.Len()+len(line) > maxBytes {
76 detail.WriteString("[Truncated: too long.]")
77 break
78 }
79 detail.WriteString(line)
80 }
81 return detail.String()
84func TestLongReasonSurvivesGoRendering(t *testing.T) {
85 t.Parallel()
86 for _, n := range []int{560, 565, 566, 567, 600, 11_309} {
87 reason := "policy check failed: " + strings.Repeat("x", n)
88 shown := goErrorDetail(blockBody("github.com/BurntSushi/toml", "v1.6.0", reason))
89 assert.NotContains(t, shown, "[Truncated", "reason of %d bytes", n)
90 assert.Contains(t, shown, "reason: policy check failed: xxx", "reason of %d bytes", n)
91 }
93 // A cut through a multi-byte rune would void the whole detail.
94 shown := goErrorDetail(blockBody("example.com/m", "v1.0.0", strings.Repeat("é", 400)))
95 assert.True(t, utf8.ValidString(shown))
96 assert.NotContains(t, shown, "[Truncated")
97 assert.True(t, strings.HasSuffix(shown, "é…"), "a cut reason ends with an ellipsis")
100func TestShortReasonIsShownVerbatim(t *testing.T) {
101 t.Parallel()
102 shown := goErrorDetail(blockBody("example.com/m", "v1.0.0", "Package 'm' violates 'Deny high volume' policy"))
103 assert.Equal(t, "blocked by the GitLab Dependency Firewall: example.com/m@v1.0.0\n\treason: Package 'm' violates 'Deny high volume' policy", shown)
106func TestBlockBodyOmitsEmptyReason(t *testing.T) {
107 t.Parallel()
108 body := blockBody("example.com/m", "v1.0.0", "")
109 assert.Equal(t, "blocked by the GitLab Dependency Firewall: example.com/m@v1.0.0", body)
110 assert.NotContains(t, body, "reason:")
113func TestSanitizeReasonCollapsesWhitespace(t *testing.T) {
114 t.Parallel()
115 assert.Equal(t, "a b c", sanitizeReason("a\n\nb\tc"))
116 assert.Empty(t, sanitizeReason(" \n\t "))
119func TestWriteBlockedContentLengthMatchesBody(t *testing.T) {
120 t.Parallel()
121 rec := httptest.NewRecorder()
122 writeBlocked(rec, "example.com/m", "v1.0.0", "nope")
123 res := rec.Result()
124 defer res.Body.Close()
125 require.Equal(t, rec.Body.Len(), int(res.ContentLength))
Diagnostics7 git calls · 348 ms
CallStatusSizeTime0348 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 KiB155 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=144
git.response.bytes=59849
git.response.cache=EXPIRED
git.response.duration_ms=155
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 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=3
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: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=3
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
fetchtree:0200361 B · 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=361
git.response.cache=HIT
git.response.duration_ms=4
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:0200379 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=379
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=21702eb6df99111b4d7a039e655c720d4209d5b6
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=379
git.pkt.payload.max=352
git.sideband.band1.bytes=352
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=352
git.pack.trailer=7d54607262a7ed73d2686c4df38d0272960c21fc
Open pack map
fetch2001.7 KiB183 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=183
git.response.bytes=1697
git.response.cache=MISS
git.response.duration_ms=183
git.fetch.wants=99fdf8de1ae4813ce80176abd51ec1135ad8b420
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=1697
git.pkt.payload.max=1670
git.sideband.band1.bytes=1670
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
a5e1acf12ac12c38
Request
11bd9330…
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