跳到主要内容

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

浏览文件

safety.go

236 行 · 7.9 KB历史下载
1// Package mounts is what a project's VM is given of this machine: the
2// project, and the entries of ~/.agent-vm/volumes, each checked first.
3package mounts
4
5import (
6 "fmt"
7 "os"
8 "strings"
9 "unicode"
10 "unicode/utf8"
11
12 "github.com/sylvinus/agent-vm/internal/paths"
13)
14
15// BaseNames are always read-only in every share. .hg: Mercurial runs the
16// hooks of .hg/hgrc in a repository the user owns, which files written
17// through a share are. .sl (Sapling) and .jj (Jujutsu) keep a repository's
18// config, which names commands to run, in the same way. .lfsconfig is not
19// one: git-lfs reads only a few URL and filter keys from it, no command.
20var BaseNames = []string{".git", ".hg", ".sl", ".jj"}
21
22// UnderName reports whether the relative path rel goes through one of names
23// (BaseNames when nil), matched as the SFTP server does: per component,
24// whatever the case.
25func UnderName(rel string, names []string) bool {
26 if names == nil {
27 names = BaseNames
28 }
29 for _, c := range strings.Split(rel, "/") {
30 for _, n := range names {
31 if c != "" && strings.EqualFold(c, n) {
32 return true
33 }
34 }
35 }
36 return false
37}
38
39// Ref is a location no share may reach.
40type Ref struct {
41 What string // "your home directory", ...
42 Path string // physical, folded
43}
44
45// SelfRef is the What of the Ref to agent-vm's own folder.
46const SelfRef = "agent-vm itself"
47
48const homeRef = "your home directory"
49
50// Refs are the locations no share may reach: the home folder (dotfiles,
51// SSH keys, every other project), agent-vm itself (the host runs its
52// files), agent-vm's state (every VM's env) and Lima's homes, agent-vm's
53// and the user's own (the VMs' SSH key and disks, its _config adding
54// mounts to every VM).
55func Refs(home, self, state string, limaHomes ...string) []Ref {
56 refs := []Ref{{"your home directory", home}, {SelfRef, self}, {"agent-vm's state", state}}
57 for _, h := range limaHomes {
58 refs = append(refs, Ref{"Lima's state", h})
59 }
60 var out []Ref
61 for _, r := range refs {
62 if r.Path == "" {
63 continue
64 }
65 // Not resolved when it does not exist: as typed, never emptied, or
66 // every path would be inside it.
67 if p, err := paths.Real(r.Path); err == nil && p != "" {
68 r.Path = p
69 } else {
70 r.Path = strings.TrimSuffix(r.Path, "/")
71 }
72 if r.Path == "" {
73 continue
74 }
75 r.Path = paths.Fold(r.Path)
76 out = append(out, r)
77 }
78 return out
79}
80
81// UnsafeLocation says why dir must not be shared with a VM, writable or
82// not, when it is or contains the home folder, or is, contains or is inside
83// one of the other refs. A project inside the home folder is what is
84// expected. Compared as physical paths, whatever the case where the file
85// system ignores it.
86func UnsafeLocation(dir string, refs []Ref) (string, bool) {
87 if p, err := paths.Real(dir); err == nil {
88 dir = p
89 }
90 d := strings.TrimSuffix(paths.Fold(dir), "/") + "/"
91 for _, r := range refs {
92 p := strings.TrimSuffix(r.Path, "/") + "/"
93 if strings.HasPrefix(p, d) {
94 return "is, or contains, " + r.What, true
95 }
96 if r.What != "your home directory" && strings.HasPrefix(d, p) {
97 return "is inside " + r.What, true
98 }
99 }
100 return "", false
101}
102
103// GitDirShare says why dir must not be a writable share: it is, or is
104// inside, a repository's own folder: a .git or .hg (by name, as the SFTP
105// server matches them), or a bare repository (HEAD, objects/ and refs/, as
106// git finds one). The read-only names apply below a share's root, so such a
107// share would be writable whole, hooks and config included.
108func GitDirShare(dir string) (string, bool) {
109 if p, err := paths.Real(dir); err == nil {
110 dir = p
111 }
112 // dir and each folder above it, the root ("/", "C:/") excepted.
113 root := paths.Root(dir)
114 for a := strings.TrimSuffix(dir, "/"); a+"/" != root; {
115 if isFile(a+"/HEAD") && isDir(a+"/objects") && isDir(a+"/refs") {
116 return fmt.Sprintf("is, or is inside, a git repository's own folder (%s)", a), true
117 }
118 i := strings.LastIndexByte(a, '/')
119 if i < 0 {
120 break
121 }
122 a = a[:i]
123 }
124 if UnderName(strings.TrimPrefix(dir, root), nil) {
125 return "is, or is inside, a folder git or Mercurial keeps its repository in (.git, .hg)", true
126 }
127 return "", false
128}
129
130// credentialRel are the places under the home folder where credentials and
131// applications' data live: keys, cloud and cluster logins, password managers,
132// browser profiles, keyrings. None is a project; one shared with a VM, read
133// or write, is a secret given away. A project may be anywhere else in the
134// home folder, so these are named. Lowercase-folded where case is ignored.
135var credentialRel = []string{
136 ".ssh", ".aws", ".gnupg", ".pki", ".kube", ".docker", ".azure", ".oci", ".terraform.d",
137 ".config", ".password-store", ".1password", ".op", ".mozilla", ".thunderbird", ".var", "snap",
138 ".local/share/keyrings", ".local/share/gnome-keyring", ".local/share/kwalletd",
139 "Library", "AppData",
140}
141
142// credentialAbs are the environment variables naming a folder of that kind
143// outside the home folder's defaults.
144var credentialAbs = []string{"APPDATA", "LOCALAPPDATA", "XDG_CONFIG_HOME"}
145
146// CredentialLocation says why dir must not be shared with a VM: it is,
147// contains or is inside a folder of credentials or applications' data under
148// the home folder (see credentialRel), found in refs. Those folders resolved
149// through their links count too, as dir does.
150func CredentialLocation(dir string, refs []Ref) (string, bool) {
151 home := ""
152 for _, r := range refs {
153 if r.What == homeRef {
154 home = r.Path
155 }
156 }
157 if home == "" {
158 return "", false
159 }
160 if p, err := paths.Real(dir); err == nil {
161 dir = p
162 }
163 d := strings.TrimSuffix(paths.Fold(dir), "/") + "/"
164 check := func(shown, p string) (string, bool) {
165 for _, c := range []string{p, realOr(p)} {
166 c = strings.TrimSuffix(paths.Fold(c), "/") + "/"
167 if strings.HasPrefix(d, c) || strings.HasPrefix(c, d) {
168 return "is, contains or is inside " + shown + ", which holds credentials or applications' data", true
169 }
170 }
171 return "", false
172 }
173 for _, rel := range credentialRel {
174 if why, ok := check("~/"+rel, strings.TrimSuffix(home, "/")+"/"+rel); ok {
175 return why, true
176 }
177 }
178 for _, v := range credentialAbs {
179 if p := paths.Host(os.Getenv(v)); p != "" && paths.IsAbs(p) {
180 if why, ok := check(p, p); ok {
181 return why, true
182 }
183 }
184 }
185 return "", false
186}
187
188// realOr is p with its links resolved when it exists, else p.
189func realOr(p string) string {
190 if r, err := paths.Real(p); err == nil {
191 return r
192 }
193 return p
194}
195
196// UnsafeProject says why the project dir must not be shared: see
197// UnsafeLocation, CredentialLocation and GitDirShare.
198func UnsafeProject(dir string, refs []Ref) (string, bool) {
199 if why, ok := UnsafeLocation(dir, refs); ok {
200 return why, true
201 }
202 if why, ok := CredentialLocation(dir, refs); ok {
203 return why, true
204 }
205 return GitDirShare(dir)
206}
207
208// Unmountable says why Lima cannot be given dir as a share: with whitespace
209// the mount fails silently, leaving a bare, root-owned mount point; a quote,
210// a backslash or a control character are refused as 0.2 refused them; and
211// what Lima would read as another path (Unwritable).
212func Unmountable(dir string) (string, bool) {
213 if strings.IndexFunc(dir, unicode.IsSpace) >= 0 {
214 return "contains whitespace, which Lima cannot mount", true
215 }
216 if strings.ContainsAny(dir, "\"\\") || strings.IndexFunc(dir, unicode.IsControl) >= 0 {
217 return "contains a quote, a backslash or a control character", true
218 }
219 return Unwritable(dir)
220}
221
222// Unwritable says why s cannot go into Lima's config as written: Lima
223// expands Go templates in share paths ({{.Home}} is the home folder), and a
224// byte that is not UTF-8 becomes U+FFFD on the way.
225func Unwritable(s string) (string, bool) {
226 if strings.Contains(s, "{{") {
227 return "contains {{, which Lima reads as a template", true
228 }
229 if !utf8.ValidString(s) {
230 return "is not valid UTF-8", true
231 }
232 return "", false
233}
234
235func isFile(p string) bool { fi, err := os.Stat(p); return err == nil && fi.Mode().IsRegular() }
236func isDir(p string) bool { fi, err := os.Stat(p); return err == nil && fi.IsDir() }