跳到主要内容

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

浏览文件

environment_versions_test.go

164 行 · 5.3 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "strings"
8 "testing"
9 "time"
10
11 "github.com/sylvinus/agent-vm/internal/cage"
12)
13
14func TestGuestConnectionRequiresSuccessfulMatchingManifest(t *testing.T) {
15 required := cage.RequiredGuestManifest()
16 for _, name := range []string{"current", "missing", "old-revision", "old-protocol", "broken"} {
17 t.Run(name, func(t *testing.T) {
18 manifest := required
19 if name == "old-revision" {
20 manifest.Revision = "previous"
21 }
22 if name == "old-protocol" {
23 manifest.Protocol++
24 }
25 encoded, _ := json.Marshal(manifest)
26 if name == "missing" {
27 encoded = []byte("null")
28 }
29 if name == "broken" {
30 encoded = []byte("{")
31 }
32 credentials, current, err := parseGuestConnection(append([]byte("{\"token\":\"guest-test\"}\npassword\n"), encoded...))
33 if name == "current" {
34 if err != nil || current == nil || credentials.Token != "guest-test" || credentials.VNCPassword != "password" {
35 t.Fatal(current, err)
36 }
37 } else if !errors.Is(err, errGuestUpdateRequired) {
38 t.Fatalf("stale guest accepted: %v", err)
39 }
40 })
41 }
42}
43
44func grantedToolUpdateServer(t *testing.T) (*Server, *cage.EgressGate) {
45 t.Helper()
46 s, gate := interfaceMonitorServer(t)
47 grantTestGate(t, gate)
48 s.ready = true
49 required := cage.RequiredGuestManifest()
50 s.guestManifest = &required
51 s.setJobForTest(Job{ID: "tools", Kind: "environment-tools", Running: true})
52 s.publishGate(gate.Status())
53 return s, gate
54}
55
56func grantTestGate(t *testing.T, gate *cage.EgressGate) {
57 t.Helper()
58 state := gate.Status()
59 report := cage.SafetyReport{Policy: cage.SafetyPolicyVersion, ID: "tool-update-test", RunID: state.RunID, CheckID: state.CheckID, Configuration: state.Configuration, CreatedAt: time.Now()}
60 for _, id := range []string{"egress_policy", "proxy_transport", "dns_transport", "guest_settings", "browser_settings", "filesystem_boundary", "management_boundary", "fail_closed", "udp_policy", "ipv6_policy", "exit_consistency"} {
61 report.Checks = append(report.Checks, cage.SafetyCheck{ID: id, Title: id, Critical: true, State: cage.CheckPassed, Evidence: "synthetic isolated fixture", CheckedAt: report.CreatedAt})
62 }
63 if err := gate.Grant(report); err != nil {
64 t.Fatal(err)
65 }
66}
67
68func TestToolUpdateLeaseHonorsPauseStopAndConfigurationAtRenewal(t *testing.T) {
69 for _, jobKind := range []string{"environment-tools", "claude-desktop-install"} {
70 t.Run(jobKind, func(t *testing.T) {
71 for _, condition := range []string{"allowed", "paused", "stop", "configuration-changed"} {
72 t.Run(condition, func(t *testing.T) {
73 s, gate := grantedToolUpdateServer(t)
74 s.setJobForTest(Job{ID: "tools", Kind: jobKind, Running: true})
75 client, err := s.gateClient()
76 if err != nil {
77 t.Fatal(err)
78 }
79 before := gate.Status()
80 // Windows can grant and renew within the same wall-clock tick.
81 // Advance past the grant tick before requiring a later deadline.
82 for !time.Now().Add(cage.GateLease).After(before.Until) {
83 time.Sleep(time.Millisecond)
84 }
85 switch condition {
86 case "paused":
87 if err := s.pauseNetworkNow("test user pause"); err != nil {
88 t.Fatal(err)
89 }
90 case "stop":
91 s.setJobForTest(Job{ID: "stop", Kind: "stop", Running: true})
92 case "configuration-changed":
93 cfg, err := loadConfig(s.configPath)
94 if err != nil {
95 t.Fatal(err)
96 }
97 cfg.Timezone = "Etc/UTC"
98 if cfg.Fingerprint() == before.Configuration {
99 cfg.Timezone = "Asia/Tokyo"
100 }
101 if err := writePrivate(s.configPath, cfg); err != nil {
102 t.Fatal(err)
103 }
104 }
105 s.finishSafetyMonitor(context.Background(), client, before, "192.0.2.1", "")
106 after := gate.Status()
107 switch condition {
108 case "allowed":
109 if after.Mode != cage.GateOpen || !after.Until.After(before.Until) {
110 t.Fatal("tool update lease was not renewed")
111 }
112 case "paused", "configuration-changed":
113 if after.Mode != cage.GateClosed {
114 t.Fatal("denied network was renewed")
115 }
116 case "stop":
117 if after.Until != before.Until {
118 t.Fatal("stopping job allowed a lease renewal")
119 }
120 }
121 })
122 }
123 })
124 }
125}
126
127func TestToolUpdateDoesNotRestorePausedOrExpiredNetworking(t *testing.T) {
128 for _, paused := range []bool{true, false} {
129 s, gate := grantedToolUpdateServer(t)
130 if paused {
131 if err := s.pauseNetworkNow("manual"); err != nil {
132 t.Fatal(err)
133 }
134 } else {
135 gate.Close("expired")
136 }
137 s.mu.Lock()
138 allowed := s.toolsNetworkAllowedLocked()
139 s.mu.Unlock()
140 if !allowed {
141 t.Fatal("business network pause blocked maintenance")
142 }
143 if gate.Status().Mode != cage.GateClosed {
144 t.Fatal("tool update restored closed network")
145 }
146 }
147}
148
149func TestEnvironmentVersionRoutesRequireHostSession(t *testing.T) {
150 s := testServer(t)
151 for _, path := range []string{"/api/environment/versions", "/api/environment/update", "/api/environment/tools"} {
152 method := "POST"
153 if strings.HasSuffix(path, "versions") {
154 method = "GET"
155 }
156 if w := request(s, method, path, s.origin, "{}", false); w.Code != 401 {
157 t.Fatalf("untrusted version action accepted: %d", w.Code)
158 }
159 }
160 w := request(s, "GET", "/api/environment/versions", s.origin, "", true)
161 if w.Code != 200 || !strings.Contains(w.Body.String(), `"status":"stopped"`) {
162 t.Fatal(w.Code, w.Body.String())
163 }
164}