跳到主要内容

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

浏览文件

safety_test.go

146 行 · 4.9 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// What Lima cannot mount, or 0.2 refused: said why.
14func TestUnmountable(t *testing.T) {
15 for dir, want := range map[string]string{
16 "/home/me/proj": "",
17 "/home/me/my proj": "whitespace",
18 "/home/me/a\tb": "whitespace",
19 `/home/me/a"b`: "a quote",
20 `/home/me/a\b`: "a backslash",
21 "/home/me/a\x01b": "a control character",
22 "/home/me/été/proj": "",
23 "/home/me/R&D": "",
24 "/{{.Home}}": "a template",
25 "/home/me/a\xffb": "not valid UTF-8",
26 "/home/me/a\u2028b": "whitespace",
27 } {
28 why, bad := Unmountable(dir)
29 if bad != (want != "") || !strings.Contains(why, want) {
30 t.Errorf("%q: %q %v", dir, why, bad)
31 }
32 }
33}
34
35// A single file given as a symlink (stow's dotfiles) is staged as the file
36// it names, in step with it: not as a link pointing out of the staging
37// folder, which the VM could not follow.
38func TestStageSymlink(t *testing.T) {
39 dir := t.TempDir()
40 real := filepath.Join(dir, "dotfiles", "gitconfig")
41 os.MkdirAll(filepath.Dir(real), 0o755)
42 os.WriteFile(real, []byte("v1"), 0o644)
43 link := filepath.Join(dir, ".gitconfig")
44 os.Symlink(real, link)
45 dst := filepath.Join(dir, "stage", "f")
46 if !Stage(link, dst, io.Discard) {
47 t.Fatal("not staged")
48 }
49 if fi, err := os.Lstat(dst); err != nil || fi.Mode()&os.ModeSymlink != 0 {
50 t.Fatalf("staged as a link: %v %v", fi, err)
51 }
52 os.WriteFile(real, []byte("v2"), 0o644)
53 if b, _ := os.ReadFile(dst); string(b) != "v2" {
54 t.Errorf("not in step with the file: %q", b)
55 }
56}
57
58// Where the file system ignores case, the home folder in other capitals is
59// the home folder.
60func TestUnsafeLocationNoCase(t *testing.T) {
61 s := newSandbox(t)
62 refs := Refs(s.home, filepath.Join(s.root, "self"), s.state, filepath.Join(s.state, "lima"))
63 upper := strings.ToUpper(s.home)
64 // On a file system that ignores case the uppercased home is the
65 // home folder, and refused below; here case matters.
66 if !paths.NoCase() {
67 if _, bad := UnsafeLocation(upper, refs); bad {
68 t.Error("refused where case matters")
69 }
70 }
71 old := paths.NoCase
72 paths.NoCase = func() bool { return true }
73 t.Cleanup(func() { paths.NoCase = old })
74 refs = Refs(s.home, filepath.Join(s.root, "self"), s.state, filepath.Join(s.state, "lima"))
75 if why, bad := UnsafeLocation(upper, refs); !bad || !strings.Contains(why, "your home directory") {
76 t.Errorf("home in capitals: %q %v", why, bad)
77 }
78 if why, bad := UnsafeLocation(strings.ToUpper(s.state)+"/x", refs); !bad {
79 t.Errorf("inside the state dir in capitals: %q %v", why, bad)
80 }
81}
82
83// Folders of credentials and applications' data under the home folder are
84// refused as projects, from inside, equal or above, through links, with the
85// case a case-ignoring file system gives them; the other folders of the home
86// folder are not.
87func TestCredentialLocation(t *testing.T) {
88 s := newSandbox(t)
89 for _, d := range []string{".ssh/keys", ".config/gcloud", "Library/Keychains", ".local/share/keyrings", ".local/src", ".sshx", "work/.ssh-notes", ".mozilla"} {
90 os.MkdirAll(filepath.Join(s.home, d), 0o755)
91 }
92 os.MkdirAll(filepath.Join(s.root, "appdata", "app"), 0o755)
93 t.Setenv("APPDATA", filepath.Join(s.root, "appdata"))
94 t.Setenv("LOCALAPPDATA", "")
95 t.Setenv("XDG_CONFIG_HOME", "")
96 os.Symlink(s.home+"/.ssh", s.proj+"/lnk")
97 refs := s.refs()
98 for _, c := range []struct {
99 dir string
100 bad bool
101 }{
102 {s.home + "/.ssh", true},
103 {s.home + "/.ssh/keys", true},
104 {s.home + "/.config", true},
105 {s.home + "/.config/gcloud", true},
106 {s.home + "/Library/Keychains", true},
107 {s.home + "/.mozilla", true},
108 {s.home + "/.local", true},
109 {s.home + "/.local/share", true},
110 {s.home + "/.local/share/keyrings", true},
111 {s.proj + "/lnk", true},
112 {s.root + "/appdata/app", true},
113 {s.proj, false},
114 {s.proj + "/sub", false},
115 {s.home + "/.local/src", false},
116 {s.home + "/.sshx", false},
117 {s.home + "/work/.ssh-notes", false},
118 {s.root + "/data/a", false},
119 } {
120 why, bad := CredentialLocation(c.dir, refs)
121 if bad != c.bad {
122 t.Errorf("%s: %q %v", c.dir, why, bad)
123 }
124 if _, bad := UnsafeProject(c.dir, refs); c.bad && !bad {
125 t.Errorf("UnsafeProject lets %s through", c.dir)
126 }
127 }
128 if why, _ := CredentialLocation(s.home+"/.ssh", refs); !strings.Contains(why, "~/.ssh") {
129 t.Errorf("why: %q", why)
130 }
131 // A credentials folder that is a link to another: that one counts too.
132 os.RemoveAll(s.home + "/.aws")
133 os.MkdirAll(s.root+"/dotfiles/aws", 0o755)
134 os.Symlink(s.root+"/dotfiles/aws", s.home+"/.aws")
135 if _, bad := CredentialLocation(s.root+"/dotfiles/aws", refs); !bad {
136 t.Error("the target of ~/.aws was let through")
137 }
138 // Other capitals, where the file system ignores case.
139 old := paths.NoCase
140 paths.NoCase = func() bool { return true }
141 t.Cleanup(func() { paths.NoCase = old })
142 refs = s.refs()
143 if _, bad := CredentialLocation(strings.ToUpper(s.home)+"/.SSH", refs); !bad {
144 t.Error("~/.SSH let through where case is ignored")
145 }
146}