Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

bridge: harden dialog auto-handling and action validation

Djain9125fda1946bd84
diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.go
index 04d7b1be..6e3ffd02 100644
--- a/internal/bridge/action_pointer.go
+++ b/internal/bridge/action_pointer.go
@@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"strings"
+ "time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
@@ -15,6 +16,12 @@ var mouseMoveByCoordinateAction = MouseMoveByCoordinate
var mouseDownByCoordinateAction = MouseDownByCoordinate
var mouseUpByCoordinateAction = MouseUpByCoordinate
+const (
+ dialogAutoHandlePollInterval = 10 * time.Millisecond
+ dialogAutoHandleSettleDelay = 40 * time.Millisecond
+ dialogAutoHandleTimeout = 750 * time.Millisecond
+)
+
type pointerState struct {
X float64
Y float64
@@ -81,10 +88,11 @@ func (b *Bridge) actionClick(ctx context.Context, req ActionRequest) (map[string
// Arm a one-shot dialog auto-handler if the caller expects the click
// to open a native JS dialog. Without this, the click would hang
// waiting for the dialog to be handled from a separate request.
- if req.DialogAction != "" && req.TabID != "" {
- if dm := b.GetDialogManager(); dm != nil {
- dm.ArmAutoHandler(req.TabID, req.DialogAction, req.DialogText)
- }
+ dm := b.GetDialogManager()
+ armedDialog := false
+ if req.DialogAction != "" && req.TabID != "" && dm != nil {
+ dm.ArmAutoHandler(req.TabID, req.DialogAction, req.DialogText)
+ armedDialog = true
}
var err error
@@ -116,12 +124,38 @@ func (b *Bridge) actionClick(ctx context.Context, req ActionRequest) (map[string
if err != nil {
return nil, err
}
+ if armedDialog {
+ waitForArmedDialogSettle(dm, req.TabID, dialogAutoHandleTimeout)
+ }
if req.WaitNav {
_ = chromedp.Run(ctx, chromedp.Sleep(b.Config.WaitNavDelay))
}
return map[string]any{"clicked": true}, nil
}
+func waitForArmedDialogSettle(dm *DialogManager, tabID string, timeout time.Duration) {
+ if dm == nil || strings.TrimSpace(tabID) == "" {
+ return
+ }
+ if timeout <= 0 {
+ timeout = dialogAutoHandleTimeout
+ }
+
+ deadline := time.Now().Add(timeout)
+ for time.Now().Before(deadline) {
+ if !dm.HasAutoHandler(tabID) {
+ // Allow the handler goroutine to finish UI side-effects before
+ // immediate follow-up reads (for example get_text assertions).
+ time.Sleep(dialogAutoHandleSettleDelay)
+ return
+ }
+ time.Sleep(dialogAutoHandlePollInterval)
+ }
+
+ // Prevent stale one-shot handlers from leaking into later clicks.
+ _ = dm.TakeAutoHandler(tabID)
+}
+
func (b *Bridge) actionDoubleClick(ctx context.Context, req ActionRequest) (map[string]any, error) {
var err error
if req.Selector != "" {
@@ -258,7 +292,13 @@ func (b *Bridge) actionMouseUp(ctx context.Context, req ActionRequest) (map[stri
func (b *Bridge) actionMouseWheel(ctx context.Context, req ActionRequest) (map[string]any, error) {
x, y, err := b.pointerCoordinatesFromRequest(ctx, req, true)
if err != nil {
- return nil, err
+ if req.HasXY || req.NodeID > 0 || req.Selector != "" || req.TabID == "" {
+ return nil, err
+ }
+ x, y, err = scrollViewportCenter(ctx)
+ if err != nil {
+ return nil, fmt.Errorf("resolve wheel viewport center: %w", err)
+ }
}
deltaX := req.DeltaX
deltaY := req.DeltaY
@@ -267,7 +307,7 @@ func (b *Bridge) actionMouseWheel(ctx context.Context, req ActionRequest) (map[s
deltaY = req.ScrollY
}
if deltaX == 0 && deltaY == 0 {
- deltaY = 800
+ deltaY = 120
}
if err := scrollByCoordinateAction(ctx, x, y, deltaX, deltaY); err != nil {
return nil, err
@@ -291,7 +331,7 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strin
scrollX := req.ScrollX
scrollY := req.ScrollY
if scrollX == 0 && scrollY == 0 {
- scrollY = 800
+ scrollY = 120
}
scrollTargetX := req.X
@@ -304,7 +344,16 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strin
}
}
- return map[string]any{"scrolled": true, "x": scrollX, "y": scrollY},
+ return map[string]any{
+ "scrolled": true,
+ // Legacy keys retained for compatibility with existing clients.
+ "x": scrollX,
+ "y": scrollY,
+ "targetX": scrollTargetX,
+ "targetY": scrollTargetY,
+ "deltaX": scrollX,
+ "deltaY": scrollY,
+ },
scrollByCoordinateAction(ctx, scrollTargetX, scrollTargetY, scrollX, scrollY)
}
diff --git a/internal/bridge/actions_test.go b/internal/bridge/actions_test.go
index 5a67b230..88749222 100644
--- a/internal/bridge/actions_test.go
+++ b/internal/bridge/actions_test.go
@@ -229,15 +229,35 @@ func TestMouseDownAction_UsesTrackedPointerWhenTargetMissing(t *testing.T) {
}
}
-func TestMouseWheelAction_RequiresKnownPointerWhenTargetMissing(t *testing.T) {
+func TestMouseWheelAction_UsesViewportCenterWhenPointerMissing(t *testing.T) {
b := New(context.TODO(), nil, &config.RuntimeConfig{})
- _, err := b.Actions[ActionMouseWheel](context.Background(), ActionRequest{TabID: "tab-missing"})
- if err == nil {
- t.Fatal("expected missing pointer error")
+ origScrollByCoordinate := scrollByCoordinateAction
+ origScrollViewportCenter := scrollViewportCenter
+ t.Cleanup(func() {
+ scrollByCoordinateAction = origScrollByCoordinate
+ scrollViewportCenter = origScrollViewportCenter
+ })
+
+ scrollViewportCenter = func(context.Context) (float64, float64, error) {
+ return 300, 200, nil
}
- if !strings.Contains(err.Error(), "move pointer first") {
- t.Fatalf("unexpected error: %v", err)
+ called := false
+ scrollByCoordinateAction = func(ctx context.Context, x, y float64, deltaX, deltaY int) error {
+ called = true
+ if x != 300 || y != 200 {
+ t.Fatalf("wheel coordinates = (%v, %v), want (300, 200)", x, y)
+ }
+ if deltaX != 0 || deltaY != 120 {
+ t.Fatalf("wheel delta = (%d, %d), want (0, 120)", deltaX, deltaY)
+ }
+ return nil
+ }
+ if _, err := b.Actions[ActionMouseWheel](context.Background(), ActionRequest{TabID: "tab-missing"}); err != nil {
+ t.Fatalf("unexpected wheel error: %v", err)
+ }
+ if !called {
+ t.Fatal("expected wheel action to use viewport center fallback")
}
}
@@ -320,8 +340,8 @@ func TestScrollAction_UsesViewportCenterWhenCoordinatesMissing(t *testing.T) {
if x != 400 || y != 300 {
t.Fatalf("wheel coordinates = (%v, %v), want (400, 300)", x, y)
}
- if deltaX != 0 || deltaY != 800 {
- t.Fatalf("wheel delta = (%d, %d), want (0, 800)", deltaX, deltaY)
+ if deltaX != 0 || deltaY != 120 {
+ t.Fatalf("wheel delta = (%d, %d), want (0, 120)", deltaX, deltaY)
}
return nil
}
@@ -333,9 +353,12 @@ func TestScrollAction_UsesViewportCenterWhenCoordinatesMissing(t *testing.T) {
if !called {
t.Fatal("expected viewport-center wheel path to be used")
}
- if result["x"] != 0 || result["y"] != 800 {
+ if result["x"] != 0 || result["y"] != 120 {
t.Fatalf("unexpected result payload: %#v", result)
}
+ if result["targetX"] != 400.0 || result["targetY"] != 300.0 {
+ t.Fatalf("unexpected scroll target payload: %#v", result)
+ }
}
func TestScrollAction_PropagatesViewportCenterError(t *testing.T) {
diff --git a/internal/bridge/tabs/dialog.go b/internal/bridge/tabs/dialog.go
index 454b1588..60a3399e 100644
--- a/internal/bridge/tabs/dialog.go
+++ b/internal/bridge/tabs/dialog.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
+ "strings"
"sync"
"github.com/chromedp/cdproto/page"
@@ -13,6 +14,8 @@ import (
const maxDialogTextBytes = 8 * 1024
+var handleDialogAction = HandleDialog
+
// DialogState represents a pending JavaScript dialog.
type DialogState struct {
Type string `json:"type"`
@@ -59,6 +62,14 @@ func (dm *DialogManager) TakeAutoHandler(tabID string) *ArmedDialogHandler {
return h
}
+// HasAutoHandler reports whether a one-shot auto-handler is armed for the tab.
+func (dm *DialogManager) HasAutoHandler(tabID string) bool {
+ dm.mu.RLock()
+ defer dm.mu.RUnlock()
+ _, ok := dm.armed[tabID]
+ return ok
+}
+
func (dm *DialogManager) SetPending(tabID string, state *DialogState) {
dm.mu.Lock()
defer dm.mu.Unlock()
@@ -115,7 +126,7 @@ func ListenDialogEvents(ctx context.Context, tabID string, dm *DialogManager, au
// command, and the enclosing ListenTarget callback runs on
// the CDP event loop — doing CDP work here would deadlock.
go func() {
- if err := HandleDialog(ctx, accept, promptText); err != nil {
+ if err := handleDialogAction(ctx, accept, promptText); err != nil {
slog.Warn("armed dialog handler failed", "tabId", tabID, "err", err)
dm.SetPending(tabID, state)
} else {
@@ -129,7 +140,7 @@ func ListenDialogEvents(ctx context.Context, tabID string, dm *DialogManager, au
state.HasBrowserHandler = true
// Same reasoning as above — dispatch in a goroutine.
go func() {
- if err := HandleDialog(ctx, true, e.DefaultPrompt); err != nil {
+ if err := handleDialogAction(ctx, true, e.DefaultPrompt); err != nil {
slog.Warn("auto-accept dialog failed", "tabId", tabID, "err", err)
dm.SetPending(tabID, state)
} else {
@@ -162,10 +173,21 @@ type DialogResult struct {
func HandlePendingDialog(ctx context.Context, tabID string, dm *DialogManager, accept bool, promptText string) (*DialogResult, error) {
state := dm.GetAndClear(tabID)
if state == nil {
- return nil, fmt.Errorf("no dialog open on tab %s", tabID)
+ // Best-effort fallback when dialog-open events were missed.
+ if err := handleDialogAction(ctx, accept, promptText); err != nil {
+ if isNoDialogOpenError(err) || isDialogContextUnavailableError(err) {
+ return nil, fmt.Errorf("no dialog open on tab %s", tabID)
+ }
+ return nil, fmt.Errorf("handle dialog: %w", err)
+ }
+ return &DialogResult{Type: "unknown", Message: "", Handled: true}, nil
}
- if err := HandleDialog(ctx, accept, promptText); err != nil {
+ if err := handleDialogAction(ctx, accept, promptText); err != nil {
+ if isNoDialogOpenError(err) {
+ // Dialog may already be handled/closed by browser auto-handler.
+ return &DialogResult{Type: state.Type, Message: state.Message, Handled: true}, nil
+ }
dm.SetPending(tabID, state)
return nil, fmt.Errorf("handle dialog: %w", err)
}
@@ -177,6 +199,24 @@ func HandlePendingDialog(ctx context.Context, tabID string, dm *DialogManager, a
}, nil
}
+func isNoDialogOpenError(err error) bool {
+ if err == nil {
+ return false
+ }
+ msg := strings.ToLower(err.Error())
+ return strings.Contains(msg, "no dialog open") ||
+ strings.Contains(msg, "no dialog is showing") ||
+ strings.Contains(msg, "not showing a dialog")
+}
+
+func isDialogContextUnavailableError(err error) bool {
+ if err == nil {
+ return false
+ }
+ msg := strings.ToLower(err.Error())
+ return strings.Contains(msg, "invalid context")
+}
+
func normalizeDialogState(state *DialogState) *DialogState {
if state == nil {
return nil
diff --git a/internal/bridge/tabs/dialog_test.go b/internal/bridge/tabs/dialog_test.go
new file mode 100644
index 00000000..41170b1c
--- /dev/null
+++ b/internal/bridge/tabs/dialog_test.go
@@ -0,0 +1,119 @@
+package tabs
+
+import (
+ "context"
+ "fmt"
+ "testing"
+)
+
+func TestDialogManager_AutoHandlerVisibility(t *testing.T) {
+ dm := NewDialogManager()
+ if dm.HasAutoHandler("tab1") {
+ t.Fatal("expected no armed handler initially")
+ }
+ dm.ArmAutoHandler("tab1", "accept", "hi")
+ if !dm.HasAutoHandler("tab1") {
+ t.Fatal("expected armed handler after ArmAutoHandler")
+ }
+ _ = dm.TakeAutoHandler("tab1")
+ if dm.HasAutoHandler("tab1") {
+ t.Fatal("expected armed handler to be cleared after TakeAutoHandler")
+ }
+}
+
+func TestHandlePendingDialog_NoPending_FallbackHandlesDialog(t *testing.T) {
+ dm := NewDialogManager()
+ orig := handleDialogAction
+ t.Cleanup(func() { handleDialogAction = orig })
+
+ called := false
+ handleDialogAction = func(ctx context.Context, accept bool, promptText string) error {
+ called = true
+ if !accept {
+ t.Fatalf("accept = false, want true")
+ }
+ if promptText != "hello" {
+ t.Fatalf("promptText = %q, want hello", promptText)
+ }
+ return nil
+ }
+
+ result, err := HandlePendingDialog(context.Background(), "tab1", dm, true, "hello")
+ if err != nil {
+ t.Fatalf("HandlePendingDialog() error = %v", err)
+ }
+ if !called {
+ t.Fatal("expected fallback dialog handler to be called")
+ }
+ if result == nil || !result.Handled || result.Type != "unknown" {
+ t.Fatalf("unexpected result: %#v", result)
+ }
+}
+
+func TestHandlePendingDialog_NoPending_NoDialogOpen(t *testing.T) {
+ dm := NewDialogManager()
+ orig := handleDialogAction
+ t.Cleanup(func() { handleDialogAction = orig })
+
+ handleDialogAction = func(ctx context.Context, accept bool, promptText string) error {
+ return fmt.Errorf("No dialog is showing")
+ }
+
+ _, err := HandlePendingDialog(context.Background(), "tab1", dm, true, "")
+ if err == nil {
+ t.Fatal("expected error for no open dialog")
+ }
+ if got := err.Error(); got != "no dialog open on tab tab1" {
+ t.Fatalf("error = %q, want %q", got, "no dialog open on tab tab1")
+ }
+}
+
+func TestHandlePendingDialog_Pending_NoDialogOpenTreatedHandled(t *testing.T) {
+ dm := NewDialogManager()
+ dm.SetPending("tab1", &DialogState{Type: "alert", Message: "hello"})
+
+ orig := handleDialogAction
+ t.Cleanup(func() { handleDialogAction = orig })
+
+ handleDialogAction = func(ctx context.Context, accept bool, promptText string) error {
+ return fmt.Errorf("No dialog is showing")
+ }
+
+ result, err := HandlePendingDialog(context.Background(), "tab1", dm, true, "")
+ if err != nil {
+ t.Fatalf("HandlePendingDialog() error = %v", err)
+ }
+ if result == nil || !result.Handled {
+ t.Fatalf("unexpected result: %#v", result)
+ }
+ if result.Type != "alert" || result.Message != "hello" {
+ t.Fatalf("unexpected result payload: %#v", result)
+ }
+ if pending := dm.GetPending("tab1"); pending != nil {
+ t.Fatalf("pending dialog should stay cleared, got %#v", pending)
+ }
+}
+
+func TestHandlePendingDialog_Pending_ErrorRequeues(t *testing.T) {
+ dm := NewDialogManager()
+ dm.SetPending("tab1", &DialogState{Type: "confirm", Message: "Are you sure?"})
+
+ orig := handleDialogAction
+ t.Cleanup(func() { handleDialogAction = orig })
+
+ handleDialogAction = func(ctx context.Context, accept bool, promptText string) error {
+ return fmt.Errorf("transport broken")
+ }
+
+ _, err := HandlePendingDialog(context.Background(), "tab1", dm, false, "")
+ if err == nil {
+ t.Fatal("expected error from dialog handler")
+ }
+ pending := dm.GetPending("tab1")
+ if pending == nil {
+ t.Fatal("expected dialog state to be re-queued")
+ }
+ if pending.Type != "confirm" {
+ t.Fatalf("pending.Type = %q, want confirm", pending.Type)
+ }
+}
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index c168721e..b5a911bb 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -65,6 +65,8 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
req.Text = q.Get("text")
req.Value = q.Get("value")
req.Key = q.Get("key")
+ req.DialogAction = strings.ToLower(strings.TrimSpace(q.Get("dialogAction")))
+ req.DialogText = q.Get("dialogText")
if v := q.Get("nodeId"); v != "" {
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
req.NodeID = n
@@ -104,6 +106,7 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
return
}
req.Kind = bridge.CanonicalActionKind(req.Kind)
+ req.DialogAction = strings.ToLower(strings.TrimSpace(req.DialogAction))
}
// Validate kind — single endpoint returns 400 for bad input (unlike batch which returns 200 with errors)
@@ -111,6 +114,10 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
httpx.Error(w, 400, fmt.Errorf("missing required field 'kind'"))
return
}
+ if req.DialogAction != "" && req.DialogAction != "accept" && req.DialogAction != "dismiss" {
+ httpx.Error(w, 400, fmt.Errorf("dialogAction must be 'accept' or 'dismiss'"))
+ return
+ }
h.recordActionRequest(r, req)
if !h.shouldUseLiteAction(req.Kind) {
if available := h.Bridge.AvailableActions(); len(available) > 0 {
diff --git a/internal/handlers/actions_test.go b/internal/handlers/actions_test.go
index 99b65ad4..ea9a7b89 100644
--- a/internal/handlers/actions_test.go
+++ b/internal/handlers/actions_test.go
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http/httptest"
+ "strings"
"testing"
"github.com/chromedp/cdproto/target"
@@ -427,6 +428,42 @@ func TestHandleAction_InvalidJSON(t *testing.T) {
}
}
+func TestHandleAction_PostRejectsInvalidDialogAction(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("POST", "/action", bytes.NewReader([]byte(`{"kind":"click","selector":"#btn","dialogAction":"maybe"}`)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+
+ h.HandleAction(w, req)
+
+ if w.Code != 400 {
+ t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
+ }
+ if !strings.Contains(w.Body.String(), "dialogAction must be 'accept' or 'dismiss'") {
+ t.Fatalf("expected dialogAction validation error, got %s", w.Body.String())
+ }
+}
+
+func TestHandleAction_GetAcceptsValidDialogAction(t *testing.T) {
+ b := &recordingActionBridge{}
+ h := New(b, &config.RuntimeConfig{}, nil, nil, nil)
+
+ req := httptest.NewRequest("GET", "/action?kind=mouse-move&x=0&y=0&dialogAction=accept&dialogText=ok", nil)
+ w := httptest.NewRecorder()
+
+ h.HandleAction(w, req)
+
+ if w.Code != 200 {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ if b.lastReq.DialogAction != "accept" {
+ t.Fatalf("dialogAction = %q, want accept", b.lastReq.DialogAction)
+ }
+ if b.lastReq.DialogText != "ok" {
+ t.Fatalf("dialogText = %q, want ok", b.lastReq.DialogText)
+ }
+}
+
func TestHandleAction_PostCanonicalMouseFieldsAreAccepted(t *testing.T) {
b := &recordingActionBridge{}
h := New(b, &config.RuntimeConfig{}, nil, nil, nil)