| 1 | package cage |
| 2 | |
| 3 | import ( |
| 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 | |
| 18 | func 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 | |
| 47 | func 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 | } |