跳到主要内容

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

浏览文件

netguard_test.go

213 行 · 7.4 KB历史下载
1package netguard
2
3import (
4 "net"
5 "net/netip"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12func ap(s string) netip.AddrPort { return netip.MustParseAddrPort(s) }
13
14// By default: the internet, never this machine, its networks, the VPN's.
15func TestDefault(t *testing.T) {
16 p := &Policy{}
17 for dst, want := range map[string]bool{
18 "1.1.1.1:443": true,
19 "[2606:4700::1111]:443": true,
20 "127.0.0.1:5432": false,
21 "[::1]:5432": false,
22 "[::ffff:127.0.0.1]:5432": false,
23 "192.168.1.1:80": false,
24 "10.1.2.3:22": false,
25 "172.16.0.1:80": false,
26 "100.100.100.100:53": false, // Tailscale
27 "169.254.169.254:80": false, // cloud metadata
28 "[fd00::1]:80": false,
29 "[fe80::1]:80": false,
30 "224.0.0.251:5353": false,
31 "0.0.0.0:80": false,
32 "255.255.255.255:67": false,
33 "[::]:80": false,
34 } {
35 if got := p.Allows("tcp", ap(dst)); got != want {
36 t.Errorf("%s: %v", dst, got)
37 }
38 }
39}
40
41// Ranges that never lead to the internet, and IPv6 forms that embed an IPv4
42// address of this machine or its networks.
43func TestReserved(t *testing.T) {
44 p := &Policy{}
45 for dst, want := range map[string]bool{
46 "192.0.0.8:80": false, // IETF protocol assignments
47 "192.0.2.1:80": false,
48 "198.18.0.1:80": false, // benchmarking, proxies' fake IPs
49 "198.19.255.255:80": false,
50 "198.20.0.1:80": true,
51 "198.51.100.1:80": false,
52 "203.0.113.1:80": false,
53 "240.0.0.1:80": false,
54 "255.255.255.254:80": false,
55 "239.255.255.250:1900": false,
56 "100.64.0.1:80": false,
57 "100.127.255.255:80": false,
58 "100.128.0.1:80": true,
59 "169.254.1.1:80": false,
60 "[fec0::1]:80": false,
61 "[ff02::1]:80": false,
62 "[::127.0.0.1]:80": false, // IPv4-compatible
63 "[::ffff:10.0.0.1]:80": false,
64 "[::ffff:8.8.8.8]:80": true,
65 "[64:ff9b::7f00:1]:80": false, // NAT64 of 127.0.0.1
66 "[64:ff9b::c0a8:101]:80": false, // of 192.168.1.1
67 "[64:ff9b::808:808]:80": true, // of 8.8.8.8
68 "[64:ff9b:1::a00:1]:80": false, // local-use NAT64
69 "[2002:7f00:1::1]:80": false, // 6to4 of 127.0.0.1
70 "[2002:a00:1::1]:80": false, // of 10.0.0.1
71 "[2002:808:808::1]:80": true, // of 8.8.8.8
72 "[2001:0:4136:e378:8000:63bf:f7f7:f7f7]:80": true, // Teredo, public server and client
73 "[2001:0:4136:e378:8000:63bf:80ff:fffe]:80": false, // client 127.0.0.1
74 "[2001:0:7f00:1:8000:63bf:f7f7:f7f7]:80": false, // server 127.0.0.1
75 "[2001:0:4136:e378:8000:63bf:f5ff:fffe]:80": false, // client 10.0.0.1
76 "[2001:db8::1]:80": false,
77 "[100::1]:80": false,
78 "[2606:4700::1111]:443": true,
79 } {
80 if got := p.Allows("tcp", ap(dst)); got != want {
81 t.Errorf("%s: %v", dst, got)
82 }
83 }
84 // A zone does not hide a link-local address.
85 if p.Allows("tcp", netip.AddrPortFrom(netip.MustParseAddr("fe80::1%en0"), 80)) {
86 t.Error("zoned link-local")
87 }
88 // An allow line still opens one.
89 q := &Policy{Allow: []Rule{{Net: netip.MustParsePrefix("198.18.0.0/15")}}}
90 if !q.Allows("tcp", ap("198.18.0.5:80")) {
91 t.Error("allowed benchmarking range")
92 }
93}
94
95// Only tcp and udp are known: anything else is refused, unless open.
96func TestNetwork(t *testing.T) {
97 p := &Policy{}
98 for network, want := range map[string]bool{"tcp": true, "udp": true, "icmp": false, "": false, "tcp4": false} {
99 if got := p.Allows(network, ap("1.1.1.1:443")); got != want {
100 t.Errorf("%q: %v", network, got)
101 }
102 }
103 if !(&Policy{Open: true}).Allows("icmp", ap("127.0.0.1:1")) {
104 t.Error("open")
105 }
106}
107
108// What the user allows is reached, at its port only when one is given.
109func TestAllow(t *testing.T) {
110 dir := t.TempDir()
111 os.WriteFile(filepath.Join(dir, FileName), []byte("# ollama on this machine\nallow localhost:11434\nallow 192.168.1.20\nallow 10.0.0.0/8 # lab\n"), 0o644)
112 p, err := Load(dir)
113 if err != nil {
114 t.Fatal(err)
115 }
116 for dst, want := range map[string]bool{
117 "127.0.0.1:11434": true,
118 "[::1]:11434": true,
119 "127.0.0.1:5432": false,
120 "192.168.1.20:22": true,
121 "192.168.1.21:22": false,
122 "10.200.0.1:443": true,
123 "8.8.8.8:53": true,
124 } {
125 if got := p.Allows("tcp", ap(dst)); got != want {
126 t.Errorf("%s: %v", dst, got)
127 }
128 }
129}
130
131// The other forms the site documents: every loopback port, IPv6 with a
132// port, a *. domain.
133func TestForms(t *testing.T) {
134 dir := t.TempDir()
135 os.WriteFile(filepath.Join(dir, FileName), []byte("allow localhost\nallow [fd00::5]:80\ndomain *.github.com\n"), 0o644)
136 p, err := Load(dir)
137 if err != nil {
138 t.Fatal(err)
139 }
140 for dst, want := range map[string]bool{
141 "127.0.0.1:1": true,
142 "[::1]:65535": true,
143 "[fd00::5]:80": true,
144 "[fd00::5]:81": false,
145 "192.168.1.20:22": false,
146 } {
147 if got := p.Allows("udp", ap(dst)); got != want {
148 t.Errorf("%s: %v", dst, got)
149 }
150 }
151 if !p.Name("github.com") || !p.Name("api.github.com") || p.Name("example.com") {
152 t.Error("*.github.com")
153 }
154}
155
156func TestLoadErrors(t *testing.T) {
157 for _, line := range []string{"allow", "allow nowhere", "allow 1.2.3.4:0", "deny 1.2.3.4", "domain a/b", "allow 1.2.3.4 5"} {
158 dir := t.TempDir()
159 os.WriteFile(filepath.Join(dir, FileName), []byte(line+"\n"), 0o644)
160 if _, err := Load(dir); err == nil || !strings.Contains(err.Error(), "line 1") {
161 t.Errorf("%q: %v", line, err)
162 }
163 }
164 p, err := Load(t.TempDir())
165 if err != nil || p.Open || len(p.Allow) != 0 {
166 t.Errorf("no file: %+v %v", p, err)
167 }
168}
169
170// With domains: those names only, and only the addresses given for them.
171func TestDomains(t *testing.T) {
172 p := &Policy{Domains: []string{"github.com", "npmjs.org"}}
173 for name, want := range map[string]bool{"github.com": true, "api.github.com.": true, "GitHub.com": true, "evilgithub.com": false, "example.com": false, "registry.npmjs.org": true} {
174 if p.Name(name) != want {
175 t.Errorf("Name(%q) != %v", name, want)
176 }
177 }
178 if p.Allows("tcp", ap("140.82.112.3:443")) {
179 t.Error("an address no allowed name gave")
180 }
181 p.Answered("github.com", []net.IP{net.ParseIP("140.82.112.3")})
182 p.Answered("example.com", []net.IP{net.ParseIP("93.184.216.34")})
183 if !p.Allows("tcp", ap("140.82.112.3:443")) || p.Allows("tcp", ap("93.184.216.34:443")) {
184 t.Error("answered addresses")
185 }
186 // Never this machine, though a name gives it.
187 p.Answered("github.com", []net.IP{net.ParseIP("127.0.0.1")})
188 if p.Allows("tcp", ap("127.0.0.1:443")) {
189 t.Error("loopback through an allowed name")
190 }
191}
192
193func TestOpen(t *testing.T) {
194 t.Setenv("AGENT_VM_UNSAFE_OPEN_NETWORK", "1")
195 p, _ := Load(t.TempDir())
196 if !p.Allows("tcp", ap("127.0.0.1:5432")) {
197 t.Error("open")
198 }
199}
200
201// This machine's own addresses, a public one included.
202func TestOwnAddress(t *testing.T) {
203 addrs, _ := net.InterfaceAddrs()
204 p := &Policy{}
205 for _, a := range addrs {
206 if n, ok := a.(*net.IPNet); ok {
207 ip, _ := netip.AddrFromSlice(n.IP)
208 if p.Allows("tcp", netip.AddrPortFrom(ip.Unmap(), 80)) {
209 t.Errorf("own address %s reachable", ip)
210 }
211 }
212 }
213}