package forge import ( "encoding/json" "fmt" "os" "os/exec" "path/filepath" "strings" "testing" ) func hostileGitEnvironment(t *testing.T, f *fixture) func() { t.Helper() template := filepath.Join(f.root, "template") hooks := filepath.Join(template, "hooks") marker := filepath.Join(f.root, "hook-ran") helperMarker := filepath.Join(f.root, "helper-ran") must(t, os.MkdirAll(hooks, 0700)) must(t, os.WriteFile(filepath.Join(hooks, "pre-push"), []byte("#!/bin/sh\ntouch "+marker+"\nexit 1\n"), 0700)) helper := filepath.Join(f.root, "credential-helper") must(t, os.WriteFile(helper, []byte("#!/bin/sh\ntouch "+helperMarker+"\nprintf 'username=fixture\\npassword=canary\\n'\n"), 0700)) config := filepath.Join(f.root, "hostile.config") must(t, os.WriteFile(config, []byte(fmt.Sprintf("[init]\n templateDir = %s\n[core]\n hooksPath = %s\n[credential]\n helper = %s\n[url \"file:///nonexistent/forged\"]\n insteadOf = %s\n insteadOf = https://amadan.net/\n", template, hooks, helper, f.remote)), 0600)) // Positive controls stay local and prove the planted template/helper work. control := filepath.Join(f.root, "control.git") gitTest(t, f.root, "init", "--bare", "--template="+template, control) if _, err := os.Stat(filepath.Join(control, "hooks", "pre-push")); err != nil { t.Fatal("template control", err) } c := exec.Command("git", "-c", "credential.helper="+helper, "credential", "fill") c.Env = []string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_NOSYSTEM=1", "GIT_TERMINAL_PROMPT=0"} c.Stdin = strings.NewReader("protocol=https\nhost=example.invalid\n\n") if _, err := c.Output(); err != nil { t.Fatal("helper control", err) } if _, err := os.Stat(helperMarker); err != nil { t.Fatal("helper control missed marker", err) } must(t, os.Remove(helperMarker)) c = exec.Command("git", "-c", "core.hooksPath="+hooks, "push", f.remote, f.auth.Candidate.Commit+":refs/heads/probe") c.Dir = f.source c.Env = []string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_NOSYSTEM=1", "GIT_TERMINAL_PROMPT=0"} if err := c.Run(); err == nil { t.Fatal("hook control did not refuse push") } if _, err := os.Stat(marker); err != nil { t.Fatal("hook control missed marker", err) } must(t, os.Remove(marker)) t.Setenv("PERSON_SECRET", "person-canary") t.Setenv("GIT_TEMPLATE_DIR", template) t.Setenv("GIT_CONFIG_GLOBAL", config) t.Setenv("GIT_CONFIG_SYSTEM", config) t.Setenv("GIT_CONFIG_NOSYSTEM", "0") t.Setenv("GIT_CONFIG_COUNT", "1") t.Setenv("GIT_CONFIG_KEY_0", "url.file:///nonexistent/forged.insteadOf") t.Setenv("GIT_CONFIG_VALUE_0", f.remote) log := filepath.Join(f.root, "git-environment") spy := filepath.Join(f.root, "git-hostile-spy") script := fmt.Sprintf(`#!/usr/bin/python3 import os,sys,json,subprocess args=sys.argv[1:] with open(%q,'a') as out: out.write(json.dumps({'args':args,'env':dict(os.environ)})+'\n') if 'push' in args: subprocess.run(['/usr/bin/git','credential','fill'],input=b'protocol=https\nhost=example.invalid\n\n',stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,env=dict(os.environ)) if 'https://amadan.net/example/repo' in args: args[args.index('https://amadan.net/example/repo')]=%q args=['-c','protocol.file.allow=always']+args os.execve('/usr/bin/git',['/usr/bin/git']+args,dict(os.environ)) `, log, f.remote) must(t, os.WriteFile(spy, []byte(script), 0700)) f.p.localRemote = nil f.p.cfg.GitPath = spy return func() { for _, path := range []string{marker, helperMarker} { if _, err := os.Stat(path); !os.IsNotExist(err) { t.Fatal("hostile Git extension ran", path, err) } } b, err := os.ReadFile(log) must(t, err) for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") { var row struct { Args []string Env map[string]string } must(t, json.Unmarshal([]byte(line), &row)) env := row.Env if env["GIT_CONFIG_GLOBAL"] != "/dev/null" || env["GIT_CONFIG_SYSTEM"] != "/dev/null" || env["GIT_CONFIG_NOSYSTEM"] != "1" || env["GIT_TEMPLATE_DIR"] != "" || env["GIT_CONFIG_VALUE_0"] != "/dev/null" || env["GIT_CONFIG_KEY_1"] != "credential.helper" || env["GIT_CONFIG_VALUE_1"] != "" || env["PERSON_SECRET"] != "" { t.Fatal("unsafe Git environment", env) } args := " " + strings.Join(row.Args, " ") + " " if strings.Contains(args, " init ") && !strings.Contains(args, " --template= ") { t.Fatal("staging template not disabled") } if strings.Contains(args, " push ") && !strings.Contains(args, " --no-verify ") { t.Fatal("push hooks not disabled") } } } }