跳到主要内容

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

浏览文件

fuzz_test.go

61 行 · 1.8 KB历史下载
1package env
2
3import (
4 "os"
5 "os/exec"
6 "strings"
7 "testing"
8)
9
10// Lookup reads a value "as a shell sourcing the file would": whenever it
11// answers, sh sourcing the file as the VM gets it gives the same value. It only answers for
12// files of plain assignments, so sh runs nothing else here.
13func FuzzLookup(f *testing.F) {
14 for _, s := range []string{"K=v\n", "K='a b'\n", "export K=\"x\" # note\n", "K='multi\nline'\n", "K=a'b'\"c\"\n", " K=1\nK=2\n", "K=v\r\n", "# c\n\nK=\n"} {
15 f.Add(s)
16 }
17 f.Fuzz(func(t *testing.T, content string) {
18 if strings.ContainsRune(content, 0) {
19 return
20 }
21 v, st := Lookup(content, "K")
22 if st != Found {
23 return
24 }
25 dir := t.TempDir()
26 file := dir + "/env"
27 // As the VM gets it: CRs dropped (a file from a Windows editor).
28 if err := os.WriteFile(file, []byte(StripCR(content)), 0o600); err != nil {
29 t.Fatal(err)
30 }
31 cmd := exec.Command("sh", "-c", `. "$1" && printf %s "$K"`, "sh", file)
32 cmd.Dir, cmd.Env = dir, []string{"PATH=/usr/bin:/bin"}
33 out, err := cmd.Output()
34 if err != nil {
35 t.Fatalf("Lookup accepted %q, sh refused it: %v", content, err)
36 }
37 if string(out) != v {
38 t.Fatalf("Lookup(%q) = %q, sh reads %q", content, v, out)
39 }
40 })
41}
42
43// A value written as key='SQEscape(value)' is read back by the shell as
44// itself: nothing in it ends the quotes and runs.
45func FuzzSQEscape(f *testing.F) {
46 for _, s := range []string{"plain", "it's", "'", "''", `'"'"'`, "$(id)", "`id`", "a\nb", "\\'", "x'; touch /tmp/pwned; '"} {
47 f.Add(s)
48 }
49 f.Fuzz(func(t *testing.T, s string) {
50 if strings.ContainsRune(s, 0) {
51 return // not in a shell word, nor in an env file
52 }
53 out, err := exec.Command("sh", "-c", "printf %s '"+SQEscape(s)+"'").Output()
54 if err != nil {
55 t.Fatalf("sh refused %q: %v", SQEscape(s), err)
56 }
57 if string(out) != s {
58 t.Fatalf("%q came back as %q", s, out)
59 }
60 })
61}