跳到主要内容

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

浏览文件

desktop_file_open_test.go

180 行 · 6.6 KB历史下载
1package workbench
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "os"
7 "path/filepath"
8 "reflect"
9 "strings"
10 "testing"
11
12 "github.com/sylvinus/agent-vm/internal/guestos"
13)
14
15func TestNativeDesktopProjectPathsUseCurrentGuestMount(t *testing.T) {
16 for _, system := range []string{"macos", "windows"} {
17 t.Run(system, func(t *testing.T) {
18 s := testServer(t)
19 project := t.TempDir()
20 file := filepath.Join(project, "中文 #+%.md")
21 if err := os.WriteFile(file, []byte("project document"), 0600); err != nil {
22 t.Fatal(err)
23 }
24 var guest string
25 closed := make(chan struct{})
26 if system == "macos" {
27 s.macosGuest = &guestos.MacOSProfile{User: "cage"}
28 guest = macOSProjectPath("cage", project)
29 } else {
30 s.windowsGuest = &guestos.WindowsProfile{}
31 guest = "C:/Cage/Projects/0123456789abcdef"
32 s.projectShares = map[string]*projectShare{project: {done: closed, windowsProject: &windowsProjectBinding{Path: guest, ID: "0123456789abcdef", Grant: "test"}}}
33 }
34 got, err := s.resolveDesktopProjectFile(guest+"/"+filepath.Base(file), []string{project})
35 want, _ := filepath.EvalSymlinks(file)
36 if err != nil || got != want {
37 t.Fatalf("guest mount did not resolve to host original: %q %v", got, err)
38 }
39 for _, invalid := range []string{guest + "-other/" + filepath.Base(file), guest + "/../outside", guest + "/missing", "relative", "file://" + guest} {
40 if _, err := s.resolveDesktopProjectFile(invalid, []string{project}); err == nil {
41 t.Fatalf("accepted invalid guest path %q", invalid)
42 }
43 }
44 if system == "windows" {
45 close(closed)
46 if _, err := s.resolveDesktopProjectFile(guest+"/"+filepath.Base(file), []string{project}); err == nil {
47 t.Fatal("revoked Windows share still granted file access")
48 }
49 }
50 })
51 }
52}
53
54func TestDesktopFilePathsStayInProjectGrants(t *testing.T) {
55 project := t.TempDir()
56 file := filepath.Join(project, "中文 Word #+%.docx")
57 if err := os.WriteFile(file, []byte("document"), 0600); err != nil {
58 t.Fatal(err)
59 }
60 for _, path := range []string{file, project} {
61 got, err := resolveProjectFilePath(guestPath(path), []string{project})
62 want, _ := filepath.EvalSymlinks(path)
63 if err != nil || got != want {
64 t.Fatalf("project file did not map: %q %v", got, err)
65 }
66 }
67 outside := filepath.Join(t.TempDir(), "secret.txt")
68 if err := os.WriteFile(outside, []byte("private"), 0600); err != nil {
69 t.Fatal(err)
70 }
71 for _, path := range []string{outside, project + "-sibling/secret.txt", guestPath(project) + "/../secret", "file:///etc/passwd", "relative", guestPath(file) + "\x00", guestPath(project) + "/missing"} {
72 if got, err := resolveProjectFilePath(path, []string{project}); err == nil {
73 t.Fatalf("ungranted path accepted: %q -> %q", path, got)
74 }
75 }
76 link := filepath.Join(project, "outside-link")
77 if err := os.Symlink(filepath.Dir(outside), link); err != nil {
78 t.Skip("symlink unavailable: " + err.Error())
79 }
80 if _, err := resolveProjectFilePath(guestPath(filepath.Join(link, "secret.txt")), []string{project}); err == nil {
81 t.Fatal("host-side symlink escaped project")
82 }
83}
84
85func TestDesktopFileCommandsPreservePathsAndRevealSemantics(t *testing.T) {
86 path := "/tmp/项目/Word #+%; $(touch unsafe).docx"
87 for _, tc := range []struct {
88 platform, action string
89 directory bool
90 want []string
91 }{
92 {"darwin", "open", false, []string{"/usr/bin/open", "--", path}},
93 {"darwin", "reveal", false, []string{"/usr/bin/open", "-R", "--", path}},
94 {"windows", "open", false, []string{"rundll32.exe", "url.dll,FileProtocolHandler", path}},
95 {"windows", "open", true, []string{"explorer.exe", path}},
96 {"windows", "reveal", false, []string{"explorer.exe", "/select," + path}},
97 {"linux", "open", false, []string{"gio", "open", path}},
98 } {
99 got := hostFileCommand(tc.platform, path, tc.action, tc.directory).Args
100 if !reflect.DeepEqual(got, tc.want) {
101 t.Fatalf("%s %s: %#v", tc.platform, tc.action, got)
102 }
103 }
104 args := hostFileCommand("linux", path, "reveal", false).Args
105 if args[0] != "gdbus" || !strings.Contains(strings.Join(args, " "), "ShowItems") || !strings.Contains(args[len(args)-2], "%23+%25") {
106 t.Fatal("Linux reveal lost selection or URI escaping", args)
107 }
108}
109
110func TestDesktopFileOpenAdmissionRejectsUntrustedRequests(t *testing.T) {
111 s := testServer(t)
112 s.ready = true
113 path := "/api/input/files/desktop/open"
114 body := `{"id":"` + testBrowserFileID + `","stage":"claim"}`
115 for _, tc := range []struct {
116 origin string
117 auth bool
118 body string
119 code int
120 }{
121 {s.origin, false, body, 401},
122 {"https://untrusted.example", true, body, 403},
123 {s.origin, true, `{"id":"../host-path","stage":"claim"}`, 400},
124 {s.origin, true, `{"id":"` + testBrowserFileID + `","stage":"open","path":"/etc/passwd"}`, 400},
125 } {
126 w := request(s, "POST", path, tc.origin, tc.body, tc.auth)
127 if w.Code != tc.code {
128 t.Fatal(w.Code, w.Body.String())
129 }
130 }
131 s.account.lock()
132 if w := request(s, "POST", path, s.origin, body, true); w.Code != 423 {
133 t.Fatal("locked file operation accepted", w.Code)
134 }
135}
136
137func TestDesktopFileErrorsReachNativeCaller(t *testing.T) {
138 origin := ""
139 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
140 if r.Header.Get("Origin") != origin || r.Header.Get(connectionHeader) != "connection-one" {
141 t.Error("missing file operation origin or connection binding")
142 }
143 http.Error(w, "只能打开 Cage 项目目录中的文件和文件夹", 400)
144 }))
145 defer server.Close()
146 origin = server.URL
147 client := &controllerClient{origin: server.URL, client: server.Client()}
148 client.client.Transport = connectionTransport{base: http.DefaultTransport, id: "connection-one"}
149 err := desktopOpenCall(client, map[string]string{"id": testBrowserFileID, "stage": "claim"}, nil)
150 if err == nil || err.Error() != "只能打开 Cage 项目目录中的文件和文件夹" {
151 t.Fatal("native caller lost useful error", err)
152 }
153}
154
155func TestDesktopFileNativeReveal(t *testing.T) {
156 if os.Getenv("CAGE_TEST_NATIVE_REVEAL") != "1" {
157 t.Skip("explicit desktop integration test")
158 }
159 project := t.TempDir()
160 file := filepath.Join(project, "Cage 生成的 Word 测试.docx")
161 if fixture := os.Getenv("CAGE_TEST_NATIVE_REVEAL_PATH"); fixture != "" {
162 file, project = fixture, filepath.Dir(fixture)
163 } else {
164 if err := os.WriteFile(file, []byte("reveal fixture"), 0600); err != nil {
165 t.Fatal(err)
166 }
167 }
168 resolved, err := resolveProjectFilePath(guestPath(file), []string{project})
169 if err != nil {
170 t.Fatal(err)
171 }
172 if err = openHostFile(resolved, "reveal"); err != nil {
173 t.Fatal(err)
174 }
175 if os.Getenv("CAGE_TEST_NATIVE_OPEN") == "1" {
176 if err = openHostFile(resolved, "open"); err != nil {
177 t.Fatal(err)
178 }
179 }
180}