跳到主要内容

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

浏览文件

request-server_fuzz_test.go

77 行 · 3.0 KB历史下载
1package sftp
2
3import (
4 "bytes"
5 "encoding/binary"
6 "io"
7 "os"
8 "testing"
9 "time"
10)
11
12type fuzzConn struct {
13 io.Reader
14}
15
16func (fuzzConn) Write(p []byte) (int, error) { return len(p), nil }
17func (fuzzConn) Close() error { return nil }
18
19func fuzzFrame(typ byte, payload ...[]byte) []byte {
20 body := append([]byte{typ}, bytes.Join(payload, nil)...)
21 return append(binary.BigEndian.AppendUint32(nil, uint32(len(body))), body...)
22}
23
24func fuzzU32(v uint32) []byte { return binary.BigEndian.AppendUint32(nil, v) }
25
26// fuzzHandler keeps nothing: what is fuzzed is the protocol, not a handler
27// (InMemHandler grows a buffer to whatever offset a WRITE names).
28type fuzzHandler struct{}
29
30type discardAt struct{}
31
32func (discardAt) WriteAt(p []byte, _ int64) (int, error) { return len(p), nil }
33
34type fuzzLister struct{}
35
36func (fuzzLister) ListAt([]os.FileInfo, int64) (int, error) { return 0, io.EOF }
37
38func (fuzzHandler) Fileread(*Request) (io.ReaderAt, error) { return bytes.NewReader([]byte("content")), nil }
39func (fuzzHandler) Filewrite(*Request) (io.WriterAt, error) { return discardAt{}, nil }
40func (fuzzHandler) Filecmd(*Request) error { return nil }
41func (fuzzHandler) Filelist(*Request) (ListerAt, error) { return fuzzLister{}, nil }
42func fuzzStr(s string) []byte { return append(fuzzU32(uint32(len(s))), s...) }
43
44// FuzzRequestServer sends what a hostile client could, after INIT: any
45// bytes, framed or not. The server, as the hostagent runs it (sshocker's
46// options), never panics and never hangs: in the hostagent, either takes the
47// VM down.
48func FuzzRequestServer(f *testing.F) {
49 open := fuzzFrame(sshFxpOpen, fuzzU32(1), fuzzStr("/f"), fuzzU32(sshFxfWrite|sshFxfCreat|sshFxfTrunc), fuzzU32(0))
50 write := fuzzFrame(sshFxpWrite, fuzzU32(2), fuzzStr("1"), make([]byte, 8), fuzzStr("data"))
51 read := fuzzFrame(sshFxpRead, fuzzU32(3), fuzzStr("1"), make([]byte, 8), fuzzU32(4))
52 closeH := fuzzFrame(sshFxpClose, fuzzU32(4), fuzzStr("1"))
53 mkdir := fuzzFrame(sshFxpMkdir, fuzzU32(5), fuzzStr("/d"), fuzzU32(0))
54 f.Add(bytes.Join([][]byte{open, write, read, closeH}, nil))
55 f.Add(bytes.Join([][]byte{mkdir, fuzzFrame(sshFxpOpendir, fuzzU32(6), fuzzStr("/d")), fuzzFrame(sshFxpReaddir, fuzzU32(7), fuzzStr("1"))}, nil))
56 f.Add(fuzzFrame(sshFxpSetstat, fuzzU32(8), fuzzStr("/f"), fuzzU32(0xffffffff)))
57 f.Add(fuzzFrame(sshFxpExtended, fuzzU32(9), fuzzStr("[email protected]"), fuzzStr("/a"), fuzzStr("/b")))
58 f.Add([]byte{0xff, 0xff, 0xff, 0xff, 1})
59 init := fuzzFrame(sshFxpInit, fuzzU32(3))
60 f.Fuzz(func(t *testing.T, data []byte) {
61 conn := fuzzConn{bytes.NewReader(append(append([]byte(nil), init...), data...))}
62 h := fuzzHandler{}
63 srv := NewRequestServer(conn, Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h},
64 WithRSSequential(), WithRSMaxHandles(16), WithRSMaxTxPacket(64*1024))
65 done := make(chan struct{})
66 go func() {
67 defer close(done)
68 _ = srv.Serve()
69 }()
70 select {
71 case <-done:
72 case <-time.After(10 * time.Second):
73 t.Fatalf("the server hangs on %q", data)
74 }
75 srv.Close()
76 })
77}