跳到主要内容

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

浏览文件

safety_guest_probe_test.go

160 行 · 6.8 KB历史下载
1package workbench
2
3import (
4 "encoding/base64"
5 "encoding/json"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12
13 "github.com/sylvinus/agent-vm/internal/cage"
14)
15
16func boolp(v bool) *bool { return &v }
17
18func TestGuestProxyEndpointUsesGatewayForHostProxies(t *testing.T) {
19 cfg := cage.DefaultConfig()
20 for _, tc := range []struct {
21 proxy, gateway, host string
22 port int
23 }{
24 {"socks5://127.0.0.1:17896", "192.168.5.2", "192.168.5.2", 17896},
25 {"http://0.0.0.0:7890", "10.88.0.1", "10.88.0.1", 7890},
26 {"http://[::1]:7890", "192.168.5.2", "192.168.5.2", 7890},
27 {"http://proxy.example.com:8080", "192.168.5.2", "proxy.example.com", 8080},
28 {"http://localhost:7890", "192.168.5.2", "192.168.5.2", 7890},
29 {"http://[2001:db8::1]:8080", "192.168.5.2", "2001:db8::1", 8080},
30 {"http://203.0.113.9:8080", "192.168.5.2", "203.0.113.9", 8080},
31 {"http://192.168.1.20:7890", "192.168.5.2", "192.168.1.20", 7890},
32 {"http://127.0.0.1", "192.168.5.2", "", 0},
33 } {
34 cfg.ProxyURL = tc.proxy
35 host, port := guestProxyEndpoint(cfg, tc.gateway)
36 if host != tc.host || port != tc.port {
37 t.Errorf("%s via %s: got %s:%d, want %s:%d", tc.proxy, tc.gateway, host, port, tc.host, tc.port)
38 }
39 }
40}
41
42// Exercise the embedded boundary entry and decoder with denied sockets and a
43// fixed Linux route/resolver fixture. No host network or sudo command is used.
44func TestSafetyProbeBoundaryReportsLinuxEvidence(t *testing.T) {
45 if runtime.GOOS == "windows" {
46 t.Skip("Linux probe requires POSIX file flags")
47 }
48 python, err := exec.LookPath("python3")
49 if err != nil {
50 t.Skip("python3 not installed")
51 }
52 dir := t.TempDir()
53 resolver := filepath.Join(dir, "resolv.conf")
54 if err := os.WriteFile(resolver, []byte("nameserver 127.0.0.53\nnameserver 192.168.5.2\n"), 0600); err != nil {
55 t.Fatal(err)
56 }
57 script := strings.ReplaceAll(guestSafetyProbe, "/etc/resolv.conf", filepath.ToSlash(resolver))
58 script = strings.ReplaceAll(script, "/run/systemd/resolve/resolv.conf", filepath.ToSlash(filepath.Join(dir, "absent")))
59 prelude := `import socket, subprocess
60class DeniedSocket:
61 def __init__(self, family, kind): self.family = family
62 def settimeout(self, timeout): pass
63 def connect(self, address):
64 assert (":" in address[0]) == (self.family == socket.AF_INET6)
65 raise ConnectionRefusedError()
66 def sendto(self, payload, address): return len(payload)
67 def close(self): pass
68def no_dns(*args, **kwargs): raise socket.gaierror()
69def linux_command(command, **kwargs):
70 if command[0] == "ip": return subprocess.CompletedProcess(command, 0, "[]", "")
71 raise OSError("no subprocess allowed")
72socket.socket = DeniedSocket
73socket.getaddrinfo = no_dns
74subprocess.run = linux_command
75`
76 input := boundaryProbeInput(17779, "test", filepath.Join(dir, "private"), filepath.Join(dir, "shared"), "challenge")
77 input["proxy_host"], input["proxy_port"] = "2001:db8::1", 7890
78 data, _ := json.Marshal(input)
79 cmd := exec.Command(python, "-I", "-c", prelude+script, base64.StdEncoding.EncodeToString(data))
80 output, err := cmd.CombinedOutput()
81 if err != nil {
82 t.Fatalf("probe failed: %v %s", err, output)
83 }
84 var v guestObservation
85 if err := json.Unmarshal(output, &v); err != nil {
86 t.Fatal(err)
87 }
88 if issue, _ := hostReachIssue(v, true, true); issue != "" {
89 t.Fatalf("host evidence: %s: %s", issue, output)
90 }
91 if issue, _ := dnsBypassIssue(v, true); issue != "" {
92 t.Fatalf("DNS evidence: %s: %s", issue, output)
93 }
94 if v.IPv6GlobalAddresses == nil || *v.IPv6GlobalAddresses != 0 {
95 t.Fatalf("missing address observation: %s", output)
96 }
97}
98
99func TestHostReachAndDNSBypassIssues(t *testing.T) {
100 var v guestObservation
101 if issue, _ := hostReachIssue(v, false, false); issue != "" {
102 t.Fatalf("empty observation reported host reach: %s", issue)
103 }
104 // Windows and macOS probes do not report the Linux-only fields; they add no demands there.
105 if issue, _ := dnsBypassIssue(v, false); issue != "" {
106 t.Fatalf("non-Linux observation demanded Linux evidence: %s", issue)
107 }
108 if issue, confirmed := dnsBypassIssue(v, true); issue == "" || confirmed {
109 t.Fatalf("missing Linux DNS evidence must be unknown, not passed or failed: %q %t", issue, confirmed)
110 }
111 v.DNSUDPAttempted = true
112 v.DNSTCPConnected, v.DoTConnected = boolp(false), boolp(false)
113 v.Resolvers = resolverObservation{Count: intp(2), Confined: boolp(true)}
114 if issue, _ := dnsBypassIssue(v, true); issue != "" {
115 t.Fatalf("confined resolvers rejected: %s", issue)
116 }
117 v.Resolvers.Confined = boolp(false)
118 if issue, confirmed := dnsBypassIssue(v, true); !confirmed || !strings.Contains(issue, "解析器") {
119 t.Fatalf("resolver outside the environment not confirmed: %q %t", issue, confirmed)
120 }
121 v.Resolvers = resolverObservation{Count: intp(-1)}
122 if issue, confirmed := dnsBypassIssue(v, true); issue == "" || confirmed {
123 t.Fatalf("unreadable resolver configuration must stay unknown: %q %t", issue, confirmed)
124 }
125 for name, mutate := range map[string]func(*guestObservation){
126 "tcp/53": func(v *guestObservation) { v.DNSTCPConnected = boolp(true) },
127 "dot/853": func(v *guestObservation) { v.DoTConnected = boolp(true) },
128 } {
129 w := guestObservation{DNSUDPAttempted: true, Resolvers: resolverObservation{Count: intp(1), Confined: boolp(true)}}
130 mutate(&w)
131 for _, linux := range []bool{true, false} {
132 if issue, confirmed := dnsBypassIssue(w, linux); !confirmed || issue == "" {
133 t.Errorf("%s reached (linux=%t) not confirmed: %q", name, linux, issue)
134 }
135 }
136 }
137 if issue, _ := hostReachIssue(guestObservation{LinkLocalConnected: boolp(true)}, true, false); !strings.Contains(issue, "169.254.169.254") {
138 t.Fatalf("link-local reach not reported: %q", issue)
139 }
140 if issue, _ := hostReachIssue(guestObservation{ProxyEndpointConnected: boolp(true)}, true, true); !strings.Contains(issue, "代理端口") {
141 t.Fatalf("proxy endpoint reach not reported: %q", issue)
142 }
143}
144
145// The Linux probe's boundary output decodes into the observation the checks read.
146func TestLinuxProbeObservationDecodes(t *testing.T) {
147 data := `{"tz":"Etc/UTC","lang":"en_US.UTF-8","lc_all":"en_US.UTF-8","system_timezone":"Etc/UTC","locale_valid":true,
148"public_connected":false,"management_connected":false,"link_local_connected":false,"proxy_endpoint_connected":false,
149"ipv6_connected":false,"ipv6_default_routes":0,"ipv6_global_addresses":0,
150"udp_attempted":true,"dns_udp_attempted":true,"dns_tcp_connected":false,"dot_connected":false,
151"resolvers":{"count":2,"confined":true},"public_dns_answered":false,"private_file":"unreachable","shared_file":true}`
152 var v guestObservation
153 if err := json.Unmarshal([]byte(data), &v); err != nil {
154 t.Fatal(err)
155 }
156 hostIssue, _ := hostReachIssue(v, true, true)
157 if issue, _ := dnsBypassIssue(v, true); issue != "" || hostIssue != "" || v.Resolvers.Confined == nil || !*v.Resolvers.Confined || v.IPv6GlobalAddresses == nil || *v.IPv6GlobalAddresses != 0 {
158 t.Fatalf("probe output misread: %+v (%s)", v, issue)
159 }
160}