oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package workspace
2
3import (
4 "encoding/base64"
5 "encoding/json"
6 "fmt"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13func TestWorkspaceGitBasicIsolation(t *testing.T) {
14 f := setup(t)
15 password := "git-canary"
16 header := "Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte("x:"+password))
17 f.forge.remote = Remote{URL: "https://amadan.test/team/repo", GitPassword: password}
18 log := filepath.Join(f.root, "observations")
19 spy := filepath.Join(f.root, "git-spy")
20 script := fmt.Sprintf(`#!/usr/bin/python3
21import os,sys,json
22args=sys.argv[1:]
23with open(%q,'a') as out:
24 out.write(json.dumps({'args':args,'env':dict(os.environ)})+'\n')
25if 'https://amadan.test/team/repo' in args:
26 args[args.index('https://amadan.test/team/repo')]=%q
27 args=['-c','protocol.file.allow=always']+args
28os.execve('/usr/bin/git',['/usr/bin/git']+args,dict(os.environ))
29`, log, f.remote)
30 put(t, spy, script)
31 if err := os.Chmod(spy, 0700); err != nil {
32 t.Fatal(err)
33 }
34 f.m.cfg.GitPath = spy
35 t.Setenv("EQUIPO_API_SECRET", "api-canary")
36 t.Setenv("GIT_TRACE_CURL", "1")
37 w := f.create(t)
38 data, err := os.ReadFile(log)
39 if err != nil {
40 t.Fatal(err)
41 }
42 fetches := 0
43 for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") {
44 var row struct {
45 Args []string
46 Env map[string]string
47 }
48 if err := json.Unmarshal([]byte(line), &row); err != nil {
49 t.Fatal(err)
50 }
51 args := strings.Join(row.Args, " ")
52 if strings.Contains(args, password) || strings.Contains(args, header) || row.Env["EQUIPO_API_SECRET"] != "" || row.Env["GIT_TRACE_CURL"] != "" {
53 t.Fatal("credentials or tracing escaped into git process")
54 }
55 if strings.Contains(args, "https://amadan.test/team/repo") {
56 fetches++
57 if row.Env["GIT_CONFIG_VALUE_9"] != header || row.Env["GIT_CONFIG_KEY_9"] != "http.extraHeader" || row.Env["GIT_CONFIG_COUNT"] != "10" {
58 t.Fatal("fetch did not use Basic PAT")
59 }
60 } else if strings.Contains(fmt.Sprint(row.Env), header) {
61 t.Fatal("PAT exposed to local git")
62 }
63 }
64 if fetches != 1 {
65 t.Fatal("HTTPS fetch not exercised")
66 }
67 if strings.Contains(fmt.Sprint(w.Environment()), password) || strings.Contains(fmt.Sprint(w.Environment()), header) {
68 t.Fatal("PAT exposed to provider environment")
69 }
70 for _, root := range []string{f.cfg.PrivateRoot, f.cfg.WorkspaceRoot} {
71 err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
72 if err != nil {
73 return err
74 }
75 if !d.Type().IsRegular() {
76 return nil
77 }
78 b, err := os.ReadFile(path)
79 if err != nil {
80 return err
81 }
82 if strings.Contains(string(b), password) || strings.Contains(string(b), header) {
83 t.Error("credential persisted in workspace state")
84 }
85 return nil
86 })
87 if err != nil {
88 t.Fatal(err)
89 }
90 }
91 // Git may echo its environment on failure; callers must get only a safe error.
92 put(t, spy, "#!/bin/sh\nprintf '%s' \"$GIT_CONFIG_VALUE_9\"\nprintf '%s' \"$GIT_CONFIG_VALUE_9\" >&2\nexit 1\n")
93 _, _, err = f.m.fetch(ctx, f.target)
94 if err == nil || strings.Contains(err.Error(), password) || strings.Contains(err.Error(), header) {
95 t.Fatal("git failure leaked credentials")
96 }
97}
98
99func TestWorkspaceGitRequiresPassword(t *testing.T) {
100 f := setup(t)
101 f.forge.remote = Remote{URL: "https://amadan.test/team/repo"}
102 marker := filepath.Join(f.root, "git-ran")
103 spy := filepath.Join(f.root, "git-spy")
104 put(t, spy, "#!/bin/sh\ntouch '"+marker+"'\nexit 1\n")
105 if err := os.Chmod(spy, 0700); err != nil {
106 t.Fatal(err)
107 }
108 f.m.cfg.GitPath = spy
109 _, _, err := f.m.fetch(ctx, f.target)
110 if err == nil || !strings.Contains(err.Error(), "git password") {
111 t.Fatal("missing PAT needs a clear configuration error", err)
112 }
113 if _, err := os.Stat(marker); !os.IsNotExist(err) {
114 t.Fatal("git ran without a PAT")
115 }
116}
117
118func TestWorkspaceGitPasswordValidation(t *testing.T) {
119 for _, password := range []string{"git\rpassword", "git\npassword", "git\x00password", "local"} {
120 t.Run(password, func(t *testing.T) {
121 f := setup(t)
122 marker := filepath.Join(f.root, "git-ran")
123 spy := filepath.Join(f.root, "git-spy")
124 put(t, spy, "#!/bin/sh\ntouch '"+marker+"'\nexit 1\n")
125 if err := os.Chmod(spy, 0700); err != nil {
126 t.Fatal(err)
127 }
128 f.m.cfg.GitPath = spy
129 if password == "local" {
130 f.forge.remote.GitPassword = password
131 } else {
132 f.forge.remote = Remote{URL: "https://amadan.test/team/repo", GitPassword: password}
133 }
134 if _, _, err := f.m.fetch(ctx, f.target); err == nil {
135 t.Fatal("invalid git credential accepted")
136 }
137 if _, err := os.Stat(marker); !os.IsNotExist(err) {
138 t.Fatal("invalid git credential reached git")
139 }
140 })
141 }
142}
143