跳到主要内容

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

浏览文件

cloud_unix_test.go

115 行 · 2.7 KB历史下载
1//go:build darwin || linux
2
3package reversesshfs
4
5import (
6 "os"
7 "path/filepath"
8 "testing"
9
10 "golang.org/x/sys/unix"
11)
12
13// Even when the client races normal file edits, neither the read nor the
14// write path may follow the replacement symlink (including on truncation).
15func TestCloudLinkSwapAndFifo(t *testing.T) {
16 tmp := t.TempDir()
17 outside, root := filepath.Join(tmp, "outside"), filepath.Join(tmp, "working")
18 if err := os.Mkdir(root, 0700); err != nil {
19 t.Fatal(err)
20 }
21 if err := os.WriteFile(outside, []byte("secret"), 0600); err != nil {
22 t.Fatal(err)
23 }
24 if err := unix.Mkfifo(filepath.Join(root, "fifo"), 0600); err != nil {
25 t.Fatal(err)
26 }
27 _, c := cloudClient(t, root, false)
28 if _, err := c.Open("/fifo"); err == nil {
29 t.Fatal("read FIFO opened")
30 }
31 if _, err := c.OpenFile("/fifo", os.O_WRONLY); err == nil {
32 t.Fatal("write FIFO opened")
33 }
34
35 swapped := filepath.Join(root, "swapped")
36 done := make(chan struct{})
37 raceErr := make(chan error, 1)
38 go func() {
39 for {
40 select {
41 case <-done:
42 raceErr <- nil
43 return
44 default:
45 }
46 link, regular := filepath.Join(root, "link-temp"), filepath.Join(root, "regular-temp")
47 if err := os.Symlink(outside, link); err != nil {
48 raceErr <- err
49 return
50 }
51 if err := os.Rename(link, swapped); err != nil {
52 raceErr <- err
53 return
54 }
55 if err := os.WriteFile(regular, []byte("public"), 0600); err != nil {
56 raceErr <- err
57 return
58 }
59 if err := os.Rename(regular, swapped); err != nil {
60 raceErr <- err
61 return
62 }
63 }
64 }()
65 defer func() {
66 close(done)
67 if err := <-raceErr; err != nil {
68 t.Error(err)
69 }
70 }()
71 for range 256 {
72 f, err := c.Open("/swapped")
73 if err == nil {
74 buf := make([]byte, 7)
75 n, _ := f.Read(buf)
76 f.Close()
77 if string(buf[:n]) == "secret" {
78 t.Fatal("read outside the grant")
79 }
80 }
81 if f, err := c.OpenFile("/swapped", os.O_WRONLY|os.O_TRUNC); err == nil {
82 f.Close()
83 }
84 }
85 b, err := os.ReadFile(outside)
86 if err != nil || string(b) != "secret" {
87 t.Fatalf("outside file changed, %q %v", b, err)
88 }
89}
90
91func TestCloudNeverSetsPrivilegeBits(t *testing.T) {
92 dir := t.TempDir()
93 _, c := cloudClient(t, dir, false)
94 f, err := c.Create("/program")
95 if err != nil {
96 t.Fatal(err)
97 }
98 defer f.Close()
99 mode := os.FileMode(0755) | os.ModeSetuid | os.ModeSetgid | os.ModeSticky
100 check := func() {
101 t.Helper()
102 info, err := os.Stat(filepath.Join(dir, "program"))
103 if err != nil || info.Mode()&(os.ModeSetuid|os.ModeSetgid|os.ModeSticky) != 0 || info.Mode().Perm() != 0755 {
104 t.Fatalf("unexpected host mode: %v %v", info, err)
105 }
106 }
107 if err := f.Chmod(mode); err != nil {
108 t.Fatal(err)
109 }
110 check()
111 if err := c.Chmod("/program", mode); err != nil {
112 t.Fatal(err)
113 }
114 check()
115}