The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.
JavaScript agent-skillsai-agentautomationbrowserbrowser-automationclaude-codecodexhermes-agentskillsskills-sh
fix(ego-browser): reject a sessionId passed inside cdp params
Joway@MacMinif960c313b6b2
diff --git a/package/ego-browser/skill-body/SKILL.md b/package/ego-browser/skill-body/SKILL.mdindex f30630e..4855fc3 100644--- a/package/ego-browser/skill-body/SKILL.md+++ b/package/ego-browser/skill-body/SKILL.md@@ -418,8 +418,10 @@ whether an unsafe follow-up requires reloading or closing the Page first.Use documented Page methods first. If a wrapper is missing or does not workreliably on the current page, use `page.cdp()` as a lower-level path fordiagnosis or control. It accepts Page, Runtime, DOM, Network, Input, and similar-commands; use `task.cdp()` for Target and Browser commands. Raw CDP invalidates-refs. Do not persist `page.targetId` across rounds.+commands; use `task.cdp()` for Target and Browser commands. Both choose their own+session: a `sessionId` in params is rejected, except on Target commands that+define it, such as `Target.detachFromTarget`. Raw CDP invalidates refs. Do not+persist `page.targetId` across rounds.## Action receipts, popups, and dialogsdiff --git a/package/ego-browser/src/cdp-eval.test.mjs b/package/ego-browser/src/cdp-eval.test.mjsindex 607d43e..d8f5e4f 100644--- a/package/ego-browser/src/cdp-eval.test.mjs+++ b/package/ego-browser/src/cdp-eval.test.mjs@@ -2,9 +2,70 @@ import test from "node:test";import assert from "node:assert/strict";import { runInNewContext } from "node:vm";-import { hasReturnStatement, js } from "../dist/src/cdp-eval.js";+import { cdp, hasReturnStatement, js } from "../dist/src/cdp-eval.js";import { setOverrides } from "../dist/src/state.js";+async function withRecordedCdp(run) {+ const calls = [];+ const restore = setOverrides({+ cdpOverride: async (method, params, sessionId) => {+ calls.push({ method, params, sessionId });+ return {};+ },+ });+ try {+ await run(calls);+ } finally {+ restore();+ }+}++test("cdp rejects a sessionId inside params instead of running in the page session", async () => {+ await withRecordedCdp(async (calls) => {+ await assert.rejects(+ () => cdp("Runtime.evaluate", { expression: "1", sessionId: "s1" }),+ (error) => {+ assert(error instanceof TypeError);+ assert.match(error.message, /Runtime\.evaluate/);+ assert.match(error.message, /cdp\(method, params, sessionId\)/);+ return true;+ },+ );+ assert.deepEqual(calls, [], "the command must never reach the browser");+ });+});++test("cdp routes a sessionId passed as the third argument", async () => {+ await withRecordedCdp(async (calls) => {+ await cdp("Runtime.evaluate", { expression: "1" }, "s1");+ assert.deepEqual(calls, [+ {+ method: "Runtime.evaluate",+ params: { expression: "1" },+ sessionId: "s1",+ },+ ]);+ });+});++test("cdp keeps Target commands whose protocol params define sessionId", async () => {+ await withRecordedCdp(async (calls) => {+ await cdp("Target.detachFromTarget", { sessionId: "s1" });+ await cdp("Target.sendMessageToTarget", { sessionId: "s1", message: "{}" });+ assert.deepEqual(+ calls.map(({ method, params, sessionId }) => [+ method,+ params.sessionId,+ sessionId,+ ]),+ [+ ["Target.detachFromTarget", "s1", undefined],+ ["Target.sendMessageToTarget", "s1", undefined],+ ],+ );+ });+});+test("hasReturnStatement ignores returns inside nested functions", () => {for (const expression of ["[1, 2].map(x => { return x * 2; })",diff --git a/package/ego-browser/src/cdp-eval.ts b/package/ego-browser/src/cdp-eval.tsindex 36c67e3..082e78f 100644--- a/package/ego-browser/src/cdp-eval.ts+++ b/package/ego-browser/src/cdp-eval.ts@@ -8,11 +8,17 @@ let hasWarnedAboutFunctionJs = false;/*** Send a raw Chrome DevTools Protocol command.* @param {string} method CDP method name, for example Runtime.evaluate.- * @param {object} [params] CDP command parameters.- * @param {string} [sessionId] Optional attached target session id.+ * @param {object} [params] CDP command parameters. A sessionId here is rejected, except on Target commands that define it.+ * @param {string} [sessionId] Optional attached target session id. Defaults to the current page session.* @returns {Promise<object>} CDP result object.*/export async function cdp(method, params: any = {}, sessionId = undefined) {+ assertNoSessionIdInParams(+ "cdp",+ method,+ params,+ "Pass it as the third argument: cdp(method, params, sessionId).",+ );const result = state.cdpOverride? await state.cdpOverride(method, params, sessionId): (await browserCdp(method, params, sessionId)).result || {};@@ -28,6 +34,21 @@ export async function cdp(method, params: any = {}, sessionId = undefined) {return result;}+/**+ * A session is selected by the CDP message envelope, not by params. Chromium+ * ignores an unknown params.sessionId, so the command would run in the default+ * session and return a plausible result from the wrong target. Only the Target+ * domain defines sessionId as a real parameter.+ */+export function assertNoSessionIdInParams(apiName, method, params, remedy) {+ if (params?.sessionId === undefined || String(method).startsWith("Target.")) {+ return;+ }+ throw new TypeError(+ `${apiName}: ${method} does not take sessionId in params; it would be ignored and the command would run in the wrong session. ${remedy}`,+ );+}+/*** Evaluate JavaScript in the current page or a target tab.* @param {string | Function} expression JavaScript source string or a function whose body should be evaluated.diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjsindex 7935f74..5ec46a8 100644--- a/package/ego-browser/src/page-model.test.mjs+++ b/package/ego-browser/src/page-model.test.mjs@@ -731,6 +731,7 @@ function createFixture(rootDir) {if (activeTarget === params.targetId) activeTarget = null;return { success: true };}+ if (method === "Target.detachFromTarget") return {};throw new Error(`unexpected CDP method ${method}`);},async showAgentMousePosition(x, y) {@@ -5301,6 +5302,46 @@ test("Page and TaskSpace CDP commands preserve their intended scope", async () =});});+test("Page and TaskSpace CDP reject a sessionId they would not honour", async () => {+ await withFixture(async (fixture) => {+ const task = taskForRound(fixture, "round-a");+ const page = await openTestPage(task, "https://example.test/first");+ fixture.calls.length = 0;++ await assert.rejects(+ () =>+ page.cdp("Runtime.evaluate", { expression: "1", sessionId: "other" }),+ (error) => {+ assert(error instanceof TypeError);+ assert.match(error.message, /Page\.cdp/);+ assert.match(error.message, /Runtime\.evaluate/);+ return true;+ },+ );+ await assert.rejects(+ () => task.cdp("Browser.getVersion", { sessionId: "other" }),+ /TaskSpace\.cdp.*Browser\.getVersion/,+ );+ assert.deepEqual(+ fixture.calls.filter(([kind]) => kind === "cdp"),+ [],+ "a rejected command must never reach the browser",+ );++ await task.cdp("Target.detachFromTarget", { sessionId: "other" });+ assert(+ fixture.calls.some(+ ([kind, method, params, sessionId]) =>+ kind === "cdp" &&+ method === "Target.detachFromTarget" &&+ params.sessionId === "other" &&+ sessionId === undefined,+ ),+ "Target commands that define params.sessionId must still pass through",+ );+ });+});+test("Page waits and events remain isolated to the addressed target", async () => {await withFixture(async (fixture) => {const task = taskForRound(fixture, "round-a");diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.tsindex b364ec9..065d7ee 100644--- a/package/ego-browser/src/page-model.ts+++ b/package/ego-browser/src/page-model.ts@@ -26,7 +26,7 @@ import {type FileChooserInterception,type FileChooserOpenedEvent,} from "./browser-runtime.js";-import { runtimeValue } from "./cdp-eval.js";+import { assertNoSessionIdInParams, runtimeValue } from "./cdp-eval.js";import { withTemporaryClipboardText } from "./clipboard.js";import {captureScreenshotForSession,@@ -4394,6 +4394,12 @@ function assertCdpCall(if (!params || typeof params !== "object" || Array.isArray(params)) {throw new TypeError("cdp params must be an object");}+ assertNoSessionIdInParams(+ apiName,+ method,+ params,+ `${apiName} selects its own session and cannot route to another one.`,+ );validatePublicApiOptions(apiName, options);}