package thin import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) func hostileGit(t *testing.T, dir string, args ...string) { t.Helper() cmd := exec.Command("git", args...) cmd.Dir = dir 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") if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("git %v: %v %s", args, err, out) } } // A hostile repository cannot make the read tools run anything or read // outside the clone: attributes asking for filters, textconv and an // external diff, hooks, an fsmonitor, and symlinks out of the tree, with // the clone's own config set as an attacker would set it. func TestReadToolsHostileRepository(t *testing.T) { root := t.TempDir() marker := filepath.Join(root, "pwned") evil := filepath.Join(root, "evil.sh") if err := os.WriteFile(evil, []byte("#!/bin/sh\ntouch "+marker+"\ncat\n"), 0700); err != nil { t.Fatal(err) } secret := filepath.Join(root, "secret.txt") if err := os.WriteFile(secret, []byte("outside-the-clone\n"), 0600); err != nil { t.Fatal(err) } seed := filepath.Join(root, "seed") if err := os.MkdirAll(filepath.Join(seed, "src"), 0700); err != nil { t.Fatal(err) } 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"} for name, body := range files { if err := os.WriteFile(filepath.Join(seed, name), []byte(body), 0600); err != nil { t.Fatal(err) } } for name, target := range map[string]string{"escape": "../../../../../../" + strings.TrimPrefix(secret, "/"), "absolute": secret} { if err := os.Symlink(target, filepath.Join(seed, name)); err != nil { t.Fatal(err) } } hostileGit(t, seed, "init", "-q", "-b", "main") hostileGit(t, seed, "add", "-A") hostileGit(t, seed, "commit", "-q", "-m", "hostile") c := &Clones{Root: filepath.Join(root, "work"), Git: "git"} repo := "amadan.net/evil/repo" dir := c.Path(repo) if err := os.MkdirAll(filepath.Dir(dir), 0700); err != nil { t.Fatal(err) } hostileGit(t, root, "clone", "-q", "--bare", seed, dir) hooks := filepath.Join(dir, "hooks") os.MkdirAll(hooks, 0700) for _, h := range []string{"pre-commit", "post-checkout", "reference-transaction", "fsmonitor-watchman", "pre-auto-gc", "post-index-change"} { os.WriteFile(filepath.Join(hooks, h), []byte("#!/bin/sh\ntouch "+marker+"\n"), 0700) } 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"} { hostileGit(t, dir, "config", k, v) } x := &Index{Clones: c} x.catalog = []wire.RepoInfo{{Repo: repo, DefaultBranch: "main"}} ctx := context.Background() for _, read := range []func() (string, error){ func() (string, error) { return x.SearchCode(ctx, repo, "Woodstar") }, func() (string, error) { return x.ReadFile(ctx, repo, "src/app.go", 1) }, func() (string, error) { return x.ReadFile(ctx, repo, "README.md", 1) }, func() (string, error) { return x.ReadFile(ctx, repo, "", 1) }, func() (string, error) { return x.ReadFile(ctx, repo, "escape", 1) }, func() (string, error) { return x.ReadFile(ctx, repo, "absolute", 1) }, } { out, err := read() if err != nil { t.Fatalf("read: %v", err) } if strings.Contains(out, "outside-the-clone") { t.Fatalf("read outside the clone: %q", out) } } if out, _ := x.SearchCode(ctx, repo, "Woodstar"); !strings.Contains(out, "src/app.go:2:// Woodstar panes") { t.Fatalf("search: %q", out) } if out, _ := x.ReadFile(ctx, repo, "escape", 1); out != "A symbolic link (not followed)." { t.Fatalf("symlink: %q", out) } for _, p := range []string{"../secret.txt", "src/../../x", "-c", "HEAD:README.md"} { if _, err := x.ReadFile(ctx, repo, p, 1); err == nil { t.Fatalf("path %q accepted", p) } } if _, err := os.Stat(marker); err == nil { t.Fatal("a hostile repository ran a program") } }