跳到主要内容

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

浏览文件

windows_attachments_http_test.go

111 行 · 3.5 KB历史下载
1package workbench
2
3import (
4 "encoding/binary"
5 "encoding/json"
6 "io"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "github.com/google/uuid"
14 "github.com/sylvinus/agent-vm/internal/guestos"
15)
16
17func init() {
18 if os.Getenv("CAGE_TEST_WINDOWS_ATTACHMENT_RELAY") != "1" {
19 return
20 }
21 if len(os.Args) != 4 || os.Args[1] != "--relay-profile" || os.Args[3] != "attachments" {
22 os.Exit(2)
23 }
24 // Read the same JSON line and framed bytes as the real native endpoint.
25 var request map[string]string
26 line := make([]byte, 0, 4096)
27 for {
28 var b [1]byte
29 if _, err := io.ReadFull(os.Stdin, b[:]); err != nil {
30 os.Exit(2)
31 }
32 if b[0] == '\n' {
33 break
34 }
35 line = append(line, b[0])
36 if len(line) > 4096 {
37 os.Exit(2)
38 }
39 }
40 if json.Unmarshal(line, &request) != nil || request["action"] != "save" {
41 os.Exit(2)
42 }
43 var size int64
44 for {
45 var header [4]byte
46 if _, err := io.ReadFull(os.Stdin, header[:]); err != nil {
47 os.Exit(2)
48 }
49 n := binary.LittleEndian.Uint32(header[:])
50 if n == 0 {
51 break
52 }
53 if n > 65536 {
54 os.Exit(2)
55 }
56 if _, err := io.CopyN(io.Discard, os.Stdin, int64(n)); err != nil {
57 os.Exit(2)
58 }
59 size += int64(n)
60 }
61 path := `C:\Users\Guest\Cage\Attachments\0123456789abcdef0123456789abcdef\` + request["name"]
62 if os.Getenv("CAGE_TEST_WINDOWS_ATTACHMENT_BAD_PATH") == "1" {
63 path = `C:\outside\` + request["name"]
64 }
65 reply := map[string]any{"ok": true, "scope": "attachments", "path": path}
66 if os.Getenv("CAGE_TEST_WINDOWS_ATTACHMENT_NO_SIZE") != "1" {
67 reply["size"] = size
68 }
69 _ = json.NewEncoder(os.Stdout).Encode(reply)
70 os.Exit(0)
71}
72
73func TestWindowsDesktopUploadHandlerKeepsPathsGuestPrivate(t *testing.T) {
74 s := testServer(t)
75 binding := guestos.WindowsProfile{Version: 1, VMID: uuid.New(), GUIUserSID: "S-1-5-21-1-2-3-1001", GUIUser: "Guest", InstanceDirectory: filepath.Join(s.profile, "unused-vm"), PairingFile: filepath.Join(s.profile, "unused-pair"), RelayExecutable: os.Args[0]}
76 if err := guestos.BindWindowsProfile(s.profile, binding); err != nil {
77 t.Fatal(err)
78 }
79 s.windowsGuest = &binding
80 s.ready = true
81 t.Setenv("CAGE_TEST_WINDOWS_ATTACHMENT_RELAY", "1")
82 id := uuid.NewString()
83 for _, sizeKnown := range []bool{true, false} {
84 if !sizeKnown {
85 t.Setenv("CAGE_TEST_WINDOWS_ATTACHMENT_NO_SIZE", "1")
86 }
87 w := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/desktop/"+id+"?name=note.txt", []byte("abc"))
88 var response map[string]any
89 if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &response) != nil || response["ok"] != true || response["id"] != id || response["name"] != "note.txt" {
90 t.Fatalf("real desktop upload handler failed: %d %s", w.Code, w.Body.String())
91 }
92 if response["path"] != nil || strings.Contains(w.Body.String(), "Attachments") {
93 t.Fatal("desktop upload exposed its native staging path")
94 }
95 _, hasSize := response["size"]
96 if hasSize != sizeKnown {
97 t.Fatal("desktop response invented or lost a native byte count")
98 }
99 if hasSize && response["size"] != float64(3) {
100 t.Fatal("desktop response changed actual received bytes")
101 }
102 }
103 result, err := s.windowsSaveAttachment(t.Context(), "note.txt", strings.NewReader("abc"))
104 if err != nil || !validWindowsAttachmentPath(result["path"].(string), "note.txt") {
105 t.Fatal("terminal/image native save lost its validated path")
106 }
107 t.Setenv("CAGE_TEST_WINDOWS_ATTACHMENT_BAD_PATH", "1")
108 if w := windowsBrowserRequestForTest(s, http.MethodPost, "/api/input/files/desktop/"+uuid.NewString()+"?name=note.txt", []byte("abc")); w.Code == http.StatusOK {
109 t.Fatal("path was hidden before validating native receipt")
110 }
111}