跳到主要内容

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

浏览文件

rooted_robust_test.go

115 行 · 3.0 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "os"
7 "path/filepath"
8 "runtime"
9 "strings"
10 "testing"
11
12 "github.com/pkg/sftp"
13)
14
15// An opened directory renamed, with a symlink to a folder outside the root put in its place,
16// must not list the metadata of the files outside. (This holds because Go's File.Readdir stats
17// relative to the directory; go.mod requires a Go that does.)
18func TestRootedListNoEscape(t *testing.T) {
19 if runtime.GOOS == "windows" {
20 t.Skip("no symlinks")
21 }
22 _, root, h := setupRootedHandlers(t, false)
23 outside := filepath.Join(filepath.Dir(root), "outside")
24 if err := os.Mkdir(filepath.Join(root, "d"), 0o755); err != nil {
25 t.Fatal(err)
26 }
27 for _, f := range []struct{ path, content string }{
28 {filepath.Join(root, "d", "a"), "x"},
29 {filepath.Join(root, "d", "b"), "x"},
30 {filepath.Join(outside, "a"), strings.Repeat("secret", 100)},
31 } {
32 if err := os.WriteFile(f.path, []byte(f.content), 0o644); err != nil {
33 t.Fatal(err)
34 }
35 }
36 l, err := h.Filelist(sftp.NewRequest("List", filepath.Join(root, "d")))
37 if err != nil {
38 t.Fatal(err)
39 }
40 defer l.(interface{ Close() error }).Close()
41 if err := os.Rename(filepath.Join(root, "d"), filepath.Join(root, "d2")); err != nil {
42 t.Fatal(err)
43 }
44 if err := os.Symlink(outside, filepath.Join(root, "d")); err != nil {
45 t.Fatal(err)
46 }
47 ls := make([]os.FileInfo, 10)
48 n, _ := l.ListAt(ls, 0)
49 for _, fi := range ls[:n] {
50 if fi.Size() != 1 {
51 t.Errorf("%s: size %d, the metadata of a file outside the root", fi.Name(), fi.Size())
52 }
53 }
54}
55
56// The setuid, setgid and sticky bits are never set by the client.
57func TestRootedNoSpecialBits(t *testing.T) {
58 if runtime.GOOS == "windows" {
59 t.Skip("no mode bits")
60 }
61 c, root, h := setupRootedHandlers(t, false)
62 p := func(s string) string { return filepath.Join(root, s) }
63 special := func(s string) os.FileMode {
64 t.Helper()
65 fi, err := os.Stat(p(s))
66 if err != nil {
67 t.Fatal(err)
68 }
69 return fi.Mode() & (os.ModeSetuid | os.ModeSetgid | os.ModeSticky)
70 }
71 const mode = 0o7755
72 attrs := []byte{0, 0, byte(mode >> 8), byte(mode & 0xff)}
73
74 r := sftp.NewRequest("Put", p("src/open"))
75 r.Flags = 0x2 | 0x8 // write, creat
76 r.Attrs = attrs
77 f, err := h.openFile(r)
78 if err != nil {
79 t.Fatal(err)
80 }
81 f.Close()
82 if m := special("src/open"); m != 0 {
83 t.Errorf("open: %v", m)
84 }
85
86 if err := c.Chmod(p("src/main.go"), mode); err != nil {
87 t.Fatal(err)
88 }
89 if m := special("src/main.go"); m != 0 {
90 t.Errorf("chmod: %v", m)
91 }
92
93 hf, err := c.OpenFile(p("src/main.go"), os.O_RDWR)
94 if err != nil {
95 t.Fatal(err)
96 }
97 if err := hf.Chmod(mode); err != nil {
98 t.Fatal(err)
99 }
100 hf.Close()
101 if m := special("src/main.go"); m != 0 {
102 t.Errorf("fchmod: %v", m)
103 }
104
105 // Mkdir takes its attributes through the sftp package; the handler applies them.
106 mk := sftp.NewRequest("Mkdir", p("src/dir"))
107 mk.Flags = 0x4 // permissions
108 mk.Attrs = append(make([]byte, 0, 4), attrs...)
109 if err := h.Filecmd(mk); err != nil {
110 t.Fatal(err)
111 }
112 if m := special("src/dir"); m != 0 {
113 t.Errorf("mkdir: %v", m)
114 }
115}