跳到主要内容

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

浏览文件

fuzz_test.go

155 行 · 4.8 KB历史下载
1package mounts
2
3import (
4 "io"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "github.com/sylvinus/agent-vm/internal/paths"
11)
12
13// Any volumes file: no panic, and every entry kept is one that may be
14// mounted: an existing source, a mode, a place that does not cover the
15// project, each place once.
16func FuzzEntries(f *testing.F) {
17 // ROOT is the sandbox's folder: data/a, data/b, data/file.txt, alink,
18 // repo/.git, bare.git, home/proj.
19 for _, s := range []string{
20 "ROOT/data/a:/mnt/data:rw\n",
21 "ROOT/data/b:rel/dir:ro:ROOT/home/*\n",
22 "ROOT/data/a:/mnt/a\nROOT/data/b:/mnt/a\n",
23 "ROOT/data/a:ROOT/home:rw\n",
24 "ROOT/data/a:ROOT/home/proj/../proj:rw\n",
25 "ROOT/data/a:..:rw\n",
26 "ROOT/data/file.txt:.claude.json:rw\n",
27 "ROOT/alink:/mnt/l:rw\n",
28 "ROOT/repo/.git:/mnt/g:rw\n",
29 "ROOT/data/a:/mnt/x:ro:[\n",
30 "# c\n ROOT/data/b \r\n",
31 "ROOT:/mnt/r:ro\n",
32 } {
33 f.Add(s)
34 }
35 f.Fuzz(func(t *testing.T, content string) {
36 s := newSandbox(t)
37 content = strings.ReplaceAll(content, "ROOT", s.root)
38 file := filepath.Join(s.state, "volumes")
39 if err := os.WriteFile(file, []byte(content), 0o644); err != nil {
40 t.Fatal(err)
41 }
42 refs := Refs(s.home, filepath.Join(s.root, "self"), s.state, filepath.Join(s.state, "lima"))
43 seen := map[string]bool{}
44 for _, e := range Entries(file, s.proj, s.home, refs, io.Discard) {
45 if _, err := os.Stat(e.Src); err != nil {
46 t.Fatalf("%q: source %q does not exist", content, e.Src)
47 }
48 if e.Mode != "ro" && e.Mode != "rw" {
49 t.Fatalf("%q: mode %q", content, e.Mode)
50 }
51 checkSafe(t, content, s, e)
52 mp := e.Dst
53 if mp == "" {
54 mp = e.Src
55 }
56 if !strings.HasPrefix(mp, "/") {
57 mp = s.proj + "/" + mp
58 }
59 mp = paths.Join("/", mp)
60 if _, in := paths.In(s.proj, mp); in {
61 t.Fatalf("%q: %q is mounted at %q, which covers the project", content, e.Src, mp)
62 }
63 if seen[mp] {
64 t.Fatalf("%q: two entries at %q", content, mp)
65 }
66 seen[mp] = true
67 }
68 })
69}
70
71// checkSafe is the oracle for FuzzEntries, written apart from UnsafeLocation
72// and GitDirShare: on the physical path of the source, nothing is the home
73// folder, above it, or the state folder or inside it, a writable folder is
74// not a repository's own or inside one, nor under .git or .hg.
75func checkSafe(t *testing.T, content string, s sandbox, e Entry) {
76 t.Helper()
77 real, err := filepath.EvalSymlinks(e.Src)
78 if err != nil {
79 t.Fatalf("%q: %q: %v", content, e.Src, err)
80 }
81 rel := func(base, p string) (string, bool) {
82 r, err := filepath.Rel(base, p)
83 return r, err == nil && r != ".." && !strings.HasPrefix(r, "../")
84 }
85 home, _ := filepath.EvalSymlinks(s.home)
86 state, _ := filepath.EvalSymlinks(s.state)
87 if _, in := rel(real, home); in {
88 t.Fatalf("%q: %q is or contains the home folder", content, e.Src)
89 }
90 if _, in := rel(state, real); in {
91 t.Fatalf("%q: %q is the state folder or inside it", content, e.Src)
92 }
93 if e.Mode != "rw" {
94 return
95 }
96 if fi, err := os.Stat(real); err != nil || !fi.IsDir() {
97 return
98 }
99 for a := real; ; a = filepath.Dir(a) {
100 if _, err := os.Stat(a + "/HEAD"); err == nil {
101 if _, err := os.Stat(a + "/objects"); err == nil {
102 if _, err := os.Stat(a + "/refs"); err == nil {
103 t.Fatalf("%q: writable %q is inside the repository folder %q", content, e.Src, a)
104 }
105 }
106 }
107 if n := filepath.Base(a); strings.EqualFold(n, ".git") || strings.EqualFold(n, ".hg") {
108 t.Fatalf("%q: writable %q is under %s", content, e.Src, n)
109 }
110 if filepath.Dir(a) == a {
111 break
112 }
113 }
114}
115
116// A filter with non-ASCII characters matches as written (0.3 took each byte
117// of é for a character before this test). A backslash escapes elsewhere,
118// and separates on Windows: both spellings, on every host.
119func TestMatchesNonASCII(t *testing.T) {
120 for _, win := range []bool{false, true} {
121 old := paths.Windows
122 paths.Windows = func() bool { return win }
123 t.Cleanup(func() { paths.Windows = old })
124 wantEscape := !win
125 for _, c := range []struct {
126 filter, dir string
127 want bool
128 }{
129 {"/home/café", "/home/café", true},
130 {"/home/caf?", "/home/café", true},
131 {"/home/caf\\é/*", "/home/café/x", wantEscape},
132 {"/home/café", "/home/cafe", false},
133 } {
134 if got := Matches(c.filter, c.dir, "/home", io.Discard); got != c.want {
135 t.Errorf("windows=%v Matches(%q, %q) = %v", win, c.filter, c.dir, got)
136 }
137 }
138 }
139}
140
141// Any project filter: no panic, and a filter without pattern characters
142// matches the folder it names.
143func FuzzMatches(f *testing.F) {
144 for _, s := range []string{"/home/*", "~/p", "[", "[!a]*", "[]]", "\\", "a\\*", "/x[a-", "/**/y"} {
145 f.Add(s, "/home/u/proj")
146 }
147 f.Fuzz(func(t *testing.T, filter, dir string) {
148 Matches(filter, dir, "/home/u", io.Discard)
149 if strings.HasPrefix(dir, "/") && !strings.ContainsAny(dir, "*?[\\") && dir != "/" && !strings.HasSuffix(dir, "/") {
150 if !Matches(dir, dir, "/home/u", io.Discard) {
151 t.Fatalf("%q does not match itself", dir)
152 }
153 }
154 })
155}