oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package forge
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13func hostileGitEnvironment(t *testing.T, f *fixture) func() {
14 t.Helper()
15 template := filepath.Join(f.root, "template")
16 hooks := filepath.Join(template, "hooks")
17 marker := filepath.Join(f.root, "hook-ran")
18 helperMarker := filepath.Join(f.root, "helper-ran")
19 must(t, os.MkdirAll(hooks, 0700))
20 must(t, os.WriteFile(filepath.Join(hooks, "pre-push"), []byte("#!/bin/sh\ntouch "+marker+"\nexit 1\n"), 0700))
21 helper := filepath.Join(f.root, "credential-helper")
22 must(t, os.WriteFile(helper, []byte("#!/bin/sh\ntouch "+helperMarker+"\nprintf 'username=fixture\\npassword=canary\\n'\n"), 0700))
23 config := filepath.Join(f.root, "hostile.config")
24 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))
25 // Positive controls stay local and prove the planted template/helper work.
26 control := filepath.Join(f.root, "control.git")
27 gitTest(t, f.root, "init", "--bare", "--template="+template, control)
28 if _, err := os.Stat(filepath.Join(control, "hooks", "pre-push")); err != nil {
29 t.Fatal("template control", err)
30 }
31 c := exec.Command("git", "-c", "credential.helper="+helper, "credential", "fill")
32 c.Env = []string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_NOSYSTEM=1", "GIT_TERMINAL_PROMPT=0"}
33 c.Stdin = strings.NewReader("protocol=https\nhost=example.invalid\n\n")
34 if _, err := c.Output(); err != nil {
35 t.Fatal("helper control", err)
36 }
37 if _, err := os.Stat(helperMarker); err != nil {
38 t.Fatal("helper control missed marker", err)
39 }
40 must(t, os.Remove(helperMarker))
41 c = exec.Command("git", "-c", "core.hooksPath="+hooks, "push", f.remote, f.auth.Candidate.Commit+":refs/heads/probe")
42 c.Dir = f.source
43 c.Env = []string{"PATH=/usr/bin:/bin", "HOME=/nonexistent", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_NOSYSTEM=1", "GIT_TERMINAL_PROMPT=0"}
44 if err := c.Run(); err == nil {
45 t.Fatal("hook control did not refuse push")
46 }
47 if _, err := os.Stat(marker); err != nil {
48 t.Fatal("hook control missed marker", err)
49 }
50 must(t, os.Remove(marker))
51 t.Setenv("PERSON_SECRET", "person-canary")
52 t.Setenv("GIT_TEMPLATE_DIR", template)
53 t.Setenv("GIT_CONFIG_GLOBAL", config)
54 t.Setenv("GIT_CONFIG_SYSTEM", config)
55 t.Setenv("GIT_CONFIG_NOSYSTEM", "0")
56 t.Setenv("GIT_CONFIG_COUNT", "1")
57 t.Setenv("GIT_CONFIG_KEY_0", "url.file:///nonexistent/forged.insteadOf")
58 t.Setenv("GIT_CONFIG_VALUE_0", f.remote)
59 log := filepath.Join(f.root, "git-environment")
60 spy := filepath.Join(f.root, "git-hostile-spy")
61 script := fmt.Sprintf(`#!/usr/bin/python3
62import os,sys,json,subprocess
63args=sys.argv[1:]
64with open(%q,'a') as out:
65 out.write(json.dumps({'args':args,'env':dict(os.environ)})+'\n')
66if 'push' in args:
67 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))
68if 'https://amadan.net/example/repo' in args:
69 args[args.index('https://amadan.net/example/repo')]=%q
70 args=['-c','protocol.file.allow=always']+args
71os.execve('/usr/bin/git',['/usr/bin/git']+args,dict(os.environ))
72`, log, f.remote)
73 must(t, os.WriteFile(spy, []byte(script), 0700))
74 f.p.localRemote = nil
75 f.p.cfg.GitPath = spy
76 return func() {
77 for _, path := range []string{marker, helperMarker} {
78 if _, err := os.Stat(path); !os.IsNotExist(err) {
79 t.Fatal("hostile Git extension ran", path, err)
80 }
81 }
82 b, err := os.ReadFile(log)
83 must(t, err)
84 for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") {
85 var row struct {
86 Args []string
87 Env map[string]string
88 }
89 must(t, json.Unmarshal([]byte(line), &row))
90 env := row.Env
91 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"] != "" {
92 t.Fatal("unsafe Git environment", env)
93 }
94 args := " " + strings.Join(row.Args, " ") + " "
95 if strings.Contains(args, " init ") && !strings.Contains(args, " --template= ") {
96 t.Fatal("staging template not disabled")
97 }
98 if strings.Contains(args, " push ") && !strings.Contains(args, " --no-verify ") {
99 t.Fatal("push hooks not disabled")
100 }
101 }
102 }
103}
104