GitLabgitlab-org

cli

main
ply7 git calls338 ms105 KiB5 cachedprotocol v2
1182 lines42 KiBRaw
1//go:build !integration
3package pm
5import (
6 "cmp"
7 "context"
8 "crypto/tls"
9 "crypto/x509"
10 "errors"
11 "fmt"
12 "io"
13 "maps"
14 "net/http"
15 "net/http/httptest"
16 "net/url"
17 "os"
18 "path/filepath"
19 "runtime"
20 "strings"
21 "sync"
22 "testing"
24 "github.com/stretchr/testify/assert"
25 "github.com/stretchr/testify/require"
27 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/cilog"
28 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
31func TestUpstreamProxyResolvers(t *testing.T) {
32 t.Parallel()
33 tests := []struct {
34 name string
35 manager PackageManager
36 env []string
37 want upstreamProxy
38 wantOK bool
39 }{
40 {
41 name: "npm https-proxy wins over proxy and HTTPS_PROXY",
42 manager: NPM(),
43 env: []string{"HTTPS_PROXY=http://env:1", "npm_config_proxy=http://proxy:2", "npm_config_https_proxy=http://https-proxy:3"},
44 want: upstreamProxy{Source: "npm_config_https_proxy", URL: "http://https-proxy:3"},
45 wantOK: true,
46 },
47 {
48 name: "npm proxy wins over HTTPS_PROXY",
49 manager: NPM(),
50 env: []string{"HTTPS_PROXY=http://env:1", "npm_config_proxy=http://proxy:2"},
51 want: upstreamProxy{Source: "npm_config_proxy", URL: "http://proxy:2"},
52 wantOK: true,
53 },
54 {
55 name: "npm config keys match case-insensitively",
56 manager: Pnpm(),
57 env: []string{"NPM_CONFIG_HTTPS_PROXY=http://https-proxy:3"},
58 want: upstreamProxy{Source: "NPM_CONFIG_HTTPS_PROXY", URL: "http://https-proxy:3"},
59 wantOK: true,
60 },
61 {
62 name: "npm empty value is unset",
63 manager: NPM(),
64 env: []string{"npm_config_https_proxy=", "npm_config_proxy=http://proxy:2"},
65 want: upstreamProxy{Source: "npm_config_proxy", URL: "http://proxy:2"},
66 wantOK: true,
67 },
68 {
69 name: "npm noproxy and NO_PROXY both apply",
70 manager: NPM(),
71 env: []string{"npm_config_https_proxy=http://https-proxy:3", "npm_config_noproxy=internal.example", "NO_PROXY=mirror.example"},
72 want: upstreamProxy{Source: "npm_config_https_proxy", URL: "http://https-proxy:3", NoProxy: "internal.example,mirror.example"},
73 wantOK: true,
74 },
75 {
76 name: "npm HTTPS_PROXY keeps npm noproxy",
77 manager: NPM(),
78 env: []string{"HTTPS_PROXY=http://env:1", "npm_config_noproxy=internal.example"},
79 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:1", NoProxy: "internal.example"},
80 wantOK: true,
81 },
82 {
83 name: "npm without a proxy",
84 manager: NPM(),
85 env: []string{"npm_config_noproxy=internal.example"},
86 },
87 {
88 name: "yarn YARN_HTTPS_PROXY wins and ignores NO_PROXY",
89 manager: Yarn(),
90 env: []string{"npm_config_https_proxy=http://https-proxy:3", "YARN_HTTPS_PROXY=http://yarn:4", "NO_PROXY=mirror.example"},
91 want: upstreamProxy{Source: "YARN_HTTPS_PROXY", URL: "http://yarn:4"},
92 wantOK: true,
93 },
94 {
95 name: "yarn reads yarn_https_proxy in any case",
96 manager: Yarn(),
97 env: []string{"yarn_https_proxy=http://yarn:4"},
98 want: upstreamProxy{Source: "yarn_https_proxy", URL: "http://yarn:4"},
99 wantOK: true,
100 },
101 {
102 name: "yarn classic npm_config_proxy",
103 manager: Yarn(),
104 env: []string{"npm_config_proxy=http://proxy:2"},
105 want: upstreamProxy{Source: "npm_config_proxy", URL: "http://proxy:2"},
106 wantOK: true,
107 },
108 {
109 name: "yarn leaves HTTPS_PROXY to the default",
110 manager: Yarn(),
111 env: []string{"HTTPS_PROXY=http://env:1"},
112 },
113 {
114 name: "pip PIP_PROXY ignores NO_PROXY",
115 manager: Pipenv(),
116 env: []string{"HTTPS_PROXY=http://env:1", "PIP_PROXY=http://pip:5", "NO_PROXY=pypi.org"},
117 want: upstreamProxy{Source: "PIP_PROXY", URL: "http://pip:5"},
118 wantOK: true,
119 },
120 {
121 name: "pip without PIP_PROXY",
122 manager: Pip(),
123 env: []string{"HTTPS_PROXY=http://env:1"},
124 },
125 {
126 name: "cargo CARGO_HTTP_PROXY keeps no_proxy",
127 manager: Cargo(),
128 env: []string{"CARGO_HTTP_PROXY=cargo:6", "no_proxy=index.crates.io"},
129 want: upstreamProxy{Source: "CARGO_HTTP_PROXY", URL: "cargo:6", NoProxy: "index.crates.io"},
130 wantOK: true,
131 },
132 {
133 name: "cargo prefers NO_PROXY over no_proxy",
134 manager: Cargo(),
135 env: []string{"CARGO_HTTP_PROXY=cargo:6", "NO_PROXY=upper.example", "no_proxy=lower.example"},
136 want: upstreamProxy{Source: "CARGO_HTTP_PROXY", URL: "cargo:6", NoProxy: "upper.example"},
137 wantOK: true,
138 },
139 {
140 name: "npm proxy=false falls back to HTTPS_PROXY",
141 manager: NPM(),
142 env: []string{"HTTPS_PROXY=http://env:1", "npm_config_proxy=false"},
143 want: upstreamProxy{Source: "HTTPS_PROXY", URL: "http://env:1"},
144 wantOK: true,
145 },
146 {
147 name: "npm https-proxy=null falls back to proxy",
148 manager: NPM(),
149 env: []string{"npm_config_https_proxy=null", "npm_config_proxy=http://proxy:2"},
150 want: upstreamProxy{Source: "npm_config_proxy", URL: "http://proxy:2"},
151 wantOK: true,
152 },
153 {
154 name: "npm noproxy key matches case-insensitively",
155 manager: NPM(),
156 env: []string{"npm_config_https_proxy=http://https-proxy:3", "NPM_CONFIG_NOPROXY=internal.example"},
157 want: upstreamProxy{Source: "npm_config_https_proxy", URL: "http://https-proxy:3", NoProxy: "internal.example"},
158 wantOK: true,
159 },
160 {
161 name: "yarn classic https-proxy wins over proxy",
162 manager: Yarn(),
163 env: []string{"npm_config_proxy=http://proxy:2", "npm_config_https_proxy=http://https-proxy:3"},
164 want: upstreamProxy{Source: "npm_config_https_proxy", URL: "http://https-proxy:3"},
165 wantOK: true,
166 },
167 {
168 name: "yarn classic proxy=false leaves HTTPS_PROXY to the default",
169 manager: Yarn(),
170 env: []string{"HTTPS_PROXY=http://env:1", "npm_config_proxy=false"},
171 },
172 {
173 name: "yarn classic https-proxy=false connects directly",
174 manager: Yarn(),
175 env: []string{"HTTPS_PROXY=http://env:1", "npm_config_proxy=http://proxy:2", "npm_config_https_proxy=false"},
176 want: upstreamProxy{Source: "npm_config_https_proxy"},
177 wantOK: true,
178 },
179 {
180 name: "yarn classic YARN_HTTPS_PROXY=false connects directly",
181 manager: Yarn(),
182 env: []string{"npm_config_https_proxy=http://https-proxy:3", "YARN_HTTPS_PROXY=false"},
183 want: upstreamProxy{Source: "YARN_HTTPS_PROXY"},
184 wantOK: true,
185 },
186 {
187 name: "cargo empty CARGO_HTTP_PROXY connects directly",
188 manager: Cargo(),
189 env: []string{"HTTPS_PROXY=http://env:1", "CARGO_HTTP_PROXY="},
190 want: upstreamProxy{Source: "CARGO_HTTP_PROXY"},
191 wantOK: true,
192 },
193 }
194 for _, tt := range tests {
195 t.Run(tt.name, func(t *testing.T) {
196 t.Parallel()
197 resolver, ok := tt.manager.(upstreamProxyResolver)
198 require.True(t, ok)
199 got, gotOK, err := resolver.resolveUpstreamProxy(tt.env, t.TempDir(), nil)
200 require.NoError(t, err)
201 assert.Equal(t, tt.wantOK, gotOK)
202 assert.Equal(t, tt.want, got)
203 })
204 }
207// The cases match the parsers of pip 26.0, 26.1.2 and 26.3, except where
208// pipProxyArg's doc comment says glab takes a --proxy that pip doesn't.
209func TestPipProxyArg(t *testing.T) {
210 t.Parallel()
211 const proxyURL = "http://alice:s3cret@corp:3128"
212 tests := []struct {
213 name string
214 args []string
215 wantValue string
216 wantRest []string
217 wantOK bool
218 }{
219 {
220 name: "after the command",
221 args: []string{"install", "--proxy", proxyURL, "six"},
222 wantValue: proxyURL,
223 wantRest: []string{"install", "six"},
224 wantOK: true,
225 },
226 {
227 name: "with = after the command",
228 args: []string{"install", "--proxy=" + proxyURL, "six"},
229 wantValue: proxyURL,
230 wantRest: []string{"install", "six"},
231 wantOK: true,
232 },
233 {
234 name: "before the command",
235 args: []string{"--proxy", proxyURL, "install", "six"},
236 wantValue: proxyURL,
237 wantRest: []string{"install", "six"},
238 wantOK: true,
239 },
240 {
241 name: "with = before the command",
242 args: []string{"--proxy=" + proxyURL, "install", "six"},
243 wantValue: proxyURL,
244 wantRest: []string{"install", "six"},
245 wantOK: true,
246 },
247 {
248 name: "an abbreviation",
249 args: []string{"install", "--prox", proxyURL, "six"},
250 wantValue: proxyURL,
251 wantRest: []string{"install", "six"},
252 wantOK: true,
253 },
254 {
255 name: "an abbreviation with = before the command",
256 args: []string{"--prox=" + proxyURL, "install", "six"},
257 wantValue: proxyURL,
258 wantRest: []string{"install", "six"},
259 wantOK: true,
260 },
261 {
262 name: "an abbreviation pip calls ambiguous for install",
263 args: []string{"install", "--pro", proxyURL, "six"},
264 wantValue: proxyURL,
265 wantRest: []string{"install", "six"},
266 wantOK: true,
267 },
268 {
269 name: "an abbreviation pip calls ambiguous for list",
270 args: []string{"list", "--outdated", "--pr", proxyURL},
271 wantValue: proxyURL,
272 wantRest: []string{"list", "--outdated"},
273 wantOK: true,
274 },
275 {
276 name: "an abbreviated option before the command does not hide one",
277 args: []string{"--ca", "install", "search", "--pr", "http://p:1", "six"},
278 wantValue: "http://p:1",
279 wantRest: []string{"--ca", "install", "search", "six"},
280 wantOK: true,
281 },
282 {
283 name: "--p is too short, as --python shares it",
284 args: []string{"search", "--p", proxyURL, "six"},
285 wantRest: []string{"search", "--p", proxyURL, "six"},
286 },
287 {
288 name: "options are case-sensitive",
289 args: []string{"install", "--PROXY", proxyURL, "six"},
290 wantRest: []string{"install", "--PROXY", proxyURL, "six"},
291 },
292 {
293 name: "options sharing only --pr are kept",
294 args: []string{"install", "--pre", "--prefix", "/opt/py", "six"},
295 wantRest: []string{"install", "--pre", "--prefix", "/opt/py", "six"},
296 },
297 {
298 name: "the last one wins",
299 args: []string{"--proxy", "http://first:1", "install", "--proxy=" + proxyURL, "six"},
300 wantValue: proxyURL,
301 wantRest: []string{"install", "six"},
302 wantOK: true,
303 },
304 {
305 name: "one after -- counts",
306 args: []string{"install", "--", "--proxy", proxyURL},
307 wantValue: proxyURL,
308 wantRest: []string{"install", "--"},
309 wantOK: true,
310 },
311 {
312 name: "the last one wins across --",
313 args: []string{"install", "--proxy", "http://first:1", "--", "--proxy", proxyURL},
314 wantValue: proxyURL,
315 wantRest: []string{"install", "--"},
316 wantOK: true,
317 },
318 {
319 name: "-- as an abbreviated general option's value",
320 args: []string{"install", "--cache-d", "--", "--proxy", proxyURL, "six"},
321 wantValue: proxyURL,
322 wantRest: []string{"install", "--cache-d", "--", "six"},
323 wantOK: true,
324 },
325 {
326 name: "-- as an abbreviated general option's value before the command",
327 args: []string{"--cache-d", "--", "install", "--proxy", proxyURL, "six"},
328 wantValue: proxyURL,
329 wantRest: []string{"--cache-d", "--", "install", "six"},
330 wantOK: true,
331 },
332 {
333 name: "-- as install -t's value",
334 args: []string{"install", "-t", "--", "--proxy", proxyURL, "six"},
335 wantValue: proxyURL,
336 wantRest: []string{"install", "-t", "--", "six"},
337 wantOK: true,
338 },
339 {
340 name: "-- as download -d's value",
341 args: []string{"download", "-d", "--", "--proxy", proxyURL, "six"},
342 wantValue: proxyURL,
343 wantRest: []string{"download", "-d", "--", "six"},
344 wantOK: true,
345 },
346 {
347 name: "-- as install --root's value",
348 args: []string{"install", "--root", "--", "--proxy", proxyURL, "six"},
349 wantValue: proxyURL,
350 wantRest: []string{"install", "--root", "--", "six"},
351 wantOK: true,
352 },
353 {
354 name: "another option's value is not an option",
355 args: []string{"--cache-dir", "--proxy", "install", "six"},
356 wantRest: []string{"--cache-dir", "--proxy", "install", "six"},
357 },
358 {
359 name: "-- as another option's value does not end the options",
360 args: []string{"install", "--log", "--", "--proxy", proxyURL, "six"},
361 wantValue: proxyURL,
362 wantRest: []string{"install", "--log", "--", "six"},
363 wantOK: true,
364 },
365 {
366 name: "the value is taken even when it looks like an option",
367 args: []string{"install", "--proxy", "--isolated", "six"},
368 wantValue: "--isolated",
369 wantRest: []string{"install", "six"},
370 wantOK: true,
371 },
372 {
373 name: "an empty value",
374 args: []string{"install", "--proxy", "", "six"},
375 wantRest: []string{"install", "six"},
376 wantOK: true,
377 },
378 {
379 name: "an empty value with =",
380 args: []string{"install", "--proxy=", "six"},
381 wantRest: []string{"install", "six"},
382 wantOK: true,
383 },
384 {
385 name: "a value option missing its value",
386 args: []string{"install", "--cache-dir"},
387 wantRest: []string{"install", "--cache-dir"},
388 },
389 {
390 name: "a missing value is left for pip to reject",
391 args: []string{"install", "six", "--proxy"},
392 wantRest: []string{"install", "six", "--proxy"},
393 },
394 {
395 name: "without --proxy",
396 args: []string{"install", "six"},
397 wantRest: []string{"install", "six"},
398 },
399 }
400 for _, tt := range tests {
401 t.Run(tt.name, func(t *testing.T) {
402 t.Parallel()
403 value, rest, ok := pipProxyArg(tt.args)
404 assert.Equal(t, tt.wantValue, value)
405 assert.Equal(t, tt.wantRest, rest)
406 assert.Equal(t, tt.wantOK, ok)
407 })
408 }
411func TestOnlyPipTakesOutItsProxyOption(t *testing.T) {
412 t.Parallel()
413 args := []string{"install", "--proxy", "http://corp:3128", "six"}
414 pip, ok := Pip().(argsRewriter)
415 require.True(t, ok)
416 assert.Equal(t, []string{"install", "six"}, pip.rewriteArgs(args))
417 pipenv, ok := Pipenv().(argsRewriter)
418 require.True(t, ok)
419 assert.Equal(t, args, pipenv.rewriteArgs(args), "pipenv has no --proxy option")
422func TestPipCommandSkipsOptionValues(t *testing.T) {
423 t.Parallel()
424 assert.Equal(t, "install", pipCommand([]string{"--cache-dir", "/tmp/c", "install", "six"}))
425 assert.Empty(t, pipCommand([]string{"--isolated"}))
428func TestPlainHTTPProxyResolvers(t *testing.T) {
429 t.Parallel()
430 tests := []struct {
431 name string
432 env []string
433 want upstreamProxy
434 wantOK bool
435 }{
436 {
437 name: "yarn ignores YARN_HTTP_PROXY for plain HTTP",
438 env: []string{"YARN_HTTP_PROXY=http://berry:7", "HTTP_PROXY=http://env:1"},
439 },
440 {
441 name: "yarn classic npm_config_proxy",
442 env: []string{"npm_config_proxy=http://proxy:2"},
443 want: upstreamProxy{Source: "npm_config_proxy", URL: "http://proxy:2"},
444 wantOK: true,
445 },
446 {
447 name: "yarn HTTPS settings leave plain HTTP to the default",
448 env: []string{"YARN_HTTPS_PROXY=http://yarn:4", "npm_config_https_proxy=http://https-proxy:3", "HTTP_PROXY=http://env:1"},
449 },
450 {
451 name: "yarn classic proxy=false leaves plain HTTP to the default",
452 env: []string{"npm_config_proxy=false"},
453 },
454 {
455 name: "yarn classic https-proxy=false leaves plain HTTP on proxy",
456 env: []string{"npm_config_https_proxy=false", "npm_config_proxy=http://proxy:2"},
457 want: upstreamProxy{Source: "npm_config_proxy", URL: "http://proxy:2"},
458 wantOK: true,
459 },
460 }
461 for _, tt := range tests {
462 t.Run(tt.name, func(t *testing.T) {
463 t.Parallel()
464 resolver, ok := Yarn().(plainHTTPProxyResolver)
465 require.True(t, ok)
466 got, gotOK, err := resolver.resolvePlainHTTPProxy(tt.env, t.TempDir())
467 require.NoError(t, err)
468 assert.Equal(t, tt.wantOK, gotOK)
469 assert.Equal(t, tt.want, got)
470 })
471 }
474func TestManagersWithoutOwnProxySettingHaveNoResolver(t *testing.T) {
475 t.Parallel()
476 // These read no proxy variable of their own, so the inspection proxy's
477 // default, glab's HTTPS_PROXY and HTTP_PROXY, applies to them.
478 for _, m := range []PackageManager{Poetry(), Uv(), Twine(), Gem(), Bundle(), Golang()} {
479 _, ok := m.(upstreamProxyResolver)
480 assert.False(t, ok, m.Name())
481 assert.Nil(t, OwnProxyVariables(m), m.Name())
482 assert.Nil(t, OwnProxyConfigFiles(m), m.Name())
483 }
486func TestPlainHTTPUsesHTTPSProxy(t *testing.T) {
487 t.Parallel()
488 assert.True(t, PlainHTTPUsesHTTPSProxy(NPM()))
489 assert.True(t, PlainHTTPUsesHTTPSProxy(Pnpm()))
490 assert.False(t, PlainHTTPUsesHTTPSProxy(Yarn()))
491 assert.False(t, PlainHTTPUsesHTTPSProxy(Pip()))
492 assert.False(t, PlainHTTPUsesHTTPSProxy(Pipenv()))
493 assert.False(t, PlainHTTPUsesHTTPSProxy(Cargo()))
494 assert.False(t, PlainHTTPUsesHTTPSProxy(Poetry()))
495 assert.False(t, PlainHTTPUsesHTTPSProxy(Maven()))
498func TestOwnProxyVariablesAreRead(t *testing.T) {
499 t.Parallel()
500 for _, m := range []PackageManager{NPM(), Pnpm(), Yarn(), Pip(), Pipenv(), Cargo()} {
501 resolver, ok := m.(upstreamProxyResolver)
502 require.True(t, ok, m.Name())
503 vars := OwnProxyVariables(m)
504 require.NotEmpty(t, vars, m.Name())
505 for _, v := range vars {
506 // No HOME or PATH, so no config file can supply the proxy instead.
507 env := []string{v + "=http://own:1"}
508 https, _, err := resolver.resolveUpstreamProxy(env, t.TempDir(), nil)
509 require.NoError(t, err)
510 plain := https
511 if r, ok := m.(plainHTTPProxyResolver); ok {
512 plain, _, err = r.resolvePlainHTTPProxy(env, t.TempDir())
513 require.NoError(t, err)
514 }
515 assert.True(t, https.Source == v || plain.Source == v, "%s lists %s but reads it for neither scheme", m.Name(), v)
516 }
517 }
520func TestOwnProxyConfigFilesAreRead(t *testing.T) {
521 t.Parallel()
522 // Each file the help lists, written with a proxy where the manager looks
523 // for it. root is the temp directory; HOME is root/home and the manager
524 // runs in root/proj.
525 berry := map[string]string{"proj/package.json": `{"packageManager": "yarn@4.9.2"}`}
526 mavenUserHome := "MAVEN_OPTS=-Duser.home=$ROOT/home"
527 mavenSettings := mavenSettingsXML("<proxy><id>own</id><host>own</host><port>1</port></proxy>")
528 gradleUserHome := "GRADLE_USER_HOME=$ROOT/guh"
529 gradleProperties := "systemProp.https.proxyHost=own\nsystemProp.https.proxyPort=1\n"
530 fixtures := map[string]struct {
531 path, content, key string
532 env []string
533 extra map[string]string
534 }{
535 "~/.npmrc": {path: "home/.npmrc", content: "https-proxy=http://own:1\n", key: "https-proxy"},
536 "$PREFIX/etc/npmrc": {path: "prefix/etc/npmrc", content: "https-proxy=http://own:1\n", key: "https-proxy", env: []string{"PREFIX=$ROOT/prefix"}},
537 "~/.yarnrc": {path: "home/.yarnrc", content: "https-proxy \"http://own:1\"\n", key: "https-proxy"},
538 "$PREFIX/etc/yarnrc": {path: "prefix/etc/yarnrc", content: "https-proxy \"http://own:1\"\n", key: "https-proxy", env: []string{"PREFIX=$ROOT/prefix"}},
539 "~/.yarnrc.yml": {path: "home/.yarnrc.yml", content: "httpsProxy: http://own:1\n", key: "httpsProxy", extra: berry},
540 "pip.conf": {path: "home/.config/pip/pip.conf", content: "[global]\nproxy = http://own:1\n", key: "global.proxy"},
541 "$PIP_CONFIG_FILE": {path: "custom/pip.conf", content: "[global]\nproxy = http://own:1\n", key: "global.proxy", env: []string{"PIP_CONFIG_FILE=$ROOT/custom/pip.conf"}},
542 "$CARGO_HOME/config.toml": {path: "cargo/config.toml", content: "[http]\nproxy = \"http://own:1\"\n", key: "http.proxy", env: []string{"CARGO_HOME=$ROOT/cargo"}},
543 "~/.gitconfig": {path: "home/.gitconfig", content: "[http]\n\tproxy = http://own:1\n", key: "http.proxy"},
544 "~/.m2/settings.xml": {path: "home/.m2/settings.xml", content: mavenSettings, key: "proxy own", env: []string{mavenUserHome}},
545 "${maven.home}/conf/settings.xml": {path: "maven/conf/settings.xml", content: mavenSettings, key: "proxy own", env: []string{mavenUserHome, "PATH=$ROOT/maven/bin"}, extra: mavenInstall("maven", map[string]string{})},
546 "$GRADLE_USER_HOME/gradle.properties": {path: "guh/gradle.properties", content: gradleProperties, key: "systemProp.https.proxyHost", env: []string{gradleUserHome}},
547 "the Gradle installation's gradle.properties": {path: "gradle/gradle.properties", content: gradleProperties, key: "systemProp.https.proxyHost", env: []string{gradleUserHome, "PATH=$ROOT/gradle/bin"}, extra: map[string]string{"gradle/bin/gradle": ""}},
548 }
549 managers := []PackageManager{NPM(), Yarn(), Cargo(), Maven(), Gradle()}
550 if runtime.GOOS != "windows" {
551 managers = append(managers, Pip(), Pipenv())
552 }
553 for _, m := range managers {
554 files := OwnProxyConfigFiles(m)
555 require.NotEmpty(t, files, m.Name())
556 var cases []configCase
557 for _, f := range files {
558 fixture, ok := fixtures[f]
559 require.True(t, ok, "no fixture for %s, which the %s help lists", f, m.Name())
560 written := map[string]string{fixture.path: fixture.content}
561 maps.Copy(written, fixture.extra)
562 cases = append(cases, configCase{
563 name: f,
564 files: written,
565 env: fixture.env,
566 args: []string{"install"},
567 want: upstreamProxy{Source: "$ROOT/" + fixture.path + " (" + fixture.key + ")", URL: "http://own:1"},
568 wantOK: true,
569 })
570 }
571 t.Run(m.Name(), func(t *testing.T) {
572 t.Parallel()
573 runConfigCases(t, m, cases)
574 })
575 }
576 assert.Nil(t, OwnProxyConfigFiles(Pnpm()))
579func TestUpstreamProxyFunc(t *testing.T) {
580 t.Parallel()
581 pick, err := upstreamProxy{URL: "corp:3128", NoProxy: "internal.example"}.proxyFunc()
582 require.NoError(t, err)
584 got, err := pick(httptest.NewRequest(http.MethodGet, "https://registry.npmjs.org/left-pad", http.NoBody))
585 require.NoError(t, err)
586 assert.Equal(t, "http://corp:3128", got.String())
588 got, err = pick(httptest.NewRequest(http.MethodGet, "https://npm.internal.example/left-pad", http.NoBody))
589 require.NoError(t, err)
590 assert.Nil(t, got, "a NoProxy host is reached directly")
593func TestUpstreamProxyFuncAcceptsSupportedSchemes(t *testing.T) {
594 t.Parallel()
595 for _, raw := range []string{"https://corp:443", "socks5://corp:1080", "socks5h://corp:1080"} {
596 _, err := upstreamProxy{URL: raw}.proxyFunc()
597 assert.NoError(t, err, raw)
598 }
601func TestUpstreamProxyFuncRejectsBadURLWithoutLeakingIt(t *testing.T) {
602 t.Parallel()
603 tests := []struct {
604 raw string
605 wantErr string
606 }{
607 {raw: "http://user:s3cret@[::1", wantErr: "not a valid proxy URL"},
608 {raw: "http://user:s3cret@", wantErr: "not a valid proxy URL"},
609 {raw: "http://", wantErr: "not a valid proxy URL"},
610 {raw: ":3128", wantErr: "not a valid proxy URL"},
611 {raw: "ftp://user:s3cret@corp:21", wantErr: "unsupported proxy scheme; use http, https, socks5 or socks5h"},
612 }
613 for _, tt := range tests {
614 _, err := upstreamProxy{URL: tt.raw}.proxyFunc()
615 require.EqualError(t, err, tt.wantErr, tt.raw)
616 }
619func TestRunRejectsInvalidManagerProxy(t *testing.T) {
620 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
621 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
622 t.Setenv("PIP_PROXY", "ftp://corp:21")
623 ios, _, _, _ := cmdtest.TestIOStreams()
624 exec := &fakeExecutor{}
626 err := Run(t.Context(), Pip(), RunOptions{
627 IO: ios,
628 Executor: exec,
629 BaseDir: t.TempDir(),
630 ProjectID: "group/proj",
631 Args: []string{"install", "six"},
632 })
633 require.ErrorContains(t, err, "invalid outbound proxy in PIP_PROXY")
634 assert.Nil(t, exec.env, "the package manager must not run")
637func TestRunRejectsInvalidPipProxyOptionWithoutLeakingIt(t *testing.T) {
638 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
639 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
640 ios, _, _, _ := cmdtest.TestIOStreams()
641 exec := &fakeExecutor{}
643 err := Run(t.Context(), Pip(), RunOptions{
644 IO: ios,
645 Executor: exec,
646 BaseDir: t.TempDir(),
647 ProjectID: "group/proj",
648 Args: []string{"install", "--proxy", "ftp://alice:s3cret@corp:21", "six"},
649 })
650 require.ErrorContains(t, err, "invalid outbound proxy in --proxy")
651 assert.NotContains(t, err.Error(), "s3cret")
652 assert.Nil(t, exec.env, "the package manager must not run")
655// refusingProxy records the target of each CONNECT and refuses it, so a test
656// can see where upstream traffic was sent without reaching the internet.
657type refusingProxy struct {
658 mu sync.Mutex
659 targets []string
662func (rp *refusingProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
663 rp.mu.Lock()
664 rp.targets = append(rp.targets, r.Host)
665 rp.mu.Unlock()
666 w.WriteHeader(http.StatusForbidden)
669func (rp *refusingProxy) Targets() []string {
670 rp.mu.Lock()
671 defer rp.mu.Unlock()
672 return append([]string(nil), rp.targets...)
675// isolateUserConfig points HOME and the other places the resolvers find user
676// and global config at empty directories, so the developer's own files
677// cannot change where a Run test sends registry traffic.
678func isolateUserConfig(t *testing.T, home string) {
679 t.Helper()
680 t.Setenv("HOME", home)
681 t.Setenv("PREFIX", t.TempDir())
682 t.Setenv("GRADLE_USER_HOME", t.TempDir())
683 t.Setenv("PATH", t.TempDir())
684 for _, k := range []string{"XDG_CONFIG_HOME", "XDG_CONFIG_DIRS", "CARGO_HOME", "PIP_CONFIG_FILE", "NPM_CONFIG_USERCONFIG", "npm_config_userconfig", "YARN_USERCONFIG", "yarn_userconfig", "MAVEN_ARGS", "MAVEN_OPTS"} {
685 t.Setenv(k, "")
686 }
689// clearProxyEnv leaves glab's environment without an outbound proxy of any
690// kind. CARGO_HTTP_PROXY and YARN_HTTPS_PROXY are unset rather than emptied,
691// because an empty one makes cargo or Yarn Berry connect directly.
692func clearProxyEnv(t *testing.T) {
693 t.Helper()
694 for _, k := range []string{
695 "HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy",
696 "npm_config_https_proxy", "npm_config_proxy", "YARN_HTTPS_PROXY", "YARN_HTTP_PROXY", "YARN_PROXY",
697 "PIP_PROXY", "CARGO_HTTP_PROXY",
698 } {
699 t.Setenv(k, "")
700 }
701 require.NoError(t, os.Unsetenv("CARGO_HTTP_PROXY"))
702 require.NoError(t, os.Unsetenv("YARN_HTTPS_PROXY"))
705func TestRunSendsRegistryTrafficThroughManagersOwnProxy(t *testing.T) {
706 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
707 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
708 corporate := &refusingProxy{}
709 server := httptest.NewServer(corporate)
710 t.Cleanup(server.Close)
711 corporateURL, err := url.Parse(server.URL)
712 require.NoError(t, err)
713 isolateUserConfig(t, t.TempDir())
714 t.Setenv("HTTPS_PROXY", "")
715 t.Setenv("NO_PROXY", "")
716 t.Setenv("npm_config_https_proxy", corporateURL.String())
718 ios, _, _, _ := cmdtest.TestIOStreams()
719 // The fetch fails because the corporate proxy refuses the tunnel; the test
720 // only checks where the inspection proxy sent it.
721 _ = Run(t.Context(), NPM(), RunOptions{
722 IO: ios,
723 Executor: &curlingExecutor{path: "/left-pad/-/left-pad-1.3.0.tgz"},
724 BaseDir: t.TempDir(),
725 ProjectID: "group/proj",
726 Args: []string{"install", "left-pad"},
727 })
729 assert.Equal(t, []string{"registry.npmjs.org:443"}, corporate.Targets())
732// gettingExecutor ignores the package-manager binary and GETs url through the
733// proxy it is handed, as a package manager does: HTTP_PROXY for a plain-HTTP
734// url, else HTTPS_PROXY, trusting the proxy CA named by caVar. A non-nil args
735// receives the arguments the package manager was run with.
736type gettingExecutor struct {
737 url, caVar string
738 args *[]string
741func (gettingExecutor) LookPath(file string) (string, error) { return "/bin/" + file, nil }
743func (e gettingExecutor) ExecWithIO(ctx context.Context, _ string, args []string, env []string, _ io.Reader, _, _ io.Writer) error {
744 if e.args != nil {
745 *e.args = args
746 }
747 transport := &http.Transport{}
748 proxyVar := "HTTP_PROXY"
749 if strings.HasPrefix(e.url, "https:") {
750 proxyVar = "HTTPS_PROXY"
751 pemBytes, err := os.ReadFile(lookupEnv(env, e.caVar))
752 if err != nil {
753 return err
754 }
755 pool := x509.NewCertPool()
756 if !pool.AppendCertsFromPEM(pemBytes) {
757 return fmt.Errorf("no proxy CA in %s", e.caVar)
758 }
759 transport.TLSClientConfig = &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}
760 }
761 proxyURL, err := url.Parse(lookupEnv(env, proxyVar))
762 if err != nil {
763 return err
764 }
765 transport.Proxy = http.ProxyURL(proxyURL)
766 client := &http.Client{Transport: transport}
767 req, err := http.NewRequestWithContext(ctx, http.MethodGet, e.url, http.NoBody)
768 if err != nil {
769 return err
770 }
771 resp, err := client.Do(req)
772 if err != nil {
773 return err
774 }
775 return resp.Body.Close()
778func TestRunSendsPlainHTTPThroughManagersPlainHTTPProxy(t *testing.T) {
779 tests := []struct {
780 name string
781 manager PackageManager
782 // env maps a variable to the proxy it points at: "want" must carry the
783 // plain-HTTP request and "other" must not see it.
784 env map[string]string
785 }{
786 {
787 name: "yarn npm_config_proxy, not YARN_HTTPS_PROXY",
788 manager: Yarn(),
789 env: map[string]string{"YARN_HTTPS_PROXY": "other", "npm_config_proxy": "want"},
790 },
791 {
792 name: "yarn without a plain-HTTP setting falls back to HTTP_PROXY",
793 manager: Yarn(),
794 env: map[string]string{"YARN_HTTPS_PROXY": "other", "HTTP_PROXY": "want"},
795 },
796 {
797 name: "yarn YARN_HTTP_PROXY does not carry plain HTTP",
798 manager: Yarn(),
799 env: map[string]string{"YARN_HTTP_PROXY": "other", "HTTP_PROXY": "want"},
800 },
801 {
802 name: "npm HTTPS_PROXY carries plain HTTP ahead of HTTP_PROXY",
803 manager: NPM(),
804 env: map[string]string{"HTTPS_PROXY": "want", "HTTP_PROXY": "other"},
805 },
806 {
807 name: "pnpm HTTPS_PROXY carries plain HTTP ahead of HTTP_PROXY",
808 manager: Pnpm(),
809 env: map[string]string{"HTTPS_PROXY": "want", "HTTP_PROXY": "other"},
810 },
811 {
812 name: "npm https-proxy carries plain HTTP too",
813 manager: NPM(),
814 env: map[string]string{"npm_config_https_proxy": "want", "HTTP_PROXY": "other"},
815 },
816 }
817 for _, tt := range tests {
818 t.Run(tt.name, func(t *testing.T) {
819 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
820 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
821 isolateUserConfig(t, t.TempDir())
822 clearProxyEnv(t)
823 proxies := map[string]*refusingProxy{"want": {}, "other": {}}
824 for k, which := range tt.env {
825 server := httptest.NewServer(proxies[which])
826 t.Cleanup(server.Close)
827 t.Setenv(k, server.URL)
828 }
830 ios, _, _, _ := cmdtest.TestIOStreams()
831 _ = Run(t.Context(), tt.manager, RunOptions{
832 IO: ios,
833 Executor: gettingExecutor{url: "http://registry.example/left-pad"},
834 BaseDir: t.TempDir(),
835 ProjectID: "group/proj",
836 Args: []string{"install", "left-pad"},
837 })
839 assert.Equal(t, []string{"registry.example"}, proxies["want"].Targets())
840 assert.Empty(t, proxies["other"].Targets())
841 })
842 }
845func TestRunSendsRegistryTrafficThroughUserConfigNotProjectConfig(t *testing.T) {
846 berry := `{"packageManager": "yarn@4.9.2"}`
847 tests := []struct {
848 name string
849 manager PackageManager
850 url string
851 caVar string
852 // files maps a path under the temp root, where HOME is root/home and
853 // the manager runs in dir (default root/proj), to its content. links
854 // maps a symlink to its target. $USER and $PROJECT stand for the two
855 // proxies: only $USER may see the request.
856 files map[string]string
857 links map[string]string
858 env []string
859 dir string
860 }{
861 {
862 name: "npm",
863 manager: NPM(),
864 url: "https://registry.invalid/left-pad",
865 caVar: "NODE_EXTRA_CA_CERTS",
866 files: map[string]string{"home/.npmrc": "https-proxy=$USER\n", "proj/package.json": "{}", "proj/.npmrc": "https-proxy=$PROJECT\n"},
867 },
868 {
869 name: "yarn classic",
870 manager: Yarn(),
871 url: "https://registry.invalid/left-pad",
872 caVar: "NODE_EXTRA_CA_CERTS",
873 files: map[string]string{"home/.yarnrc": "https-proxy \"$USER\"\n", "proj/package.json": "{}", "proj/.yarnrc": "https-proxy \"$PROJECT\"\n", "proj/.npmrc": "https-proxy=$PROJECT\n"},
874 },
875 {
876 name: "yarn berry",
877 manager: Yarn(),
878 url: "https://registry.invalid/left-pad",
879 caVar: "NODE_EXTRA_CA_CERTS",
880 files: map[string]string{"home/.yarnrc.yml": "httpsProxy: $USER\n", "proj/package.json": berry, "proj/.yarnrc.yml": "httpsProxy: $PROJECT\n"},
881 },
882 {
883 name: "pip",
884 manager: Pip(),
885 url: "https://registry.invalid/simple/six/",
886 caVar: "PIP_CERT",
887 files: map[string]string{
888 "home/.config/pip/pip.conf": "[global]\nproxy = $USER\n",
889 "proj/.venv/bin/pip": "",
890 "proj/.venv/pyvenv.cfg": "",
891 "proj/.venv/pip.conf": "[global]\nproxy = $PROJECT\n",
892 },
893 env: []string{"PATH=$ROOT/proj/.venv/bin"},
894 },
895 {
896 name: "cargo",
897 manager: Cargo(),
898 url: "https://registry.invalid/config.json",
899 caVar: "CARGO_HTTP_CAINFO",
900 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"$USER\"\n", "proj/.cargo/config.toml": "[http]\nproxy = \"$PROJECT\"\n"},
901 },
902 {
903 name: "cargo with CARGO_HOME inside the project",
904 manager: Cargo(),
905 url: "https://registry.invalid/config.json",
906 caVar: "CARGO_HTTP_CAINFO",
907 files: map[string]string{"proj/.cargo/config.toml": "[http]\nproxy = \"$PROJECT\"\n", "home/.gitconfig": "[http]\n\tproxy = $USER\n"},
908 env: []string{"CARGO_HOME=$ROOT/proj/.cargo"},
909 },
910 {
911 name: "npm with NPM_CONFIG_USERCONFIG inside the project",
912 manager: NPM(),
913 url: "https://registry.invalid/left-pad",
914 caVar: "NODE_EXTRA_CA_CERTS",
915 files: map[string]string{"proj/.npmrc-ci": "https-proxy=$PROJECT\n"},
916 env: []string{"NPM_CONFIG_USERCONFIG=$ROOT/proj/.npmrc-ci", "HTTPS_PROXY=$USER"},
917 },
918 {
919 name: "npm with PREFIX inside the project",
920 manager: NPM(),
921 url: "https://registry.invalid/left-pad",
922 caVar: "NODE_EXTRA_CA_CERTS",
923 files: map[string]string{"proj/tools/etc/npmrc": "https-proxy=$PROJECT\n", "home/.npmrc": "proxy=$USER\n"},
924 env: []string{"PREFIX=$ROOT/proj/tools"},
925 },
926 {
927 name: "npm with a user file that links into the project",
928 manager: NPM(),
929 url: "https://registry.invalid/left-pad",
930 caVar: "NODE_EXTRA_CA_CERTS",
931 files: map[string]string{"proj/.npmrc": "https-proxy=$PROJECT\n"},
932 links: map[string]string{"home/.npmrc": "proj/.npmrc"},
933 env: []string{"HTTPS_PROXY=$USER"},
934 },
935 {
936 name: "npm with HOME inside the project",
937 manager: NPM(),
938 url: "https://registry.invalid/left-pad",
939 caVar: "NODE_EXTRA_CA_CERTS",
940 files: map[string]string{"proj/home/.npmrc": "https-proxy=$PROJECT\n"},
941 env: []string{"HOME=$ROOT/proj/home", "HTTPS_PROXY=$USER"},
942 },
943 {
944 name: "pip with PIP_CONFIG_FILE inside the project",
945 manager: Pip(),
946 url: "https://registry.invalid/simple/six/",
947 caVar: "PIP_CERT",
948 files: map[string]string{"proj/pip.conf": "[global]\nproxy = $PROJECT\n"},
949 env: []string{"PIP_CONFIG_FILE=$ROOT/proj/pip.conf", "HTTPS_PROXY=$USER"},
950 },
951 {
952 name: "pip from a subdirectory of the git work tree",
953 manager: Pip(),
954 url: "https://registry.invalid/simple/six/",
955 caVar: "PIP_CERT",
956 files: map[string]string{
957 "proj/.git/HEAD": "",
958 "home/.config/pip/pip.conf": "[global]\nproxy = $USER\n",
959 "proj/.venv/bin/pip": "",
960 "proj/.venv/pyvenv.cfg": "",
961 "proj/.venv/pip.conf": "[global]\nproxy = $PROJECT\n",
962 },
963 env: []string{"PATH=$ROOT/proj/.venv/bin"},
964 dir: "proj/sub",
965 },
966 }
967 for _, tt := range tests {
968 t.Run(tt.name, func(t *testing.T) {
969 if tt.manager.Name() == "pip" && runtime.GOOS == "windows" {
970 t.Skip("pip's user config lives under %APPDATA% on Windows")
971 }
972 if len(tt.links) > 0 && runtime.GOOS == "windows" {
973 t.Skip("symlinks need privileges on Windows")
974 }
975 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
976 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
977 user, project := &refusingProxy{}, &refusingProxy{}
978 userServer, projectServer := httptest.NewServer(user), httptest.NewServer(project)
979 t.Cleanup(userServer.Close)
980 t.Cleanup(projectServer.Close)
981 root := t.TempDir()
982 expand := strings.NewReplacer("$ROOT", root, "$USER", userServer.URL, "$PROJECT", projectServer.URL).Replace
983 writeConfigFiles(t, root, tt.files, expand)
984 for link, target := range tt.links {
985 require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(root, link)), 0o755))
986 require.NoError(t, os.Symlink(filepath.Join(root, target), filepath.Join(root, link)))
987 }
988 dir := filepath.Join(root, cmp.Or(tt.dir, "proj"))
989 require.NoError(t, os.MkdirAll(dir, 0o755))
990 isolateUserConfig(t, filepath.Join(root, "home"))
991 clearProxyEnv(t)
992 for _, e := range tt.env {
993 k, v, _ := strings.Cut(expand(e), "=")
994 t.Setenv(k, v)
995 }
997 ios, _, _, _ := cmdtest.TestIOStreams()
998 _ = Run(t.Context(), tt.manager, RunOptions{
999 IO: ios,
1000 Executor: gettingExecutor{url: tt.url, caVar: tt.caVar},
1001 BaseDir: dir,
1002 ProjectID: "group/proj",
1003 Args: []string{"install", "left-pad"},
1004 })
1006 assert.NotEmpty(t, user.Targets(), "the proxy from the user's config must carry the request")
1007 assert.Empty(t, project.Targets(), "a proxy from a file in the project must never see the request")
1008 })
1012func TestRunConnectsDirectlyWhenTheManagerDisablesItsProxy(t *testing.T) {
1013 tests := []struct {
1014 name string
1015 manager PackageManager
1016 caVar string
1017 files map[string]string
1018 env []string
1019 // wantDefault is set for the control case: with no setting of the
1020 // manager's own, HTTPS_PROXY must carry the request.
1021 wantDefault bool
1022 }{
1024 name: "cargo with an empty CARGO_HTTP_PROXY",
1025 manager: Cargo(),
1026 caVar: "CARGO_HTTP_CAINFO",
1027 env: []string{"CARGO_HTTP_PROXY="},
1028 },
1030 name: "cargo with an empty http.proxy",
1031 manager: Cargo(),
1032 caVar: "CARGO_HTTP_CAINFO",
1033 files: map[string]string{"home/.cargo/config.toml": "[http]\nproxy = \"\"\n"},
1034 },
1036 name: "yarn classic with https-proxy false",
1037 manager: Yarn(),
1038 caVar: "NODE_EXTRA_CA_CERTS",
1039 files: map[string]string{"home/.yarnrc": "https-proxy false\n"},
1040 },
1042 name: "yarn berry with an empty httpsProxy",
1043 manager: Yarn(),
1044 caVar: "NODE_EXTRA_CA_CERTS",
1045 files: map[string]string{"proj/package.json": `{"packageManager": "yarn@4.9.2"}`, "home/.yarnrc.yml": "httpsProxy: \"\"\n"},
1046 },
1048 name: "cargo without a proxy of its own",
1049 manager: Cargo(),
1050 caVar: "CARGO_HTTP_CAINFO",
1051 wantDefault: true,
1052 },
1054 for _, tt := range tests {
1055 t.Run(tt.name, func(t *testing.T) {
1056 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
1057 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
1058 glabProxy := &refusingProxy{}
1059 server := httptest.NewServer(glabProxy)
1060 t.Cleanup(server.Close)
1061 root := t.TempDir()
1062 writeConfigFiles(t, root, tt.files, func(s string) string { return s })
1063 require.NoError(t, os.MkdirAll(filepath.Join(root, "proj"), 0o755))
1064 isolateUserConfig(t, filepath.Join(root, "home"))
1065 clearProxyEnv(t)
1066 t.Setenv("HTTPS_PROXY", server.URL)
1067 for _, e := range tt.env {
1068 k, v, _ := strings.Cut(e, "=")
1069 t.Setenv(k, v)
1072 ios, _, _, _ := cmdtest.TestIOStreams()
1073 // registry.invalid never resolves, so a direct request fails
1074 // without leaving the machine.
1075 err := Run(t.Context(), tt.manager, RunOptions{
1076 IO: ios,
1077 Executor: gettingExecutor{url: "https://registry.invalid/left-pad", caVar: tt.caVar},
1078 BaseDir: filepath.Join(root, "proj"),
1079 ProjectID: "group/proj",
1080 Args: []string{"install", "left-pad"},
1081 })
1083 _, ran := errors.AsType[*ExitError](err)
1084 require.True(t, ran, "the package manager must run: %v", err)
1085 if tt.wantDefault {
1086 assert.Equal(t, []string{"registry.invalid:443"}, glabProxy.Targets())
1087 } else {
1088 assert.Empty(t, glabProxy.Targets(), "a direct setting must not fall back to HTTPS_PROXY")
1090 })
1094func TestRunSendsPipTrafficThroughItsProxyOption(t *testing.T) {
1095 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
1096 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
1097 option, environ := &refusingProxy{}, &refusingProxy{}
1098 optionServer, environServer := httptest.NewServer(option), httptest.NewServer(environ)
1099 t.Cleanup(optionServer.Close)
1100 t.Cleanup(environServer.Close)
1101 isolateUserConfig(t, t.TempDir())
1102 clearProxyEnv(t)
1103 t.Setenv("PIP_PROXY", environServer.URL)
1104 optionURL := strings.Replace(optionServer.URL, "http://", "http://alice:s3cret@", 1)
1105 dir := t.TempDir()
1106 var args []string
1107 debugLog := captureDebugLog(t)
1109 ios, _, _, stderr := cmdtest.TestIOStreams()
1110 _ = Run(t.Context(), Pip(), RunOptions{
1111 IO: ios,
1112 Executor: gettingExecutor{url: "https://registry.invalid/simple/six/", caVar: "PIP_CERT", args: &args},
1113 BaseDir: dir,
1114 ProjectID: "group/proj",
1115 Args: []string{"install", "--proxy", optionURL, "six"},
1116 })
1118 assert.Equal(t, []string{"install", "six"}, args, "pip must not get --proxy, which outranks the PIP_PROXY glab sets")
1119 assert.Equal(t, []string{"registry.invalid:443"}, option.Targets())
1120 assert.Empty(t, environ.Targets(), "--proxy outranks PIP_PROXY")
1121 log, err := cilog.Load(dir)
1122 require.NoError(t, err)
1123 assert.Equal(t, "pip install six", log.Session.Command)
1124 assert.NotContains(t, stderr.String(), "s3cret")
1125 assert.Contains(t, debugLog.String(), "reaching https registries through the outbound proxy from --proxy")
1126 assert.NotContains(t, debugLog.String(), "s3cret")
1129func TestRunKeepsTheDefaultUpstreamForAnEmptyPipProxyOption(t *testing.T) {
1130 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
1131 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
1132 pipProxy, httpsProxy := &refusingProxy{}, &refusingProxy{}
1133 pipProxyServer, httpsProxyServer := httptest.NewServer(pipProxy), httptest.NewServer(httpsProxy)
1134 t.Cleanup(pipProxyServer.Close)
1135 t.Cleanup(httpsProxyServer.Close)
1136 isolateUserConfig(t, t.TempDir())
1137 clearProxyEnv(t)
1138 t.Setenv("PIP_PROXY", pipProxyServer.URL)
1139 t.Setenv("HTTPS_PROXY", httpsProxyServer.URL)
1140 var args []string
1141 debugLog := captureDebugLog(t)
1143 ios, _, _, _ := cmdtest.TestIOStreams()
1144 _ = Run(t.Context(), Pip(), RunOptions{
1145 IO: ios,
1146 Executor: gettingExecutor{url: "https://registry.invalid/simple/six/", caVar: "PIP_CERT", args: &args},
1147 BaseDir: t.TempDir(),
1148 ProjectID: "group/proj",
1149 Args: []string{"install", "--proxy=", "six"},
1150 })
1152 assert.Equal(t, []string{"install", "six"}, args, "an empty --proxy would make pip 26.3 bypass glab's proxy")
1153 assert.Equal(t, []string{"registry.invalid:443"}, httpsProxy.Targets())
1154 assert.Empty(t, pipProxy.Targets(), "an empty --proxy clears PIP_PROXY")
1155 assert.Contains(t, debugLog.String(), "pip's --proxy is empty, so glab keeps its default outbound proxy")
1158// argsSettingPip is pip as a manager that also reads its arguments, as Maven
1159// does, to check that SetArgs sees what the binary runs with.
1160type argsSettingPip struct {
1161 pyPIManager
1162 args []string
1165func (m *argsSettingPip) SetArgs(args []string) { m.args = args }
1167func TestRunSetsTheArgsTheBinaryRunsWith(t *testing.T) {
1168 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
1169 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
1170 ios, _, _, _ := cmdtest.TestIOStreams()
1171 m := &argsSettingPip{pyPIManager: pyPIManager{name: "pip", binary: "pip"}}
1173 err := Run(t.Context(), m, RunOptions{
1174 IO: ios,
1175 Executor: &fakeExecutor{},
1176 BaseDir: t.TempDir(),
1177 ProjectID: "group/proj",
1178 Args: []string{"install", "--proxy", "http://corp:3128", "six"},
1179 })
1180 require.NoError(t, err)
1181 assert.Equal(t, []string{"install", "six"}, m.args)
Diagnostics7 git calls · 338 ms
CallStatusSizeTime0338 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 KiB · hit2 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=2
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 · 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=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=44
Open 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 · 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=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.3 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=1327
git.response.cache=HIT
git.response.duration_ms=2
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=c83fc6004a9a1360845513c0fb16272b5c14a3e4
Open pack map
fetch2008.6 KiB328 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=327
git.response.bytes=8763
git.response.cache=MISS
git.response.duration_ms=328
git.fetch.wants=7fc42446482d3481748191e963717b2149ee462d
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=8763
git.pkt.payload.max=8736
git.sideband.band1.bytes=8736
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
a5e1f9ec5dc224ce
Request
b4019518…
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