跳到主要内容

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

浏览文件

macos_desktop_files_test.go

198 行 · 7.5 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "bytes"
6 "encoding/base64"
7 "encoding/binary"
8 "encoding/json"
9 "image"
10 "image/png"
11 "io"
12 "net/http"
13 "strings"
14 "sync"
15 "testing"
16
17 "golang.org/x/crypto/ssh"
18)
19
20func TestMacOSDesktopAttachmentsUsePrivateNativeOperations(t *testing.T) {
21 const id = "aa447f34-18bc-48ba-b3f8-93bbf7f7a315"
22 var mu sync.Mutex
23 var uploaded, pasted []byte
24 var calls []string
25 var rejectAdd, discarded bool
26 guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) {
27 reader := bufio.NewReader(channel)
28 if command == "exec '/Applications/Cage Guest Clipboard.app/Contents/MacOS/CageGuestClipboard' --client request" {
29 var request struct {
30 Operation string
31 Files []string
32 }
33 if json.NewDecoder(reader).Decode(&request) != nil || request.Operation != "discard" || len(request.Files) != 1 || request.Files[0] != "/tmp/cage-clipboard-12345/资料.bin" {
34 return 1, true
35 }
36 mu.Lock()
37 discarded = true
38 mu.Unlock()
39 json.NewEncoder(channel).Encode(map[string]any{"ok": true})
40 return 0, true
41 }
42 if command == "exec '/Applications/Cage Guest Clipboard.app/Contents/MacOS/CageGuestClipboard' --client upload" {
43 line, err := reader.ReadBytes('\n')
44 var request struct{ Name string }
45 if err != nil || json.Unmarshal(line, &request) != nil || (request.Name != "资料.bin" && request.Name != "image.png") {
46 return 1, true
47 }
48 var data bytes.Buffer
49 for {
50 var header [4]byte
51 if _, err := io.ReadFull(reader, header[:]); err != nil {
52 return 1, true
53 }
54 length := binary.LittleEndian.Uint32(header[:])
55 if length == 0 {
56 break
57 }
58 if length > 65536 {
59 return 1, true
60 }
61 if _, err := io.CopyN(&data, reader, int64(length)); err != nil {
62 return 1, true
63 }
64 }
65 mu.Lock()
66 if request.Name == "image.png" {
67 pasted = append([]byte(nil), data.Bytes()...)
68 } else {
69 uploaded = append([]byte(nil), data.Bytes()...)
70 }
71 mu.Unlock()
72 json.NewEncoder(channel).Encode(map[string]any{"paths": []string{"/tmp/cage-clipboard-12345/" + request.Name}})
73 return 0, true
74 }
75 if !strings.Contains(command, `dev.cage.GuestClaudeWindow/window.sock`) {
76 return 1, false
77 }
78 line, err := reader.ReadBytes('\n')
79 var request struct {
80 Action, ID, Kind string
81 Files []string
82 Length int
83 PID, Window int
84 }
85 if err != nil || json.Unmarshal(line, &request) != nil {
86 return 1, true
87 }
88 mu.Lock()
89 calls = append(calls, request.Action)
90 mu.Unlock()
91 switch request.Action {
92 case "files-start":
93 if (request.Kind != "paste" && request.Kind != "image") || request.PID != 100 || request.Window != 7 {
94 return 1, true
95 }
96 json.NewEncoder(channel).Encode(map[string]any{"ok": true, "id": id})
97 case "files-validate", "files-finish", "files-finish-image", "files-cancel":
98 if request.ID != id {
99 return 1, true
100 }
101 json.NewEncoder(channel).Encode(map[string]any{"ok": true})
102 case "files-add":
103 if request.ID != id || len(request.Files) != 1 || (request.Files[0] != "/tmp/cage-clipboard-12345/资料.bin" && request.Files[0] != "/tmp/cage-clipboard-12345/image.png") {
104 return 1, true
105 }
106 mu.Lock()
107 reject := rejectAdd
108 mu.Unlock()
109 if reject {
110 json.NewEncoder(channel).Encode(map[string]any{"error": "attachment_cancelled"})
111 return 0, true
112 }
113 json.NewEncoder(channel).Encode(map[string]any{"ok": true, "id": id})
114 case "files-poll":
115 json.NewEncoder(channel).Encode(map[string]any{"available": true, "uploads": []any{map[string]any{"id": id, "kind": "chooser", "multiple": true, "target": "desktop"}}, "downloads": []any{}})
116 case "paste-image":
117 if request.Length <= 0 || request.Length > maxImageBytes || request.PID != 100 || request.Window != 7 {
118 return 1, true
119 }
120 data := make([]byte, request.Length)
121 if _, err := io.ReadFull(reader, data); err != nil {
122 return 1, true
123 }
124 mu.Lock()
125 pasted = data
126 mu.Unlock()
127 json.NewEncoder(channel).Encode(map[string]any{"ok": true})
128 default:
129 return 1, true
130 }
131 return 0, true
132 })
133 s := guest.s
134 missingTarget := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/start", []byte(`{"target":"desktop","kind":"paste"}`))
135 if missingTarget.Code == http.StatusOK {
136 t.Fatal("attachment without an observed window was accepted")
137 }
138 start := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/start", []byte(`{"target":"desktop","kind":"paste","pid":100,"window":7}`))
139 if start.Code != http.StatusOK || !strings.Contains(start.Body.String(), id) {
140 t.Fatalf("start failed: %d %s", start.Code, start.Body.String())
141 }
142 content := []byte{0, 255, 128, 1, 2, 3}
143 upload := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/desktop/"+id+"?name=%E8%B5%84%E6%96%99.bin", content)
144 if upload.Code != http.StatusOK || strings.Contains(upload.Body.String(), "/tmp/") {
145 t.Fatalf("upload failed or exposed private path: %d %s", upload.Code, upload.Body.String())
146 }
147 for _, action := range []string{"finish", "cancel"} {
148 response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/"+action, []byte(`{"target":"desktop","id":"`+id+`"}`))
149 if response.Code != http.StatusOK {
150 t.Fatalf("%s failed: %s", action, response.Body.String())
151 }
152 }
153 poll := windowsBrowserRequestForTest(s, http.MethodGet, "/api/input/files/desktop/requests", nil)
154 if poll.Code != http.StatusOK || !strings.Contains(poll.Body.String(), `"kind":"chooser"`) {
155 t.Fatalf("chooser poll failed: %d %s", poll.Code, poll.Body.String())
156 }
157 var picture bytes.Buffer
158 if err := png.Encode(&picture, image.NewRGBA(image.Rect(0, 0, 3, 2))); err != nil {
159 t.Fatal(err)
160 }
161 prepared := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/paste/start", []byte(`{"target":"desktop","pid":100,"window":7}`))
162 if prepared.Code != http.StatusOK || !strings.Contains(prepared.Body.String(), id) {
163 t.Fatalf("image preparation failed: %d %s", prepared.Code, prepared.Body.String())
164 }
165 body, _ := json.Marshal(map[string]any{"target": "desktop", "png": base64.StdEncoding.EncodeToString(picture.Bytes()), "transfer": id, "pid": 100, "window": 7})
166 response := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/image", body)
167 if response.Code != http.StatusOK {
168 t.Fatalf("image failed: %d %s", response.Code, response.Body.String())
169 }
170 mu.Lock()
171 if !bytes.Equal(uploaded, content) || !bytes.Equal(pasted, picture.Bytes()) {
172 mu.Unlock()
173 t.Fatal("private transports changed file or PNG bytes")
174 }
175 if strings.Join(calls, ",") != "files-start,files-validate,files-add,files-finish,files-cancel,files-poll,files-start,files-validate,files-add,files-finish-image" {
176 mu.Unlock()
177 t.Fatalf("native operations diverged: %v", calls)
178 }
179 rejectAdd = true
180 mu.Unlock()
181 failed := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/desktop/"+id+"?name=%E8%B5%84%E6%96%99.bin", content)
182 mu.Lock()
183 defer mu.Unlock()
184 if failed.Code == http.StatusOK || !discarded {
185 t.Fatal("a completed upload abandoned by its window was not cleaned")
186 }
187}
188
189func TestMacOSDesktopAttachmentPathsRejectEscapes(t *testing.T) {
190 if !validMacOSClipboardAttachment("/var/folders/ab/xyz/T/cage-clipboard-123/资料.bin", "资料.bin") {
191 t.Fatal("private guest cache refused")
192 }
193 for _, value := range []string{"/Users/cage/secret.txt", "/tmp/cage-clipboard-123/../secret.txt", "/tmp/cage-clipboard-123/file:stream", "/tmp/other-123/资料.bin", "/tmp/cage-clipboard-abc/资料.bin"} {
194 if validMacOSClipboardAttachment(value, "资料.bin") {
195 t.Fatal("unregistered path form accepted")
196 }
197 }
198}