跳到主要内容

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

浏览文件

policy.go

83 行 · 3.0 KB历史下载
1package cli
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "fmt"
7 "io"
8 "os"
9 "slices"
10 "strings"
11
12 "github.com/sylvinus/agent-vm/internal/guard"
13 "github.com/sylvinus/agent-vm/internal/netguard"
14 "github.com/sylvinus/agent-vm/internal/state"
15)
16
17// The network and guarded-files policies are read when a VM boots, by its
18// hostagent, and kept until it stops. Starting a VM records what they were
19// (PolicyOf); a later start that finds the VM running says so when they
20// differ now.
21
22// quiet is AGENT_VM_QUIET=1: notices that are not about a risk to the host
23// are not printed.
24func quiet() bool { return os.Getenv("AGENT_VM_QUIET") == "1" }
25
26// policyFingerprint identifies the policies as the hostagent would load
27// them now: the network file's rules and the guarded paths, comments and
28// order not counting, and the two switches in the environment.
29func policyFingerprint(st state.Dir) string {
30 var parts []string
31 if pol, err := netguard.Load(string(st)); err == nil {
32 parts = append(parts, fmt.Sprintf("open=%t", pol.Open))
33 for _, r := range pol.Allow {
34 parts = append(parts, fmt.Sprintf("allow %v %d", r.Net, r.Port))
35 }
36 for _, d := range pol.Domains {
37 parts = append(parts, "domain "+d)
38 }
39 } else {
40 parts = append(parts, "network: "+err.Error())
41 }
42 if g, err := guard.Load(string(st), ""); err == nil {
43 for p := range g.Paths {
44 parts = append(parts, "guard "+p)
45 }
46 } else {
47 parts = append(parts, "guarded: "+err.Error())
48 }
49 parts = append(parts, fmt.Sprintf("deny=%t", os.Getenv("AGENT_VM_GUARDED_WRITES") == "deny"))
50 slices.Sort(parts)
51 sum := sha256.Sum256([]byte(strings.Join(parts, "\n")))
52 return hex.EncodeToString(sum[:])
53}
54
55// recordPolicy notes the policies the VM was just booted with.
56func (s *start) recordPolicy() {
57 if err := s.state.Write(state.PolicyOf+s.name, []byte(policyFingerprint(s.state)+"\n")); err != nil {
58 s.warnf("Warning: could not record the network and guarded-files policies VM '%s' booted with: %v", s.name, err)
59 }
60}
61
62// notePolicyChange says so when the running VM booted with other policies
63// than the ones in force now. A VM with no record (booted by an agent-vm
64// that kept none) is not said anything about.
65func (s *start) notePolicyChange() {
66 booted := s.state.ReadMarker(state.PolicyOf, s.name)
67 if booted == "" || booted == policyFingerprint(s.state) {
68 return
69 }
70 s.warnf("Note: VM '%s' booted with other network or guarded-files settings than now (%s, %s, AGENT_VM_UNSAFE_OPEN_NETWORK, AGENT_VM_GUARDED_WRITES). The running VM keeps the old ones: 'agent-vm stop', then run again to apply them.",
71 s.name, netguard.FileName, guard.FileName)
72}
73
74// warnDomains says, once per start, that the network file's 'domain' lines
75// are experimental.
76func warnDomains(st state.Dir, w io.Writer) {
77 if quiet() {
78 return
79 }
80 if pol, err := netguard.Load(string(st)); err == nil && len(pol.Domains) > 0 {
81 fmt.Fprintf(w, "Warning: the 'domain' lines in %s are experimental: do not rely on them alone to confine a VM. AGENT_VM_QUIET=1 hides this.\n", st.Path(netguard.FileName))
82 }
83}