GitLabgitlab-org
cli
main
1//go:build !integration
3package proxy
5import (
6 "bytes"
7 "mime/multipart"
8 "net/http"
9 "net/http/httptest"
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 TestPyPIMatchWheelDownload(t *testing.T) {
19 t.Parallel()
20 req := httptest.NewRequest(http.MethodGet, "https://files.pythonhosted.org/packages/ab/cd/requests-2.31.0-py3-none-any.whl", http.NoBody)
21 m := PyPIMatcher{}.Match(req)
22 assert.True(t, m.Matched)
23 assert.True(t, m.Pass)
24 assert.Equal(t, policy.Coordinate{Ecosystem: "pypi", Name: "requests", Version: "2.31.0"}, m.Coordinate)
25 assert.Equal(t, policy.Download, m.Operation)
26}
28func TestPyPIMatchWheelMetadataDownload(t *testing.T) {
29 t.Parallel()
30 req := httptest.NewRequest(http.MethodGet, "https://files.pythonhosted.org/packages/ab/cd/requests-2.31.0-py3-none-any.whl.metadata", http.NoBody)
31 m := PyPIMatcher{}.Match(req)
32 assert.True(t, m.Matched)
33 assert.True(t, m.Pass)
34 assert.Equal(t, policy.Coordinate{Ecosystem: "pypi", Name: "requests", Version: "2.31.0"}, m.Coordinate)
35 assert.Equal(t, policy.Download, m.Operation)
36}
38func TestPyPIMatchSdistDownload(t *testing.T) {
39 t.Parallel()
40 // PyPI serves sdists as .tar.gz or .zip (modern) and still serves the
41 // legacy .tar.bz2 and .tgz for older packages. Every served format must be
42 // policy-checked; an unrecognized one would be forwarded without a check.
43 for _, ext := range []string{".tar.gz", ".zip", ".tar.bz2", ".tgz"} {
44 t.Run(ext, func(t *testing.T) {
45 t.Parallel()
46 req := httptest.NewRequest(http.MethodGet, "https://files.pythonhosted.org/packages/ab/cd/Flask-3.0.2"+ext, http.NoBody)
47 m := PyPIMatcher{}.Match(req)
48 assert.True(t, m.Matched)
49 assert.True(t, m.Pass)
50 assert.Equal(t, "flask", m.Coordinate.Name)
51 assert.Equal(t, "3.0.2", m.Coordinate.Version)
52 })
53 }
54}
56func TestPyPISimpleIndexIsNoMatch(t *testing.T) {
57 t.Parallel()
58 req := httptest.NewRequest(http.MethodGet, "https://pypi.org/simple/requests/", http.NoBody)
59 m := PyPIMatcher{}.Match(req)
60 assert.False(t, m.Matched)
61}
63func TestPyPIMatchUpload(t *testing.T) {
64 t.Parallel()
65 var buf bytes.Buffer
66 w := multipart.NewWriter(&buf)
67 require.NoError(t, w.WriteField("name", "requests"))
68 require.NoError(t, w.WriteField("version", "2.31.0"))
69 require.NoError(t, w.Close())
71 req := httptest.NewRequest(http.MethodPost, "https://upload.pypi.org/legacy/", &buf)
72 req.Header.Set("Content-Type", w.FormDataContentType())
74 m := PyPIMatcher{}.Match(req)
75 assert.True(t, m.Matched)
76 assert.True(t, m.Pass)
77 assert.Equal(t, "requests", m.Coordinate.Name)
78 assert.Equal(t, "2.31.0", m.Coordinate.Version)
79 assert.Equal(t, policy.Upload, m.Operation)
80}
82func TestPyPIMatchUploadIgnoresNonLegacyPath(t *testing.T) {
83 t.Parallel()
84 var buf bytes.Buffer
85 w := multipart.NewWriter(&buf)
86 require.NoError(t, w.WriteField("name", "requests"))
87 require.NoError(t, w.WriteField("version", "2.31.0"))
88 require.NoError(t, w.Close())
90 req := httptest.NewRequest(http.MethodPost, "https://example.com/some/other/endpoint", &buf)
91 req.Header.Set("Content-Type", w.FormDataContentType())
93 m := PyPIMatcher{}.Match(req)
94 assert.False(t, m.Matched, "a multipart POST outside the /legacy/ upload endpoint must not be treated as a PyPI upload")
95}
97func TestPyPIMatchUploadOverLimitFailsClosed(t *testing.T) {
98 t.Parallel()
99 var buf bytes.Buffer
100 w := multipart.NewWriter(&buf)
101 require.NoError(t, w.WriteField("name", "requests"))
102 require.NoError(t, w.WriteField("version", "2.31.0"))
103 require.NoError(t, w.Close())
104 // Ensure the body clears the 64-byte hard limit set above.
105 require.Greater(t, buf.Len(), 64)
107 req := httptest.NewRequest(http.MethodPost, "https://upload.pypi.org/legacy/", &buf)
108 req.Header.Set("Content-Type", w.FormDataContentType())
110 m := PyPIMatcher{limits: peekLimits{inMemory: 16, max: 64}}.Match(req)
111 assert.True(t, m.Matched)
112 assert.False(t, m.Pass, "an over-limit upload must fail closed (Pass = false)")
113 assert.Equal(t, "pypi", m.Coordinate.Ecosystem)
114 assert.Equal(t, policy.Upload, m.Operation)
115}
117func TestPyPIMatchUploadUnparseableFailsClosed(t *testing.T) {
118 t.Parallel()
119 // The request hits the /legacy/ endpoint with a multipart body but carries
120 // no "name" field, so it is a recognized-but-unparseable upload and must
121 // fail closed rather than being forwarded uninspected.
122 var buf bytes.Buffer
123 w := multipart.NewWriter(&buf)
124 require.NoError(t, w.WriteField("version", "2.31.0"))
125 require.NoError(t, w.Close())
127 req := httptest.NewRequest(http.MethodPost, "https://upload.pypi.org/legacy/", &buf)
128 req.Header.Set("Content-Type", w.FormDataContentType())
130 m := PyPIMatcher{}.Match(req)
131 assert.True(t, m.Matched)
132 assert.False(t, m.Pass, "an unparseable upload body must fail closed (Pass = false)")
133 assert.Equal(t, "pypi", m.Coordinate.Ecosystem)
134 assert.Equal(t, policy.Upload, m.Operation)
135}
Diagnostics7 git calls · 263 ms
CallStatusSizeTime0263 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 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=4 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:02001.0 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=1051 git.response.cache=HIT git.response.duration_ms=1 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
fetch2001.3 KiB252 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=251 git.response.bytes=1308 git.response.cache=MISS git.response.duration_ms=252 git.fetch.wants=f6b88d380e7649562070792e32be2b54d376b7b6 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=1308 git.pkt.payload.max=1281 git.sideband.band1.bytes=1281 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
- a5e271d41db9d806
- Request
- 8c7fa36c…
- 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