跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

project_authorization_test.go

80 行 · 2.8 KB历史下载
1package workbench
2
3import (
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
16func 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
59func 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 // Existing versions could persist this grant. A new connection must apply
69 // the current authorization rule before starting a transport.
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}