GitLabgitlab-org
cli
main
1//go:build !integration
3package pm
5import (
6 "bytes"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
15 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
16)
18// TestManagerEnvironAndCATrust pins the exact proxy-routing and CA-trust env
19// vars each manager emits. A typo in any of these strings would silently fail
20// to route traffic through the firewall proxy (fail-open) — the same class of
21// bug as the Yarn Berry issue — so assert the concrete values, not just types.
22func TestManagerEnvironAndCATrust(t *testing.T) {
23 t.Parallel()
24 const proxyURL = "http://127.0.0.1:9999"
25 const caPath = "/tmp/ca.pem"
27 t.Run("yarn", func(t *testing.T) {
28 t.Parallel()
29 // Yarn Berry (v2+) honors YARN_HTTP(S)_PROXY; Yarn Classic (v1) reads
30 // .npmrc and the npm_config_* env vars, so both families are pinned,
31 // with npm_config_noproxy neutralized to a non-empty sentinel.
32 env := Yarn().Environment(proxyURL)
33 assert.Contains(t, env, "YARN_HTTPS_PROXY="+proxyURL)
34 assert.Contains(t, env, "YARN_HTTP_PROXY="+proxyURL)
35 assert.Contains(t, env, "npm_config_proxy="+proxyURL)
36 assert.Contains(t, env, "npm_config_https_proxy="+proxyURL)
37 assert.Contains(t, env, "npm_config_noproxy=localhost")
38 assert.NotContains(t, env, "npm_config_noproxy=", "empty noproxy is overridden by .npmrc; must be a non-empty sentinel")
39 assert.Equal(t, []string{"NODE_EXTRA_CA_CERTS=" + caPath}, Yarn().CATrustEnviron(caPath))
40 })
42 // pip/pipenv/poetry pin PIP_PROXY so pip's trust_env=False path (triggered
43 // by its own PIP_PROXY/pip.conf proxy) can't route around the MITM;
44 // uv/twine/gem route through the universal HTTP(S)_PROXY the engine sets in
45 // proxyEnviron, so their Environment adds nothing.
46 t.Run("pip", func(t *testing.T) {
47 t.Parallel()
48 assert.Equal(t, []string{"PIP_PROXY=" + proxyURL}, Pip().Environment(proxyURL))
49 assert.Equal(t, []string{"PIP_CERT=" + caPath, "REQUESTS_CA_BUNDLE=" + caPath}, Pip().CATrustEnviron(caPath))
50 })
52 t.Run("pipenv", func(t *testing.T) {
53 t.Parallel()
54 assert.Equal(t, []string{"PIP_PROXY=" + proxyURL}, Pipenv().Environment(proxyURL))
55 assert.Equal(t, []string{"PIP_CERT=" + caPath, "REQUESTS_CA_BUNDLE=" + caPath}, Pipenv().CATrustEnviron(caPath))
56 })
58 t.Run("uv", func(t *testing.T) {
59 t.Parallel()
60 assert.Empty(t, Uv().Environment(proxyURL))
61 assert.Equal(t, []string{"SSL_CERT_FILE=" + caPath}, Uv().CATrustEnviron(caPath))
62 })
64 t.Run("poetry", func(t *testing.T) {
65 t.Parallel()
66 assert.Equal(t, []string{"PIP_PROXY=" + proxyURL}, Poetry().Environment(proxyURL))
67 assert.Equal(t, []string{"PIP_CERT=" + caPath, "REQUESTS_CA_BUNDLE=" + caPath}, Poetry().CATrustEnviron(caPath))
68 })
70 t.Run("twine", func(t *testing.T) {
71 t.Parallel()
72 assert.Empty(t, Twine().Environment(proxyURL))
73 assert.Equal(t, []string{"TWINE_CERT=" + caPath}, Twine().CATrustEnviron(caPath))
74 })
76 t.Run("gem", func(t *testing.T) {
77 t.Parallel()
78 assert.Empty(t, Gem().Environment(proxyURL))
79 assert.Equal(t, []string{"SSL_CERT_FILE=" + caPath}, Gem().CATrustEnviron(caPath))
80 })
82 t.Run("bundle", func(t *testing.T) {
83 t.Parallel()
84 // Only the lowercase names Ruby's Net::HTTP reads are added; the
85 // uppercase names are already set by the engine and not duplicated.
86 env := Bundle().Environment(proxyURL)
87 assert.Contains(t, env, "https_proxy="+proxyURL)
88 assert.Contains(t, env, "http_proxy="+proxyURL)
89 assert.NotContains(t, env, "HTTPS_PROXY="+proxyURL, "uppercase is the engine's job; must not be duplicated here")
90 assert.Equal(t, []string{"SSL_CERT_FILE=" + caPath, "BUNDLE_SSL_CA_CERT=" + caPath}, Bundle().CATrustEnviron(caPath))
91 })
93 t.Run("cargo", func(t *testing.T) {
94 t.Parallel()
95 // cargo pins CARGO_HTTP_PROXY: an http.proxy in cargo or git config
96 // outranks the engine's HTTPS_PROXY, and curl ignores the uppercase
97 // HTTP_PROXY for plaintext registries.
98 assert.Equal(t, []string{"CARGO_HTTP_PROXY=" + proxyURL}, Cargo().Environment(proxyURL))
99 assert.Equal(t, []string{"CARGO_HTTP_CAINFO=" + caPath}, Cargo().CATrustEnviron(caPath))
100 })
102 t.Run("golang", func(t *testing.T) {
103 t.Parallel()
104 assert.Equal(t, []string{"SSL_CERT_FILE=" + caPath}, Golang().CATrustEnviron(caPath))
105 })
106}
108// TestManagerExistingBundleVars pins each manager's declared CA-preservation
109// variables. Every variable a manager's CATrustEnviron overwrites must be
110// listed here, or a user who set only the undeclared one silently loses their
111// trust anchors — a "wrong string, silent breakage" bug the engine can't catch.
112func TestManagerExistingBundleVars(t *testing.T) {
113 t.Parallel()
114 cases := []struct {
115 m PackageManager
116 want []string
117 }{
118 {NPM(), []string{"NODE_EXTRA_CA_CERTS"}},
119 {Pnpm(), []string{"NODE_EXTRA_CA_CERTS"}},
120 {Yarn(), []string{"NODE_EXTRA_CA_CERTS"}},
121 {Pip(), []string{"PIP_CERT", "REQUESTS_CA_BUNDLE"}},
122 {Pipenv(), []string{"PIP_CERT", "REQUESTS_CA_BUNDLE"}},
123 {Uv(), []string{"SSL_CERT_FILE"}},
124 {Poetry(), []string{"PIP_CERT", "REQUESTS_CA_BUNDLE"}},
125 {Twine(), []string{"TWINE_CERT", "REQUESTS_CA_BUNDLE"}},
126 {Gem(), []string{"SSL_CERT_FILE"}},
127 {Bundle(), []string{"SSL_CERT_FILE", "BUNDLE_SSL_CA_CERT"}},
128 // Maven and Gradle preserve no CA bundle var: they trust the proxy
129 // through a generated JVM truststore, not an env-named CA file.
130 {Maven(), nil},
131 {Gradle(), nil},
132 {Cargo(), []string{"CARGO_HTTP_CAINFO"}},
133 {Golang(), []string{"SSL_CERT_FILE"}},
134 }
135 for _, c := range cases {
136 t.Run(c.m.Name(), func(t *testing.T) {
137 t.Parallel()
138 assert.Equal(t, c.want, c.m.ExistingBundleVars())
139 // Every declared var must be one CATrustEnviron actually sets, so
140 // the declaration and the overwrite can't drift apart.
141 set := c.m.CATrustEnviron("/tmp/ca.pem")
142 declared := make(map[string]struct{}, len(c.m.ExistingBundleVars()))
143 for _, v := range c.m.ExistingBundleVars() {
144 declared[v] = struct{}{}
145 if v == "REQUESTS_CA_BUNDLE" && c.m.Name() == "twine" {
146 continue // twine reads REQUESTS_CA_BUNDLE but sets only TWINE_CERT
147 }
148 found := false
149 for _, e := range set {
150 if k, _, _ := strings.Cut(e, "="); k == v {
151 found = true
152 break
153 }
154 }
155 assert.Truef(t, found, "%s declares %q but CATrustEnviron does not set it", c.m.Name(), v)
156 }
157 // And the reverse (pm.go: "Every variable CATrustEnviron sets must
158 // appear here"): a new CA-bundle var added to CATrustEnviron but not
159 // declared here would silently clobber a user's anchors on that var
160 // and still pass the forward check above.
161 for _, e := range set {
162 k, _, _ := strings.Cut(e, "=")
163 _, ok := declared[k]
164 assert.Truef(t, ok, "%s CATrustEnviron sets %q but ExistingBundleVars does not declare it", c.m.Name(), k)
165 }
166 // Registry connections trust these files whichever manager runs.
167 for _, v := range c.m.ExistingBundleVars() {
168 assert.Containsf(t, upstreamCAVars, v, "%s declares %q but upstreamCAVars does not list it", c.m.Name(), v)
169 }
170 })
171 }
172}
174func TestRunMergesUserCAIntoOverriddenVar(t *testing.T) {
175 cases := []struct {
176 m PackageManager
177 key string
178 }{
179 {Poetry(), "PIP_CERT"},
180 }
181 for _, c := range cases {
182 t.Run(c.m.Name()+"/"+c.key, func(t *testing.T) {
183 t.Setenv("GITLAB_CI", "")
184 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
185 for _, v := range upstreamCAVars {
186 t.Setenv(v, "")
187 }
188 userPEM := newTestAuthority(t).pem()
189 userPath := filepath.Join(t.TempDir(), "corp-ca.pem")
190 require.NoError(t, os.WriteFile(userPath, userPEM, 0o600))
191 t.Setenv(c.key, userPath)
192 ios, _, _, _ := cmdtest.TestIOStreams()
193 exec := &fakeExecutor{}
194 var bundle []byte
195 exec.onExec = func() { bundle, _ = os.ReadFile(lookupEnv(exec.env, c.key)) }
197 require.NoError(t, Run(t.Context(), c.m, RunOptions{
198 IO: ios, Executor: exec, BaseDir: t.TempDir(), ProjectID: "group/proj", Args: []string{"install"},
199 }))
201 assert.NotEqual(t, userPath, lookupEnv(exec.env, c.key))
202 assert.True(t, bytes.HasPrefix(bundle, userPEM), "the child must still trust the user's CA")
203 assert.Equal(t, 2+len(systemRoots()), countPEMBlocks(bundle), "the bundle must hold the user's CA, the proxy CA and the system roots")
204 })
205 }
206}
208// TestCleanupCAFilesNoOpForEnvProxyManagers asserts the env-proxy managers'
209// CleanupCAFiles does not touch the CA bundle path (only the engine removes it)
210// and does not panic.
211func TestCleanupCAFilesNoOpForEnvProxyManagers(t *testing.T) {
212 t.Parallel()
213 caPath := filepath.Join(t.TempDir(), "ca.pem")
214 require.NoError(t, os.WriteFile(caPath, []byte("x"), 0o600))
215 for _, m := range []PackageManager{NPM(), Pnpm(), Yarn(), Pip(), Pipenv(), Uv(), Poetry(), Twine(), Gem(), Bundle(), Cargo(), Golang()} {
216 m.CleanupCAFiles(caPath)
217 }
218 _, err := os.Stat(caPath)
219 assert.NoError(t, err, "env-proxy managers must not remove the engine-owned CA bundle")
220}
222func TestYarnSetupInjectsFirewallPlugin(t *testing.T) {
223 t.Parallel()
224 cases := []struct {
225 name string
226 parent []string
227 userPlugins string
228 }{
229 {"uppercase", []string{"YARN_PLUGINS=/home/u/user.cjs"}, "/home/u/user.cjs"},
230 {"lowercase", []string{"yarn_plugins=/home/u/user.cjs"}, "/home/u/user.cjs"},
231 {"last of both cases wins", []string{"YARN_PLUGINS=/home/u/a.cjs", "yarn_plugins=/home/u/b.cjs"}, "/home/u/b.cjs"},
232 }
233 for _, c := range cases {
234 t.Run(c.name, func(t *testing.T) {
235 t.Parallel()
236 res, err := yarnManager{}.Setup(t.Context(), setupInput{parent: c.parent})
237 require.NoError(t, err)
238 assert.Nil(t, res.verdicts, "yarn records no verdicts of its own")
239 pluginPath, userPlugins, ok := strings.Cut(lookupEnv(res.env, "YARN_PLUGINS"), ";")
240 require.True(t, ok, "YARN_PLUGINS must list the firewall plugin and keep the user's")
241 assert.Equal(t, c.userPlugins, userPlugins)
243 plugin, err := os.ReadFile(pluginPath)
244 require.NoError(t, err)
245 assert.Equal(t, yarnFirewallPlugin, plugin)
247 res.stop()
248 _, err = os.Stat(pluginPath)
249 assert.ErrorIs(t, err, os.ErrNotExist)
250 })
251 }
252}
254func TestYarnSetupWithoutUserPluginsListsOnlyFirewallPlugin(t *testing.T) {
255 t.Parallel()
256 res, err := yarnManager{}.Setup(t.Context(), setupInput{parent: []string{"PATH=/usr/bin"}})
257 require.NoError(t, err)
258 t.Cleanup(res.stop)
259 // A trailing ";" would add an empty entry, which Berry resolves to the
260 // working directory and tries to load as a plugin.
261 pluginPath := lookupEnv(res.env, "YARN_PLUGINS")
262 assert.NotContains(t, pluginPath, ";")
263 assert.FileExists(t, pluginPath)
264}
266func TestYarnSetupFailsWhenPluginCannotBeWritten(t *testing.T) {
267 missing := filepath.Join(t.TempDir(), "missing")
268 t.Setenv("TMPDIR", missing)
269 t.Setenv("TMP", missing)
270 t.Setenv("TEMP", missing)
272 res, err := yarnManager{}.Setup(t.Context(), setupInput{})
273 require.Error(t, err)
274 assert.Empty(t, res.env, "a failed write must not point YARN_PLUGINS at any path")
275}
Diagnostics7 git calls · 304 ms
CallStatusSizeTime0304 ms
discover200185 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273 git.response.cache=HIT git.response.duration_ms=2 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 · hit2 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=2 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
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.3 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=292 git.response.bytes=1327 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62 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=1327 git.pkt.payload.max=1300 git.sideband.band1.bytes=1300 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1300 git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2003.4 KiB292 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3473 git.response.cache=MISS git.response.duration_ms=292 git.fetch.wants=9a4576af3dafb382618f2f024b5db5edfe9227a3 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=3473 git.pkt.payload.max=3446 git.sideband.band1.bytes=3446 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
- a5e2823d29f96483
- Request
- 6140978e…
- 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