GitLabgitlab-org

cli

main
ply7 git calls227 ms98 KiB5 cachedprotocol v2
234 lines7.1 KiBRaw
1//go:build !integration
3package proxy
5import (
6 "bytes"
7 "encoding/binary"
8 "io"
9 "net/http"
10 "net/http/httptest"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
16 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
19func cargoReq(method, path string) *http.Request {
20 return httptest.NewRequest(method, "https://crates.io"+path, http.NoBody)
23func TestCargoMatchDownload(t *testing.T) {
24 t.Parallel()
25 m := CargoMatcher{}.Match(cargoReq(http.MethodGet, "/api/v1/crates/serde/1.0.197/download"))
26 assert.True(t, m.Matched)
27 assert.Equal(t, policy.Coordinate{Ecosystem: "cargo", Name: "serde", Version: "1.0.197"}, m.Coordinate)
28 assert.Equal(t, policy.Download, m.Operation)
31func TestCargoMatchSparseDownloadEndpoint(t *testing.T) {
32 t.Parallel()
33 cases := []struct {
34 name string
35 host string
36 path string
37 crate string
38 version string
39 }{
40 {
41 // crates.io's config.json "dl" has no markers, so cargo appends
42 // "/{crate}/{version}/download" to the "static.crates.io/crates"
43 // base — no "/api/v1" prefix. This is the real request cargo 1.96
44 // makes and is what regressed as unmatched.
45 name: "crates.io sparse download endpoint",
46 host: "https://static.crates.io",
47 path: "/crates/hashbrown/0.17.1/download",
48 crate: "hashbrown",
49 version: "0.17.1",
50 },
51 {
52 name: "hyphenated crate on download endpoint",
53 host: "https://static.crates.io",
54 path: "/crates/allocator-api2/0.2.21/download",
55 crate: "allocator-api2",
56 version: "0.2.21",
57 },
58 }
59 for _, tc := range cases {
60 t.Run(tc.name, func(t *testing.T) {
61 t.Parallel()
62 req := httptest.NewRequest(http.MethodGet, tc.host+tc.path, http.NoBody)
63 m := CargoMatcher{}.Match(req)
64 assert.True(t, m.Matched)
65 assert.True(t, m.Pass)
66 assert.Equal(t, policy.Coordinate{Ecosystem: "cargo", Name: tc.crate, Version: tc.version}, m.Coordinate)
67 assert.Equal(t, policy.Download, m.Operation)
68 })
69 }
72func TestCargoMatchSparseDownload(t *testing.T) {
73 t.Parallel()
74 cases := []struct {
75 name string
76 host string
77 path string
78 crate string
79 version string
80 }{
81 {
82 name: "crates.io sparse cdn",
83 host: "https://static.crates.io",
84 path: "/crates/anyhow/anyhow-1.0.104.crate",
85 crate: "anyhow",
86 version: "1.0.104",
87 },
88 {
89 name: "hyphenated crate name",
90 host: "https://static.crates.io",
91 path: "/crates/serde-derive/serde-derive-1.0.197.crate",
92 crate: "serde-derive",
93 version: "1.0.197",
94 },
95 {
96 name: "prerelease version",
97 host: "https://static.crates.io",
98 path: "/crates/foo/foo-1.0.0-beta.1.crate",
99 crate: "foo",
100 version: "1.0.0-beta.1",
101 },
102 {
103 name: "third-party registry path layout",
104 host: "https://artifactory.example.com",
105 path: "/artifactory/api/cargo/crates-remote/serde-1.0.197.crate",
106 crate: "serde",
107 version: "1.0.197",
108 },
109 }
110 for _, tc := range cases {
111 t.Run(tc.name, func(t *testing.T) {
112 t.Parallel()
113 req := httptest.NewRequest(http.MethodGet, tc.host+tc.path, http.NoBody)
114 m := CargoMatcher{}.Match(req)
115 assert.True(t, m.Matched)
116 assert.True(t, m.Pass)
117 assert.Equal(t, policy.Coordinate{Ecosystem: "cargo", Name: tc.crate, Version: tc.version}, m.Coordinate)
118 assert.Equal(t, policy.Download, m.Operation)
119 })
120 }
123func TestCargoMetadataIsNoMatch(t *testing.T) {
124 t.Parallel()
125 m := CargoMatcher{}.Match(cargoReq(http.MethodGet, "/api/v1/crates/serde"))
126 assert.False(t, m.Matched)
129func TestCargoSparseUnparseableCrateFailsClosed(t *testing.T) {
130 t.Parallel()
131 // A ".crate" filename is recognizably a crate download; one this heuristic
132 // cannot split into name/version must fail closed (Matched, !Pass) rather
133 // than falling out of classification and being forwarded uninspected.
134 cases := []string{
135 "https://static.crates.io/crates/foo/foo.crate",
136 "https://static.crates.io/crates/foo/1.0.0.crate",
137 "https://static.crates.io/crates/foo/.crate",
138 }
139 for _, url := range cases {
140 t.Run(url, func(t *testing.T) {
141 t.Parallel()
142 req := httptest.NewRequest(http.MethodGet, url, http.NoBody)
143 m := CargoMatcher{}.Match(req)
144 assert.True(t, m.Matched)
145 assert.False(t, m.Pass, "an unparseable crate download must fail closed (Pass = false)")
146 assert.Equal(t, policy.Download, m.Operation)
147 assert.Equal(t, "cargo", m.Coordinate.Ecosystem)
148 })
149 }
152func cargoPublishBody(name, vers string) []byte {
153 jsonMeta := []byte(`{"name":"` + name + `","vers":"` + vers + `"}`)
154 crate := []byte("fake-crate-bytes")
155 var b bytes.Buffer
156 _ = binary.Write(&b, binary.LittleEndian, uint32(len(jsonMeta)))
157 b.Write(jsonMeta)
158 _ = binary.Write(&b, binary.LittleEndian, uint32(len(crate)))
159 b.Write(crate)
160 return b.Bytes()
163func TestCargoMatchPublish(t *testing.T) {
164 t.Parallel()
165 body := cargoPublishBody("mycrate", "0.2.0")
166 req := httptest.NewRequest(http.MethodPut, "https://crates.io/api/v1/crates/new", bytes.NewReader(body))
168 m := CargoMatcher{}.Match(req)
169 assert.True(t, m.Matched)
170 assert.Equal(t, policy.Upload, m.Operation)
171 assert.Equal(t, "mycrate", m.Coordinate.Name)
172 assert.Equal(t, "0.2.0", m.Coordinate.Version)
174 restored, err := io.ReadAll(req.Body)
175 require.NoError(t, err)
176 assert.Equal(t, body, restored)
179func TestCargoPublishMalformedFrameFailsClosed(t *testing.T) {
180 t.Parallel()
181 cases := []struct {
182 name string
183 body []byte
184 }{
185 {"too short", []byte{0x01, 0x02}},
186 {"zero json len", func() []byte {
187 var b bytes.Buffer
188 _ = binary.Write(&b, binary.LittleEndian, uint32(0))
189 return b.Bytes()
190 }()},
191 {"json len exceeds body", func() []byte {
192 var b bytes.Buffer
193 _ = binary.Write(&b, binary.LittleEndian, uint32(9999))
194 b.WriteString("{}")
195 return b.Bytes()
196 }()},
197 {"json len high bit set", func() []byte {
198 var b bytes.Buffer
199 _ = binary.Write(&b, binary.LittleEndian, uint32(0x80000000))
200 b.WriteString(`{"name":"x","vers":"1"}`)
201 return b.Bytes()
202 }()},
203 {"bad json", func() []byte {
204 var b bytes.Buffer
205 meta := []byte("not json")
206 _ = binary.Write(&b, binary.LittleEndian, uint32(len(meta)))
207 b.Write(meta)
208 return b.Bytes()
209 }()},
210 }
211 for _, tc := range cases {
212 t.Run(tc.name, func(t *testing.T) {
213 t.Parallel()
214 req := httptest.NewRequest(http.MethodPut, "https://crates.io/api/v1/crates/new", bytes.NewReader(tc.body))
215 m := CargoMatcher{}.Match(req)
216 // The request matched "PUT …/api/v1/crates/new" exactly, so a frame
217 // we cannot decode fails closed rather than falling out of scope.
218 assert.True(t, m.Matched)
219 assert.False(t, m.Pass, "an unparseable publish frame must fail closed (Pass = false)")
220 assert.Equal(t, policy.Upload, m.Operation)
221 assert.Equal(t, "cargo", m.Coordinate.Ecosystem)
222 })
223 }
226func TestCargoPublishOverLimitFailsClosed(t *testing.T) {
227 t.Parallel()
228 body := cargoPublishBody("mycrate", "0.2.0")
229 req := httptest.NewRequest(http.MethodPut, "https://crates.io/api/v1/crates/new", bytes.NewReader(body))
230 m := CargoMatcher{limits: peekLimits{inMemory: 4, max: 8}}.Match(req)
231 assert.True(t, m.Matched)
232 assert.False(t, m.Pass, "an over-limit upload must fail closed (Pass = false)")
233 assert.Equal(t, policy.Upload, m.Operation)
Diagnostics7 git calls · 227 ms
CallStatusSizeTime0227 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 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=2
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 · 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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273
git.response.cache=HIT
git.response.duration_ms=5
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 · 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=1051
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2001.9 KiB213 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=213
git.response.bytes=1941
git.response.cache=MISS
git.response.duration_ms=213
git.fetch.wants=00fa0f60d263c664275e606180dee153fc7fa736
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=1941
git.pkt.payload.max=1914
git.sideband.band1.bytes=1914
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
a5e28a631b64bd4a
Request
0ca72681…
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