| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | func TestGuestClientMountActivationAndRevocation(t *testing.T) { |
| 18 | s := testServer(t) |
| 19 | s.ready = true |
| 20 | s.preferences.Workspace = t.TempDir() |
| 21 | prefsFile := filepath.Join(s.profile, "workbench.json") |
| 22 | if err := writePrivate(prefsFile, s.preferences); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | before, err := os.ReadFile(prefsFile) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | path := clientMountRoot + "/" + strings.Repeat("b", 32) |
| 30 | ctx, cancel := context.WithCancel(context.Background()) |
| 31 | defer cancel() |
| 32 | mount := &guestClientMount{ctx: ctx} |
| 33 | s.clientMounts = map[string]*guestClientMount{path: mount} |
| 34 | if _, err := s.connectProject(path, true); !errors.Is(err, errClientMountGone) { |
| 35 | t.Fatalf("accepted project before mount verification: %v", err) |
| 36 | } |
| 37 | mount.ready = true |
| 38 | body := `{"workspace":"` + path + `","add":false,"activate":true}` |
| 39 | if got := request(s, "POST", "/api/projects/open", s.origin, body, true); got.Code != http.StatusOK { |
| 40 | t.Fatalf("activate: %d %s", got.Code, got.Body) |
| 41 | } |
| 42 | cwd, err := s.connectProject("", false) |
| 43 | if err != nil || cwd != path { |
| 44 | t.Fatalf("default terminal workspace = %q, %v", cwd, err) |
| 45 | } |
| 46 | if !strings.HasPrefix(terminalScript(cwd, "claude", "claude"), "cd "+path+" &&") || |
| 47 | !strings.HasPrefix(managedClaudeTerminalScript(cwd, "claude", "default", "sub", false), "cd "+path+" &&") { |
| 48 | t.Fatal("CLI would launch outside the mounted workspace") |
| 49 | } |
| 50 | after, err := os.ReadFile(prefsFile) |
| 51 | if err != nil || string(after) != string(before) { |
| 52 | t.Fatal("activating a client mount persisted it as the default") |
| 53 | } |
| 54 | if _, err := os.Stat(filepath.Join(s.profile, "projects.json")); !os.IsNotExist(err) { |
| 55 | t.Fatal("created a persistent grant for the ephemeral guest path") |
| 56 | } |
| 57 | cancel() |
| 58 | delete(s.clientMounts, path) |
| 59 | for _, name := range []string{"", path, path + "/..", path + "/subdir"} { |
| 60 | if _, err := s.connectProject(name, true); !errors.Is(err, errClientMountGone) { |
| 61 | t.Fatalf("admitted revoked/unregistered guest path %q: %v", name, err) |
| 62 | } |
| 63 | } |
| 64 | paths, err := s.projectPaths() |
| 65 | if err != nil || slices.Contains(paths, path) { |
| 66 | t.Fatal("revoked share remained in available projects", err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestGuestClientShareRequiresWorkbenchSessionOriginAndUnlock(t *testing.T) { |
| 71 | s := testServer(t) |
| 72 | url := "/api/projects/client-share?id=" + strings.Repeat("c", 32) |
| 73 | if got := request(s, "POST", url, s.origin, "", false); got.Code != http.StatusUnauthorized { |
| 74 | t.Fatalf("missing session: HTTP %d", got.Code) |
| 75 | } |
| 76 | if got := request(s, "POST", url, "https://untrusted.example", "", true); got.Code != http.StatusForbidden { |
| 77 | t.Fatalf("foreign origin: HTTP %d", got.Code) |
| 78 | } |
| 79 | s.account.lock() |
| 80 | if got := request(s, "POST", url, s.origin, "", true); got.Code != http.StatusLocked { |
| 81 | t.Fatalf("locked account: HTTP %d", got.Code) |
| 82 | } |
| 83 | } |