跳到主要内容

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

浏览文件

guard_test.go

71 行 · 1.9 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "os"
7 "path/filepath"
8 "slices"
9 "testing"
10)
11
12// Guard is asked before each write the read-only names allow, with the path
13// relative to the served directory; a no refuses it and nothing changes.
14// Reads never ask.
15func TestRootedGuard(t *testing.T) {
16 var asked []string
17 allow := false
18 Guard = func(root, rel string, entry bool) bool {
19 asked = append(asked, rel)
20 return allow || rel != ".envrc" && rel != "src/new"
21 }
22 t.Cleanup(func() { Guard = nil })
23 c, root := setupRooted(t, false)
24 os.WriteFile(filepath.Join(root, ".envrc"), []byte("orig"), 0o644)
25 p := func(s string) string { return filepath.Join(root, s) }
26
27 for _, op := range []struct {
28 name string
29 rel string
30 f func() error
31 }{
32 {"write", ".envrc", func() error {
33 f, err := c.OpenFile(p(".envrc"), os.O_WRONLY|os.O_TRUNC)
34 if err == nil {
35 f.Close()
36 }
37 return err
38 }},
39 {"remove", ".envrc", func() error { return c.Remove(p(".envrc")) }},
40 {"rename onto", ".envrc", func() error { return c.PosixRename(p("src/main.go"), p(".envrc")) }},
41 {"chmod", ".envrc", func() error { return c.Chmod(p(".envrc"), 0o755) }},
42 {"mkdir", "src/new", func() error { return c.Mkdir(p("src/new")) }},
43 } {
44 asked = nil
45 if err := op.f(); err == nil {
46 t.Errorf("%s: allowed though Guard said no", op.name)
47 }
48 if !slices.Contains(asked, op.rel) {
49 t.Errorf("%s: Guard asked %q, not %q", op.name, asked, op.rel)
50 }
51 }
52 if b, _ := os.ReadFile(p(".envrc")); string(b) != "orig" {
53 t.Errorf(".envrc changed: %q", b)
54 }
55 if _, err := os.Stat(p("src/main.go")); err != nil {
56 t.Error("src/main.go moved")
57 }
58 asked = nil
59 f, err := c.Open(p(".envrc"))
60 if err != nil {
61 t.Fatal(err)
62 }
63 f.Close()
64 if len(asked) != 0 {
65 t.Errorf("a read asked: %q", asked)
66 }
67 allow = true
68 if err := c.Chmod(p(".envrc"), 0o600); err != nil {
69 t.Errorf("allowed: %v", err)
70 }
71}