High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: harden humanized click dialogs and stealth provider routing
Luigi Agostid1f97b53a812
diff --git a/internal/assets/stealth.js b/internal/assets/stealth.jsindex 953a83f4..11ce491a 100644--- a/internal/assets/stealth.js+++ b/internal/assets/stealth.js@@ -396,9 +396,13 @@ if (navigator.connection) {})();if (!(window.devicePixelRatio > 0)) {+ const winProto = Object.getPrototypeOf(window) || Window.prototype;try {- Object.defineProperty(window, 'devicePixelRatio', { get: () => 1, configurable: true });- } catch (e) {}+ delete window.devicePixelRatio;+ Object.defineProperty(winProto, 'devicePixelRatio', { get: () => 1, configurable: true });+ } catch (e) {+ try { Object.defineProperty(window, 'devicePixelRatio', { get: () => 1, configurable: true }); } catch (e2) {}+ }}// BATTERY API - Headless often lacks this@@ -668,10 +672,15 @@ installFunctionToStringMask(Function.prototype);const numericWindowValue = (value) => {return Number.isFinite(value) ? value : 0;};++ const winProto = Object.getPrototypeOf(window) || Window.prototype;const defineWindowMetric = (name, value) => {try {- Object.defineProperty(window, name, { get: () => value, configurable: true });- } catch (e) {}+ delete window[name];+ Object.defineProperty(winProto, name, { get: () => value, configurable: true });+ } catch (e) {+ try { Object.defineProperty(window, name, { get: () => value, configurable: true }); } catch (e2) {}+ }};const screenX = Math.max(numericWindowValue(window.screenX), numericWindowValue(window.screenLeft));diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.goindex 3caaee23..0ab9898d 100644--- a/internal/bridge/action_pointer.go+++ b/internal/bridge/action_pointer.go@@ -16,6 +16,7 @@ var scrollByCoordinateAction = ScrollByCoordinatevar mouseMoveByCoordinateAction = MouseMoveByCoordinatevar mouseDownByCoordinateAction = MouseDownByCoordinatevar mouseUpByCoordinateAction = MouseUpByCoordinate+var clickElementAction = ClickElementvar clickByNodeIDAction = ClickByNodeIDvar jsClickByBackendNodeAction = JSClickByBackendNodevar dispatchClickByBackendNodeAction = JSDispatchClickByBackendNode@@ -614,12 +615,22 @@ func (b *Bridge) actionHumanizedClick(ctx context.Context, req ActionRequest) (rreturn nil, fmt.Errorf("need selector, ref, or nodeId")}- // Run the multi-step humanized click (bezier mouse-move + press + release) in- // a goroutine and poll for blocking dialogs. Without this, a dialog or- // dialog-like popup opened by the click would hang the renderer for the- // full action timeout. Mirrors the wrapping used by actionClick.+ // If the caller expects this click to open a native JS dialog, arm a+ // one-shot auto-handler so it gets accepted/dismissed instead of leaving the+ // renderer blocked. Without this the humanized path can only ever report+ // dialog_blocking. Mirrors actionClick's non-humanized branch.dm := b.GetDialogManager()- detectDialog := req.TabID != "" && dm != nil+ armedDialog := false+ if req.DialogAction != "" && req.TabID != "" && dm != nil {+ dm.ArmAutoHandler(req.TabID, req.DialogAction, req.DialogText)+ armedDialog = true+ }++ // Run the multi-step humanized click (bezier mouse-move + press + release) in+ // a goroutine. When no dialog-action was provided, poll for an unexpected+ // blocking dialog so it surfaces as ErrDialogBlocking rather than hanging the+ // renderer for the full action timeout.+ detectDialog := !armedDialog && req.TabID != "" && dm != nilclickCtx := ctxvar clickCancel context.CancelFuncif detectDialog {@@ -629,7 +640,7 @@ func (b *Bridge) actionHumanizedClick(ctx context.Context, req ActionRequest) (rresultCh := make(chan error, 1)go func() {- resultCh <- ClickElement(clickCtx, backendNodeID)+ resultCh <- clickElementAction(clickCtx, backendNodeID)}()if detectDialog {@@ -659,6 +670,9 @@ func (b *Bridge) actionHumanizedClick(ctx context.Context, req ActionRequest) (rif err := <-resultCh; err != nil {return nil, err}+ if armedDialog {+ waitForArmedDialogSettle(dm, req.TabID, dialogAutoHandleTimeout)+ }return map[string]any{"clicked": true, "human": true}, nil}diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.goindex 63a8422d..128596dc 100644--- a/internal/bridge/action_request.go+++ b/internal/bridge/action_request.go@@ -154,7 +154,7 @@ func hasJSONKey(raw map[string]json.RawMessage, key string) bool {func (b *Bridge) ExecuteAction(ctx context.Context, kind string, req ActionRequest) (map[string]any, error) {kind = CanonicalActionKind(kind)req.Kind = CanonicalActionKind(req.Kind)- if kind == ActionClick && req.Humanize != nil && *req.Humanize && strings.TrimSpace(req.Mode) != "" {+ if kind == ActionClick && b.effectiveHumanize(req) && strings.TrimSpace(req.Mode) != "" {return nil, fmt.Errorf("mode and humanize are mutually exclusive")}fn, ok := b.Actions[kind]diff --git a/internal/bridge/actions_test.go b/internal/bridge/actions_test.goindex 64cea3c3..54cd9209 100644--- a/internal/bridge/actions_test.go+++ b/internal/bridge/actions_test.go@@ -7,6 +7,7 @@ import ("strings""testing"+ "github.com/chromedp/cdproto/cdp""github.com/pinchtab/pinchtab/internal/config")@@ -350,19 +351,77 @@ func TestRemovedHumanActionKindsAreUnknown(t *testing.T) {}}-func TestExecuteAction_ClickRejectsModeAndHumanizeTogether(t *testing.T) {- b := New(context.TODO(), nil, &config.RuntimeConfig{})- _, err := b.ExecuteAction(context.Background(), ActionClick, ActionRequest{+func TestExecuteAction_ClickRejectsModeAndEffectiveHumanizeTogether(t *testing.T) {+ tests := []struct {+ name string+ config *config.RuntimeConfig+ req ActionRequest+ }{+ {+ name: "request humanize true",+ config: &config.RuntimeConfig{},+ req: ActionRequest{+ Kind: ActionClick,+ Ref: "e5",+ Mode: "dom",+ Humanize: boolPtr(true),+ },+ },+ {+ name: "instance humanize default",+ config: &config.RuntimeConfig{Humanize: true},+ req: ActionRequest{+ Kind: ActionClick,+ Ref: "e5",+ Mode: "dispatch",+ },+ },+ }++ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ b := New(context.TODO(), nil, tc.config)+ _, err := b.ExecuteAction(context.Background(), ActionClick, tc.req)+ if err == nil {+ t.Fatal("expected error when mode and humanize are both set")+ }+ if !strings.Contains(err.Error(), "mutually exclusive") {+ t.Fatalf("expected mutually exclusive error, got: %v", err)+ }+ })+ }+}++func TestExecuteAction_ClickModeAllowedWhenHumanizeOverrideDisablesDefault(t *testing.T) {+ origJSClick := jsClickByBackendNodeAction+ t.Cleanup(func() {+ jsClickByBackendNodeAction = origJSClick+ })++ called := false+ jsClickByBackendNodeAction = func(ctx context.Context, nodeID int64) error {+ called = true+ if nodeID != 42 {+ t.Fatalf("nodeID = %d, want 42", nodeID)+ }+ return nil+ }++ b := New(context.TODO(), nil, &config.RuntimeConfig{Humanize: true})+ res, err := b.ExecuteAction(context.Background(), ActionClick, ActionRequest{Kind: ActionClick,- Ref: "e5",+ NodeID: 42,Mode: "dom",- Humanize: boolPtr(true),+ Humanize: boolPtr(false),})- if err == nil {- t.Fatal("expected error when mode and humanize are both set")+ if err != nil {+ t.Fatalf("expected raw mode click to be allowed when humanize=false overrides default, got: %v", err)}- if !strings.Contains(err.Error(), "mutually exclusive") {- t.Fatalf("expected mutually exclusive error, got: %v", err)+ if !called {+ t.Fatal("expected dom mode click path to run")+ }+ if clicked, _ := res["clicked"].(bool); !clicked {+ t.Fatalf("expected clicked result, got %#v", res)}}@@ -401,6 +460,50 @@ func TestClickAction_HumanizeOptInUsesHumanizedPath(t *testing.T) {}}+func TestClickAction_HumanizedDialogActionArmsAutoHandler(t *testing.T) {+ origClickElement := clickElementAction+ t.Cleanup(func() {+ clickElementAction = origClickElement+ })++ b := New(context.TODO(), nil, &config.RuntimeConfig{Humanize: true})+ dm := b.GetDialogManager()++ clickElementAction = func(ctx context.Context, backendNodeID cdp.BackendNodeID) error {+ if backendNodeID != 42 {+ return errors.New("unexpected backend node id")+ }+ armed := dm.TakeAutoHandler("tab-dialog")+ if armed == nil {+ return errors.New("dialog auto-handler was not armed")+ }+ if armed.Action != "accept" || armed.Text != "typed response" {+ return errors.New("dialog auto-handler had wrong action or text")+ }+ return nil+ }++ res, err := b.ExecuteAction(context.Background(), ActionClick, ActionRequest{+ Kind: ActionClick,+ TabID: "tab-dialog",+ NodeID: 42,+ DialogAction: "accept",+ DialogText: "typed response",+ })+ if err != nil {+ t.Fatalf("humanized click with dialogAction returned error: %v", err)+ }+ if clicked, _ := res["clicked"].(bool); !clicked {+ t.Fatalf("expected clicked result, got %#v", res)+ }+ if human, _ := res["human"].(bool); !human {+ t.Fatalf("expected human=true result, got %#v", res)+ }+ if dm.HasAutoHandler("tab-dialog") {+ t.Fatal("dialog auto-handler should be consumed or cleaned up after click")+ }+}+func TestClickByNodeIDWithJSFallback_UsesTrustedPathFirst(t *testing.T) {origTrusted := clickByNodeIDActionorigFallback := jsClickByBackendNodeActiondiff --git a/internal/bridge/bridge_lifecycle.go b/internal/bridge/bridge_lifecycle.goindex a5ae3bac..abbb6ae8 100644--- a/internal/bridge/bridge_lifecycle.go+++ b/internal/bridge/bridge_lifecycle.go@@ -12,6 +12,7 @@ import ("time""github.com/chromedp/cdproto/page"+ cdpruntime "github.com/chromedp/cdproto/runtime""github.com/chromedp/chromedp""github.com/pinchtab/pinchtab/internal/browsers""github.com/pinchtab/pinchtab/internal/browsers/chrome"@@ -92,6 +93,22 @@ func (b *Bridge) tabSetup(ctx context.Context) {slog.Warn("no-animations injection failed", "err", err)}}++ // Anti-CDP detection: in full stealth, disable Runtime event dispatching after+ // setup. chromedp enables Runtime during target init; detectors (DataDome's+ // isAutomatedWithCDP, deviceandbrowserinfo) call console.log(Error) with a+ // custom stack getter and observe the side effect when Runtime.consoleAPICalled+ // serializes the stack. Runtime.evaluate is command-based and keeps working.+ // In full mode, eager console capture is already disabled (see+ // shouldEagerlyCaptureConsole); EnsureConsoleCapture will re-enable Runtime+ // on demand if the caller fetches console/error logs.+ if b.Config != nil && stealth.NormalizeLevel(b.Config.StealthLevel) == stealth.LevelFull {+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ return cdpruntime.Disable().Do(ctx)+ })); err != nil {+ slog.Warn("runtime.Disable failed", "err", err)+ }+ }}func (b *Bridge) ensureStealthBundle() {diff --git a/internal/bridge/input_human.go b/internal/bridge/input_human.goindex 737b20f5..8eb26038 100644--- a/internal/bridge/input_human.go+++ b/internal/bridge/input_human.go@@ -179,17 +179,39 @@ func Click(ctx context.Context, x, y float64) error {)}-// ClickElement clicks on an element identified by its backend DOM node ID.-// This uses the backendDOMNodeId from the accessibility tree, NOT a regular DOM nodeId.-func ClickElement(ctx context.Context, backendNodeID cdp.BackendNodeID) error {+var scrollIntoViewIfNeededAction = func(ctx context.Context, backendNodeID cdp.BackendNodeID) error {+ return chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ return dom.ScrollIntoViewIfNeeded().WithBackendNodeID(backendNodeID).Do(ctx)+ }))+}++var boxModelForBackendNodeAction = func(ctx context.Context, backendNodeID cdp.BackendNodeID) (*dom.BoxModel, error) {var box *dom.BoxModel- if err := chromedp.Run(ctx,+ err := chromedp.Run(ctx,chromedp.ActionFunc(func(ctx context.Context) error {var err errorbox, err = dom.GetBoxModel().WithBackendNodeID(backendNodeID).Do(ctx)return err}),- ); err != nil {+ )+ return box, err+}++var clickCoordinateHumanAction = Click++// ClickElement clicks on an element identified by its backend DOM node ID.+// This uses the backendDOMNodeId from the accessibility tree, NOT a regular DOM nodeId.+func ClickElement(ctx context.Context, backendNodeID cdp.BackendNodeID) error {+ // Scroll the target into the visual viewport first. The humanized click is+ // coordinate-based (Input.dispatchMouseEvent at the box-model center), so an+ // element below the fold or freshly revealed (lazy-loaded list item,+ // modal-triggered button) would otherwise produce coordinates that land on+ // whatever is currently visible there — the dispatch "succeeds" but the+ // intended click handler never fires. Best-effort: not every node scrolls.+ _ = scrollIntoViewIfNeededAction(ctx, backendNodeID)++ box, err := boxModelForBackendNodeAction(ctx, backendNodeID)+ if err != nil {return err}@@ -203,7 +225,7 @@ func ClickElement(ctx context.Context, backendNodeID cdp.BackendNodeID) error {offsetX := (humanRand.Float64() - 0.5) * 10offsetY := (humanRand.Float64() - 0.5) * 10- return Click(ctx, centerX+offsetX, centerY+offsetY)+ return clickCoordinateHumanAction(ctx, centerX+offsetX, centerY+offsetY)}func Type(text string, fast bool) []chromedp.Action {diff --git a/internal/bridge/input_human_test.go b/internal/bridge/input_human_test.goindex 1a44190e..463a24f7 100644--- a/internal/bridge/input_human_test.go+++ b/internal/bridge/input_human_test.go@@ -2,8 +2,12 @@ package bridgeimport ("context"+ "errors""math/rand""testing"++ "github.com/chromedp/cdproto/cdp"+ "github.com/chromedp/cdproto/dom")func TestSetHumanRandSeed(t *testing.T) {@@ -89,3 +93,37 @@ func TestClickElement_RequiresMinContentLength(t *testing.T) {t.Error("expected error without browser connection")}}++func TestClickElement_ScrollsBeforeReadingBoxModel(t *testing.T) {+ origScroll := scrollIntoViewIfNeededAction+ origBoxModel := boxModelForBackendNodeAction+ t.Cleanup(func() {+ scrollIntoViewIfNeededAction = origScroll+ boxModelForBackendNodeAction = origBoxModel+ })++ stop := errors.New("stop after box lookup")+ var calls []string+ scrollIntoViewIfNeededAction = func(ctx context.Context, backendNodeID cdp.BackendNodeID) error {+ if backendNodeID != 99 {+ t.Fatalf("scroll backendNodeID = %d, want 99", backendNodeID)+ }+ calls = append(calls, "scroll")+ return errors.New("scroll failed but should be best-effort")+ }+ boxModelForBackendNodeAction = func(ctx context.Context, backendNodeID cdp.BackendNodeID) (*dom.BoxModel, error) {+ if backendNodeID != 99 {+ t.Fatalf("box backendNodeID = %d, want 99", backendNodeID)+ }+ calls = append(calls, "box")+ return nil, stop+ }++ err := ClickElement(context.Background(), 99)+ if !errors.Is(err, stop) {+ t.Fatalf("ClickElement error = %v, want %v", err, stop)+ }+ if len(calls) != 2 || calls[0] != "scroll" || calls[1] != "box" {+ t.Fatalf("calls = %v, want [scroll box]", calls)+ }+}diff --git a/internal/config/config_file_marshal.go b/internal/config/config_file_marshal.goindex 7361ef62..5d563c73 100644--- a/internal/config/config_file_marshal.go+++ b/internal/config/config_file_marshal.go@@ -592,5 +592,37 @@ func FileConfigFromRuntime(cfg *RuntimeConfig) FileConfig {Browsers: browsersBlock,}+ reconcileDefaultTargetProvider(&fc.Browser, browsersDefault, cfg)+return fc}++// reconcileDefaultTargetProvider keeps the serialized default browser target+// consistent with browsers.default, which is the authoritative provider source.+// config.Load() eagerly synthesizes a "default" target from the legacy chrome+// fields, so when a caller later overrides DefaultBrowser (for example the+// orchestrator selecting cloak for a child instance) without rewriting Targets,+// the stale target would otherwise shadow browsers.default on reload because+// explicit targets win over the legacy fields. Only the lone auto-synthesized+// "default" target is reconciled; explicit multi-target configs are left intact.+func reconcileDefaultTargetProvider(bc *BrowserConfig, browsersDefault string, cfg *RuntimeConfig) {+ if bc == nil || cfg == nil || len(bc.Targets) != 1 {+ return+ }+ target, ok := bc.Targets[DefaultBrowserTargetName]+ if !ok {+ return+ }+ want := NormalizeBrowser(browsersDefault)+ if NormalizeBrowser(target.Provider) == want {+ return+ }+ // browsers.default won; rewrite the default target from the authoritative+ // runtime fields so the round-trip preserves the selected provider.+ target.Provider = want+ target.Binary = cfg.ChromeBinary+ target.ExtraFlags = cfg.ChromeExtraFlags+ target.Cloak = cloakBrowserConfigFromRuntime(cfg)+ target.Proxy = cloneBrowserProxyConfig(cfg.Proxy)+ bc.Targets[DefaultBrowserTargetName] = target+}diff --git a/internal/config/config_types.go b/internal/config/config_types.goindex c5a67cf0..c95b9d4d 100644--- a/internal/config/config_types.go+++ b/internal/config/config_types.go@@ -324,7 +324,7 @@ type BrowserConfig struct {// BrowserTargetsConfig maps target name -> target config. Names must match `^[a-z][a-z0-9-]{0,31}$`.type BrowserTargetsConfig map[string]BrowserTargetConfig-// BrowserTargetConfig is a single named browser target. See docs/architecture/browser-targets.md.+// BrowserTargetConfig is a single named browser target. See docs/architecture/browser-abstraction.md.type BrowserTargetConfig struct {Provider string `json:"provider,omitempty"`Binary string `json:"binary,omitempty"`diff --git a/scripts/lib/smoke-config.sh b/scripts/lib/smoke-config.shindex 0a3400f2..9de9d3cd 100644--- a/scripts/lib/smoke-config.sh+++ b/scripts/lib/smoke-config.sh@@ -20,7 +20,6 @@ import syspath, provider, token, fixtures_host, chrome_binary, cloak_binary = sys.argv[1:7]browser = {- "provider": provider,"extensionPaths": [],}if provider == "cloak":@@ -47,6 +46,14 @@ cfg = {"token": token,"stateDir": "/data",},+ # browsers.default is the authoritative provider selector. The legacy+ # browser.provider field is rejected at validation time (it breaks+ # `config set`/`config validate`); browser.binary/cloak below remain+ # accepted and migrate into the default target on load.+ "browsers": {+ "default": provider,+ "available": [provider],+ },"browser": browser,"instanceDefaults": {"mode": "headless",@@ -62,7 +69,10 @@ cfg = {"allowStateExport": True,"allowedDomains": [fixtures_host, "127.0.0.1", "localhost", "::1"],"downloadAllowedDomains": [fixtures_host],- "trustedResolveCIDRs": ["127.0.0.1/32"],+ # 127.0.0.1/32 alone blocks fixtures that resolve to the Docker bridge+ # network (e.g. 172.18.x) even when the host is in allowedDomains;+ # trust the RFC1918 range the container networks use.+ "trustedResolveCIDRs": ["127.0.0.1/32", "172.16.0.0/12"],},"profiles": {"baseDir": "/data/profiles",@@ -137,7 +147,9 @@ cfg = {"allowStateExport": True,"allowedDomains": [fixtures_host, "127.0.0.1", "localhost", "::1"],"downloadAllowedDomains": [fixtures_host],- "trustedResolveCIDRs": ["127.0.0.1/32"],+ # See write_provider_config: trust the Docker bridge range so fixtures+ # resolving to 172.18.x are not blocked by the private-IP guard.+ "trustedResolveCIDRs": ["127.0.0.1/32", "172.16.0.0/12"],},"profiles": {"baseDir": "/data/profiles",