跳到主要内容

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

浏览文件

exit_probe_errors_test.go

78 行 · 2.5 KB历史下载
1package cage
2
3import (
4 "context"
5 "errors"
6 "io"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "path/filepath"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/sylvinus/agent-vm/internal/diagnostics"
16)
17
18func TestExitProbeBodyTimeoutKeepsDeadlineAndShortReadCause(t *testing.T) {
19 for _, stall := range []bool{false, true} {
20 name := "incomplete-body"
21 if stall {
22 name = "body-timeout"
23 }
24 t.Run(name, func(t *testing.T) {
25 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26 w.Header().Set("Content-Length", "100")
27 _, _ = io.WriteString(w, "8.8.8.8")
28 w.(http.Flusher).Flush()
29 if stall {
30 <-r.Context().Done()
31 }
32 }))
33 defer server.Close()
34 client := &http.Client{Timeout: 100 * time.Millisecond}
35 ip, err := queryExitIP(context.Background(), client, server.URL)
36 want := error(io.ErrUnexpectedEOF)
37 if stall {
38 want = context.DeadlineExceeded
39 }
40 if ip != "" || !errors.Is(err, want) || strings.Contains(err.Error(), server.URL) {
41 t.Fatalf("body error lost its safe cause: ip=%q error=%v", ip, err)
42 }
43 })
44 }
45}
46
47func TestExitProbeProxyFailureKeepsServiceAndHTTPStatusAfterRedaction(t *testing.T) {
48 profile := t.TempDir()
49 store := diagnostics.New(profile)
50 previous := diagnostics.Default()
51 diagnostics.SetDefault(store)
52 t.Cleanup(func() { diagnostics.SetDefault(previous) })
53 proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54 w.WriteHeader(http.StatusProxyAuthRequired)
55 _, _ = io.WriteString(w, "private-response")
56 }))
57 defer proxy.Close()
58 cfg := DefaultConfig()
59 cfg.ProxyURL = strings.Replace(proxy.URL, "://", "://private-user:private-password@", 1)
60 ip, err := ProbeExitIP(context.Background(), cfg)
61 if ip != "" || err == nil || !strings.Contains(err.Error(), "HTTP 407") || errors.Is(err, context.DeadlineExceeded) {
62 t.Fatalf("proxy rejection was hidden or treated as a timeout: %s %v", ip, err)
63 }
64 log, readErr := os.ReadFile(filepath.Join(profile, "logs", "events.jsonl"))
65 if readErr != nil {
66 t.Fatal(readErr)
67 }
68 for _, marker := range []string{"service=icanhazip", "service=ipify", "duration_ms=", "HTTP 407"} {
69 if !strings.Contains(string(log), marker) {
70 t.Fatalf("missing diagnostic %q: %s", marker, log)
71 }
72 }
73 for _, private := range []string{"private-response", "private-user", "private-password", proxy.URL} {
74 if strings.Contains(err.Error(), private) || strings.Contains(string(log), private) {
75 t.Fatalf("private proxy data leaked: %v %s", err, log)
76 }
77 }
78}