跳到主要内容

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

浏览文件

safety.go

109 行 · 4.3 KB历史下载
1package cage
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7 "time"
8
9 "github.com/sylvinus/agent-vm/internal/diagnostics"
10 "github.com/sylvinus/agent-vm/internal/exitpolicy"
11)
12
13// SafetyPolicyVersion changes when the mandatory evidence contract changes.
14// Reports authorize one running VM with one applied configuration.
15const SafetyPolicyVersion = "cage-safety/3"
16const SafetyReportLifetime = 24 * time.Hour
17
18type CheckState = exitpolicy.State
19
20const (
21 CheckPassed = exitpolicy.Passed
22 CheckFailed = exitpolicy.Failed
23 CheckUnknown = exitpolicy.Unknown
24)
25
26type SafetyCheck struct {
27 ID string `json:"id"`
28 Title string `json:"title"`
29 State CheckState `json:"state"`
30 Critical bool `json:"critical"`
31 Evidence string `json:"evidence"`
32 Remedy string `json:"remedy,omitempty"`
33 CheckedAt time.Time `json:"checked_at"`
34}
35
36type SafetyReport struct {
37 Policy string `json:"policy"`
38 ID string `json:"id"`
39 RunID string `json:"run_id"`
40 CheckID string `json:"check_id"`
41 Configuration string `json:"configuration"`
42 CreatedAt time.Time `json:"created_at"`
43 Checks []SafetyCheck `json:"checks"`
44}
45
46var requiredSafetyChecks = [...]string{
47 "egress_policy", "proxy_transport", "dns_transport", "guest_settings",
48 "browser_settings", "filesystem_boundary", "management_boundary",
49 "fail_closed", "udp_policy", "ipv6_policy", "exit_consistency",
50}
51
52// Authorizes rejects missing, duplicate, unknown, stale or mismatched evidence.
53// The required IDs are defined here, never supplied by the frontend or a provider.
54func (r SafetyReport) Authorizes(runID, configuration string, now time.Time) error {
55 if r.Policy != SafetyPolicyVersion || r.ID == "" || r.CheckID == "" || runID == "" || configuration == "" ||
56 r.RunID != runID || r.Configuration != configuration {
57 return errors.New("检测报告不属于当前环境配置,请重新检测")
58 }
59 if r.CreatedAt.IsZero() || r.CreatedAt.After(now.Add(time.Minute)) || !now.Before(r.CreatedAt.Add(SafetyReportLifetime)) {
60 return errors.New("检测报告已过期,请重新检测")
61 }
62 seen := make(map[string]SafetyCheck, len(r.Checks))
63 for _, check := range r.Checks {
64 if _, duplicate := seen[check.ID]; duplicate {
65 return errors.New("检测报告包含重复项目")
66 }
67 if check.State != CheckPassed && check.State != CheckFailed && check.State != CheckUnknown {
68 return errors.New("检测状态无效")
69 }
70 seen[check.ID] = check
71 if check.Critical && check.State != CheckPassed {
72 state := "未通过"
73 if check.State == CheckUnknown {
74 state = "未能确认"
75 }
76 title := safetyCheckTitle(check)
77 evidence := strings.TrimRight(diagnostics.Redact(check.Evidence), "。;;")
78 if evidence == "" {
79 evidence = "没有取得检测结果"
80 }
81 next := strings.TrimRight(diagnostics.Redact(check.Remedy), "。;;")
82 if next == "" {
83 next = "打开“检查清单”查看此项结果,重新检测;仍失败时在设置中导出诊断包"
84 }
85 return fmt.Errorf("联网检查未通过:%s%s。%s。%s。外网保持关闭。", title, state, evidence, next)
86 }
87 }
88 for _, id := range requiredSafetyChecks {
89 check, found := seen[id]
90 if !found || !check.Critical || check.State != CheckPassed || check.Evidence == "" ||
91 check.CheckedAt.Before(r.CreatedAt) || check.CheckedAt.After(now.Add(time.Minute)) {
92 return fmt.Errorf("联网检查缺少有效证据:%s的记录缺失、过期或不完整。请重新检测;仍失败时在设置中导出诊断包。外网保持关闭。", safetyCheckTitle(SafetyCheck{ID: id}))
93 }
94 }
95 return nil
96}
97
98func safetyCheckTitle(check SafetyCheck) string {
99 if check.Title != "" && check.Title != check.ID {
100 return check.Title
101 }
102 titles := map[string]string{
103 "egress_policy": "环境外出站约束", "proxy_transport": "代理连接", "dns_transport": "DNS 解析", "guest_settings": "系统时区与语言", "browser_settings": "浏览器时区与语言", "filesystem_boundary": "项目目录共享", "management_boundary": "宿主管理接口隔离", "fail_closed": "代理故障断网", "udp_policy": "UDP 出站约束", "ipv6_policy": "IPv6 出站约束", "exit_consistency": "出口 IP 与地区",
104 }
105 if title := titles[check.ID]; title != "" {
106 return title
107 }
108 return "必需检测项目"
109}