跳到主要内容

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

浏览文件

guard_test.go

173 行 · 5.2 KB历史下载
1package guard
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11type asker struct {
12 answers []bool // nil: no one to ask
13 asked []string
14 told []string
15}
16
17func (a *asker) guard(t *testing.T, list string) *Guard {
18 t.Helper()
19 dir := t.TempDir()
20 if list != "" {
21 os.WriteFile(filepath.Join(dir, FileName), []byte(list), 0o644)
22 }
23 g, err := Load(dir, "proj-1a2b3c4d")
24 if err != nil {
25 t.Fatal(err)
26 }
27 g.Ask = func(_ context.Context, title, msg string) (bool, bool) {
28 a.asked = append(a.asked, msg)
29 if a.answers == nil {
30 return false, false
31 }
32 ans := a.answers[0]
33 a.answers = a.answers[1:]
34 return ans, true
35 }
36 g.Tell = func(title, msg string) { a.told = append(a.told, msg) }
37 return g
38}
39
40// Only the guarded paths ask, at any depth, case ignored; an answer holds:
41// a yes until the VM stops, a no for a while.
42func TestAllow(t *testing.T) {
43 a := &asker{answers: []bool{false, true}}
44 g := a.guard(t, "")
45 for _, rel := range []string{"src/main.go", "Makefile", "package.json", ".envrc.bak", "x.vscode/tasks.json"} {
46 if !g.Allow("/p", rel, false) {
47 t.Errorf("%s refused", rel)
48 }
49 }
50 if len(a.asked) != 0 {
51 t.Fatalf("asked for unguarded paths: %q", a.asked)
52 }
53 if g.Allow("/p", "sub/.ENVRC", false) {
54 t.Error("no was not a no")
55 }
56 if !strings.Contains(a.asked[0], "change .envrc in /p") || !strings.Contains(a.asked[0], "Path: sub/.ENVRC") || !strings.Contains(a.asked[0], "direnv runs it") {
57 t.Errorf("question: %q", a.asked[0])
58 }
59 if g.Allow("/p", "sub/.ENVRC", false) || len(a.asked) != 1 {
60 t.Error("asked again right after a no")
61 }
62 if !g.Allow("/p", ".vscode/tasks.json", false) || !g.Allow("/p", ".vscode/tasks.json", false) || len(a.asked) != 2 {
63 t.Errorf("yes, then asked again: %d", len(a.asked))
64 }
65}
66
67// The user adds paths (Makefile) and removes defaults.
68func TestList(t *testing.T) {
69 a := &asker{answers: []bool{false}}
70 g := a.guard(t, "# mine\nMakefile\n!.envrc\n /scripts/deploy.sh \n")
71 if !g.Allow("/p", ".envrc", false) {
72 t.Error("removed default still guarded")
73 }
74 if g.Allow("/p", "Makefile", false) {
75 t.Error("added path not guarded")
76 }
77 if !strings.Contains(a.asked[0], "You asked agent-vm to guard it") {
78 t.Errorf("question: %q", a.asked[0])
79 }
80 if _, ok := g.match("app/scripts/deploy.sh", false); !ok {
81 t.Error("a path with folders")
82 }
83}
84
85// AGENT_VM_GUARDED_WRITES=deny: refused, no dialog.
86func TestDenyMode(t *testing.T) {
87 t.Setenv("AGENT_VM_GUARDED_WRITES", "deny")
88 g, err := Load(t.TempDir(), "p")
89 if err != nil {
90 t.Fatal(err)
91 }
92 g.Tell = func(string, string) { t.Error("told") }
93 if g.Allow("/p", ".envrc", false) || !g.Allow("/p", "main.go", false) {
94 t.Error("deny mode")
95 }
96}
97
98// No one to ask: refused, and said so.
99func TestNoOneToAsk(t *testing.T) {
100 a := &asker{}
101 g := a.guard(t, "")
102 if g.Allow("/p", ".envrc", false) {
103 t.Error("allowed with no one to ask")
104 }
105 if len(a.told) != 1 || !strings.Contains(a.told[0], "no dialog could ask you") {
106 t.Errorf("told: %q", a.told)
107 }
108}
109
110// On a Windows host Explorer sends your NTLM hash to the server a shortcut,
111// a library or a desktop.ini names, when it shows the file: they are guarded
112// by extension, and under the names Windows makes into them.
113func TestWindowsHostGuards(t *testing.T) {
114 old := windowsHost
115 t.Cleanup(func() { windowsHost = old })
116 for _, rel := range []string{"a.lnk", "docs/B.URL", "x.library-ms", "x.searchConnector-ms", "z.scf", "sub/desktop.ini",
117 "a.lnk.", "a.lnk ", "a.lnk:Zone.Identifier", "a.lnk::$DATA", "A.LnK. . "} {
118 windowsHost = true
119 a := &asker{answers: []bool{false}}
120 g := a.guard(t, "")
121 if g.Allow("/p", rel, false) || len(a.asked) != 1 {
122 t.Errorf("%q: let through (asked %d)", rel, len(a.asked))
123 }
124 windowsHost = false
125 a = &asker{}
126 if g = a.guard(t, ""); !g.Allow("/p", rel, false) || len(a.asked) != 0 {
127 t.Errorf("%q: asked on a host that is not Windows", rel)
128 }
129 }
130 windowsHost = true
131 a := &asker{}
132 g := a.guard(t, "")
133 for _, rel := range []string{"a.txt", "lnk", "a.lnkx", "notes/url", "src/main.go"} {
134 if !g.Allow("/p", rel, false) {
135 t.Errorf("%q: refused", rel)
136 }
137 }
138 if len(a.asked) != 0 {
139 t.Errorf("asked: %q", a.asked)
140 }
141 // The user's list removes a default, in any case.
142 g = a.guard(t, "!*.LNK\n")
143 if !g.Allow("/p", "a.lnk", false) || g.Allow("/p", "a.url", false) {
144 t.Error("!*.LNK")
145 }
146}
147
148// A folder moved or removed is looked into for a guarded file; one with more
149// entries than are looked at is asked about, not let through.
150func TestEntryTooBig(t *testing.T) {
151 root := t.TempDir()
152 os.MkdirAll(filepath.Join(root, "d", "z"), 0o755)
153 for _, f := range []string{"d/a", "d/b", "d/c", "d/z/.envrc"} {
154 os.WriteFile(filepath.Join(root, f), nil, 0o644)
155 }
156 old := maxScan
157 t.Cleanup(func() { maxScan = old })
158 maxScan = 2
159 a := &asker{answers: []bool{false}}
160 g := a.guard(t, "")
161 if g.Allow(root, "d", true) {
162 t.Error("a folder too big to look into was let through")
163 }
164 if len(a.asked) != 1 || !strings.Contains(a.asked[0], "more than 2 entries") {
165 t.Errorf("asked: %q", a.asked)
166 }
167 maxScan = old
168 a = &asker{answers: []bool{false}}
169 g = a.guard(t, "")
170 if g.Allow(root, "d", true) || len(a.asked) != 1 || !strings.Contains(a.asked[0], "holds .envrc") {
171 t.Errorf("a folder holding .envrc: %q", a.asked)
172 }
173}