跳到主要内容

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

浏览文件

rooted_guard_test.go

101 行 · 3.2 KB历史下载
1//go:build linux || darwin || windows
2
3package reversesshfs
4
5import (
6 "os"
7 "path/filepath"
8 "runtime"
9 "slices"
10 "strings"
11 "testing"
12)
13
14type guardCall struct {
15 rel string
16 entry bool
17}
18
19// A guest builds a guarded file's parent folder elsewhere and moves it into place: Guard is told
20// that the names are being created, moved, linked or removed (entry), so it can refuse the
21// parent folder of a guarded path, which a write-only check on the path would not see.
22func TestRootedGuardParents(t *testing.T) {
23 if runtime.GOOS == "windows" {
24 t.Skip("Windows has no symlinks here and asks with normalized names")
25 }
26 var calls []guardCall
27 allow := false
28 Guard = func(root, rel string, entry bool) bool {
29 calls = append(calls, guardCall{rel, entry})
30 parts := strings.Split(strings.ToLower(rel), "/")
31 last := parts[len(parts)-1]
32 if allow {
33 return true
34 }
35 if last == ".vscode" && entry {
36 return false
37 }
38 return !(last == "tasks.json" && len(parts) > 1 && parts[len(parts)-2] == ".vscode")
39 }
40 t.Cleanup(func() { Guard = nil })
41 c, root := setupRooted(t, false)
42 p := func(s string) string { return filepath.Join(root, s) }
43 must := func(err error) {
44 t.Helper()
45 if err != nil {
46 t.Fatal(err)
47 }
48 }
49 must(os.MkdirAll(p(".vscode"), 0o755))
50 must(os.WriteFile(p(".vscode/tasks.json"), []byte("orig"), 0o644))
51 must(c.Mkdir(p("evil")))
52 f, err := c.Create(p("evil/tasks.json"))
53 must(err)
54 _, _ = f.Write([]byte("pwn"))
55 must(f.Close())
56 if !slices.Contains(calls, guardCall{"evil/tasks.json", false}) {
57 t.Errorf("a write is not an entry: %v", calls)
58 }
59
60 for _, op := range []struct {
61 name string
62 rel string
63 f func() error
64 }{
65 {"rename onto the parent", ".vscode", func() error { return c.PosixRename(p("evil"), p(".vscode")) }},
66 {"rename onto the parent, no replace", ".vscode", func() error { return c.Rename(p("evil"), p(".vscode")) }},
67 {"symlink as the parent", ".vscode", func() error { return c.Symlink("evil", p(".vscode")) }},
68 {"rename the parent away", ".vscode", func() error { return c.PosixRename(p(".vscode"), p("old")) }},
69 {"remove the parent", ".vscode", func() error { return c.RemoveDirectory(p(".vscode")) }},
70 {"hard link to the file", ".vscode/tasks.json", func() error { return c.Link(p(".vscode/tasks.json"), p("evil/alias")) }},
71 {"mkdir the parent", "sub/.vscode", func() error { return c.Mkdir(p("sub/.vscode")) }},
72 } {
73 if op.rel == "sub/.vscode" {
74 must(os.Mkdir(p("sub"), 0o755))
75 }
76 calls = nil
77 if err := op.f(); err == nil {
78 t.Errorf("%s: allowed though Guard said no", op.name)
79 }
80 if !slices.Contains(calls, guardCall{op.rel, true}) {
81 t.Errorf("%s: Guard asked %v, not %q as an entry", op.name, calls, op.rel)
82 }
83 }
84 if b, _ := os.ReadFile(p(".vscode/tasks.json")); string(b) != "orig" {
85 t.Errorf("tasks.json: %q", b)
86 }
87 if fi, err := os.Lstat(p(".vscode")); err != nil || !fi.IsDir() {
88 t.Errorf(".vscode: %v, %v", fi, err)
89 }
90 if _, err := os.Lstat(p("evil/alias")); err == nil {
91 t.Error("hard link made")
92 }
93
94 // With Guard saying yes, the same moves work: they were refused by Guard alone.
95 allow = true
96 must(c.PosixRename(p(".vscode"), p("old")))
97 must(c.PosixRename(p("evil"), p(".vscode")))
98 if b, _ := os.ReadFile(p(".vscode/tasks.json")); string(b) != "pwn" {
99 t.Errorf("tasks.json: %q", b)
100 }
101}