跳到主要内容

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

浏览文件

rooted_e2e_test.go

248 行 · 6.7 KB历史下载
1//go:build linux
2
3package reversesshfs
4
5import (
6 "os"
7 "os/exec"
8 "path/filepath"
9 "testing"
10 "time"
11
12 "github.com/lima-vm/sshocker/pkg/util"
13 "golang.org/x/sys/unix"
14)
15
16// TestRootedSSHFS mounts the rooted server with a real sshfs in slave mode.
17// It runs only when $SSHFS names an sshfs binary and FUSE is usable.
18func TestRootedSSHFS(t *testing.T) {
19 sshfs := os.Getenv("SSHFS")
20 if sshfs == "" {
21 t.Skip("SSHFS is not set")
22 }
23 tmp := t.TempDir()
24 root := filepath.Join(tmp, "root")
25 mnt := filepath.Join(tmp, "mnt")
26 for _, d := range []string{filepath.Join(root, "src"), mnt} {
27 if err := os.MkdirAll(d, 0o755); err != nil {
28 t.Fatal(err)
29 }
30 }
31 git := func(dir string, args ...string) string {
32 t.Helper()
33 cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
34 cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_AUTHOR_NAME=a", "GIT_AUTHOR_EMAIL=a@a", "GIT_COMMITTER_NAME=a", "GIT_COMMITTER_EMAIL=a@a")
35 out, err := cmd.CombinedOutput()
36 if err != nil {
37 t.Fatalf("git %v: %v: %s", args, err, out)
38 }
39 return string(out)
40 }
41 if err := os.WriteFile(filepath.Join(root, "src", "main.go"), []byte("package main\n"), 0o644); err != nil {
42 t.Fatal(err)
43 }
44 git(root, "init", "-q")
45 git(root, "add", ".")
46 git(root, "commit", "-q", "-m", "init")
47
48 cmd := exec.Command(sshfs, ":"+root, mnt, "-f", "-o", "slave")
49 stdin, err := cmd.StdinPipe()
50 if err != nil {
51 t.Fatal(err)
52 }
53 stdout, err := cmd.StdoutPipe()
54 if err != nil {
55 t.Fatal(err)
56 }
57 cmd.Stderr = os.Stderr
58 srv, h, err := newRootedServer(&util.RWC{ReadCloser: stdout, WriteCloser: stdin}, root, false, []string{".git"})
59 if err != nil {
60 t.Fatal(err)
61 }
62 if err := cmd.Start(); err != nil {
63 t.Fatal(err)
64 }
65 go func() {
66 _ = srv.Serve()
67 _ = h.Close()
68 }()
69 t.Cleanup(func() {
70 _ = exec.Command("fusermount3", "-u", mnt).Run()
71 _ = cmd.Wait()
72 })
73 for i := 0; ; i++ {
74 if _, err := os.Stat(filepath.Join(mnt, "src", "main.go")); err == nil {
75 break
76 }
77 if i == 50 {
78 t.Fatal("sshfs did not mount")
79 }
80 time.Sleep(100 * time.Millisecond)
81 }
82
83 sh := func(script string) error {
84 c := exec.Command("sh", "-euc", script)
85 c.Dir = mnt
86 out, err := c.CombinedOutput()
87 t.Logf("%s: %v: %s", script, err, out)
88 return err
89 }
90 allowed := []string{
91 "echo x > src/new.go && cat src/new.go",
92 "mkdir -p a/b/c && touch a/b/c/f && rm -rf a",
93 "sed -i s/main/foo/ src/main.go",
94 "cp -a src src2 && rm -r src2",
95 "chmod 600 src/new.go && truncate -s 0 src/new.go",
96 "ln -s main.go src/link && readlink src/link && cat src/link && rm src/link",
97 "mv src/new.go src/renamed.go && rm src/renamed.go",
98 "df .",
99 "cat .git/HEAD && ls .git/hooks",
100 "git status --short",
101 "git log --oneline",
102 }
103 for _, s := range allowed {
104 if err := sh(s); err != nil {
105 t.Errorf("%s: %v", s, err)
106 }
107 }
108 denied := []string{
109 "echo evil > .git/hooks/pre-commit",
110 "echo evil >> .git/config",
111 "mv .git x",
112 "rm .git/HEAD",
113 "chmod 777 .git/config",
114 // Not @0: sshfs replaces a zero time with the current time.
115 "touch -d @1 .git/config",
116 "mkdir src/.git",
117 "ln -s .git g && echo evil > g/hooks/post-checkout",
118 "git add src",
119 "git commit -q --allow-empty -m evil",
120 }
121 for _, s := range denied {
122 if err := sh(s); err == nil {
123 t.Errorf("%s: expected an error", s)
124 }
125 }
126 if fi, err := os.Stat(filepath.Join(root, ".git", "config")); err != nil || fi.ModTime().Unix() == 1 {
127 t.Errorf(".git/config times were changed: %v, %v", fi.ModTime(), err)
128 }
129 if _, err := os.Stat(filepath.Join(root, ".git", "hooks", "pre-commit")); err == nil {
130 t.Error("hook was written")
131 }
132 if n := git(root, "rev-list", "--count", "HEAD"); n != "1\n" {
133 t.Errorf("commit count changed: %q", n)
134 }
135
136 // Relay a host change the way Lima's guest agent does for mountInotify.
137 for _, f := range []string{".git/HEAD", "src/main.go"} {
138 if err := os.WriteFile(filepath.Join(root, f), []byte("ref: refs/heads/other\n"), 0o644); err != nil {
139 t.Fatal(err)
140 }
141 fi, err := os.Stat(filepath.Join(root, f))
142 if err != nil {
143 t.Fatal(err)
144 }
145 if !gotAttribEvent(t, filepath.Join(mnt, f), fi.ModTime()) {
146 t.Errorf("%s: no IN_ATTRIB event in the mount", f)
147 }
148 }
149
150 // Relay a host deletion: the hostagent calls ExpectRemove, then the guest agent removes the path.
151 for _, tc := range []struct {
152 name string
153 recreate bool
154 guestSeen bool
155 guestListed bool
156 delay time.Duration
157 }{
158 {name: "src/gone.txt", guestSeen: true},
159 {name: "src/listed.txt", guestListed: true},
160 {name: ".git/index.lock", guestSeen: true},
161 {name: "src/later.txt", guestSeen: true, delay: 2 * time.Second},
162 {name: "src/again.txt", guestSeen: true, recreate: true},
163 {name: "src/unseen.txt"},
164 } {
165 hostPath := filepath.Join(root, tc.name)
166 if err := os.WriteFile(hostPath, []byte("old"), 0o644); err != nil {
167 t.Fatal(err)
168 }
169 if tc.guestSeen {
170 if _, err := os.Stat(filepath.Join(mnt, tc.name)); err != nil {
171 t.Fatal(err)
172 }
173 }
174 if tc.guestListed {
175 if _, err := os.ReadDir(filepath.Dir(filepath.Join(mnt, tc.name))); err != nil {
176 t.Fatal(err)
177 }
178 }
179 if err := os.Remove(hostPath); err != nil {
180 t.Fatal(err)
181 }
182 if tc.recreate {
183 if err := os.WriteFile(hostPath, []byte("new"), 0o644); err != nil {
184 t.Fatal(err)
185 }
186 }
187 time.Sleep(tc.delay)
188 h.expectRemove(hostPath)
189 got := gotDeleteEvent(t, filepath.Join(mnt, tc.name))
190 t.Logf("%s (seen: %v, listed: %v, delay: %v): IN_DELETE: %v", tc.name, tc.guestSeen, tc.guestListed, tc.delay, got)
191 if tc.guestSeen && !got {
192 t.Errorf("%s: no IN_DELETE event in the mount", tc.name)
193 }
194 if tc.recreate {
195 if b, err := os.ReadFile(hostPath); err != nil || string(b) != "new" {
196 t.Errorf("%s: the recreated file was modified: %q, %v", tc.name, b, err)
197 }
198 }
199 }
200}
201
202func gotDeleteEvent(t *testing.T, p string) bool {
203 t.Helper()
204 fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
205 if err != nil {
206 t.Fatal(err)
207 }
208 defer unix.Close(fd)
209 if _, err := unix.InotifyAddWatch(fd, filepath.Dir(p), unix.IN_DELETE); err != nil {
210 t.Fatal(err)
211 }
212 if err := os.Remove(p); err != nil {
213 t.Logf("remove %s: %v", p, err)
214 return false
215 }
216 buf := make([]byte, 4096)
217 for range 20 {
218 if n, err := unix.Read(fd, buf); err == nil && n > 0 {
219 return true
220 }
221 time.Sleep(50 * time.Millisecond)
222 }
223 return false
224}
225
226func gotAttribEvent(t *testing.T, p string, mtime time.Time) bool {
227 t.Helper()
228 fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
229 if err != nil {
230 t.Fatal(err)
231 }
232 defer unix.Close(fd)
233 if _, err := unix.InotifyAddWatch(fd, p, unix.IN_ATTRIB); err != nil {
234 t.Fatal(err)
235 }
236 if err := os.Chtimes(p, mtime, mtime); err != nil {
237 t.Logf("chtimes %s: %v", p, err)
238 return false
239 }
240 buf := make([]byte, 4096)
241 for range 20 {
242 if n, err := unix.Read(fd, buf); err == nil && n > 0 {
243 return true
244 }
245 time.Sleep(50 * time.Millisecond)
246 }
247 return false
248}