GitLabgitlab-org
cli
main
1//go:build !integration
3package proxy
5import (
6 "io"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
16)
18func get(path string) *http.Request {
19 return httptest.NewRequest(http.MethodGet, "https://registry.npmjs.org"+path, http.NoBody)
20}
22func TestNPMMatchTarballDownload(t *testing.T) {
23 t.Parallel()
24 m := NPMMatcher{}.Match(get("/left-pad/-/left-pad-1.3.0.tgz"))
25 assert.True(t, m.Matched)
26 assert.True(t, m.Pass)
27 assert.Equal(t, policy.Coordinate{Ecosystem: "npm", Name: "left-pad", Version: "1.3.0"}, m.Coordinate)
28 assert.Equal(t, policy.Download, m.Operation)
29}
31func TestNPMMatchScopedTarball(t *testing.T) {
32 t.Parallel()
33 m := NPMMatcher{}.Match(get("/@babel/core/-/core-7.24.0.tgz"))
34 assert.True(t, m.Matched)
35 assert.True(t, m.Pass)
36 assert.Equal(t, "@babel/core", m.Coordinate.Name)
37 assert.Equal(t, "7.24.0", m.Coordinate.Version)
38}
40func TestNPMMatchPathPrefixedRegistryTarball(t *testing.T) {
41 t.Parallel()
42 cases := map[string]struct {
43 path string
44 name string
45 version string
46 }{
47 "artifactory": {"/artifactory/api/npm/npm/left-pad/-/left-pad-1.3.0.tgz", "left-pad", "1.3.0"},
48 "gitlab packages": {"/api/v4/projects/1/packages/npm/left-pad/-/left-pad-1.3.0.tgz", "left-pad", "1.3.0"},
49 "scoped under prefix": {"/artifactory/api/npm/npm/@babel/core/-/core-7.24.0.tgz", "@babel/core", "7.24.0"},
50 "gitlab scoped packages": {"/api/v4/projects/1/packages/npm/@scope/pkg/-/pkg-1.0.0.tgz", "@scope/pkg", "1.0.0"},
51 }
52 for name, c := range cases {
53 t.Run(name, func(t *testing.T) {
54 t.Parallel()
55 m := NPMMatcher{}.Match(get(c.path))
56 assert.True(t, m.Matched, "path-prefixed registry tarball must be inspected, not forwarded uninspected")
57 assert.True(t, m.Pass)
58 assert.Equal(t, policy.Coordinate{Ecosystem: "npm", Name: c.name, Version: c.version}, m.Coordinate)
59 assert.Equal(t, policy.Download, m.Operation)
60 })
61 }
62}
64func TestNPMMatchMetadataIsNoMatch(t *testing.T) {
65 t.Parallel()
66 m := NPMMatcher{}.Match(get("/left-pad"))
67 assert.False(t, m.Matched)
68}
70func TestNPMMatchUploadPut(t *testing.T) {
71 t.Parallel()
72 body := `{"name":"left-pad","versions":{"9.9.9":{}},"_attachments":{"left-pad-9.9.9.tgz":{}}}`
73 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(body))
74 m := NPMMatcher{}.Match(req)
75 assert.True(t, m.Matched)
76 assert.True(t, m.Pass)
77 assert.Equal(t, "left-pad", m.Coordinate.Name)
78 assert.Equal(t, "9.9.9", m.Coordinate.Version)
79 assert.Equal(t, policy.Upload, m.Operation)
81 rest, err := io.ReadAll(req.Body)
82 require.NoError(t, err)
83 assert.Equal(t, body, string(rest))
84}
86func TestNPMMatchUploadSpillsButStillMatches(t *testing.T) {
87 t.Parallel()
88 // The packument JSON sits at the front and fits within the in-memory
89 // window, but trailing attachment bytes push the total body past it so
90 // it spills to a temp file. The coordinate must still be extracted and
91 // the full body restored for the upstream round trip.
92 json := `{"name":"left-pad","versions":{"9.9.9":{}},"_attachments":{"left-pad-9.9.9.tgz":{}}}`
93 body := json + strings.Repeat(" ", 300) // total > in-memory window below
94 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(body))
96 m := NPMMatcher{limits: peekLimits{inMemory: int64(len(json) + 8), max: 1 << 20}}.Match(req)
97 assert.True(t, m.Matched)
98 assert.True(t, m.Pass)
99 assert.Equal(t, "left-pad", m.Coordinate.Name)
100 assert.Equal(t, "9.9.9", m.Coordinate.Version)
102 rest, err := io.ReadAll(req.Body)
103 require.NoError(t, err)
104 require.NoError(t, req.Body.Close())
105 assert.Equal(t, body, string(rest), "full body must reach upstream even when spilled")
106}
108func TestNPMMatchUploadOverLimitFailsClosed(t *testing.T) {
109 t.Parallel()
110 body := strings.Repeat("x", 200) // exceeds the 64-byte hard limit
111 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(body))
113 m := NPMMatcher{limits: peekLimits{inMemory: 16, max: 64}}.Match(req)
114 assert.True(t, m.Matched)
115 assert.False(t, m.Pass, "an over-limit upload must fail closed (Pass = false)")
116 assert.Equal(t, "npm", m.Coordinate.Ecosystem)
117 assert.Equal(t, policy.Upload, m.Operation)
118}
120func TestNPMMatchUploadUnparseableFailsClosed(t *testing.T) {
121 t.Parallel()
122 // A PUT is the npm publish endpoint; a body whose packument cannot be
123 // decoded (bad JSON, missing name, no version) is a recognized-but-
124 // unparseable upload and must fail closed rather than being forwarded
125 // uninspected.
126 cases := map[string]string{
127 "bad json": `{not json`,
128 "truncated publish": `{"name":"left-pad","versions":{"1.0.0":{}},"_attachments":{"left-pad-1.0.0.tgz":{"data":"H4sI`,
129 "missing name": `{"versions":{"1.0.0":{}},"_attachments":{"x-1.0.0.tgz":{}}}`,
130 "missing version": `{"name":"left-pad","versions":{},"_attachments":{"left-pad-1.0.0.tgz":{}}}`,
131 }
132 for name, body := range cases {
133 t.Run(name, func(t *testing.T) {
134 t.Parallel()
135 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(body))
136 m := NPMMatcher{}.Match(req)
137 assert.True(t, m.Matched)
138 assert.False(t, m.Pass, "an unparseable publish body must fail closed (Pass = false)")
139 assert.Equal(t, "npm", m.Coordinate.Ecosystem)
140 assert.Equal(t, policy.Upload, m.Operation)
141 })
142 }
143}
145func TestNPMMatchNonPublishPutIsForwarded(t *testing.T) {
146 t.Parallel()
147 // npm issues PUTs that carry no package: dist-tags, ownership, stars, teams,
148 // legacy login, deprecation. Only a publish carries _attachments, so these
149 // are forwarded untouched, as is a non-JSON PUT from a lifecycle script.
150 cases := map[string]struct{ path, body string }{
151 "dist-tag add": {"/-/package/left-pad/dist-tags/beta", `"1.3.0"`},
152 "owner add": {"/left-pad/-rev/3-abc", `{"_id":"left-pad","_rev":"3-abc","maintainers":[{"name":"u"}]}`},
153 "star": {"/left-pad", `{"_id":"left-pad","_rev":"3-abc","users":{"u":true}}`},
154 "team add": {"/-/team/org/devs/user", `{"user":"u"}`},
155 "legacy adduser": {"/-/user/org.couchdb.user:u", `{"_id":"org.couchdb.user:u","name":"u","password":"p"}`},
156 "deprecate": {"/left-pad", `{"name":"left-pad","versions":{"1.3.0":{"deprecated":"use pad-left"}}}`},
157 "binary to bucket": {"/videos/run-1.mp4", "\x00\x00\x00\x18ftypmp42"},
158 }
159 for name, c := range cases {
160 t.Run(name, func(t *testing.T) {
161 t.Parallel()
162 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org"+c.path, strings.NewReader(c.body))
163 m := NPMMatcher{}.Match(req)
164 assert.False(t, m.Matched)
166 rest, err := io.ReadAll(req.Body)
167 require.NoError(t, err)
168 assert.Equal(t, c.body, string(rest), "a forwarded body must reach upstream intact")
169 })
170 }
171}
173func TestNPMMatchUploadLargerThanWindowIsChecked(t *testing.T) {
174 t.Parallel()
175 // A tarball bigger than the in-memory window cuts the packument off mid
176 // JSON in peeked.data; the matcher must read the spilled body instead of
177 // blocking a legitimate publish.
178 body := `{"name":"left-pad","versions":{"1.3.0":{}},"_attachments":{"left-pad-1.3.0.tgz":{"data":"` +
179 strings.Repeat("A", 4096) + `"}}}`
180 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(body))
182 m := NPMMatcher{limits: peekLimits{inMemory: 64, max: 1 << 20}}.Match(req)
183 assert.True(t, m.Matched)
184 assert.True(t, m.Pass)
185 assert.Equal(t, policy.Coordinate{Ecosystem: "npm", Name: "left-pad", Version: "1.3.0"}, m.Coordinate)
187 rest, err := io.ReadAll(req.Body)
188 require.NoError(t, err)
189 require.NoError(t, req.Body.Close())
190 assert.Equal(t, body, string(rest), "full body must reach upstream")
191}
193func TestNPMMatchUploadChecksCoordinateDeclaredAfterWindow(t *testing.T) {
194 t.Parallel()
195 // A second name/versions pair after the tarball overrides the first for
196 // the registry, so it is the coordinate that must be checked.
197 body := `{"name":"benign","versions":{"1.0.0":{}},"_attachments":{"x.tgz":{"data":"` +
198 strings.Repeat("A", 4096) + `"}},"name":"evil","versions":{"6.6.6":{}}}`
199 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/evil", strings.NewReader(body))
201 m := NPMMatcher{limits: peekLimits{inMemory: 64, max: 1 << 20}}.Match(req)
202 assert.True(t, m.Matched)
203 assert.True(t, m.Pass)
204 assert.Equal(t, policy.Coordinate{Ecosystem: "npm", Name: "evil", Version: "6.6.6"}, m.Coordinate)
205}
207func TestNPMMatchUploadMultipleVersionsFailsClosed(t *testing.T) {
208 t.Parallel()
209 // A publish carries exactly one version; with several, any one picked for
210 // the check may not be the one the registry stores.
211 body := `{"name":"left-pad","versions":{"1.3.0":{},"6.6.6":{}},"_attachments":{"left-pad-1.3.0.tgz":{}}}`
212 req := httptest.NewRequest(http.MethodPut, "https://registry.npmjs.org/left-pad", strings.NewReader(body))
214 m := NPMMatcher{}.Match(req)
215 assert.True(t, m.Matched)
216 assert.False(t, m.Pass)
217 assert.Equal(t, policy.Upload, m.Operation)
218}
Diagnostics7 git calls · 228 ms
CallStatusSizeTime0228 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 · 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 · 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=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:0200361 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=361 git.response.cache=HIT git.response.duration_ms=2 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:02001.0 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=1051 git.response.cache=HIT git.response.duration_ms=2 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=7503ef36a75d3595b473035c69e639fd0ce41598Open pack map
fetch2002.4 KiB218 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=218 git.response.bytes=2446 git.response.cache=MISS git.response.duration_ms=218 git.fetch.wants=a27bf200f294d52b635fb2dc4eaf35da1a5b7fb8 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=2446 git.pkt.payload.max=2419 git.sideband.band1.bytes=2419 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
- a5e18ffd5c1d5b65
- Request
- 3c5450ef…
- 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