High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: handoff pause blocks clicks but not navigate, reload or back (PIN-049)
Luigi Agosti2485eb45c8eb
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.goindex f262da19..5f3f28f9 100644--- a/internal/handlers/actions.go+++ b/internal/handlers/actions.go@@ -47,24 +47,6 @@ func (h *Handlers) enforceTabLease(tabID, owner string) error {return nil}-func (h *Handlers) enforceTabNotPausedForHandoff(tabID string) error {- if tabID == "" {- return nil- }- ctrl, ok := h.handoffController()- if !ok {- return nil- }- state, exists := ctrl.TabHandoffState(tabID)- if !exists || state.Status != "paused_handoff" {- return nil- }- if state.Reason != "" {- return fmt.Errorf("tab %s is paused for human handoff (%s)", tabID, state.Reason)- }- return fmt.Errorf("tab %s is paused for human handoff", tabID)-}-// rejectMixedBrowsers returns the first action's browser as the request browser,// rejecting (with a 400) any later action that names a different browser, since a// batch/macro executes on a single browser. noun/field tune the error wording@@ -324,8 +306,7 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {return}- if err := h.enforceTabNotPausedForHandoff(resolvedTabID); err != nil {- httpx.ErrorCode(w, 409, "tab_paused_handoff", err.Error(), false, h.handoffErrorDetails(resolvedTabID))+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {return}defer h.armAutoCloseIfEnabled(resolvedTabID)diff --git a/internal/handlers/dialog.go b/internal/handlers/dialog.goindex f046145e..247bb9bc 100644--- a/internal/handlers/dialog.go+++ b/internal/handlers/dialog.go@@ -39,6 +39,10 @@ func (h *Handlers) HandleDialog(w http.ResponseWriter, r *http.Request) {return}+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedID) {+ return+ }+accept := req.Action == "accept"h.handleDialogAction(w, r, ctx, resolvedID, accept, req.Text)}diff --git a/internal/handlers/evaluate.go b/internal/handlers/evaluate.goindex 0a938e68..ec8182b5 100644--- a/internal/handlers/evaluate.go+++ b/internal/handlers/evaluate.go@@ -49,6 +49,9 @@ func (h *Handlers) HandleEvaluate(w http.ResponseWriter, r *http.Request) {if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {return}+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {+ return+ }tCtx, tCancel := context.WithTimeout(ctx, h.Config.ActionTimeout)defer tCancel()diff --git a/internal/handlers/handoff.go b/internal/handlers/handoff.goindex 49fdc5d1..a3c0bbde 100644--- a/internal/handlers/handoff.go+++ b/internal/handlers/handoff.go@@ -48,6 +48,37 @@ func (h *Handlers) handoffErrorDetails(tabID string) map[string]any {return details}+func (h *Handlers) enforceTabNotPausedForHandoff(tabID string) error {+ if tabID == "" {+ return nil+ }+ ctrl, ok := h.handoffController()+ if !ok {+ return nil+ }+ state, exists := ctrl.TabHandoffState(tabID)+ if !exists || state.Status != "paused_handoff" {+ return nil+ }+ if state.Reason != "" {+ return fmt.Errorf("tab %s is paused for human handoff (%s)", tabID, state.Reason)+ }+ return fmt.Errorf("tab %s is paused for human handoff", tabID)+}++// enforceTabNotPausedForHandoffOrRespond is the single writer of the paused-tab+// refusal: every endpoint that mutates what the human is looking at calls it, so+// a client sees one status, code and remedy hint regardless of which one it hit.+// Reports ok=false once the 409 has been written.+func (h *Handlers) enforceTabNotPausedForHandoffOrRespond(w http.ResponseWriter, tabID string) bool {+ err := h.enforceTabNotPausedForHandoff(tabID)+ if err == nil {+ return true+ }+ httpx.ErrorCode(w, http.StatusConflict, "tab_paused_handoff", err.Error(), false, h.handoffErrorDetails(tabID))+ return false+}+// pauseTabForHandoff marks a tab as paused for human handoff and broadcasts// a dashboard event. source identifies what triggered the handoff// (e.g. "autosolver", "manual"). Returns the applied reason.diff --git a/internal/handlers/handoff_guard_test.go b/internal/handlers/handoff_guard_test.gonew file mode 100644index 00000000..7198c067--- /dev/null+++ b/internal/handlers/handoff_guard_test.go@@ -0,0 +1,293 @@+package handlers++import (+ "bytes"+ "context"+ "encoding/json"+ "net/http/httptest"+ "os"+ "strings"+ "testing"+ "time"++ "github.com/pinchtab/pinchtab/internal/bridge"+ "github.com/pinchtab/pinchtab/internal/config"+)++// pausedTabBridge reports one tab paused for human handoff and records every+// page-mutating call, so a test can assert both the refusal and that the page+// never moved.+type pausedTabBridge struct {+ handoffRecordingBridge+ pageURL string+ navigated []string+ reloads int+ backs int+ uploads int+}++func newPausedTabBridge() *pausedTabBridge {+ b := &pausedTabBridge{pageURL: "https://pinchtab.com/"}+ b.state = bridge.TabHandoffState{+ Status: "paused_handoff",+ Reason: "manual_handoff",+ PausedAt: time.Now().UTC(),+ LastUpdatedAt: time.Now().UTC(),+ }+ b.has = true+ b.navigateResult = &bridge.NavigateResult{TabID: "tab1", URL: "https://pinchtab.com/docs/"}+ return b+}++func (b *pausedTabBridge) Navigate(ctx context.Context, url string, params bridge.NavigateParams) (*bridge.NavigateResult, error) {+ b.navigated = append(b.navigated, url)+ b.pageURL = url+ return b.handoffRecordingBridge.Navigate(ctx, url, params)+}++func (b *pausedTabBridge) Reload(context.Context) error {+ b.reloads+++ b.pageURL = "https://pinchtab.com/reloaded"+ return nil+}++func (b *pausedTabBridge) GoBack(context.Context) (bool, error) {+ b.backs+++ b.pageURL = "https://pinchtab.com/previous"+ return true, nil+}++func (b *pausedTabBridge) CurrentURL(context.Context) (string, error) { return b.pageURL, nil }++func (b *pausedTabBridge) EvaluateInFrame(_ context.Context, _, _ string, result any, _ bridge.EvalOpts) error {+ if payload, ok := result.(*inspectPayload); ok {+ payload.URL = b.pageURL+ }+ return nil+}++func (b *pausedTabBridge) SetFileInputFiles(context.Context, int64, []string) error {+ b.uploads+++ return nil+}++func pausedTabHandlers(b *pausedTabBridge, tmpDir string) *Handlers {+ return New(b, &config.RuntimeConfig{+ ActionTimeout: time.Second,+ AllowEvaluate: true,+ AllowUpload: true,+ StateDir: tmpDir,+ }, nil, nil, nil)+}++// assertHandoffRefusal pins the one refusal shape every gated endpoint shares:+// 409, the tab_paused_handoff code, and the remedy hint in the details.+func assertHandoffRefusal(t *testing.T, w *httptest.ResponseRecorder) {+ t.Helper()+ if w.Code != 409 {+ t.Fatalf("expected 409, got %d: %s", w.Code, w.Body.String())+ }+ var resp struct {+ Error string `json:"error"`+ Code string `json:"code"`+ Details map[string]any `json:"details"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("decode refusal: %v", err)+ }+ if resp.Code != "tab_paused_handoff" {+ t.Fatalf("code = %q, want tab_paused_handoff: %s", resp.Code, w.Body.String())+ }+ if !strings.Contains(resp.Error, "paused for human handoff") {+ t.Fatalf("error should name the pause: %s", resp.Error)+ }+ if resp.Details["hint"] != handoffHintMessage {+ t.Fatalf("details hint = %v, want the shared handoff hint", resp.Details["hint"])+ }+ if resp.Details["reason"] != "manual_handoff" {+ t.Fatalf("details reason = %v, want manual_handoff", resp.Details["reason"])+ }+}++func TestPausedTabRefusesNavigate(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ req := httptest.NewRequest("POST", "/navigate", bytes.NewReader([]byte(`{"tabId":"tab1","url":"about:blank"}`)))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleNavigate(w, req)++ assertHandoffRefusal(t, w)+ if len(b.navigated) != 0 {+ t.Fatalf("navigate reached the bridge: %v", b.navigated)+ }+ if b.pageURL != "https://pinchtab.com/" {+ t.Fatalf("page moved to %q", b.pageURL)+ }+}++func TestPausedTabRefusesReload(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ w := httptest.NewRecorder()+ h.HandleReload(w, httptest.NewRequest("POST", "/reload?tabId=tab1", nil))++ assertHandoffRefusal(t, w)+ if b.reloads != 0 {+ t.Fatalf("reload reached the bridge %d times", b.reloads)+ }+ if b.pageURL != "https://pinchtab.com/" {+ t.Fatalf("page moved to %q", b.pageURL)+ }+}++func TestPausedTabRefusesBack(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ w := httptest.NewRecorder()+ h.HandleBack(w, httptest.NewRequest("POST", "/back?tabId=tab1", nil))++ assertHandoffRefusal(t, w)+ if b.backs != 0 {+ t.Fatalf("back reached the bridge %d times", b.backs)+ }+ if b.pageURL != "https://pinchtab.com/" {+ t.Fatalf("page moved to %q", b.pageURL)+ }+}++func TestPausedTabRefusesEvaluate(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ req := httptest.NewRequest("POST", "/evaluate", bytes.NewReader([]byte(`{"tabId":"tab1","expression":"location.href='/gone'"}`)))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleEvaluate(w, req)++ assertHandoffRefusal(t, w)+ if b.evaluateCalls != 0 {+ t.Fatalf("evaluate ran %d expressions on a paused tab", b.evaluateCalls)+ }+}++func TestPausedTabRefusesDialog(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ req := httptest.NewRequest("POST", "/dialog", bytes.NewReader([]byte(`{"tabId":"tab1","action":"accept"}`)))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleDialog(w, req)++ assertHandoffRefusal(t, w)+}++func TestPausedTabRefusesSolve(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ req := httptest.NewRequest("POST", "/solve", bytes.NewReader([]byte(`{"tabId":"tab1"}`)))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleSolve(w, req)++ assertHandoffRefusal(t, w)+}++func TestPausedTabRefusesUpload(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ req := httptest.NewRequest("POST", "/upload?tabId=tab1", bytes.NewReader([]byte(`{"files":["aGVsbG8="]}`)))+ req.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleUpload(w, req)++ assertHandoffRefusal(t, w)+ if b.uploads != 0 {+ t.Fatalf("upload attached files %d times on a paused tab", b.uploads)+ }+}++// The watch-and-resume flow depends on reads and resume staying open while a+// human works, so gating them would be as much a defect as the missing guard.+func TestPausedTabAllowsReadsAndResume(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ w := httptest.NewRecorder()+ h.HandleURL(w, httptest.NewRequest("GET", "/url?tabId=tab1", nil))+ if w.Code != 200 {+ t.Fatalf("url on a paused tab = %d: %s", w.Code, w.Body.String())+ }++ statusReq := httptest.NewRequest("GET", "/tabs/tab1/handoff", nil)+ statusReq.SetPathValue("id", "tab1")+ w = httptest.NewRecorder()+ h.HandleTabHandoffStatus(w, statusReq)+ if w.Code != 200 {+ t.Fatalf("handoff-status on a paused tab = %d: %s", w.Code, w.Body.String())+ }+ if !strings.Contains(w.Body.String(), "paused_handoff") {+ t.Fatalf("handoff-status should still report the pause: %s", w.Body.String())+ }++ resumeReq := httptest.NewRequest("POST", "/tabs/tab1/resume", nil)+ resumeReq.SetPathValue("id", "tab1")+ w = httptest.NewRecorder()+ h.HandleTabResume(w, resumeReq)+ if w.Code != 200 {+ t.Fatalf("resume on a paused tab = %d: %s", w.Code, w.Body.String())+ }+}++// snap and text cannot be driven through the handlers mock (they need a real+// CDP context), so their exemption is pinned at the source: the guard must not+// appear in the read handlers.+func TestReadHandlersDoNotGateOnHandoff(t *testing.T) {+ for _, file := range []string{"text.go", "snapshot.go", "inspect.go"} {+ src, err := os.ReadFile(file)+ if err != nil {+ t.Fatalf("read %s: %v", file, err)+ }+ if strings.Contains(string(src), "enforceTabNotPausedForHandoff") {+ t.Fatalf("%s gates a read on handoff; reads must stay allowed while a human works", file)+ }+ }+}++func TestResumedTabAllowsNavigateReloadAndBack(t *testing.T) {+ b := newPausedTabBridge()+ h := pausedTabHandlers(b, t.TempDir())++ if err := b.ResumeTabHandoff("tab1"); err != nil {+ t.Fatalf("resume: %v", err)+ }++ navReq := httptest.NewRequest("POST", "/navigate", bytes.NewReader([]byte(`{"tabId":"tab1","url":"about:blank"}`)))+ navReq.Header.Set("Content-Type", "application/json")+ w := httptest.NewRecorder()+ h.HandleNavigate(w, navReq)+ if w.Code == 409 {+ t.Fatalf("navigate still refused after resume: %s", w.Body.String())+ }+ if len(b.navigated) == 0 {+ t.Fatal("navigate did not reach the bridge after resume")+ }++ w = httptest.NewRecorder()+ h.HandleReload(w, httptest.NewRequest("POST", "/reload?tabId=tab1", nil))+ if w.Code != 200 || b.reloads != 1 {+ t.Fatalf("reload after resume: status %d reloads %d: %s", w.Code, b.reloads, w.Body.String())+ }++ w = httptest.NewRecorder()+ h.HandleBack(w, httptest.NewRequest("POST", "/back?tabId=tab1", nil))+ if w.Code != 200 || b.backs != 1 {+ t.Fatalf("back after resume: status %d backs %d: %s", w.Code, b.backs, w.Body.String())+ }+}diff --git a/internal/handlers/history.go b/internal/handlers/history.goindex 0549b131..d3ee1858 100644--- a/internal/handlers/history.go+++ b/internal/handlers/history.go@@ -38,6 +38,9 @@ func (h *Handlers) handleHistoryNav(w http.ResponseWriter, r *http.Request,WriteTabContextError(w, err, 404)return}+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedID) {+ return+ }if err := navigate(ctx, resolvedID, dismissBanners); err != nil {httpx.Error(w, 500, err)returndiff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.goindex ee421627..97a8e417 100644--- a/internal/handlers/navigation.go+++ b/internal/handlers/navigation.go@@ -433,6 +433,9 @@ func (h *Handlers) executeNavigate(w http.ResponseWriter, r *http.Request, req nWriteTabContextError(w, err, 404)return}+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {+ return+ }// Navigate signals fresh work on this tab — drop any pending auto-close// timer; the next read/action will re-arm.h.cancelAutoCloseIfEnabled(resolvedTabID)diff --git a/internal/handlers/solver.go b/internal/handlers/solver.goindex 6e858376..806aacde 100644--- a/internal/handlers/solver.go+++ b/internal/handlers/solver.go@@ -83,6 +83,9 @@ func (h *Handlers) HandleSolve(w http.ResponseWriter, r *http.Request) {if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {return}+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {+ return+ }action := "solve"if req.Solver != "" {diff --git a/internal/handlers/upload.go b/internal/handlers/upload.goindex 096b80dd..03463743 100644--- a/internal/handlers/upload.go+++ b/internal/handlers/upload.go@@ -183,6 +183,9 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {return}+ if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {+ return+ }tCtx, tCancel := context.WithTimeout(ctx, h.Config.ActionTimeout)defer tCancel()