Skip to content

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

fix: drag ignores --button entirely — the flag is registered, always sent, and never read, so a right-button drag reports success as a left-button drag (PIN-202)

Luigi Agosti79da4bf71fd4
diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.go
index 70efd94f..9b418643 100644
--- a/internal/bridge/action_pointer.go
+++ b/internal/bridge/action_pointer.go
@@ -607,13 +607,16 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strin
func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]any, error) {
if req.hasDragDestination() {
+ if req.DragX != 0 || req.DragY != 0 {
+ return nil, fmt.Errorf("drag takes a destination (toSelector/toNodeId/toX+toY) or an offset (dragX/dragY), not both")
+ }
return b.dragToDestination(ctx, req)
}
if req.DragX == 0 && req.DragY == 0 {
return nil, fmt.Errorf("dragX or dragY required for drag")
}
if req.NodeID > 0 {
- err := DragByNodeID(ctx, req.NodeID, req.DragX, req.DragY)
+ err := DragByNodeID(ctx, req.NodeID, req.DragX, req.DragY, req.Button)
if err != nil {
return nil, err
}
@@ -624,7 +627,7 @@ func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]
if err != nil {
return nil, err
}
- err = DragByNodeID(ctx, int64(node.BackendNodeID), req.DragX, req.DragY)
+ err = DragByNodeID(ctx, int64(node.BackendNodeID), req.DragX, req.DragY, req.Button)
if err != nil {
return nil, err
}
@@ -633,10 +636,6 @@ func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]
return nil, fmt.Errorf("need selector, ref, or nodeId")
}
-// dragToDestination is the target form: source and destination in one action, so the
-// interpolated move sequence Chrome needs to recognise a drag happens between them. The
-// zero-offset refusal above does not apply here — a drop onto an overlapping target is a
-// legitimate request, and it is the destination, not a delta, that says where to go.
func (b *Bridge) dragToDestination(ctx context.Context, req ActionRequest) (map[string]any, error) {
fromX, fromY, err := dragPointFor(ctx, dragEnd{nodeID: req.NodeID, selector: req.Selector, x: req.X, y: req.Y, hasPoint: req.HasXY})
if err != nil {
@@ -646,7 +645,7 @@ func (b *Bridge) dragToDestination(ctx context.Context, req ActionRequest) (map[
if err != nil {
return nil, fmt.Errorf("drag destination: %w", err)
}
- if err := DragBetweenPoints(ctx, fromX, fromY, toX, toY); err != nil {
+ if err := DragBetweenPoints(ctx, fromX, fromY, toX, toY, req.Button); err != nil {
return nil, err
}
return map[string]any{
@@ -658,8 +657,6 @@ func (b *Bridge) dragToDestination(ctx context.Context, req ActionRequest) (map[
}, nil
}
-// dragEnd is one end of a target drag. Both ends accept the same three vocabularies, so
-// they resolve through one function rather than two bodies that can drift apart.
type dragEnd struct {
nodeID int64
selector string
diff --git a/internal/bridge/action_pointer_drag_test.go b/internal/bridge/action_pointer_drag_test.go
index 6ac5a5d0..fcb9a46f 100644
--- a/internal/bridge/action_pointer_drag_test.go
+++ b/internal/bridge/action_pointer_drag_test.go
@@ -21,13 +21,17 @@ const dragFixtureHTML = `<style>div{width:120px;height:80px;display:inline-block
<script>
window.h5 = 'no';
window.mouse = 'no';
+window.pressedButton = -1;
+window.heldButtons = -1;
const h5Src = document.getElementById('h5Src'), h5Dst = document.getElementById('h5Dst');
h5Src.addEventListener('dragstart', e => { window.h5 = 'started'; e.dataTransfer.setData('text/plain', 'payload'); });
h5Dst.addEventListener('dragover', e => e.preventDefault());
h5Dst.addEventListener('drop', e => { e.preventDefault(); window.h5 = 'DROPPED'; });
const mouseSrc = document.getElementById('mouseSrc'), mouseDst = document.getElementById('mouseDst');
let pressed = false;
-mouseSrc.addEventListener('mousedown', () => { pressed = true; });
+mouseSrc.addEventListener('mousedown', e => { pressed = true; window.pressedButton = e.button; });
+mouseSrc.addEventListener('contextmenu', e => e.preventDefault());
+document.addEventListener('mousemove', e => { if (pressed && window.heldButtons < 0) window.heldButtons = e.buttons; });
mouseDst.addEventListener('mouseup', () => { if (pressed) window.mouse = 'DROPPED'; });
</script>`
@@ -236,6 +240,83 @@ func TestThePointerPrimitivesStillDispatchOneJumpEach(t *testing.T) {
}
}
+func (f *dragFixture) buttonRecord(t *testing.T) (pressed int, held int) {
+ t.Helper()
+ if err := chromedp.Run(f.ctx,
+ chromedp.Evaluate(`window.pressedButton`, &pressed),
+ chromedp.Evaluate(`window.heldButtons`, &held),
+ ); err != nil {
+ t.Fatal(err)
+ }
+ return pressed, held
+}
+
+// dragCmd registers --button with a default, so the flag reaches the body on every drag and
+// "is it set" proves nothing — only the page can say which button was actually held. Both
+// forms have to honour it: the press, and the interpolated moves that carry the held mask.
+func TestDragHoldsTheRequestedButton(t *testing.T) {
+ f := newDragFixture(t)
+
+ for _, tc := range []struct {
+ button string
+ wantPressed int
+ wantHeld int
+ }{
+ {button: "", wantPressed: 0, wantHeld: 1},
+ {button: "left", wantPressed: 0, wantHeld: 1},
+ {button: "right", wantPressed: 2, wantHeld: 2},
+ {button: "middle", wantPressed: 1, wantHeld: 4},
+ } {
+ for _, form := range []struct {
+ name string
+ req func(t *testing.T) ActionRequest
+ }{
+ {
+ name: "target form",
+ req: func(t *testing.T) ActionRequest {
+ return ActionRequest{Kind: ActionDrag, Selector: "#mouseSrc", ToSelector: "#mouseDst", Button: tc.button}
+ },
+ },
+ {
+ name: "offset form",
+ req: func(t *testing.T) ActionRequest {
+ srcX, srcY := f.point(t, "#mouseSrc")
+ dstX, dstY := f.point(t, "#mouseDst")
+ return ActionRequest{Kind: ActionDrag, Selector: "#mouseSrc", DragX: int(dstX - srcX), DragY: int(dstY - srcY), Button: tc.button}
+ },
+ },
+ } {
+ f.reload(t)
+ if _, err := f.b.actionDrag(f.ctx, form.req(t)); err != nil {
+ t.Fatalf("button %q %s: %v", tc.button, form.name, err)
+ }
+
+ pressed, held := f.buttonRecord(t)
+ if pressed != tc.wantPressed {
+ t.Errorf("button %q %s: page saw mousedown button %d, want %d", tc.button, form.name, pressed, tc.wantPressed)
+ }
+ if held != tc.wantHeld {
+ t.Errorf("button %q %s: page saw buttons=%d during the drag, want %d; the moves must carry the same button as the press", tc.button, form.name, held, tc.wantHeld)
+ }
+ }
+ }
+}
+
+// A drag cannot be both a destination and an offset. Picking one silently discards the
+// other, which is a confidently wrong drag reported as success.
+func TestDragRefusesADestinationAndAnOffsetTogether(t *testing.T) {
+ f := newDragFixture(t)
+ f.reload(t)
+
+ _, err := f.b.actionDrag(f.ctx, ActionRequest{Kind: ActionDrag, Selector: "#h5Src", ToSelector: "#h5Dst", DragX: 180})
+ if err == nil {
+ t.Fatal("a drag carrying both a destination and an offset was accepted, so one of them was silently dropped")
+ }
+ if html5, _ := f.state(t); html5 != "no" {
+ t.Errorf("the refused drag still moved the pointer: h5=%q", html5)
+ }
+}
+
// handAssembledDrag is the sequence the CLI used to post as four independent requests: one
// move to the source, a press, ONE move to the destination, a release.
func (f *dragFixture) handAssembledDrag(t *testing.T, from, to string) {
diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.go
index 7ad61cc3..2c49a156 100644
--- a/internal/bridge/action_request.go
+++ b/internal/bridge/action_request.go
@@ -87,13 +87,9 @@ type ActionRequest struct {
DragX int `json:"dragX"`
DragY int `json:"dragY"`
- // ToSelector is a drag's DESTINATION, in the same unified selector vocabulary as
- // Selector (ref, CSS, text:), resolved to ToNodeID by the handler. It is what makes
- // "drag this onto that" one action: assembling it from four pointer requests moved the
- // pointer in a single jump, which Chrome never reads as the start of a drag, so an
- // HTML5 draggable saw nothing while every request answered OK. ToX/ToY are the same
- // destination given as a point, and HasToXY separates an absent destination from the
- // legitimate one at (0,0).
+ // A drag's destination. ToSelector takes the same unified selector vocabulary as
+ // Selector and the handler resolves it to ToNodeID; HasToXY separates an absent
+ // destination from the legitimate one at (0,0).
ToSelector string `json:"toSelector,omitempty"`
ToNodeID int64 `json:"toNodeId,omitempty"`
ToX float64 `json:"toX,omitempty"`
diff --git a/internal/bridge/cdpops/pointer.go b/internal/bridge/cdpops/pointer.go
index fd7707d7..ebe581dc 100644
--- a/internal/bridge/cdpops/pointer.go
+++ b/internal/bridge/cdpops/pointer.go
@@ -356,21 +356,30 @@ func DoubleClickByNodeID(ctx context.Context, nodeID int64) error {
return chromedp.Run(ctx, actions...)
}
-func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int) error {
+func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int, button string) error {
x, y, err := PointerPointForNode(ctx, nodeID, true)
if err != nil {
return err
}
- return DragBetweenPoints(ctx, x, y, x+float64(dx), y+float64(dy))
+ return DragBetweenPoints(ctx, x, y, x+float64(dx), y+float64(dy), button)
}
-// DragBetweenPoints is the one drag implementation, and the interpolation is the whole of
-// it: Chrome starts an HTML5 drag only after sustained movement follows the press, so a
-// single jump from source to destination never fires dragstart. That is why the CLI's
-// four-request from->to sequence — move, down, ONE move, up — silently did nothing on a
-// draggable element while reporting success, and why the destination has to be resolved
-// into one action rather than assembled from the pointer primitives.
-func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64) error {
+// heldButton maps a button name to the pair Input.dispatchMouseEvent needs while that
+// button is down: the enum for "button" and the bitmask for "buttons".
+func heldButton(button string) (input.MouseButton, int64) {
+ switch normalizeMouseButton(button) {
+ case "right":
+ return input.Right, 2
+ case "middle":
+ return input.Middle, 4
+ default:
+ return input.Left, 1
+ }
+}
+
+// Chrome starts an HTML5 drag only after sustained movement follows the press, so the
+// interpolation is load-bearing: a single jump from source to destination fires no dragstart.
+func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64, button string) error {
dx := endX - x
dy := endY - y
dist := math.Sqrt(dx*dx + dy*dy)
@@ -381,13 +390,15 @@ func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64) error {
if steps > 20 {
steps = 20
}
+ held, heldMask := heldButton(button)
+ name := normalizeMouseButton(button)
if err := dispatchMouseMove(ctx, x, y, input.None, 0); err != nil {
return err
}
if err := dispatchMouseEvent(ctx, map[string]any{
"type": "mousePressed",
- "button": "left",
+ "button": name,
"clickCount": 1,
"x": x, "y": y,
}); err != nil {
@@ -395,13 +406,13 @@ func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64) error {
}
for i := 1; i <= steps; i++ {
t := float64(i) / float64(steps)
- if err := dispatchMouseMove(ctx, x+t*dx, y+t*dy, input.Left, 1); err != nil {
+ if err := dispatchMouseMove(ctx, x+t*dx, y+t*dy, held, heldMask); err != nil {
return err
}
}
return dispatchMouseEvent(ctx, map[string]any{
"type": "mouseReleased",
- "button": "left",
+ "button": name,
"clickCount": 1,
"x": endX, "y": endY,
})
diff --git a/internal/bridge/cdpops_facade.go b/internal/bridge/cdpops_facade.go
index 1d04bb82..6269eb90 100644
--- a/internal/bridge/cdpops_facade.go
+++ b/internal/bridge/cdpops_facade.go
@@ -102,12 +102,12 @@ func JSDoubleClickByBackendNode(ctx context.Context, nodeID int64) error {
return bridgecdpops.JSDoubleClickByBackendNode(ctx, nodeID)
}
-func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int) error {
- return bridgecdpops.DragByNodeID(ctx, nodeID, dx, dy)
+func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int, button string) error {
+ return bridgecdpops.DragByNodeID(ctx, nodeID, dx, dy, button)
}
-func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64) error {
- return bridgecdpops.DragBetweenPoints(ctx, x, y, endX, endY)
+func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64, button string) error {
+ return bridgecdpops.DragBetweenPoints(ctx, x, y, endX, endY, button)
}
func HoverByCoordinate(ctx context.Context, x, y float64) error {
diff --git a/internal/cli/actions/actions_element.go b/internal/cli/actions/actions_element.go
index 569f54ef..3ff6c6b3 100644
--- a/internal/cli/actions/actions_element.go
+++ b/internal/cli/actions/actions_element.go
@@ -358,9 +358,6 @@ func actionBodyForTarget(kind string, target dragTarget) map[string]any {
}
func Drag(client *http.Client, base, token string, args []string, cmd *cobra.Command) {
- // Two modes, both ONE "drag" action: the offset form (dragX/dragY) and the target form
- // (toSelector/toX/toY). The target form used to be four separate pointer requests, which
- // moved the pointer in a single jump — never a drag as far as Chrome is concerned.
hasDragX := cmd.Flags().Changed("drag-x")
hasDragY := cmd.Flags().Changed("drag-y")
diff --git a/internal/handlers/action_selector_resolution.go b/internal/handlers/action_selector_resolution.go
index 5d6bbfb4..47baad89 100644
--- a/internal/handlers/action_selector_resolution.go
+++ b/internal/handlers/action_selector_resolution.go
@@ -79,12 +79,9 @@ func (h *Handlers) resolveActionRequestSelector(ctx context.Context, tabID strin
return actionSelectorResolution{status: http.StatusConflict}, fmt.Errorf("topmost dialog changed twice during selector resolution; retry after the page settles")
}
-// resolveActionRequestDestination resolves a drag's ToSelector the same way the source is
-// resolved, so a destination can be a ref, a CSS selector or a semantic one — the target
-// form exists to say "onto that element", and refs are what a snapshot hands an agent. It
-// runs a scratch request through the source resolver rather than reimplementing the frame
-// and modal scoping, and reports refMissing so the caller answers 404 with the recovery
-// hint instead of dragging to (0,0).
+// resolveActionRequestDestination runs a scratch request through the source resolver so a
+// drag destination accepts the same selector vocabulary, with the same frame and modal
+// scoping, as the source.
func (h *Handlers) resolveActionRequestDestination(ctx context.Context, tabID string, req *bridge.ActionRequest) (actionSelectorResolution, error) {
if req.ToSelector == "" || req.ToNodeID != 0 {
return actionSelectorResolution{}, nil