GitLabgitlab-org
cli
main
1//go:build !integration
3package pm
5import (
6 "bytes"
7 "context"
8 "crypto/rand"
9 "crypto/rsa"
10 "crypto/tls"
11 "crypto/x509"
12 "encoding/pem"
13 "errors"
14 "fmt"
15 "io"
16 "math/big"
17 "net"
18 "net/http"
19 "net/http/httptest"
20 "net/url"
21 "os"
22 "os/exec"
23 "path/filepath"
24 "runtime"
25 "strconv"
26 "strings"
27 "testing"
29 "github.com/stretchr/testify/assert"
30 "github.com/stretchr/testify/require"
32 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/cilog"
33 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/proxy"
34 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
35 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
36)
38func TestManagerMetadata(t *testing.T) {
39 t.Parallel()
40 cases := []struct {
41 m PackageManager
42 name, bin string
43 }{
44 {NPM(), "npm", "npm"},
45 {Pnpm(), "pnpm", "pnpm"},
46 {Yarn(), "yarn", "yarn"},
47 {Pip(), "pip", "pip"},
48 {Pipenv(), "pipenv", "pipenv"},
49 {Uv(), "uv", "uv"},
50 {Poetry(), "poetry", "poetry"},
51 {Twine(), "twine", "twine"},
52 {Gem(), "gem", "gem"},
53 {Bundle(), "bundle", "bundle"},
54 {Maven(), "maven", "mvn"},
55 {Gradle(), "gradle", "gradle"},
56 {Cargo(), "cargo", "cargo"},
57 {Golang(), "go", "go"},
58 }
59 for _, c := range cases {
60 t.Run(c.name, func(t *testing.T) {
61 t.Parallel()
62 assert.Equal(t, c.name, c.m.Name())
63 assert.Equal(t, c.bin, c.m.Binary())
64 })
65 }
66}
68func TestNPMCATrust(t *testing.T) {
69 t.Parallel()
70 assert.Equal(t, []string{"NODE_EXTRA_CA_CERTS=/tmp/ca.pem"}, NPM().CATrustEnviron("/tmp/ca.pem"))
71}
73func TestProxyEnvironStripsInheritedProxyVarsAndSetsHTTPS(t *testing.T) {
74 t.Parallel()
75 parent := []string{
76 "PATH=/usr/bin",
77 "HTTP_PROXY=http://corp",
78 "HTTPS_PROXY=http://corp",
79 "https_proxy=http://corp", // lowercase must also be stripped
80 "NO_PROXY=registry.npmjs.org",
81 "no_proxy=*",
82 "NPM_CONFIG_PROXY=http://corp", // a manager-owned key
83 "NPM_CONFIG_HTTPS_PROXY=http://corp", // a manager-owned key
84 }
85 // Pass the npm manager's own routing keys so the engine strips them too
86 // without hardcoding any ecosystem's variables.
87 got := proxyEnviron(parent, "http://127.0.0.1:8080", "NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS_PROXY")
89 // Every inherited proxy/no-proxy variable (any case), plus the manager's
90 // own routing keys, must be gone. HTTP_PROXY and HTTPS_PROXY are then set
91 // to the inspection proxy (not merely stripped): a plaintext-HTTP registry
92 // must go through the proxy to be checked rather than be fetched directly,
93 // and a surviving NO_PROXY would route traffic past the MITM proxy and
94 // silently fail open.
95 for _, e := range got {
96 key, _, _ := strings.Cut(e, "=")
97 switch strings.ToUpper(key) {
98 case "HTTP_PROXY":
99 if e == "HTTP_PROXY=http://127.0.0.1:8080" {
100 continue // the one we set
101 }
102 t.Fatalf("inherited HTTP_PROXY survived sanitization: %q", e)
103 case "HTTPS_PROXY":
104 if e == "HTTPS_PROXY=http://127.0.0.1:8080" {
105 continue // the one we set
106 }
107 t.Fatalf("inherited HTTPS_PROXY survived sanitization: %q", e)
108 case "NO_PROXY", "NPM_CONFIG_PROXY", "NPM_CONFIG_HTTPS_PROXY":
109 t.Fatalf("inherited proxy var survived sanitization: %q", e)
110 }
111 }
112 assert.Contains(t, got, "PATH=/usr/bin", "non-proxy vars must be preserved")
113 assert.Contains(t, got, "HTTPS_PROXY=http://127.0.0.1:8080", "HTTPS_PROXY must point at the inspection proxy")
114 assert.Contains(t, got, "HTTP_PROXY=http://127.0.0.1:8080", "HTTP_PROXY must point at the inspection proxy so plaintext fetches are checked")
115}
117func TestProxyEnvironWithoutManagerKeysLeavesThemForRun(t *testing.T) {
118 t.Parallel()
119 // Without manager keys, only the universal proxy vars are stripped; a
120 // manager-specific var the caller didn't declare is left untouched (Run
121 // declares them via envKeys(manager.Environment)).
122 parent := []string{"NPM_CONFIG_HTTPS_PROXY=http://corp"}
123 got := proxyEnviron(parent, "http://127.0.0.1:8080")
124 assert.Contains(t, got, "NPM_CONFIG_HTTPS_PROXY=http://corp")
125}
127func TestLookupEnvLastOccurrenceWins(t *testing.T) {
128 t.Parallel()
129 env := []string{"K=first", "OTHER=x", "K=second"}
130 assert.Equal(t, "second", lookupEnv(env, "K"))
131 assert.Empty(t, lookupEnv(env, "MISSING"))
132}
134func TestNPMEnvironRoutesThroughProxy(t *testing.T) {
135 t.Parallel()
136 env := NPM().Environment("http://127.0.0.1:9999")
137 assert.Contains(t, env, "HTTPS_PROXY=http://127.0.0.1:9999")
138 assert.Contains(t, env, "NPM_CONFIG_PROXY=http://127.0.0.1:9999")
139 assert.Contains(t, env, "NPM_CONFIG_HTTPS_PROXY=http://127.0.0.1:9999")
140 // noproxy must be pinned to a non-empty sentinel that never matches a real
141 // registry: npm treats an empty env value as unset and lets a project
142 // .npmrc "noproxy=" line win, which would reopen the proxy bypass.
143 assert.Contains(t, env, "NPM_CONFIG_NOPROXY=localhost")
144 assert.NotContains(t, env, "NPM_CONFIG_NOPROXY=", "empty NOPROXY is overridden by .npmrc; must be a non-empty sentinel")
145}
147func TestDedupEnvLastWinsPreservesOrder(t *testing.T) {
148 t.Parallel()
149 in := []string{
150 "PATH=/usr/bin",
151 "HTTPS_PROXY=http://old",
152 "PIP_CERT=/user/ca.pem",
153 "HTTPS_PROXY=http://proxy",
154 "PIP_CERT=/proxy/ca.pem",
155 "NO_EQUALS_ENTRY",
156 }
157 got := dedupEnv(in)
158 assert.Equal(t, []string{
159 "PATH=/usr/bin",
160 "HTTPS_PROXY=http://proxy",
161 "PIP_CERT=/proxy/ca.pem",
162 "NO_EQUALS_ENTRY",
163 }, got)
164}
166func TestMatcherTypes(t *testing.T) {
167 t.Parallel()
168 // Spelled out per manager so a failure names the specific one.
169 _, ok := NPM().Matcher().(proxy.NPMMatcher)
170 assert.True(t, ok, "npm")
171 _, ok = Pnpm().Matcher().(proxy.NPMMatcher)
172 assert.True(t, ok, "pnpm")
173 _, ok = Yarn().Matcher().(proxy.NPMMatcher)
174 assert.True(t, ok, "yarn")
175 _, ok = Pip().Matcher().(proxy.PyPIMatcher)
176 assert.True(t, ok, "pip")
177 _, ok = Pipenv().Matcher().(proxy.PyPIMatcher)
178 assert.True(t, ok, "pipenv")
179 _, ok = Uv().Matcher().(proxy.PyPIMatcher)
180 assert.True(t, ok, "uv")
181 _, ok = Poetry().Matcher().(proxy.PyPIMatcher)
182 assert.True(t, ok, "poetry")
183 _, ok = Twine().Matcher().(proxy.PyPIMatcher)
184 assert.True(t, ok, "twine")
185 _, ok = Gem().Matcher().(proxy.GemMatcher)
186 assert.True(t, ok, "gem")
187 _, ok = Bundle().Matcher().(proxy.GemMatcher)
188 assert.True(t, ok, "bundle")
189 _, ok = Maven().Matcher().(proxy.MavenMatcher)
190 assert.True(t, ok, "maven")
191 _, ok = Gradle().Matcher().(proxy.MavenMatcher)
192 assert.True(t, ok, "gradle")
193 _, ok = Cargo().Matcher().(proxy.CargoMatcher)
194 assert.True(t, ok, "cargo")
195 _, ok = Golang().Matcher().(passThroughMatcher)
196 assert.True(t, ok, "golang")
197}
199type fakeExecutor struct {
200 env []string
201 onExec func()
202 execErr error
203}
205func (f *fakeExecutor) LookPath(file string) (string, error) { return "/bin/" + file, nil }
207func (f *fakeExecutor) ExecWithIO(_ context.Context, _ string, _ []string, env []string, _ io.Reader, _, _ io.Writer) error {
208 f.env = env
209 if f.onExec != nil {
210 f.onExec()
211 }
212 return f.execErr
213}
215func TestRunWritesLogAndSetsProxyEnv(t *testing.T) {
216 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
217 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
218 dir := t.TempDir()
219 ios, _, _, _ := cmdtest.TestIOStreams()
220 exec := &fakeExecutor{}
222 err := Run(t.Context(), NPM(), RunOptions{
223 IO: ios,
224 Executor: exec,
225 BaseDir: dir,
226 ProjectID: "group/proj",
227 Args: []string{"install", "left-pad"},
228 })
229 require.NoError(t, err)
231 var sawProxy, sawCA bool
232 for _, e := range exec.env {
233 if strings.HasPrefix(e, "HTTPS_PROXY=") {
234 sawProxy = true
235 }
236 if strings.HasPrefix(e, "NODE_EXTRA_CA_CERTS=") {
237 sawCA = true
238 }
239 }
240 assert.True(t, sawProxy, "child env must set HTTPS_PROXY")
241 assert.True(t, sawCA, "child env must set NODE_EXTRA_CA_CERTS")
243 _, statErr := os.Stat(cilog.Path(dir))
244 require.NoError(t, statErr)
246 entries, _ := os.ReadDir(dir)
247 for _, e := range entries {
248 assert.NotEqual(t, ".npmrc", e.Name(), "Run must not write .npmrc")
249 }
250}
252func TestRunExitErrorPropagates(t *testing.T) {
253 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
254 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
255 dir := t.TempDir()
256 ios, _, _, _ := cmdtest.TestIOStreams()
257 exec := &fakeExecutor{execErr: errors.New("boom")}
259 err := Run(t.Context(), NPM(), RunOptions{
260 IO: ios, Executor: exec, BaseDir: dir, ProjectID: "p", Args: []string{"x"},
261 })
262 require.Error(t, err)
263 var ee *ExitError
264 require.ErrorAs(t, err, &ee)
265 // A generic (non-*exec.ExitError) failure maps to the generic exit code 1.
266 assert.Equal(t, 1, ee.Code)
267 // The child never actually ran, so callers must not silence this error.
268 assert.False(t, ee.ChildExited, "a generic run failure means the child never exited")
269}
271type failingSetupManager struct{ PackageManager }
273func (failingSetupManager) Setup(context.Context, setupInput) (setupResult, error) {
274 return setupResult{}, errors.New("disk full")
275}
277func TestRunFailsClosedWhenSetupFails(t *testing.T) {
278 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
279 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
280 ios, _, _, _ := cmdtest.TestIOStreams()
281 exec := &fakeExecutor{}
283 err := Run(t.Context(), failingSetupManager{NPM()}, RunOptions{
284 IO: ios, Executor: exec, BaseDir: t.TempDir(), ProjectID: "p", Args: []string{"install"},
285 })
286 require.ErrorContains(t, err, "disk full")
287 assert.Nil(t, exec.env, "the child must not start when its setup failed")
288}
290type sidecarManager struct {
291 PackageManager
292 in setupInput
293 stops int
294 recorded []verdict.Entry
295 onStop []verdict.Entry
296}
298func (m *sidecarManager) Setup(_ context.Context, in setupInput) (setupResult, error) {
299 m.in = in
300 return setupResult{
301 stop: func() {
302 m.stops++
303 m.recorded = append(m.recorded, m.onStop...)
304 },
305 verdicts: func() []verdict.Entry { return m.recorded },
306 }, nil
307}
309func TestRunPassesRunInputsToSetup(t *testing.T) {
310 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
311 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
312 t.Setenv("GLAB_DF_TEST_PARENT", "inherited")
313 ios, _, _, _ := cmdtest.TestIOStreams()
314 exec := &fakeExecutor{}
315 m := &sidecarManager{PackageManager: NPM()}
317 err := Run(t.Context(), m, RunOptions{
318 IO: ios, Executor: exec, BaseDir: t.TempDir(), ProjectID: "group/proj", Args: []string{"install"},
319 })
320 require.NoError(t, err)
322 assert.Equal(t, "/bin/npm", m.in.bin)
323 assert.Equal(t, "group/proj", m.in.projectID)
324 assert.Same(t, exec, m.in.exec)
325 assert.Same(t, ios, m.in.io)
326 assert.NotNil(t, m.in.checker)
327 assert.Equal(t, "inherited", lookupEnv(m.in.parent, "GLAB_DF_TEST_PARENT"))
328 assert.Equal(t, lookupEnv(exec.env, "NODE_EXTRA_CA_CERTS"), m.in.caPath)
329}
331func TestRunRecordsSetupVerdicts(t *testing.T) {
332 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
333 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
334 dir := t.TempDir()
335 ios, _, _, errOut := cmdtest.TestIOStreams()
336 m := &sidecarManager{
337 PackageManager: NPM(),
338 onStop: []verdict.Entry{{Package: "example.com/drained", Version: "v1.0.0", Verdict: verdict.Blocked}},
339 }
340 exec := &fakeExecutor{onExec: func() {
341 m.recorded = append(m.recorded, verdict.Entry{Package: "example.com/fetched", Version: "v2.0.0", Verdict: verdict.Blocked})
342 }}
344 err := Run(t.Context(), m, RunOptions{
345 IO: ios, Executor: exec, BaseDir: dir, ProjectID: "p", Args: []string{"install"},
346 })
347 require.NoError(t, err)
349 got, err := cilog.Load(dir)
350 require.NoError(t, err)
351 var packages []string
352 for _, e := range got.Entries {
353 packages = append(packages, e.Package)
354 }
355 assert.ElementsMatch(t, []string{"example.com/fetched", "example.com/drained"}, packages)
356 assert.Contains(t, errOut.String(), "example.com/fetched")
357 assert.Contains(t, errOut.String(), "example.com/drained", "a verdict recorded while stopping must not be lost")
358}
360func TestRunStopsSetupOnce(t *testing.T) {
361 for name, execErr := range map[string]error{"success": nil, "child failure": errors.New("boom")} {
362 t.Run(name, func(t *testing.T) {
363 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
364 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
365 ios, _, _, _ := cmdtest.TestIOStreams()
366 m := &sidecarManager{PackageManager: NPM()}
368 _ = Run(t.Context(), m, RunOptions{
369 IO: ios, Executor: &fakeExecutor{execErr: execErr}, BaseDir: t.TempDir(), ProjectID: "p", Args: []string{"install"},
370 })
371 assert.Equal(t, 1, m.stops)
372 })
373 }
374}
376func TestRunStopsSetupOnceOnInterrupt(t *testing.T) {
377 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
378 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
379 ios, _, _, _ := cmdtest.TestIOStreams()
380 m := &sidecarManager{PackageManager: NPM()}
381 fe := &blockingExecutor{started: make(chan struct{})}
382 sigChReady := make(chan chan<- os.Signal, 1)
384 errCh := make(chan error, 1)
385 go func() {
386 errCh <- Run(t.Context(), m, RunOptions{
387 IO: ios, Executor: fe, BaseDir: t.TempDir(), ProjectID: "p", Args: []string{"install"},
388 notify: func(c chan<- os.Signal, _ ...os.Signal) { sigChReady <- c },
389 })
390 }()
391 <-fe.started
392 (<-sigChReady) <- os.Interrupt
394 var ee *ExitError
395 require.ErrorAs(t, <-errCh, &ee)
396 assert.Equal(t, interruptExitCode, ee.Code)
397 assert.Equal(t, 1, m.stops)
398}
400// exitExecutor returns an *exec.ExitError carrying a specific exit code, so we
401// can assert the child's real status is propagated (not the generic 1).
402type exitExecutor struct{ code int }
404func (exitExecutor) LookPath(file string) (string, error) { return "/bin/" + file, nil }
405func (e exitExecutor) ExecWithIO(_ context.Context, _ string, _ []string, _ []string, _ io.Reader, _, _ io.Writer) error {
406 // Run a real command that exits with e.code to obtain a genuine
407 // *exec.ExitError whose ExitCode() the engine must surface.
408 cmd := exec.Command("sh", "-c", "exit "+strconv.Itoa(e.code))
409 return cmd.Run()
410}
412func TestRunPropagatesChildExitCode(t *testing.T) {
413 if runtime.GOOS == "windows" {
414 t.Skip("uses sh to produce a real *exec.ExitError")
415 }
416 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
417 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
418 dir := t.TempDir()
419 ios, _, _, _ := cmdtest.TestIOStreams()
421 err := Run(t.Context(), NPM(), RunOptions{
422 IO: ios, Executor: exitExecutor{code: 42}, BaseDir: dir, ProjectID: "p", Args: []string{"x"},
423 })
424 require.Error(t, err)
425 var ee *ExitError
426 require.ErrorAs(t, err, &ee)
427 assert.Equal(t, 42, ee.Code, "engine must surface the child's exit code, not a generic 1")
428 // The child actually ran and exited non-zero, so this error is safe to
429 // silence in favor of the rendered summary.
430 assert.True(t, ee.ChildExited, "a real child exit must be marked ChildExited")
431}
433func TestRunInterruptReturnsExitCode130(t *testing.T) {
434 if runtime.GOOS == "windows" {
435 // os.CreateTemp("") honors TMP/TEMP (not TMPDIR) on Windows, so the
436 // leaked-file assertion below can't be isolated reliably; the signal
437 // path is POSIX-oriented anyway.
438 t.Skip("temp-dir isolation and signal semantics are POSIX-specific")
439 }
440 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
441 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
442 t.Chdir(t.TempDir())
444 // Isolate os.CreateTemp("") (used for the CA bundle) to a private dir so
445 // the leaked-file assertion below is unaffected by other tests or runs.
446 tmp := t.TempDir()
447 t.Setenv("TMPDIR", tmp)
449 sigChReady := make(chan chan<- os.Signal, 1)
451 fe := &blockingExecutor{started: make(chan struct{})}
453 ios, _, _, _ := cmdtest.TestIOStreams()
454 errCh := make(chan error, 1)
455 go func() {
456 errCh <- Run(t.Context(), NPM(), RunOptions{
457 IO: ios,
458 Executor: fe,
459 BaseDir: ".",
460 ProjectID: "g/p",
461 Args: []string{"install"},
462 notify: func(c chan<- os.Signal, sig ...os.Signal) { sigChReady <- c },
463 })
464 }()
466 <-fe.started
467 sigCh := <-sigChReady
468 require.NotNil(t, sigCh)
469 sigCh <- os.Interrupt
471 err := <-errCh
472 var ee *ExitError
473 require.ErrorAs(t, err, &ee)
474 assert.Equal(t, 130, ee.Code)
476 // The interrupt path must still run the deferred cleanup that os.Exit
477 // previously skipped: no CA bundle temp files may be left behind.
478 leaked, _ := filepath.Glob(filepath.Join(tmp, "glab-df-ca-*"))
479 assert.Empty(t, leaked, "interrupt path must remove the CA bundle temp file")
480}
482type blockingExecutor struct{ started chan struct{} }
484func (b *blockingExecutor) LookPath(file string) (string, error) { return "/usr/bin/" + file, nil }
485func (b *blockingExecutor) ExecWithIO(ctx context.Context, name string, args, env []string, stdin io.Reader, stdout, stderr io.Writer) error {
486 close(b.started)
487 <-ctx.Done()
488 return ctx.Err()
489}
491// testCA returns a throwaway self-signed certificate for CA-bundle tests.
492func testCA(t *testing.T) *x509.Certificate {
493 t.Helper()
494 key, err := rsa.GenerateKey(rand.Reader, 2048)
495 require.NoError(t, err)
496 tmpl := &x509.Certificate{SerialNumber: big.NewInt(1)}
497 der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
498 require.NoError(t, err)
499 cert, err := x509.ParseCertificate(der)
500 require.NoError(t, err)
501 return cert
502}
504func TestWriteCABundleEmitsValidPEM(t *testing.T) {
505 t.Parallel()
506 ios, _, _, _ := cmdtest.TestIOStreams()
507 path, err := writeCABundle(ios, nil, nil, testCA(t), nil)
508 require.NoError(t, err)
509 defer func() { _ = os.Remove(path) }()
511 raw, err := os.ReadFile(path)
512 require.NoError(t, err)
513 block, _ := pem.Decode(raw)
514 require.NotNil(t, block, "output must be valid PEM")
515 assert.Equal(t, "CERTIFICATE", block.Type)
516}
518// countPEMBlocks returns how many PEM blocks pemBytes decodes into.
519func countPEMBlocks(pemBytes []byte) int {
520 var blocks int
521 for rest := pemBytes; ; {
522 var b *pem.Block
523 b, rest = pem.Decode(rest)
524 if b == nil {
525 break
526 }
527 blocks++
528 }
529 return blocks
530}
532func TestWriteCABundlePrependsExistingBundle(t *testing.T) {
533 t.Parallel()
534 // A user-provided bundle named by the manager's ExistingBundleVars must be
535 // preserved: the output should contain the user's PEM followed by the
536 // proxy CA, so the child trusts both.
537 userPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: testCA(t).Raw})
538 userPath := filepath.Join(t.TempDir(), "user-ca.pem")
539 require.NoError(t, os.WriteFile(userPath, userPEM, 0o600))
541 ios, _, _, _ := cmdtest.TestIOStreams()
542 parent := []string{"NODE_EXTRA_CA_CERTS=" + userPath}
543 path, err := writeCABundle(ios, parent, []string{"NODE_EXTRA_CA_CERTS"}, testCA(t), nil)
544 require.NoError(t, err)
545 defer func() { _ = os.Remove(path) }()
547 raw, err := os.ReadFile(path)
548 require.NoError(t, err)
549 assert.True(t, bytes.HasPrefix(raw, userPEM), "user's bundle must be prepended")
550 assert.Equal(t, 2, countPEMBlocks(raw), "bundle must contain the user CA and the proxy CA")
551}
553func TestWriteCABundleMergesMultipleDeclaredVars(t *testing.T) {
554 t.Parallel()
555 // A user who set only the second declared variable (e.g. REQUESTS_CA_BUNDLE
556 // but not PIP_CERT) must still have their anchors preserved. Two distinct
557 // user CAs, each named by a different declared var, both land in the bundle.
558 caA := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: testCA(t).Raw})
559 caB := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: testCA(t).Raw})
560 pathA := filepath.Join(t.TempDir(), "a.pem")
561 pathB := filepath.Join(t.TempDir(), "b.pem")
562 require.NoError(t, os.WriteFile(pathA, caA, 0o600))
563 require.NoError(t, os.WriteFile(pathB, caB, 0o600))
565 ios, _, _, _ := cmdtest.TestIOStreams()
566 parent := []string{"PIP_CERT=" + pathA, "REQUESTS_CA_BUNDLE=" + pathB}
567 path, err := writeCABundle(ios, parent, []string{"PIP_CERT", "REQUESTS_CA_BUNDLE"}, testCA(t), nil)
568 require.NoError(t, err)
569 defer func() { _ = os.Remove(path) }()
571 raw, err := os.ReadFile(path)
572 require.NoError(t, err)
573 assert.Equal(t, 3, countPEMBlocks(raw), "both user CAs plus the proxy CA must be present")
574}
576func TestWriteCABundleDeduplicatesSharedPath(t *testing.T) {
577 t.Parallel()
578 // Two declared vars pointing at the same file must not double-append it.
579 userPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: testCA(t).Raw})
580 userPath := filepath.Join(t.TempDir(), "user-ca.pem")
581 require.NoError(t, os.WriteFile(userPath, userPEM, 0o600))
583 ios, _, _, _ := cmdtest.TestIOStreams()
584 parent := []string{"SSL_CERT_FILE=" + userPath, "BUNDLE_SSL_CA_CERT=" + userPath}
585 path, err := writeCABundle(ios, parent, []string{"SSL_CERT_FILE", "BUNDLE_SSL_CA_CERT"}, testCA(t), nil)
586 require.NoError(t, err)
587 defer func() { _ = os.Remove(path) }()
589 raw, err := os.ReadFile(path)
590 require.NoError(t, err)
591 assert.Equal(t, 2, countPEMBlocks(raw), "shared path must be read once: one user CA plus the proxy CA")
592}
594func TestWriteCABundleWarnsOnUnreadableExisting(t *testing.T) {
595 t.Parallel()
596 // An unreadable existing bundle must warn and fall back to the proxy CA
597 // alone rather than failing the run.
598 ios, _, _, errOut := cmdtest.TestIOStreams()
599 parent := []string{"NODE_EXTRA_CA_CERTS=" + filepath.Join(t.TempDir(), "does-not-exist.pem")}
600 path, err := writeCABundle(ios, parent, []string{"NODE_EXTRA_CA_CERTS"}, testCA(t), nil)
601 require.NoError(t, err)
602 defer func() { _ = os.Remove(path) }()
604 assert.Contains(t, errOut.String(), "could not read existing NODE_EXTRA_CA_CERTS")
605 raw, err := os.ReadFile(path)
606 require.NoError(t, err)
607 block, _ := pem.Decode(raw)
608 require.NotNil(t, block, "must still emit the proxy CA")
609}
611func TestWriteCABundleAppendsSystemRoots(t *testing.T) {
612 t.Parallel()
613 proxyCA, root := testCA(t), testCA(t)
614 ios, _, _, _ := cmdtest.TestIOStreams()
615 path, err := writeCABundle(ios, nil, nil, proxyCA, []*x509.Certificate{root})
616 require.NoError(t, err)
617 defer func() { _ = os.Remove(path) }()
619 raw, err := os.ReadFile(path)
620 require.NoError(t, err)
621 certs := parseCertsPEM(raw)
622 require.Len(t, certs, 2)
623 assert.True(t, certs[0].Equal(proxyCA))
624 assert.True(t, certs[1].Equal(root), "a tool pointed at the bundle must keep trusting the system roots")
625}
627func TestWriteLogMergesSequentialRuns(t *testing.T) {
628 t.Parallel()
629 // Two firewall runs in one CI job (e.g. npm then pip) must accumulate
630 // verdicts, not clobber each other, or ci-summary would miss the first
631 // run's blocks and wrongly exit 0.
632 dir := t.TempDir()
633 ios, _, _, _ := cmdtest.TestIOStreams()
635 writeLog(ios, dir, "npm", []string{"install"},
636 []verdict.Entry{{Package: "left-pad", Version: "1.3.0", Verdict: verdict.Blocked}})
637 writeLog(ios, dir, "pip", []string{"install"},
638 []verdict.Entry{{Package: "requests", Version: "2.31.0", Verdict: verdict.Warning}})
640 got, err := cilog.Load(dir)
641 require.NoError(t, err)
642 require.Len(t, got.Entries, 2, "second run must not overwrite the first")
643 names := []string{got.Entries[0].Package, got.Entries[1].Package}
644 assert.Contains(t, names, "left-pad")
645 assert.Contains(t, names, "requests")
646}
648func TestWriteLogPopulatesSessionOnFirstRun(t *testing.T) {
649 t.Parallel()
650 // On the normal (missing-file) path cilog.Load returns a zero-value log,
651 // so only writeLog sets the Session. A missing Session.Command would make
652 // every ordinary run persist an empty session block.
653 dir := t.TempDir()
654 ios, _, _, _ := cmdtest.TestIOStreams()
656 writeLog(ios, dir, "npm", []string{"install", "left-pad"}, nil)
658 got, err := cilog.Load(dir)
659 require.NoError(t, err)
660 assert.Equal(t, "npm install left-pad", got.Session.Command, "first run must record the originating command")
661 assert.NotEmpty(t, got.Session.StartedAt, "first run must record a start time")
662}
664func TestWriteLogPreservesFirstSessionAcrossRuns(t *testing.T) {
665 t.Parallel()
666 // A later run must not overwrite the session of the run that created the
667 // log, so ci-summary attributes the log to its originating command.
668 dir := t.TempDir()
669 ios, _, _, _ := cmdtest.TestIOStreams()
671 writeLog(ios, dir, "npm", []string{"install"}, nil)
672 writeLog(ios, dir, "pip", []string{"install"}, nil)
674 got, err := cilog.Load(dir)
675 require.NoError(t, err)
676 assert.Equal(t, "npm install", got.Session.Command, "session must stay pinned to the first run")
677}
679func TestWriteLogLeavesCorruptLogInPlace(t *testing.T) {
680 t.Parallel()
681 // A present-but-corrupt log must NOT be overwritten: starting fresh would
682 // discard an earlier run's Blocked entries and flip ci-summary's
683 // fail-closed unreadable-log exit into a pass. The corrupt bytes must
684 // remain so ci-summary fails on them.
685 dir := t.TempDir()
686 require.NoError(t, os.MkdirAll(filepath.Dir(cilog.Path(dir)), 0o755))
687 corrupt := []byte("{ this is not valid json")
688 require.NoError(t, os.WriteFile(cilog.Path(dir), corrupt, 0o600))
690 ios, _, _, errOut := cmdtest.TestIOStreams()
691 writeLog(ios, dir, "npm", []string{"install"},
692 []verdict.Entry{{Package: "left-pad", Version: "1.3.0", Verdict: verdict.Blocked}})
694 raw, err := os.ReadFile(cilog.Path(dir))
695 require.NoError(t, err)
696 assert.Equal(t, corrupt, raw, "corrupt log must be left untouched for ci-summary to fail on")
697 assert.Contains(t, errOut.String(), "unreadable")
698}
700// TestRunComposedBlockThroughProxy exercises the full engine path end to end:
701// Run starts the real inspection proxy, the executor sends a real HTTPS
702// request for an npm tarball through the HTTPS_PROXY it received, trusting the
703// proxy CA from NODE_EXTRA_CA_CERTS; the fake checker blocks the coordinate.
704// It asserts the child sees a 403, the Blocked verdict reaches the saved CI
705// log, and the summary is rendered — none of which the fakeExecutor-based
706// tests prove, since they never dial the proxy.
707func TestRunComposedBlockThroughProxy(t *testing.T) {
708 if runtime.GOOS == "windows" {
709 t.Skip("relies on POSIX temp-dir isolation for the CA bundle")
710 }
711 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
712 t.Setenv("GLAB_DF_FAKE_BLOCK", "npm:left-pad@1.3.0")
714 dir := t.TempDir()
715 ios, _, _, errOut := cmdtest.TestIOStreams()
716 ce := &curlingExecutor{path: "/left-pad/-/left-pad-1.3.0.tgz"}
718 err := Run(t.Context(), NPM(), RunOptions{
719 IO: ios,
720 Executor: ce,
721 BaseDir: dir,
722 ProjectID: "group/proj",
723 Args: []string{"install", "left-pad"},
724 })
725 require.Error(t, err, "a blocked fetch must fail the run")
727 require.NotNil(t, ce.resp, "executor must have received a response through the proxy")
728 assert.Equal(t, http.StatusForbidden, ce.resp.StatusCode, "blocked coordinate must yield a 403 to the child")
730 got, loadErr := cilog.Load(dir)
731 require.NoError(t, loadErr)
732 require.Len(t, got.Entries, 1, "the composed path must record exactly one verdict")
733 assert.Equal(t, "left-pad", got.Entries[0].Package)
734 assert.Equal(t, verdict.Blocked, got.Entries[0].Verdict)
736 assert.Contains(t, errOut.String(), "left-pad", "summary must mention the blocked package")
737 assert.Contains(t, errOut.String(), "dependency firewall blocked left-pad 1.3.0", "the block is reported while the package manager runs")
738}
740// curlingExecutor ignores the package-manager binary and instead performs a
741// real HTTPS GET of path through the HTTPS_PROXY and CA it is handed, mimicking
742// what a package manager would do. It records the response so the test can
743// assert on the synthesized 403.
744type curlingExecutor struct {
745 path string
746 resp *http.Response
747}
749func (curlingExecutor) LookPath(file string) (string, error) { return "/bin/" + file, nil }
751func (c *curlingExecutor) ExecWithIO(ctx context.Context, _ string, _ []string, env []string, _ io.Reader, _, _ io.Writer) error {
752 proxyRaw := lookupEnv(env, "HTTPS_PROXY")
753 proxyURL, err := url.Parse(proxyRaw)
754 if err != nil {
755 return err
756 }
757 caPath := lookupEnv(env, "NODE_EXTRA_CA_CERTS")
758 pemBytes, err := os.ReadFile(caPath)
759 if err != nil {
760 return err
761 }
762 pool := x509.NewCertPool()
763 if !pool.AppendCertsFromPEM(pemBytes) {
764 return errors.New("failed to load proxy CA from NODE_EXTRA_CA_CERTS")
765 }
766 client := &http.Client{
767 Transport: &http.Transport{
768 Proxy: http.ProxyURL(proxyURL),
769 TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12},
770 },
771 }
772 // A blocked coordinate is refused before the upstream round trip, so the
773 // registry host is never actually dialed; any resolvable-looking host works.
774 req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://registry.npmjs.org"+c.path, http.NoBody)
775 if err != nil {
776 return err
777 }
778 resp, err := client.Do(req)
779 if err != nil {
780 return err
781 }
782 defer resp.Body.Close()
783 _, _ = io.Copy(io.Discard, resp.Body)
784 c.resp = resp
785 // Mirror a package manager: a 403 from the registry is a failure.
786 if resp.StatusCode != http.StatusOK {
787 return fmt.Errorf("registry returned %d", resp.StatusCode)
788 }
789 return nil
790}
792// TestRunTrustsConfiguredCAsUpstream proves the inspection proxy verifies the
793// registry against the CAs the user configured: glab's ca_cert (CACertPath)
794// and any package-manager CA variable, even one the running manager ignores.
795func TestRunTrustsConfiguredCAsUpstream(t *testing.T) {
796 if runtime.GOOS == "windows" {
797 t.Skip("relies on POSIX temp-dir isolation for the CA bundle")
798 }
799 // Not httptest's shared certificate: on Linux the process caches the system
800 // roots, and an earlier test that sets SSL_CERT_FILE to that certificate
801 // would make the "neither" registry trusted.
802 ca := newTestAuthority(t)
803 upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
804 _, _ = io.WriteString(w, "ok")
805 }))
806 upstream.TLS = &tls.Config{MinVersion: tls.VersionTLS12, Certificates: []tls.Certificate{ca.serverCert(t, net.IPv4(127, 0, 0, 1))}}
807 upstream.StartTLS()
808 t.Cleanup(upstream.Close)
809 upstreamCA := writeTestFile(t, "upstream-ca.pem", ca.pem())
811 cases := []struct {
812 name string
813 caCertPath string
814 requestsCA string
815 wantOK bool
816 }{
817 {name: "ca_cert", caCertPath: upstreamCA, wantOK: true},
818 {name: "REQUESTS_CA_BUNDLE", requestsCA: upstreamCA, wantOK: true},
819 {name: "neither", wantOK: false},
820 }
821 for _, c := range cases {
822 t.Run(c.name, func(t *testing.T) {
823 t.Setenv("GITLAB_CI", "")
824 t.Setenv("GLAB_DF_FAKE_BLOCK", "npm:left-pad@1.3.0")
825 t.Setenv("REQUESTS_CA_BUNDLE", c.requestsCA)
827 ios, _, _, errOut := cmdtest.TestIOStreams()
828 fe := &upstreamFetchExecutor{url: upstream.URL + "/ping"}
829 err := Run(t.Context(), NPM(), RunOptions{
830 IO: ios,
831 Executor: fe,
832 BaseDir: t.TempDir(),
833 ProjectID: "group/proj",
834 CACertPath: c.caCertPath,
835 })
836 if c.wantOK {
837 require.NoError(t, err)
838 require.NotNil(t, fe.resp)
839 assert.Equal(t, http.StatusOK, fe.resp.StatusCode)
840 assert.NotContains(t, errOut.String(), "warning:")
841 return
842 }
843 require.Error(t, err)
844 if fe.resp != nil {
845 assert.NotEqual(t, http.StatusOK, fe.resp.StatusCode, "an untrusted registry must not be fetched")
846 }
847 assert.Contains(t, errOut.String(), "warning: dependency firewall request to registry "+upstream.Listener.Addr().String()+" failed")
848 })
849 }
850}
852// upstreamFetchExecutor GETs url through the HTTPS_PROXY it is handed. The
853// inspection proxy mints its leaf for an IP authority with a DNS SAN only, so
854// this child skips hostname verification on its own hop to the proxy; the
855// proxy's hop to the registry is what the test exercises.
856type upstreamFetchExecutor struct {
857 url string
858 resp *http.Response
859}
861func (upstreamFetchExecutor) LookPath(file string) (string, error) { return "/bin/" + file, nil }
863func (u *upstreamFetchExecutor) ExecWithIO(ctx context.Context, _ string, _ []string, env []string, _ io.Reader, _, _ io.Writer) error {
864 proxyURL, err := url.Parse(lookupEnv(env, "HTTPS_PROXY"))
865 if err != nil {
866 return err
867 }
868 client := &http.Client{Transport: &http.Transport{
869 Proxy: http.ProxyURL(proxyURL),
870 TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, InsecureSkipVerify: true}, //nolint:gosec // child -> inspection proxy hop only
871 }}
872 req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.url, http.NoBody)
873 if err != nil {
874 return err
875 }
876 resp, err := client.Do(req)
877 if err != nil {
878 return err
879 }
880 defer resp.Body.Close()
881 _, _ = io.Copy(io.Discard, resp.Body)
882 u.resp = resp
883 if resp.StatusCode != http.StatusOK {
884 return fmt.Errorf("registry returned %d", resp.StatusCode)
885 }
886 return nil
887}
Diagnostics7 git calls · 224 ms
CallStatusSizeTime0224 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 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=3 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 · 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
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 · 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=211 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
fetch2008.5 KiB211 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=8750 git.response.cache=MISS git.response.duration_ms=211 git.fetch.wants=c99967ab5b8d8a969b7495b83719d7810ec677c0 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=8750 git.pkt.payload.max=8723 git.sideband.band1.bytes=8723 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
- a5e2462b2e34ee28
- Request
- 008652b7…
- 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