| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | func hostileGit(t *testing.T, dir string, args ...string) { |
| 15 | t.Helper() |
| 16 | cmd := exec.Command("git", args...) |
| 17 | cmd.Dir = dir |
| 18 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.com", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.com", "GIT_CONFIG_GLOBAL=/dev/null") |
| 19 | if out, err := cmd.CombinedOutput(); err != nil { |
| 20 | t.Fatalf("git %v: %v %s", args, err, out) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // A hostile repository cannot make the read tools run anything or read |
| 25 | // outside the clone: attributes asking for filters, textconv and an |
| 26 | // external diff, hooks, an fsmonitor, and symlinks out of the tree, with |
| 27 | // the clone's own config set as an attacker would set it. |
| 28 | func TestReadToolsHostileRepository(t *testing.T) { |
| 29 | root := t.TempDir() |
| 30 | marker := filepath.Join(root, "pwned") |
| 31 | evil := filepath.Join(root, "evil.sh") |
| 32 | if err := os.WriteFile(evil, []byte("#!/bin/sh\ntouch "+marker+"\ncat\n"), 0700); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | secret := filepath.Join(root, "secret.txt") |
| 36 | if err := os.WriteFile(secret, []byte("outside-the-clone\n"), 0600); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | seed := filepath.Join(root, "seed") |
| 40 | if err := os.MkdirAll(filepath.Join(seed, "src"), 0700); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | files := map[string]string{".gitattributes": "* filter=evil diff=evil\n*.go textconv=evil\n", "src/app.go": "package app\n// Woodstar panes\n", "README.md": "# Woodstar\n\nWoodstar panes.\n"} |
| 44 | for name, body := range files { |
| 45 | if err := os.WriteFile(filepath.Join(seed, name), []byte(body), 0600); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | for name, target := range map[string]string{"escape": "../../../../../../" + strings.TrimPrefix(secret, "/"), "absolute": secret} { |
| 50 | if err := os.Symlink(target, filepath.Join(seed, name)); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | } |
| 54 | hostileGit(t, seed, "init", "-q", "-b", "main") |
| 55 | hostileGit(t, seed, "add", "-A") |
| 56 | hostileGit(t, seed, "commit", "-q", "-m", "hostile") |
| 57 | c := &Clones{Root: filepath.Join(root, "work"), Git: "git"} |
| 58 | repo := "amadan.net/evil/repo" |
| 59 | dir := c.Path(repo) |
| 60 | if err := os.MkdirAll(filepath.Dir(dir), 0700); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | hostileGit(t, root, "clone", "-q", "--bare", seed, dir) |
| 64 | hooks := filepath.Join(dir, "hooks") |
| 65 | os.MkdirAll(hooks, 0700) |
| 66 | for _, h := range []string{"pre-commit", "post-checkout", "reference-transaction", "fsmonitor-watchman", "pre-auto-gc", "post-index-change"} { |
| 67 | os.WriteFile(filepath.Join(hooks, h), []byte("#!/bin/sh\ntouch "+marker+"\n"), 0700) |
| 68 | } |
| 69 | for k, v := range map[string]string{"filter.evil.smudge": evil, "filter.evil.clean": evil, "filter.evil.process": evil, "diff.evil.textconv": evil, "diff.external": evil, "core.fsmonitor": evil, "core.hooksPath": hooks, "core.pager": evil, "grep.patternType": "perl", "attr.tree": "HEAD"} { |
| 70 | hostileGit(t, dir, "config", k, v) |
| 71 | } |
| 72 | x := &Index{Clones: c} |
| 73 | x.catalog = []wire.RepoInfo{{Repo: repo, DefaultBranch: "main"}} |
| 74 | ctx := context.Background() |
| 75 | for _, read := range []func() (string, error){ |
| 76 | func() (string, error) { return x.SearchCode(ctx, repo, "Woodstar") }, |
| 77 | func() (string, error) { return x.ReadFile(ctx, repo, "src/app.go", 1) }, |
| 78 | func() (string, error) { return x.ReadFile(ctx, repo, "README.md", 1) }, |
| 79 | func() (string, error) { return x.ReadFile(ctx, repo, "", 1) }, |
| 80 | func() (string, error) { return x.ReadFile(ctx, repo, "escape", 1) }, |
| 81 | func() (string, error) { return x.ReadFile(ctx, repo, "absolute", 1) }, |
| 82 | } { |
| 83 | out, err := read() |
| 84 | if err != nil { |
| 85 | t.Fatalf("read: %v", err) |
| 86 | } |
| 87 | if strings.Contains(out, "outside-the-clone") { |
| 88 | t.Fatalf("read outside the clone: %q", out) |
| 89 | } |
| 90 | } |
| 91 | if out, _ := x.SearchCode(ctx, repo, "Woodstar"); !strings.Contains(out, "src/app.go:2:// Woodstar panes") { |
| 92 | t.Fatalf("search: %q", out) |
| 93 | } |
| 94 | if out, _ := x.ReadFile(ctx, repo, "escape", 1); out != "A symbolic link (not followed)." { |
| 95 | t.Fatalf("symlink: %q", out) |
| 96 | } |
| 97 | for _, p := range []string{"../secret.txt", "src/../../x", "-c", "HEAD:README.md"} { |
| 98 | if _, err := x.ReadFile(ctx, repo, p, 1); err == nil { |
| 99 | t.Fatalf("path %q accepted", p) |
| 100 | } |
| 101 | } |
| 102 | if _, err := os.Stat(marker); err == nil { |
| 103 | t.Fatal("a hostile repository ran a program") |
| 104 | } |
| 105 | } |
| 106 | |