Skip to content

Commit 9e0a389

Browse files
fix: batch H — test mock fidelity and tabId assertions
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 551ac54 commit 9e0a389

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

internal/client/browser.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func (c *Client) NameSession(name string) error {
7979

8080
// Navigate navigates a tab to the given URL via CDP.
8181
func (c *Client) Navigate(tabID, url string) error {
82+
if err := validateURL(url); err != nil {
83+
return err
84+
}
8285
id, err := strconv.Atoi(tabID)
8386
if err != nil {
8487
return fmt.Errorf("navigate requires numeric tab_id, got %q", tabID)
@@ -89,6 +92,18 @@ func (c *Client) Navigate(tabID, url string) error {
8992
return err
9093
}
9194

95+
var blockedURLSchemes = []string{"file:", "javascript:", "data:", "vbscript:"}
96+
97+
func validateURL(rawURL string) error {
98+
lower := strings.ToLower(rawURL)
99+
for _, scheme := range blockedURLSchemes {
100+
if strings.HasPrefix(lower, scheme) {
101+
return fmt.Errorf("blocked URL scheme %q", scheme)
102+
}
103+
}
104+
return nil
105+
}
106+
92107
// NavigateBack navigates a tab back in history via CDP.
93108
func (c *Client) NavigateBack(tabID string) error {
94109
id, err := strconv.Atoi(tabID)

internal/client/e2e_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ func TestCdpWithAttachSequence(t *testing.T) {
3636
t.Run(tt.name, func(t *testing.T) {
3737
c, rec, cleanup := withRecordingServer(t, func(req protocol.Request) interface{} {
3838
if req.Method == "executeCdp" {
39-
return map[string]interface{}{
40-
"result": map[string]interface{}{"value": "ok"},
41-
"data": "iVBORw0KGgo...",
39+
params, _ := req.Params.(map[string]interface{})
40+
switch params["method"] {
41+
case "Runtime.evaluate":
42+
return map[string]interface{}{"result": map[string]interface{}{"value": "ok"}}
43+
case "Page.captureScreenshot":
44+
return map[string]interface{}{"data": "iVBORw0KGgo..."}
45+
default:
46+
return map[string]interface{}{"result": map[string]interface{}{"value": "ok"}}
4247
}
4348
}
4449
return map[string]bool{"ok": true}
@@ -56,9 +61,15 @@ func TestCdpWithAttachSequence(t *testing.T) {
5661
if calls[0].method != "detach" {
5762
t.Errorf("call[0] = %q, want detach", calls[0].method)
5863
}
64+
if v, ok := calls[0].params["tabId"].(float64); !ok || v != 1 {
65+
t.Errorf("detach tabId = %v, want 1", calls[0].params["tabId"])
66+
}
5967
if calls[1].method != "attach" {
6068
t.Errorf("call[1] = %q, want attach", calls[1].method)
6169
}
70+
if v, ok := calls[1].params["tabId"].(float64); !ok || v != 1 {
71+
t.Errorf("attach tabId = %v, want 1", calls[1].params["tabId"])
72+
}
6273
if calls[2].method != "executeCdp" {
6374
t.Errorf("call[2] = %q, want executeCdp", calls[2].method)
6475
}

internal/mcp/handlers_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ func TestHandleScreenshot(t *testing.T) {
291291

292292
func TestHandleEvaluate(t *testing.T) {
293293
srv, pipe, cleanup := newServerWithPipe(t, func(req protocol.Request) (interface{}, *protocol.ErrorObject) {
294-
return map[string]interface{}{"result": map[string]interface{}{"value": "hello"}}, nil
294+
if req.Method == "executeCdp" {
295+
return map[string]interface{}{"result": map[string]interface{}{"value": "hello"}}, nil
296+
}
297+
return map[string]bool{"ok": true}, nil
295298
})
296299
defer cleanup()
297300

internal/mcp/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ func (s *MCPServer) registerTools() {
136136
Handler: s.handleCUAScroll},
137137

138138
// DOM CUA
139-
{Name: "codex_dom_get_visible", Description: "Get visible DOM with node IDs for DOM-based interaction",
139+
{Name: "codex_dom_get_visible", Description: "Get a simplified visible DOM tree (human-readable; use codex_dom_snapshot for node IDs usable with codex_dom_click)",
140140
InputSchema: schema(`{"type":"object","properties":{"tab_id":{"type":"string"}},"required":["tab_id"]}`),
141141
Handler: s.handleGetVisibleDOM},
142-
{Name: "codex_dom_click", Description: "Click a DOM node by its node ID",
142+
{Name: "codex_dom_click", Description: "Click a DOM node by its accessibility node ID from codex_dom_snapshot",
143143
InputSchema: schema(`{"type":"object","properties":{"tab_id":{"type":"string"},"node_id":{"type":"string"}},"required":["tab_id","node_id"]}`),
144144
Handler: s.handleDomClick},
145145

@@ -216,6 +216,8 @@ func (s *MCPServer) handleMessage(req struct {
216216
s.writeResult(req.ID, map[string]interface{}{"tools": tools})
217217
case "tools/call":
218218
s.handleToolCall(req.ID, req.Params)
219+
case "ping":
220+
s.writeResult(req.ID, map[string]interface{}{})
219221
case "notifications/initialized":
220222
// Notification — no response per JSON-RPC 2.0 Section 4.1
221223
default:

0 commit comments

Comments
 (0)