High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: the cdptk clip builder resolves nodes in the main world, where page script can steer the clip origin (PIN-062)
Luigi Agostic3db2cba6696
diff --git a/internal/bridge/action_resolve.go b/internal/bridge/action_resolve.goindex 62c3f681..341a1c1d 100644--- a/internal/bridge/action_resolve.go+++ b/internal/bridge/action_resolve.go@@ -10,6 +10,7 @@ import ("github.com/chromedp/chromedp"bridgecdpops "github.com/pinchtab/pinchtab/internal/bridge/cdpops"+ "github.com/pinchtab/pinchtab/internal/cdptk""github.com/pinchtab/pinchtab/internal/selector")@@ -73,43 +74,15 @@ func isolatedExecutionContextID(ctx context.Context, frameID string) (int64, err}// IsolatedNodeObjectID converts a backend node id to a JS object handle in the-// isolated world. DOM.resolveNode without an executionContextId hands back a-// main-world object, and every Runtime.callFunctionOn against it then runs where-// page script can redefine the DOM methods it uses — so a scoped selector or a-// clip origin could be steered by the page it is inspecting.+// isolated world, so Runtime.callFunctionOn against it cannot be answered by+// page script that has redefined the DOM methods the call uses.//-// The isolated world is per-frame but a handle from any frame's world reaches a-// node in another same-process frame, so the top frame's world is used rather-// than the node's own: a bare backend node id does not carry its frame, and-// DOM.describeNode reports frameId only for frame owner elements.+// The rule has one owner in internal/cdptk, the lowest CDP layer, because the+// same resolution is needed by the clip builder there and this package already+// depends on it. A second implementation here is how the two capture paths+// drifted apart before.func IsolatedNodeObjectID(ctx context.Context, backendNodeID int64) (string, error) {- execID, err := isolatedExecutionContextID(ctx, "")- if err != nil {- return "", err- }-- var raw json.RawMessage- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {- return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", map[string]any{- "backendNodeId": backendNodeID,- "executionContextId": execID,- }, &raw)- })); err != nil {- return "", err- }-- var parsed struct {- Object struct {- ObjectID string `json:"objectId"`- } `json:"object"`- }- if err := json.Unmarshal(raw, &parsed); err != nil {- return "", err- }- if parsed.Object.ObjectID == "" {- return "", fmt.Errorf("backend node %d is no longer attached", backendNodeID)- }- return parsed.Object.ObjectID, nil+ return cdptk.IsolatedNodeObjectID(ctx, backendNodeID)}func frameDocumentObjectID(ctx context.Context, frameID string) (string, error) {diff --git a/internal/bridge/isolated_world_test.go b/internal/bridge/isolated_world_test.goindex cbf2b237..157cac21 100644--- a/internal/bridge/isolated_world_test.go+++ b/internal/bridge/isolated_world_test.go@@ -3,7 +3,9 @@ package bridgeimport ("context""encoding/base64"+ "io/fs""os"+ "path/filepath""strings""testing""time"@@ -167,28 +169,44 @@ func TestIsolatedNodeObjectIDFailsClosedWithoutContext(t *testing.T) {// that is the whole claim, and this table is what keeps the claim honest. A new// unprotected site appearing, or one of these being fixed without updating the// list, both fail here.+// Keys are module-relative paths: pinchtab spreads CDP work over internal/bridge,+// internal/bridge/cdpops and internal/cdptk, and a census of one directory would+// report a clean boundary while three packages went unchecked.var mainWorldResolvers = map[string]string{- "action_form.go": "form fill operates on an already-chosen field",- "semantic_metadata.go": "metadata enrichment reads an already-chosen node",- "tab_auto_switch.go": "tab switching acts on an already-chosen target",+ "internal/bridge/action_form.go": "form fill operates on an already-chosen field",+ "internal/bridge/semantic_metadata.go": "metadata enrichment reads an already-chosen node",+ "internal/bridge/tab_auto_switch.go": "tab switching acts on an already-chosen target",+ "internal/bridge/cdpops/element_ops.go": "element ops act on an already-chosen node",+ "internal/bridge/cdpops/frame_dom.go": "callFunctionOn helper for an already-chosen node",+ "internal/bridge/cdpops/pointer.go": "pointer actions act on an already-chosen node",+ "internal/bridge/cdpops/validation.go": "post-action validation reads an already-chosen node",+ "internal/bridge/cdpops/geometry.go": "click-point geometry for an already-chosen node",+ "internal/cdptk/annotate.go": "annotation reads already-chosen nodes",}func TestIsolatedWorldBoundaryCensus(t *testing.T) {- entries, err := os.ReadDir(".")+ root, err := filepath.Abs("../..")if err != nil {t.Fatal(err)}found := map[string]bool{}checked := 0- for _, entry := range entries {- name := entry.Name()- if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {- continue+ walkErr := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {+ if err != nil {+ return err+ }+ if d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {+ return nil}- raw, err := os.ReadFile(name)+ rel, relErr := filepath.Rel(root, path)+ if relErr != nil {+ return relErr+ }+ name := filepath.ToSlash(rel)+ raw, err := os.ReadFile(path)if err != nil {- t.Fatal(err)+ return err}for _, block := range strings.Split(string(raw), `"DOM.resolveNode"`)[1:] {checked++@@ -204,9 +222,13 @@ func TestIsolatedWorldBoundaryCensus(t *testing.T) {t.Errorf("%s resolves a node without an isolated executionContextId and is not a recorded operation path:\n%s", name, head)}}+ return nil+ })+ if walkErr != nil {+ t.Fatal(walkErr)}if checked == 0 {- t.Fatal("no DOM.resolveNode call found in the package — this census is checking nothing")+ t.Fatal("no DOM.resolveNode call found in the module — this census is checking nothing")}for name, why := range mainWorldResolvers {if !found[name] {diff --git a/internal/cdptk/isolated_world.go b/internal/cdptk/isolated_world.gonew file mode 100644index 00000000..dd823ec7--- /dev/null+++ b/internal/cdptk/isolated_world.go@@ -0,0 +1,102 @@+package cdptk++import (+ "context"+ "encoding/json"+ "fmt"++ "github.com/chromedp/chromedp"+)++// isolatedWorldName labels the world reused for node resolution.+// Page.createIsolatedWorld keys on the name, so repeat calls return the same+// context rather than minting one per resolution.+const isolatedWorldName = "pinchtab-node-scope"++// IsolatedNodeObjectID converts a backend node id to a JS object handle in the+// top frame's isolated world.+//+// DOM.resolveNode without an executionContextId hands back a main-world object,+// and every Runtime.callFunctionOn against it then runs where page script can+// redefine the DOM methods it uses — so geometry read through such a handle is+// whatever the page says it is, not where the element actually sits.+//+// The isolated world is per-frame, but a handle from any frame's world reaches a+// node in another same-process frame, so the top frame's world serves all of+// them: a bare backend node id does not carry its frame, and DOM.describeNode+// reports frameId only for frame owner elements.+//+// It never returns a usable zero. A caller that cannot obtain an isolated+// context gets an error rather than a main-world handle, so the boundary fails+// closed.+func IsolatedNodeObjectID(ctx context.Context, backendNodeID int64) (string, error) {+ execID, err := topFrameIsolatedContextID(ctx)+ if err != nil {+ return "", err+ }++ var raw json.RawMessage+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", map[string]any{+ "backendNodeId": backendNodeID,+ "executionContextId": execID,+ }, &raw)+ })); err != nil {+ return "", err+ }++ var parsed struct {+ Object struct {+ ObjectID string `json:"objectId"`+ } `json:"object"`+ }+ if err := json.Unmarshal(raw, &parsed); err != nil {+ return "", err+ }+ if parsed.Object.ObjectID == "" {+ return "", fmt.Errorf("backend node %d is no longer attached", backendNodeID)+ }+ return parsed.Object.ObjectID, nil+}++func topFrameIsolatedContextID(ctx context.Context) (int64, error) {+ // GetFrameTree issues a CDP call, so it needs an executor context; calling it+ // with the caller's bare ctx fails with "invalid context".+ var frameID string+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ tree, err := GetFrameTree(ctx)+ if err != nil {+ return err+ }+ if tree != nil && tree.Frame != nil {+ frameID = tree.Frame.ID.String()+ }+ return nil+ })); err != nil {+ return 0, fmt.Errorf("resolve top frame: %w", err)+ }+ if frameID == "" {+ return 0, fmt.Errorf("resolve top frame: frame id is empty")+ }++ var raw json.RawMessage+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Page.createIsolatedWorld", map[string]any{+ "frameId": frameID,+ "worldName": isolatedWorldName,+ }, &raw)+ })); err != nil {+ return 0, fmt.Errorf("create isolated world: %w", err)+ }++ var resp struct {+ ExecutionContextID int64 `json:"executionContextId"`+ }+ if err := json.Unmarshal(raw, &resp); err != nil {+ return 0, err+ }+ if resp.ExecutionContextID == 0 {+ return 0, fmt.Errorf("top frame has no isolated execution context")+ }+ return resp.ExecutionContextID, nil+}diff --git a/internal/cdptk/screenshot.go b/internal/cdptk/screenshot.goindex f155abb8..c8c47cc7 100644--- a/internal/cdptk/screenshot.go+++ b/internal/cdptk/screenshot.go@@ -18,27 +18,13 @@ func ClipForNode(ctx context.Context, nodeID int64, css1x bool) (*ScreenshotClipreturn nil, fmt.Errorf("scroll into view: %w", err)}- var resolveResult json.RawMessage- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {- return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", map[string]any{- "backendNodeId": nodeID,- }, &resolveResult)- })); err != nil {+ // Isolated world: boxFn reads getBoundingClientRect, so a main-world handle+ // would let the page steer the clip origin by redefining it.+ objectID, err := IsolatedNodeObjectID(ctx, nodeID)+ if err != nil {return nil, fmt.Errorf("resolve node: %w", err)}- var resolved struct {- Object struct {- ObjectID string `json:"objectId"`- } `json:"object"`- }- if err := json.Unmarshal(resolveResult, &resolved); err != nil {- return nil, fmt.Errorf("parse resolved node: %w", err)- }- if resolved.Object.ObjectID == "" {- return nil, fmt.Errorf("element not found in DOM (backendNodeId=%d)", nodeID)- }-// Translate the element box into top-level page coordinates. captureScreenshot// clip coordinates are page-relative, so viewport-relative rects need scroll// offsets from the current document and each ancestor frame.@@ -76,7 +62,7 @@ func ClipForNode(ctx context.Context, nodeID int64, css1x bool) (*ScreenshotClipif err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {return chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.callFunctionOn", map[string]any{"functionDeclaration": boxFn,- "objectId": resolved.Object.ObjectID,+ "objectId": objectID,"returnByValue": true,}, &callResult)})); err != nil {