|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +// runGit runs git in dir with an isolated config so host/global settings (default |
| 16 | +// branch, signing, hooks) can't perturb the test. Fails the test on git error. |
| 17 | +func runGit(t *testing.T, dir string, args ...string) { |
| 18 | + t.Helper() |
| 19 | + cmd := exec.Command("git", args...) |
| 20 | + cmd.Dir = dir |
| 21 | + cmd.Env = append(os.Environ(), |
| 22 | + "GIT_CONFIG_GLOBAL=/dev/null", |
| 23 | + "GIT_CONFIG_SYSTEM=/dev/null", |
| 24 | + ) |
| 25 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 26 | + t.Fatalf("git %v: %v\n%s", args, err, out) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// TestGitCheckoutRejectsDashBranch is the regression guard for the argv-injection |
| 31 | +// fix: a branch beginning with "-" (e.g. "-f") must be rejected with 400 BEFORE |
| 32 | +// any git runs. Previously it flowed straight into the argv as `git checkout -f`, |
| 33 | +// which force-switched and silently discarded all uncommitted work — returning a |
| 34 | +// 200 OK that masked the data loss. |
| 35 | +func TestGitCheckoutRejectsDashBranch(t *testing.T) { |
| 36 | + repo := t.TempDir() |
| 37 | + runGit(t, repo, "init", "-q") |
| 38 | + runGit(t, repo, "config", "user.email", "t@example.com") |
| 39 | + runGit(t, repo, "config", "user.name", "t") |
| 40 | + file := filepath.Join(repo, "a.txt") |
| 41 | + if err := os.WriteFile(file, []byte("committed\n"), 0o644); err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + runGit(t, repo, "add", "a.txt") |
| 45 | + runGit(t, repo, "commit", "-q", "-m", "init") |
| 46 | + // Uncommitted change that a stray `git checkout -f` would revert. |
| 47 | + const dirty = "DIRTY uncommitted\n" |
| 48 | + if err := os.WriteFile(file, []byte(dirty), 0o644); err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + s := &Server{Engine: &Engine{pwd: repo}, ctx: context.Background()} |
| 53 | + rec := httptest.NewRecorder() |
| 54 | + req := httptest.NewRequest(http.MethodPost, "/api/git/checkout", strings.NewReader(`{"branch":"-f"}`)) |
| 55 | + s.handleGitCheckout(rec, req) |
| 56 | + |
| 57 | + if rec.Code != http.StatusBadRequest { |
| 58 | + t.Fatalf("dash branch: want 400, got %d body=%q", rec.Code, rec.Body.String()) |
| 59 | + } |
| 60 | + // The uncommitted change must survive untouched. |
| 61 | + got, err := os.ReadFile(file) |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + if string(got) != dirty { |
| 66 | + t.Fatalf("uncommitted work was destroyed: got %q want %q", got, dirty) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestBlockKind covers the tracked/untracked classifier used to pick safe |
| 71 | +// recovery options in the branch-switch UI. |
| 72 | +func TestBlockKind(t *testing.T) { |
| 73 | + untracked := "error: The following untracked working tree files would be overwritten by checkout:\n\tfoo.txt\n" + |
| 74 | + "Please move or remove them before you switch branches.\nAborting" |
| 75 | + tracked := "error: Your local changes to the following files would be overwritten by checkout:\n\tfoo.txt\n" + |
| 76 | + "Please commit your changes or stash them before you switch branches.\nAborting" |
| 77 | + if got := blockKind(untracked); got != "untracked" { |
| 78 | + t.Errorf("untracked message: got %q want %q", got, "untracked") |
| 79 | + } |
| 80 | + if got := blockKind(tracked); got != "tracked" { |
| 81 | + t.Errorf("tracked message: got %q want %q", got, "tracked") |
| 82 | + } |
| 83 | + if got := blockKind("some unrelated git error"); got != "tracked" { |
| 84 | + t.Errorf("default: got %q want %q", got, "tracked") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestValidatePathsMissingDetection guards the workspace missing-detection fix: a |
| 89 | +// path is reported missing only when it confirmably does not exist (or is not a |
| 90 | +// directory) — never on a transient/permission stat error, which previously hid |
| 91 | +// still-valid workspaces from the picker. |
| 92 | +func TestValidatePathsMissingDetection(t *testing.T) { |
| 93 | + s := &Server{} |
| 94 | + base := t.TempDir() |
| 95 | + notExist := filepath.Join(base, "nope") |
| 96 | + regularFile := filepath.Join(base, "file.txt") |
| 97 | + if err := os.WriteFile(regularFile, []byte("x"), 0o644); err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + |
| 101 | + post := func(paths []string) []string { |
| 102 | + t.Helper() |
| 103 | + body, _ := json.Marshal(map[string][]string{"paths": paths}) |
| 104 | + rec := httptest.NewRecorder() |
| 105 | + s.handleValidatePaths(rec, httptest.NewRequest(http.MethodPost, "/api/validate-paths", strings.NewReader(string(body)))) |
| 106 | + if rec.Code != http.StatusOK { |
| 107 | + t.Fatalf("code=%d body=%q", rec.Code, rec.Body.String()) |
| 108 | + } |
| 109 | + var resp struct { |
| 110 | + Missing []string `json:"missing"` |
| 111 | + } |
| 112 | + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + return resp.Missing |
| 116 | + } |
| 117 | + |
| 118 | + got := post([]string{base, notExist, regularFile}) |
| 119 | + want := map[string]bool{notExist: true, regularFile: true} |
| 120 | + if len(got) != len(want) { |
| 121 | + t.Fatalf("missing detection: got %v, want exactly {notExist, regularFile}", got) |
| 122 | + } |
| 123 | + for _, p := range got { |
| 124 | + if !want[p] { |
| 125 | + t.Fatalf("unexpected missing path %q (existing dir must not be missing); got %v", p, got) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // The fix's core: a path under an unsearchable parent yields EACCES (not |
| 130 | + // not-exist) and must NOT be reported missing. |
| 131 | + if os.Geteuid() == 0 { |
| 132 | + t.Skip("permission check is a no-op as root") |
| 133 | + } |
| 134 | + denied := filepath.Join(base, "denied") |
| 135 | + if err := os.Mkdir(denied, 0o000); err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + // Restore perms so t.TempDir cleanup can remove it (runs before TempDir's own |
| 139 | + // cleanup, which was registered earlier — LIFO). |
| 140 | + t.Cleanup(func() { _ = os.Chmod(denied, 0o755) }) |
| 141 | + if got := post([]string{filepath.Join(denied, "ws")}); len(got) != 0 { |
| 142 | + t.Fatalf("EACCES path must not be reported missing, got %v", got) |
| 143 | + } |
| 144 | +} |
0 commit comments