| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 14 | ) |
| 15 | |
| 16 | func TestProjectAuthorizationProtectsCredentialsAcrossGuests(t *testing.T) { |
| 17 | home := t.TempDir() |
| 18 | t.Setenv("HOME", home) |
| 19 | t.Setenv("USERPROFILE", home) |
| 20 | credentials := filepath.Join(home, ".ssh") |
| 21 | siblingState := filepath.Join(home, ".claude-cage", "state") |
| 22 | for _, directory := range []string{credentials, siblingState} { |
| 23 | if err := os.MkdirAll(directory, 0700); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | } |
| 27 | for _, system := range []string{"linux", "macos", "windows"} { |
| 28 | t.Run(system, func(t *testing.T) { |
| 29 | s := testServer(t) |
| 30 | s.ready = true |
| 31 | if system == "macos" { |
| 32 | s.macosGuest = &guestos.MacOSProfile{Instance: "mac", User: "guestdev"} |
| 33 | } |
| 34 | if system == "windows" { |
| 35 | s.windowsGuest = &guestos.WindowsProfile{} |
| 36 | } |
| 37 | for _, directory := range []string{credentials, siblingState} { |
| 38 | body, _ := json.Marshal(map[string]any{"workspace": directory, "add": true}) |
| 39 | response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/projects/open", body) |
| 40 | if response.Code != http.StatusBadRequest || !strings.Contains(response.Body.String(), "共享") { |
| 41 | t.Fatalf("credential project admission: status=%d body=%s", response.Code, response.Body.String()) |
| 42 | } |
| 43 | paths, err := s.projectPaths() |
| 44 | if err != nil || slices.Contains(paths, directory) || len(s.projectShares) != 0 { |
| 45 | t.Fatalf("rejected project retained a grant or connection: %v", err) |
| 46 | } |
| 47 | response = windowsBrowserRequestForTest(s, http.MethodGet, "/api/projects/directories?path="+url.QueryEscape(directory), nil) |
| 48 | if response.Code == http.StatusOK { |
| 49 | var listing struct{ Selectable bool } |
| 50 | if json.Unmarshal(response.Body.Bytes(), &listing) != nil || listing.Selectable { |
| 51 | t.Fatal("directory browser offered a protected project") |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestProjectRestoreRevalidatesExistingCredentialGrant(t *testing.T) { |
| 60 | home := t.TempDir() |
| 61 | t.Setenv("HOME", home) |
| 62 | t.Setenv("USERPROFILE", home) |
| 63 | s := testServer(t) |
| 64 | credentials := filepath.Join(home, ".ssh") |
| 65 | if err := os.Mkdir(credentials, 0700); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | |
| 69 | |
| 70 | if err := s.rememberSharedProject(credentials); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | s.restoreProjectShares() |
| 74 | if len(s.projectShares) != 0 || len(s.projectErrors) != 1 || s.projectErrors[0].Path != credentials { |
| 75 | t.Fatalf("old credential grant was not rejected on restore: %+v", s.projectErrors) |
| 76 | } |
| 77 | if !strings.Contains(s.projectErrors[0].Message, "凭据") { |
| 78 | t.Fatalf("restore did not report the authorization failure: %+v", s.projectErrors) |
| 79 | } |
| 80 | } |