跳到主要内容

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

浏览文件

rooted_fuzz_test.go

196 行 · 5.2 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "io"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11 "unicode/utf8"
12)
13
14var sameNameSeeds = [][2]string{
15 {".git", ".GIT"},
16 {".git", ".g" + zwnj + "it"},
17 {".git", bom + ".git"},
18 {"café", "café"},
19 {"glass", "glaß"},
20 {"k", "K"},
21 {"hooks", "HOOKS"},
22 {".husky", ".Husky"},
23 {"tools", "toòls"},
24}
25
26// Whatever the host's file system takes as the same entry, sameName must
27// too: otherwise the client writes a read-only name under another spelling.
28// The file system decides: on a case-sensitive one, only identical names
29// meet; on macOS and Windows, case, normalization and ignored code points.
30func FuzzSameNameFS(f *testing.F) {
31 for _, s := range sameNameSeeds {
32 f.Add(s[0], s[1])
33 }
34 dir := f.TempDir()
35 f.Fuzz(func(t *testing.T, name, other string) {
36 if !utf8.ValidString(name) || !utf8.ValidString(other) || !plainName(name) || !plainName(other) {
37 return
38 }
39 d, err := os.MkdirTemp(dir, "")
40 if err != nil {
41 t.Fatal(err)
42 }
43 defer os.RemoveAll(d)
44 if err := os.Mkdir(filepath.Join(d, name), 0o755); err != nil {
45 return // a name this file system refuses
46 }
47 a, err := os.Stat(filepath.Join(d, name))
48 if err != nil {
49 t.Fatal(err)
50 }
51 b, err := os.Stat(filepath.Join(d, other))
52 if err != nil || !os.SameFile(a, b) {
53 return
54 }
55 if !sameName(other, name) {
56 t.Errorf("%q opens %q on this file system, and sameName says it does not", other, name)
57 }
58 })
59}
60
61// A name in one path component, which the file system takes as itself.
62func plainName(s string) bool {
63 return s != "" && s != "." && s != ".." && len(s) < 200 && !strings.ContainsAny(s, "/\\:\x00") &&
64 !strings.HasSuffix(s, ".") && !strings.HasSuffix(s, " ")
65}
66
67// FuzzRootedOps plays a hostile client: any sequence of requests, on paths
68// made of "..", symlinks, absolute paths and spellings of .git. Whatever it
69// sends, nothing outside the root changes or is read, and .git stays as it
70// was.
71func FuzzRootedOps(f *testing.F) {
72 for _, seed := range [][]byte{
73 {0, 3, 1, 2, 5}, // write .git/config
74 {5, 9, 0, 4, 7, 2, 6, 0},
75 {6, 2, 2, 3, 1, 0, 0, 9, 3},
76 {7, 8, 10, 0, 12, 1, 1, 0},
77 {3, 9, 2, 0, 0, 11, 10, 0, 0, 13},
78 } {
79 f.Add(seed)
80 }
81 f.Fuzz(func(t *testing.T, ops []byte) {
82 if len(ops) > 64 {
83 return
84 }
85 c, root := setupRooted(t, false)
86 secretDir := filepath.Join(filepath.Dir(root), "secret-dir")
87 if err := os.MkdirAll(secretDir, 0o755); err != nil {
88 t.Fatal(err)
89 }
90 // Told apart by its size: the client writes 3 bytes at a time.
91 secret := strings.Repeat("SECRET", 200)[:1000]
92 if err := os.WriteFile(filepath.Join(secretDir, "s"), []byte(secret), 0o644); err != nil {
93 t.Fatal(err)
94 }
95 comps := []string{"", ".", "..", ".git", ".GIT", ".g" + zwnj + "it", "hooks", "config", "src", "gitlink", "configlink", "outside", "secret-dir", "s", "x"}
96 bases := []string{"", root + "/", "/", filepath.Dir(root) + "/"}
97 next := func() byte {
98 if len(ops) == 0 {
99 return 0
100 }
101 b := ops[0]
102 ops = ops[1:]
103 return b
104 }
105 pathOf := func() string {
106 b := next()
107 p := bases[int(b>>4)%len(bases)]
108 for i := 0; i <= int(b&3); i++ {
109 if i > 0 {
110 p += "/"
111 }
112 p += comps[int(next())%len(comps)]
113 }
114 if p == "" {
115 p = "."
116 }
117 return p
118 }
119 seen := func(b []byte) {
120 if strings.Contains(string(b), "SECRET") {
121 t.Fatalf("read a file outside the root: %q", b)
122 }
123 }
124 for len(ops) > 0 {
125 switch next() % 13 {
126 case 0:
127 if fh, err := c.OpenFile(pathOf(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC); err == nil {
128 fh.Write([]byte("PWN"))
129 fh.Close()
130 }
131 case 1:
132 if fh, err := c.OpenFile(pathOf(), os.O_WRONLY|os.O_APPEND); err == nil {
133 fh.Write([]byte("PWN"))
134 fh.Close()
135 }
136 case 2:
137 if fh, err := c.Open(pathOf()); err == nil {
138 b, _ := io.ReadAll(fh)
139 fh.Close()
140 seen(b)
141 }
142 case 3:
143 c.Mkdir(pathOf())
144 case 4:
145 c.Remove(pathOf())
146 case 5:
147 c.PosixRename(pathOf(), pathOf())
148 case 6:
149 c.Rename(pathOf(), pathOf())
150 case 7:
151 c.Symlink(pathOf(), pathOf())
152 case 8:
153 c.Link(pathOf(), pathOf())
154 case 9:
155 c.Chmod(pathOf(), 0o777)
156 case 10:
157 c.Truncate(pathOf(), 0)
158 case 11:
159 if ents, err := c.ReadDir(pathOf()); err == nil {
160 for _, e := range ents {
161 if e.Size() == int64(len(secret)) {
162 t.Fatalf("listed a folder outside the root")
163 }
164 }
165 }
166 case 12:
167 if l, err := c.ReadLink(pathOf()); err == nil {
168 seen([]byte(l))
169 }
170 }
171 }
172 assertUnchanged(t, root)
173 if b, err := os.ReadFile(filepath.Join(secretDir, "s")); err != nil || string(b) != secret {
174 t.Fatalf("secret-dir/s changed: %q, %v", b, err)
175 }
176 if ents, err := os.ReadDir(secretDir); err != nil || len(ents) != 1 {
177 t.Fatalf("secret-dir changed: %v, %v", ents, err)
178 }
179 })
180}
181
182// agent-vm decides which names to give from strings.EqualFold: every name it
183// takes as the same, the server must take as the same too.
184func FuzzSameNameEqualFold(f *testing.F) {
185 for _, s := range sameNameSeeds {
186 f.Add(s[0], s[1])
187 }
188 f.Fuzz(func(t *testing.T, a, b string) {
189 if strings.EqualFold(a, b) && !sameName(a, b) {
190 t.Errorf("EqualFold(%q, %q), and sameName says they differ", a, b)
191 }
192 if sameName(a, b) != sameName(b, a) {
193 t.Errorf("sameName(%q, %q) is not symmetric", a, b)
194 }
195 })
196}