跳到主要内容

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

浏览文件

nofollow_test.go

160 行 · 5.1 KB历史下载
1//go:build !windows
2
3package env
4
5import (
6 "errors"
7 "os"
8 "path/filepath"
9 "strconv"
10 "syscall"
11 "testing"
12
13 "github.com/sylvinus/agent-vm/internal/bashref"
14)
15
16func code(err error) int {
17 switch {
18 case err == nil:
19 return 0
20 case errors.Is(err, ErrNoFile):
21 return 1
22 case errors.Is(err, ErrUnsafe):
23 return 3
24 }
25 return 2
26}
27
28// layout makes a project with every kind of thing NoFollow meets, and a
29// secret outside it.
30func layout(t *testing.T) (top, secret string) {
31 t.Helper()
32 root := t.TempDir()
33 top = filepath.Join(root, "proj")
34 secret = filepath.Join(root, "secret")
35 must := func(err error) {
36 if err != nil {
37 t.Fatal(err)
38 }
39 }
40 must(os.MkdirAll(filepath.Join(top, "d/e"), 0o755))
41 must(os.WriteFile(secret, []byte("SECRET\n"), 0o600))
42 must(os.MkdirAll(filepath.Join(root, "outdir"), 0o755))
43 must(os.WriteFile(filepath.Join(top, "plain"), []byte("A=1\n"), 0o644))
44 must(os.WriteFile(filepath.Join(top, "d/e/f"), []byte("deep\n"), 0o644))
45 must(os.Symlink(secret, filepath.Join(top, "link")))
46 must(os.Symlink(filepath.Join(root, "outdir"), filepath.Join(top, "dirlink")))
47 must(os.Symlink("plain", filepath.Join(top, "innerlink")))
48 must(syscall.Mkfifo(filepath.Join(top, "fifo"), 0o644))
49 return top, secret
50}
51
52var rels = []string{"plain", "d/e/f", "missing", "new/dir/file", "link", "dirlink/x", "innerlink", "fifo", "d", "d/e", "../secret", "", "./plain", "d//e/./f"}
53
54func TestNoFollowBash(t *testing.T) {
55 if _, err := os.Stat("/usr/bin/perl"); err != nil {
56 t.Skip("no perl for the bash reference")
57 }
58 for _, op := range []string{"read", "write", "mkdir", "touch"} {
59 for _, rel := range rels {
60 gtop, gsecret := layout(t)
61 btop, bsecret := layout(t)
62 var got []byte
63 var err error
64 switch op {
65 case "read":
66 got, err = ReadIn(gtop, rel)
67 case "write":
68 err = WriteIn(gtop, rel, []byte("NEW\n"))
69 case "mkdir":
70 err = MkdirIn(gtop, rel)
71 case "touch":
72 err = TouchIn(gtop, rel)
73 }
74 r := bashref.Script(t, "", nil, `printf 'NEW\n' | _agent_vm_nofollow "$@"`, op, btop, rel)
75 if code(err) != r.Code {
76 t.Errorf("%s %q: go %d (%v), bash %d %s", op, rel, code(err), err, r.Code, r.Stderr)
77 }
78 if op == "read" && err == nil && string(got) != r.Stdout {
79 t.Errorf("read %q: go %q, bash %q", rel, got, r.Stdout)
80 }
81 if b, _ := os.ReadFile(gsecret); string(b) != "SECRET\n" {
82 t.Errorf("%s %q changed the secret outside the project", op, rel)
83 }
84 if b, _ := os.ReadFile(bsecret); string(b) != "SECRET\n" {
85 t.Errorf("bash %s %q changed the secret", op, rel)
86 }
87 if _, err := os.Stat(filepath.Join(filepath.Dir(gtop), "outdir", "x")); err == nil {
88 t.Errorf("%s %q wrote through a linked folder", op, rel)
89 }
90 if op == "write" && err == nil {
91 if b, _ := os.ReadFile(filepath.Join(gtop, rel)); string(b) != "NEW\n" {
92 t.Errorf("write %q: %q", rel, b)
93 }
94 fi, _ := os.Lstat(filepath.Join(gtop, rel))
95 if fi.Mode().Perm() != 0o600 || !fi.Mode().IsRegular() {
96 t.Errorf("write %q: mode %v", rel, fi.Mode())
97 }
98 }
99 }
100 }
101}
102
103func TestProjectRelBash(t *testing.T) {
104 root := t.TempDir()
105 proj := filepath.Join(root, "proj")
106 os.MkdirAll(filepath.Join(proj, "sub"), 0o755)
107 os.MkdirAll(filepath.Join(root, "else"), 0o755)
108 os.Symlink(proj, filepath.Join(root, "projlink"))
109 os.Symlink(filepath.Join(root, "else"), filepath.Join(root, "elselink"))
110 os.Symlink(filepath.Join(proj, "sub"), filepath.Join(root, "else", "into"))
111 os.Symlink("../proj/sub", filepath.Join(root, "rellink"))
112 loop := filepath.Join(root, "loop")
113 os.Symlink(loop, loop)
114 for _, target := range []string{
115 proj + "/.agent-vm.env", proj + "/sub/x", root + "/projlink/x", root + "/elselink/x", root + "/else/into/y",
116 root + "/rellink/z", root + "/proj", root + "/other/x", loop + "/x", "/etc/passwd", proj + "/../proj/x",
117 root + "/elselink/../proj/x",
118 } {
119 r := bashref.Run(t, "", nil, "_agent_vm_project_rel", proj, target)
120 rel, ok := ProjectRel(proj, target)
121 want := r.Stdout
122 if len(want) > 0 {
123 want = want[:len(want)-1]
124 }
125 if ok != (r.Code == 0) || (ok && rel != want) {
126 t.Errorf("ProjectRel(%q) = %q %v, bash %q %d", target, rel, ok, want, r.Code)
127 }
128 }
129}
130
131func TestPayloadBash(t *testing.T) {
132 root := t.TempDir()
133 proj := filepath.Join(root, "proj")
134 os.MkdirAll(proj, 0o755)
135 st := filepath.Join(root, "state")
136 os.MkdirAll(st, 0o755)
137 for i, c := range []struct{ shared, projEnv, projFile string }{
138 {"A=1\r\nB=2", "", ""},
139 {"A=1\n", "../outside.env", "P=1\r\n"},
140 {"", ".agent-vm.env", "P=1\n"},
141 {"A=1", "", ""},
142 } {
143 os.Remove(filepath.Join(st, "env"))
144 if c.shared != "" {
145 os.WriteFile(filepath.Join(st, "env"), []byte(c.shared), 0o600)
146 }
147 envs := []string{"AGENT_VM_STATE_DIR=" + st}
148 if c.projEnv != "" {
149 envs = append(envs, "AGENT_VM_PROJECT_ENV="+c.projEnv)
150 t.Setenv("AGENT_VM_PROJECT_ENV", c.projEnv)
151 os.WriteFile(ProjectPath(proj, c.projEnv), []byte(c.projFile), 0o600)
152 } else {
153 t.Setenv("AGENT_VM_PROJECT_ENV", "")
154 }
155 r := bashref.Run(t, "", envs, "_agent_vm_env_payload", proj)
156 if got := Payload(filepath.Join(st, "env"), proj); got != r.Stdout {
157 t.Errorf("case %s: Payload = %q, bash %q", strconv.Itoa(i), got, r.Stdout)
158 }
159 }
160}