package workspace import ( "encoding/base64" "encoding/json" "fmt" "os" "path/filepath" "strings" "testing" ) func TestWorkspaceGitBasicIsolation(t *testing.T) { f := setup(t) password := "git-canary" header := "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte("x:"+password)) f.forge.remote = Remote{URL: "https://amadan.test/team/repo", GitPassword: password} log := filepath.Join(f.root, "observations") spy := filepath.Join(f.root, "git-spy") script := fmt.Sprintf(`#!/usr/bin/python3 import os,sys,json args=sys.argv[1:] with open(%q,'a') as out: out.write(json.dumps({'args':args,'env':dict(os.environ)})+'\n') if 'https://amadan.test/team/repo' in args: args[args.index('https://amadan.test/team/repo')]=%q args=['-c','protocol.file.allow=always']+args os.execve('/usr/bin/git',['/usr/bin/git']+args,dict(os.environ)) `, log, f.remote) put(t, spy, script) if err := os.Chmod(spy, 0700); err != nil { t.Fatal(err) } f.m.cfg.GitPath = spy t.Setenv("EQUIPO_API_SECRET", "api-canary") t.Setenv("GIT_TRACE_CURL", "1") w := f.create(t) data, err := os.ReadFile(log) if err != nil { t.Fatal(err) } fetches := 0 for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { var row struct { Args []string Env map[string]string } if err := json.Unmarshal([]byte(line), &row); err != nil { t.Fatal(err) } args := strings.Join(row.Args, " ") if strings.Contains(args, password) || strings.Contains(args, header) || row.Env["EQUIPO_API_SECRET"] != "" || row.Env["GIT_TRACE_CURL"] != "" { t.Fatal("credentials or tracing escaped into git process") } if strings.Contains(args, "https://amadan.test/team/repo") { fetches++ if row.Env["GIT_CONFIG_VALUE_9"] != header || row.Env["GIT_CONFIG_KEY_9"] != "http.extraHeader" || row.Env["GIT_CONFIG_COUNT"] != "10" { t.Fatal("fetch did not use Basic PAT") } } else if strings.Contains(fmt.Sprint(row.Env), header) { t.Fatal("PAT exposed to local git") } } if fetches != 1 { t.Fatal("HTTPS fetch not exercised") } if strings.Contains(fmt.Sprint(w.Environment()), password) || strings.Contains(fmt.Sprint(w.Environment()), header) { t.Fatal("PAT exposed to provider environment") } for _, root := range []string{f.cfg.PrivateRoot, f.cfg.WorkspaceRoot} { err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { if err != nil { return err } if !d.Type().IsRegular() { return nil } b, err := os.ReadFile(path) if err != nil { return err } if strings.Contains(string(b), password) || strings.Contains(string(b), header) { t.Error("credential persisted in workspace state") } return nil }) if err != nil { t.Fatal(err) } } // Git may echo its environment on failure; callers must get only a safe error. put(t, spy, "#!/bin/sh\nprintf '%s' \"$GIT_CONFIG_VALUE_9\"\nprintf '%s' \"$GIT_CONFIG_VALUE_9\" >&2\nexit 1\n") _, _, err = f.m.fetch(ctx, f.target) if err == nil || strings.Contains(err.Error(), password) || strings.Contains(err.Error(), header) { t.Fatal("git failure leaked credentials") } } func TestWorkspaceGitRequiresPassword(t *testing.T) { f := setup(t) f.forge.remote = Remote{URL: "https://amadan.test/team/repo"} marker := filepath.Join(f.root, "git-ran") spy := filepath.Join(f.root, "git-spy") put(t, spy, "#!/bin/sh\ntouch '"+marker+"'\nexit 1\n") if err := os.Chmod(spy, 0700); err != nil { t.Fatal(err) } f.m.cfg.GitPath = spy _, _, err := f.m.fetch(ctx, f.target) if err == nil || !strings.Contains(err.Error(), "git password") { t.Fatal("missing PAT needs a clear configuration error", err) } if _, err := os.Stat(marker); !os.IsNotExist(err) { t.Fatal("git ran without a PAT") } } func TestWorkspaceGitPasswordValidation(t *testing.T) { for _, password := range []string{"git\rpassword", "git\npassword", "git\x00password", "local"} { t.Run(password, func(t *testing.T) { f := setup(t) marker := filepath.Join(f.root, "git-ran") spy := filepath.Join(f.root, "git-spy") put(t, spy, "#!/bin/sh\ntouch '"+marker+"'\nexit 1\n") if err := os.Chmod(spy, 0700); err != nil { t.Fatal(err) } f.m.cfg.GitPath = spy if password == "local" { f.forge.remote.GitPassword = password } else { f.forge.remote = Remote{URL: "https://amadan.test/team/repo", GitPassword: password} } if _, _, err := f.m.fetch(ctx, f.target); err == nil { t.Fatal("invalid git credential accepted") } if _, err := os.Stat(marker); !os.IsNotExist(err) { t.Fatal("invalid git credential reached git") } }) } }