跳到主要内容

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

浏览文件

checks.go

206 行 · 7.6 KB历史下载
1package cli
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "strings"
9
10 "github.com/sylvinus/agent-vm/internal/gitguard"
11 "github.com/sylvinus/agent-vm/internal/mounts"
12 "github.com/sylvinus/agent-vm/internal/vm"
13)
14
15// promptsDisabledBy is what turned the security questions off, if
16// something did: --unsafe-disable-security-prompts, or
17// AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS=1 in the shell. Never a file of
18// the project, which the VM can write.
19func (s *start) promptsDisabledBy() (string, bool) {
20 switch {
21 case s.opts.UnsafeNoPrompts:
22 return "--unsafe-disable-security-prompts", true
23 case os.Getenv("AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS") == "1":
24 return "AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS=1", true
25 }
26 return "", false
27}
28
29// confirmUnsafe is the question after a security warning: true to go on.
30// No by default, and when it cannot be asked; yes when the questions are
31// off.
32func (s *start) confirmUnsafe() bool {
33 if by, ok := s.promptsDisabledBy(); ok {
34 fmt.Fprintf(s.io.Stderr, "Continuing: %s.\n", by)
35 return true
36 }
37 return s.ui.CanAsk() && s.ui.AskYN("Continue anyway?", false)
38}
39
40func (s *start) warnf(format string, a ...any) {
41 fmt.Fprintf(s.io.Stderr, format+"\n", a...)
42}
43
44// writableGitWarning is printed on every start with .git left writable.
45func (s *start) writableGitWarning() {
46 why := "AGENT_VM_UNSAFE_WRITABLE_GIT=1"
47 if s.opts.UnsafeWritableGit {
48 why = "--unsafe-writable-git"
49 }
50 fmt.Fprintf(s.io.Stderr, `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
51!! WARNING: %s. The VM can write every .git.
52!!
53!! The agent can change .git/config and .git/hooks in the shared folders, and
54!! git on this machine runs what they name: on your next commit, and whenever
55!! your editor or shell prompt calls git. That is running commands on your
56!! host, outside the VM. Without it, .git stays read-only.
57!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
58`, why)
59}
60
61// checks are the security checks of a start, before anything changes. A
62// running VM that keeps running (s.vmUp) gets the warnings and nothing is
63// asked. False when the start must stop.
64func (s *start) checks() bool {
65 s.protect, s.names, s.notes = false, nil, ""
66 if !s.opts.Scratch {
67 if writableGitOptOut(s.opts.UnsafeWritableGit) {
68 if !cageWritableGit() {
69 s.writableGitWarning()
70 }
71 } else {
72 s.protect = true
73 }
74 }
75 if s.protect {
76 // The names the shares need: those always listed, and one per hooks
77 // folder git runs from in a share, its first component. A dot-name
78 // (.husky, .githooks) is taken as is; any other is read-only in
79 // every folder of every share, so it is asked first (yes by
80 // default, and when no one can answer). A folder that cannot be
81 // protected, or one declined, is a risk to accept. So are the
82 // scan's risks. Under --readonly the VM writes none of them.
83 scan := s.scan(s.ctx, s.dir)
84 var hooks []string
85 for _, h := range scan.Hooks {
86 rel, inRepo, _ := strings.Cut(h, "\t")
87 if rel == "" {
88 continue
89 }
90 name, ok := gitguard.HooksName(inRepo)
91 if s.opts.ReadOnly {
92 if ok {
93 hooks = append(hooks, name)
94 }
95 continue
96 }
97 if !ok {
98 switch {
99 case rel == ".":
100 s.warnf("Warning: git's core.hooksPath is the project directory itself, which the VM can write: git on this machine runs the hooks the agent puts there.")
101 case inRepo == ".":
102 s.warnf("Warning: git's core.hooksPath is '%s', which the VM can write, and no read-only name covers it (the top of a repository, or in a writable volume): git on this machine runs the hooks the agent puts there.", rel)
103 default:
104 s.warnf("Warning: git's core.hooksPath is '%s', which the VM can write, and its name cannot be made read-only: git on this machine runs the hooks the agent puts there.", rel)
105 }
106 if !s.vmUp && !s.confirmUnsafe() {
107 s.warnf("Aborted. Point core.hooksPath to a folder of the project (agent-vm keeps it read-only), or outside it.")
108 return false
109 }
110 continue
111 }
112 if _, off := s.promptsDisabledBy(); !strings.HasPrefix(name, ".") && !s.vmUp && !off && s.ui.CanAsk() &&
113 !s.ui.AskYN(fmt.Sprintf("Git runs hooks from %s. Make every '%s' folder in the project read-only for the VM?", rel, name), true) {
114 s.warnf("Warning: '%s' stays writable, and git on this machine runs the hooks the agent puts in %s.", name, rel)
115 if !s.confirmUnsafe() {
116 s.warnf("Aborted. Answer yes to protect it, or point core.hooksPath outside the project.")
117 return false
118 }
119 continue
120 }
121 hooks = append(hooks, name)
122 s.notes += fmt.Sprintf("Note: git runs hooks from %s (core.hooksPath): every '%s' in the project is read-only for the VM too.\n", rel, name)
123 }
124 s.names = gitguard.NamesWith(hooks)
125 // What the names accepted keep read-only is no risk; a name declined
126 // keeps nothing.
127 if risks := scan.Risks(s.names); !s.opts.ReadOnly && len(risks) > 0 {
128 s.warnf("Warning: git on this machine uses these, and the VM can write them:")
129 for _, r := range risks {
130 s.warnf(" %s", r)
131 }
132 if !s.vmUp && !s.confirmUnsafe() {
133 s.warnf("Aborted. Move them out of the shared folders, or have them name commands outside them.")
134 return false
135 }
136 }
137 }
138 // A repository not named .git, which no name protects: only a VM that
139 // can write needs the setting.
140 if !s.opts.ReadOnly && !s.opts.Scratch {
141 return s.checkBareRepo()
142 }
143 return true
144}
145
146// checkBareRepo offers to set safe.bareRepository=explicit when it can ask.
147// Not set: a security question, no by default. With the questions off,
148// nothing is offered: the global git config is not changed unasked. For a
149// VM already running, a warning only.
150func (s *start) checkBareRepo() bool {
151 st := gitguard.BareState(s.ctx)
152 if st == gitguard.BareOK || st == gitguard.BareNoGit {
153 return true
154 }
155 if st == gitguard.BareUnavailable {
156 s.warnf("Aborted. Host Git could not be checked. Check the Git installation, then retry.")
157 return false
158 }
159 if s.vmUp {
160 s.warnf("Warning: git on this machine uses repositories a VM creates under another name than .git ('agent-vm doctor' says more).")
161 return true
162 }
163 s.ui.Box("Git: repositories not named .git", gitguard.BareHint)
164 if st == gitguard.BareOld {
165 out, _ := exec.CommandContext(s.ctx, "git", "--version").Output()
166 s.warnf("Warning: %s is older than 2.38 and ignores that setting.", strings.TrimSpace(string(out)))
167 if s.confirmUnsafe() {
168 return true
169 }
170 s.warnf("Aborted. Upgrade git, then run the command above.")
171 return false
172 }
173 if _, off := s.promptsDisabledBy(); !off && s.ui.CanAsk() && s.ui.AskYN("Run it now? It changes your global git config.", true) {
174 err := exec.CommandContext(s.ctx, "git", "config", "--global", "safe.bareRepository", "explicit").Run()
175 if err == nil && gitguard.BareState(s.ctx) == gitguard.BareOK {
176 fmt.Fprintln(s.io.Stdout, "Git on this machine now ignores repositories not named .git unless you name them.")
177 return true
178 }
179 s.warnf("Warning: the setting did not take.")
180 }
181 s.warnf("Warning: not set. Until it is, git on this machine can run what a VM writes.")
182 if s.confirmUnsafe() {
183 return true
184 }
185 s.warnf("Aborted. Run the command above, then run agent-vm again.")
186 return false
187}
188
189// start is one start of a project's VM.
190type start struct {
191 *app
192 ctx context.Context
193 opts VMOpts
194 name string // the VM
195 dir string // the project, as the shell spells it
196
197 created bool // this run made the VM, and it is still there
198 vmUp bool // running when the start began, and not reset
199 protect bool // every .git read-only
200 names []string // the read-only names, when protect
201 notes string // hooks notes, printed once the names apply
202
203 built []vm.Mount // see shares
204 builtFiles []mounts.FileMount
205 builtKey string
206}