跳到主要内容

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

浏览文件

config_test.go

134 行 · 4.4 KB历史下载
1package containerenv
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func fixtureConfig() Config {
11 return Config{Engine: "docker", Executable: filepath.Join(os.TempDir(), "cage-test-bin", "docker"), Socket: "unix:///tmp/cage-test.sock", Image: "sha256:" + strings.Repeat("a", 64), CPUs: 2, MemoryBytes: 4 << 30}
12}
13
14func TestConfigRequiresPinnedLocalRootlessEngine(t *testing.T) {
15 for _, change := range []func(*Config){
16 func(c *Config) { c.Socket = "tcp://127.0.0.1:2375" },
17 func(c *Config) { c.Socket = "unix:///tmp/cage.sock\n--privileged" },
18 func(c *Config) { c.Image = "debian:13" },
19 func(c *Config) { c.Executable = "docker" },
20 } {
21 config := fixtureConfig()
22 change(&config)
23 if config.Validate() == nil {
24 t.Fatalf("accepted unsafe config %+v", config)
25 }
26 }
27 profile := t.TempDir()
28 current, err := Configure(profile, fixtureConfig())
29 if err != nil {
30 t.Fatal(err)
31 }
32 reloaded, err := Load(profile)
33 if err != nil {
34 t.Fatal(err)
35 }
36 if current.Name() != reloaded.Name() || current.Config() != reloaded.Config() {
37 t.Fatal("configuration identity did not persist")
38 }
39 changed := fixtureConfig()
40 changed.Image = "sha256:" + strings.Repeat("b", 64)
41 if _, err := Configure(profile, changed); err == nil {
42 t.Fatal("replacement would strand original writable root filesystem")
43 }
44 if err := os.WriteFile(filepath.Join(profile, "container.json"), []byte(`{"engine":"docker","privileged":true}`), 0600); err != nil {
45 t.Fatal(err)
46 }
47 if _, err := Load(profile); err == nil {
48 t.Fatal("unknown configuration field accepted")
49 }
50}
51
52func TestRejectContainerEscapeConfiguration(t *testing.T) {
53 valid := &engineContainer{}
54 valid.HostConfig.NetworkMode = "none"
55 valid.HostConfig.CgroupnsMode = "private"
56 valid.HostConfig.IpcMode = "private"
57 if err := checkContainerConfiguration(valid); err != nil {
58 t.Fatal(err)
59 }
60 valid.HostConfig.ExtraHosts = []string{"cage:127.0.1.1"}
61 if err := checkContainerConfiguration(valid); err != nil {
62 t.Fatal(err)
63 }
64 for _, mutate := range []func(*engineContainer){
65 func(c *engineContainer) { c.HostConfig.Privileged = true },
66 func(c *engineContainer) { c.HostConfig.NetworkMode = "bridge" },
67 func(c *engineContainer) { c.HostConfig.NetworkMode = "host" },
68 func(c *engineContainer) { c.HostConfig.PidMode = "host" },
69 func(c *engineContainer) { c.HostConfig.PidMode = "container:other" },
70 func(c *engineContainer) { c.HostConfig.CgroupnsMode = "host" },
71 func(c *engineContainer) { c.HostConfig.Binds = []string{"/:/host"} },
72 func(c *engineContainer) { c.HostConfig.ExtraHosts = []string{"cage:host-gateway"} },
73 func(c *engineContainer) { c.HostConfig.ExtraHosts = []string{"cage:192.168.64.1"} },
74 func(c *engineContainer) { c.HostConfig.CapAdd = []string{"ALL"} },
75 func(c *engineContainer) {
76 c.Mounts = append(c.Mounts, struct{ Type, Source, Destination string }{"bind", "/var/run/docker.sock", "/run/docker.sock"})
77 },
78 } {
79 c := &engineContainer{}
80 c.HostConfig.NetworkMode = "none"
81 c.HostConfig.CgroupnsMode = "private"
82 c.HostConfig.IpcMode = "private"
83 mutate(c)
84 if checkContainerConfiguration(c) == nil {
85 t.Fatal("unsafe container configuration accepted")
86 }
87 }
88}
89
90func TestCreateResolvesHostnameWithinContainerLoopback(t *testing.T) {
91 for _, engine := range []string{"docker", "podman"} {
92 config := fixtureConfig()
93 config.Engine = engine
94 runtime := newRuntime(t.TempDir(), config)
95 args := runtime.createArguments()
96 found := false
97 for i, arg := range args {
98 if arg == "--add-host" && i+1 < len(args) {
99 if args[i+1] != "cage:127.0.1.1" {
100 t.Fatalf("%s resolves cage outside its loopback: %q", engine, args[i+1])
101 }
102 found = true
103 }
104 }
105 if !found {
106 t.Fatalf("%s does not resolve the container hostname", engine)
107 }
108 }
109}
110
111func TestSSHKeyIsStableAndHostKeyPathsAreQuoted(t *testing.T) {
112 r := newRuntime(filepath.Join(t.TempDir(), "profile with spaces"), fixtureConfig())
113 first, err := r.ensureSSHKey()
114 if err != nil {
115 t.Fatal(err)
116 }
117 second, err := r.ensureSSHKey()
118 if err != nil {
119 t.Fatal(err)
120 }
121 if string(first) != string(second) {
122 t.Fatal("SSH key changed on reconnect")
123 }
124 if err := r.writeSSHConfig(17770); err != nil {
125 t.Fatal(err)
126 }
127 data, err := os.ReadFile(r.SSHConfig())
128 if err != nil {
129 t.Fatal(err)
130 }
131 if !strings.Contains(string(data), "StrictHostKeyChecking yes") || !strings.Contains(string(data), `IdentityFile "`) {
132 t.Fatal("SSH trust or paths were not preserved")
133 }
134}