High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: MCP record_stop timeout and early path validation
Mario90cae90c4d08
diff --git a/internal/mcp/client.go b/internal/mcp/client.goindex 31b9a17f..cf04a0fa 100644--- a/internal/mcp/client.go+++ b/internal/mcp/client.go@@ -33,6 +33,17 @@ func NewClient(baseURL, token string) *Client {}}+// withTimeout returns a shallow copy of the Client whose HTTPClient uses the+// given timeout. The original Transport is preserved.+func (c *Client) withTimeout(d time.Duration) *Client {+ clone := *c+ clone.HTTPClient = &http.Client{+ Timeout: d,+ Transport: c.HTTPClient.Transport,+ }+ return &clone+}+func (c *Client) url(path string) string {return c.BaseURL + path}diff --git a/internal/mcp/handlers_record.go b/internal/mcp/handlers_record.goindex 41170e36..cf0004da 100644--- a/internal/mcp/handlers_record.go+++ b/internal/mcp/handlers_record.go@@ -6,6 +6,7 @@ import ("io""os""path/filepath"+ "time""github.com/mark3labs/mcp-go/mcp")@@ -14,6 +15,10 @@ import (// unbounded disk writes (matches the server-side maxOutputBytes).const maxRecordFileBytes = 256 << 20 // 256 MiB+// recordStopTimeout matches the server-side encodeTimeout (2m) plus the+// write-deadline extension (30s), with headroom for network transfer.+const recordStopTimeout = 3 * time.Minute+func handleRecordStart(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) {return func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) {file, err := r.RequireString("file")@@ -34,6 +39,10 @@ func handleRecordStart(c *Client) func(context.Context, mcp.CallToolRequest) (*mreturn mcp.NewToolResultError(fmt.Sprintf("unsupported format %q — use .gif, .webm, or .mp4", ext)), nil}+ if _, err := safeRecordPath(file); err != nil {+ return mcp.NewToolResultError(fmt.Sprintf("invalid output path: %v", err)), nil+ }+payload := map[string]any{"format": format}if fps, ok := optInt(r, "fps"); ok {payload["fps"] = fps@@ -57,18 +66,32 @@ func handleRecordStart(c *Client) func(context.Context, mcp.CallToolRequest) (*m}func handleRecordStop(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) {+ long := c.withTimeout(recordStopTimeout)+return func(ctx context.Context, r mcp.CallToolRequest) (*mcp.CallToolResult, error) {file := optString(r, "file")if file == "" {return mcp.NewToolResultError("file parameter is required"), nil}- dest, err := safeRecordPath(file)- if err != nil {- return mcp.NewToolResultError(err.Error()), nil+ dest, pathErr := safeRecordPath(file)+ if pathErr != nil {+ // Path is invalid, but a recording may be active server-side.+ // Stop it so we don't leave it dangling.+ stopBody, stopCode, stopErr := long.PostStream(ctx, "/record/stop", map[string]any{})+ if stopErr == nil {+ _ = stopBody.Close()+ }+ msg := fmt.Sprintf("invalid output path: %v", pathErr)+ if stopErr != nil {+ msg += fmt.Sprintf(" (also failed to stop recording: %v)", stopErr)+ } else if stopCode >= 400 {+ msg += fmt.Sprintf(" (recording stop returned HTTP %d)", stopCode)+ }+ return mcp.NewToolResultError(msg), nil}- body, code, err := c.PostStream(ctx, "/record/stop", map[string]any{})+ body, code, err := long.PostStream(ctx, "/record/stop", map[string]any{})if err != nil {return mcp.NewToolResultError(err.Error()), nil}diff --git a/internal/mcp/handlers_record_test.go b/internal/mcp/handlers_record_test.goindex f3d1ce87..dca11420 100644--- a/internal/mcp/handlers_record_test.go+++ b/internal/mcp/handlers_record_test.go@@ -1,10 +1,18 @@package mcpimport (+ "context"+ "encoding/json"+ "io"+ "net/http"+ "net/http/httptest""os""path/filepath""strings""testing"+ "time"++ "github.com/mark3labs/mcp-go/mcp")func TestSafeRecordPath_RejectsRelativePath(t *testing.T) {@@ -93,3 +101,130 @@ func TestStreamToFile_RefusesOverwrite(t *testing.T) {t.Fatal("expected error for existing file")}}++func TestRecordStartRejectsRelativePath(t *testing.T) {+ srv := mockPinchTab()+ defer srv.Close()++ r := callTool(t, "pinchtab_record_start", map[string]any{+ "file": "relative/out.gif",+ }, srv)++ text := resultText(t, r)+ if !strings.Contains(text, "absolute path") {+ t.Fatalf("expected absolute path error, got %q", text)+ }+}++func TestRecordStartRejectsExistingFile(t *testing.T) {+ dir := t.TempDir()+ existing := filepath.Join(dir, "exists.webm")+ if err := os.WriteFile(existing, []byte("data"), 0600); err != nil {+ t.Fatal(err)+ }++ srv := mockPinchTab()+ defer srv.Close()++ r := callTool(t, "pinchtab_record_start", map[string]any{+ "file": existing,+ }, srv)++ text := resultText(t, r)+ if !strings.Contains(text, "already exists") {+ t.Fatalf("expected exists error, got %q", text)+ }+}++func TestRecordStopBadPathStillStopsRecording(t *testing.T) {+ stopCalled := false+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path == "/record/stop" && r.Method == http.MethodPost {+ stopCalled = true+ w.WriteHeader(200)+ _, _ = w.Write([]byte("fakedata"))+ return+ }+ w.WriteHeader(200)+ _ = json.NewEncoder(w).Encode(map[string]any{"status": "ok"})+ }))+ defer srv.Close()++ c := NewClient(srv.URL, "")+ handlers := handlerMap(c)+ h := handlers["pinchtab_record_stop"]++ req := mcp.CallToolRequest{}+ req.Params.Name = "pinchtab_record_stop"+ req.Params.Arguments = map[string]any{"file": "relative/bad.gif"}++ result, err := h(context.Background(), req)+ if err != nil {+ t.Fatalf("handler error: %v", err)+ }++ text := resultText(t, result)+ if !strings.Contains(text, "absolute path") {+ t.Fatalf("expected path error in result, got %q", text)+ }+ if !stopCalled {+ t.Fatal("expected POST /record/stop to be called even when path is invalid")+ }+}++func TestWithTimeoutCreatesIndependentClient(t *testing.T) {+ c := NewClient("http://localhost:9867", "tok")+ long := c.withTimeout(5 * time.Minute)++ if long.HTTPClient.Timeout != 5*time.Minute {+ t.Fatalf("long timeout = %v, want 5m", long.HTTPClient.Timeout)+ }+ if c.HTTPClient.Timeout != 120*time.Second {+ t.Fatalf("original timeout changed to %v", c.HTTPClient.Timeout)+ }+ if long.Token != "tok" {+ t.Fatalf("token not copied: %q", long.Token)+ }+ if long.BaseURL != c.BaseURL {+ t.Fatalf("BaseURL not copied: %q", long.BaseURL)+ }+}++func TestRecordStopUsesLongTimeout(t *testing.T) {+ var gotTimeout time.Duration+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path == "/record/stop" {+ deadline, ok := r.Context().Deadline()+ if ok {+ gotTimeout = time.Until(deadline)+ }+ w.WriteHeader(200)+ _, _ = io.WriteString(w, "videodata")+ return+ }+ w.WriteHeader(200)+ }))+ defer srv.Close()++ c := NewClient(srv.URL, "")+ handlers := handlerMap(c)+ h := handlers["pinchtab_record_stop"]++ dir := t.TempDir()+ outFile := filepath.Join(dir, "test.gif")++ req := mcp.CallToolRequest{}+ req.Params.Name = "pinchtab_record_stop"+ req.Params.Arguments = map[string]any{"file": outFile}++ _, err := h(context.Background(), req)+ if err != nil {+ t.Fatalf("handler error: %v", err)+ }++ // The http.Client.Timeout sets a context deadline on the request.+ // With recordStopTimeout=3m, the deadline should be well over 2m.+ if gotTimeout > 0 && gotTimeout < 2*time.Minute {+ t.Fatalf("request deadline too short: %v (expected > 2m from 3m client timeout)", gotTimeout)+ }+}