跳到主要内容

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

浏览文件

nofollow.go

64 行 · 1.9 KB历史下载
1package env
2
3import (
4 "errors"
5 "strings"
6)
7
8// The ways a NoFollow call fails.
9var (
10 // ErrNoFile: the file is not there (Read only).
11 ErrNoFile = errors.New("no such file")
12 // ErrUnsafe: a symlink, or something other than a folder or a regular
13 // file, is on the way.
14 ErrUnsafe = errors.New("a symlink, or not a regular file or folder, on the way")
15)
16
17// The NoFollow operations, each on top/rel, following no symlink below top:
18// the one way the host touches the project, which the VM can write. Through
19// a link planted there, a plain read would copy any file of the user's into
20// the project, and a write could land anywhere; checking for links first is
21// not enough, the VM can swap one in between the check and the use.
22//
23// Missing folders are made, except by Read. Write replaces the file with a
24// new one, mode 600, renamed over it: a link there is replaced, not followed.
25
26// ReadIn is the content of top/rel.
27func ReadIn(top, rel string) ([]byte, error) { return noFollow("read", top, rel, nil) }
28
29// WriteIn replaces top/rel with data.
30func WriteIn(top, rel string, data []byte) error {
31 _, err := noFollow("write", top, rel, data)
32 return err
33}
34
35// MkdirIn makes the folder top/rel and those above it.
36func MkdirIn(top, rel string) error {
37 _, err := noFollow("mkdir", top, rel, nil)
38 return err
39}
40
41// TouchIn makes top/rel an empty file unless it is a regular file already.
42func TouchIn(top, rel string) error {
43 _, err := noFollow("touch", top, rel, nil)
44 return err
45}
46
47// split is rel's folders and its last name; ErrUnsafe for ".." anywhere, or
48// no name.
49func split(rel string) (dirs []string, name string, err error) {
50 for _, c := range strings.Split(rel, "/") {
51 if c != "" && c != "." {
52 dirs = append(dirs, c)
53 }
54 }
55 if len(dirs) == 0 {
56 return nil, "", ErrUnsafe
57 }
58 for _, c := range dirs {
59 if c == ".." {
60 return nil, "", ErrUnsafe
61 }
62 }
63 return dirs[:len(dirs)-1], dirs[len(dirs)-1], nil
64}