Skip to content

Commit 3e3a36b

Browse files
committed
fix(browser): isolate relay binding ownership by session
1 parent 7781b90 commit 3e3a36b

7 files changed

Lines changed: 298 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Docs: https://docs.openclaw.ai
7070

7171
### Fixes
7272

73+
- **Browser extension relay:** route Runtime binding callbacks only to registered clients and preserve shared bindings during client cleanup, preventing raw callback payloads from reaching unrelated Playwright sessions.
7374
- **Grok web search startup:** avoid loading the full agent runtime before the first search, while preserving agent-scoped credentials and OAuth fallback behavior.
7475
- Gateway/subagents: keep plugin completion turns bound to the owning Gateway runtime so successful native subagents can finish delivery without losing the published reply runtime.
7576
- **Upgrade config repair:** retain plugin-owned channel configuration migrations alongside core schemas so `doctor --fix` can repair older settings before an external plugin is installed or granted capabilities, while preserving installed-plugin ownership and state-migration boundaries.

‎docs/tools/chrome-extension.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ enables Runtime, the extension checks current tab access before the relay
258258
replays existing execution contexts to that new subscriber. This does not
259259
reset another client's Runtime session.
260260

261+
Runtime binding callbacks go only to logical sessions that successfully registered
262+
the binding name, independently of `Runtime.enable` and `Runtime.disable`.
263+
Removing a binding or disconnecting a client preserves other clients' registrations
264+
of the same name. Context-specific registrations with the same name still share
265+
the underlying native Runtime; use distinct names when clients need separate
266+
context selection.
267+
261268
Fetch request interception has one owner per native target session. Another
262269
client can use other CDP domains, but cannot replace that owner's interception
263270
settings or resolve its paused requests. Competing interception requests return

‎extensions/browser/chrome-extension/bootstrap.chromium.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,22 @@ describe.runIf(runE2E)("Chrome native bootstrap Chromium E2E", () => {
512512
relayPlaywrightContext = relayPage.context();
513513
expect(connectOverCdp).not.toHaveBeenCalled();
514514
const bindingSession = await relayPlaywrightContext.newCDPSession(relayPage);
515+
const observerSession = await relayPlaywrightContext.newCDPSession(relayPage);
515516
const bindingName = "__openclawRelayBindingProof";
516517
const bindingPayloads: string[] = [];
518+
const observerPayloads: string[] = [];
519+
observerSession.on("Runtime.bindingCalled", (event) => {
520+
if (event.name === bindingName) {
521+
observerPayloads.push(event.payload);
522+
}
523+
});
517524
bindingSession.on("Runtime.bindingCalled", (event) => {
518525
if (event.name === bindingName) {
519526
bindingPayloads.push(event.payload);
520527
}
521528
});
522529
try {
530+
await observerSession.send("Runtime.enable");
523531
await bindingSession.send("Runtime.addBinding", { name: bindingName });
524532
await bindingSession.send("Runtime.evaluate", {
525533
expression: "globalThis.__openclawRelayBindingProof('before-enable')",
@@ -531,11 +539,13 @@ describe.runIf(runE2E)("Chrome native bootstrap Chromium E2E", () => {
531539
expression: "globalThis.__openclawRelayBindingProof('after-disable')",
532540
});
533541
await expect.poll(() => bindingPayloads).toEqual(["before-enable", "after-disable"]);
542+
expect(observerPayloads).toEqual([]);
534543
} finally {
535544
await bindingSession
536545
.send("Runtime.removeBinding", { name: bindingName })
537546
.catch(() => {});
538547
await bindingSession.detach().catch(() => {});
548+
await observerSession.detach().catch(() => {});
539549
}
540550
} finally {
541551
connectOverCdp.mockRestore();

‎extensions/browser/src/browser/extension-relay/relay-bridge.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,9 @@ export class ExtensionRelayBridge {
881881
const result =
882882
request.method === "Runtime.enable"
883883
? await runtime.enable(session, emit, send)
884-
: await send();
884+
: request.method === "Runtime.addBinding" || request.method === "Runtime.removeBinding"
885+
? await runtime.binding(session, emit, request.method, request.params)
886+
: await send();
885887
if (client.sessions.get(sessionId) !== session) {
886888
throw new Error(`Session detached: ${sessionId}`);
887889
}

‎extensions/browser/src/browser/extension-relay/relay-runtime.test.ts‎

Lines changed: 156 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function fixture() {
2424
onTestFinished(() => bridge.dispose());
2525
const enabled = new Set<string>();
2626
const held: number[] = [];
27-
let hold = false;
27+
let heldMethod: string | undefined;
2828
let accessAllowed = true;
2929
let contextOffset = -10;
3030
const extension = wireExtension(bridge, (msg) => {
@@ -51,14 +51,14 @@ function fixture() {
5151
msg.sessionId,
5252
);
5353
}
54-
if (hold) {
55-
held.push(msg.seq);
56-
return null;
57-
}
5854
}
5955
if (msg.method === "Runtime.disable") {
6056
enabled.delete(key);
6157
}
58+
if (msg.method === heldMethod) {
59+
held.push(msg.seq);
60+
return null;
61+
}
6262
}
6363
return { type: "result", seq: msg.seq, result: {} };
6464
});
@@ -136,11 +136,11 @@ function fixture() {
136136
revokeAccess: () => {
137137
accessAllowed = false;
138138
},
139-
hold: () => {
140-
hold = true;
139+
hold: (method = "Runtime.enable") => {
140+
heldMethod = method;
141141
},
142142
release: () => {
143-
hold = false;
143+
heldMethod = undefined;
144144
for (const seq of held.splice(0)) {
145145
extension.handlers.onMessage(JSON.stringify({ type: "result", seq, result: {} }));
146146
}
@@ -739,7 +739,9 @@ describe("relay logical Runtime subscriptions", () => {
739739
const f = fixture();
740740
const client = f.client();
741741
const root = await client.attach();
742-
await f.client().attach();
742+
const peer = f.client();
743+
const peerRoot = await peer.attach();
744+
await peer.request("Runtime.enable", peerRoot);
743745
expect(
744746
(await client.request("Runtime.addBinding", root, { name: "relayBinding" })).error,
745747
).toBeUndefined();
@@ -754,13 +756,158 @@ describe("relay logical Runtime subscriptions", () => {
754756
method: "Runtime.bindingCalled",
755757
params,
756758
});
759+
expect(
760+
peer.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
761+
).toEqual([]);
757762
await client.request("Target.detachFromTarget", undefined, { sessionId: root });
758763
const before = client.socket.frames().length;
759764
f.event("Runtime.bindingCalled", params);
760765
expect(client.socket.frames()).toHaveLength(before);
761766
},
762767
);
763768

769+
it.each(["Runtime.removeBinding", "Target.detachFromTarget", "socket close"])(
770+
"preserves a peer's same-name binding after %s and removes the final native registration",
771+
async (operation) => {
772+
const f = fixture();
773+
const first = f.client();
774+
const firstRoot = await first.attach();
775+
const second = f.client();
776+
const secondRoot = await second.attach();
777+
const observer = f.client();
778+
const observerRoot = await observer.attach();
779+
const name = "sharedBinding";
780+
for (const [client, root] of [
781+
[first, firstRoot],
782+
[second, secondRoot],
783+
] as const) {
784+
expect((await client.request("Runtime.addBinding", root, { name })).error).toBeUndefined();
785+
}
786+
await observer.request("Runtime.removeBinding", observerRoot, { name });
787+
expect(f.commands("Runtime.removeBinding")).toHaveLength(0);
788+
if (operation === "socket close") {
789+
first.handlers.onClose();
790+
await flush();
791+
} else if (operation === "Target.detachFromTarget") {
792+
await first.request(operation, undefined, { sessionId: firstRoot });
793+
} else {
794+
await first.request(operation, firstRoot, { name });
795+
}
796+
expect(f.commands("Runtime.removeBinding")).toHaveLength(0);
797+
const params = { name, payload: "still-owned", executionContextId: 1 };
798+
f.event("Runtime.bindingCalled", params);
799+
expect(
800+
first.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
801+
).toEqual([]);
802+
expect(
803+
observer.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
804+
).toEqual([]);
805+
expect(
806+
second.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
807+
).toEqual([{ sessionId: secondRoot, method: "Runtime.bindingCalled", params }]);
808+
await second.request("Runtime.removeBinding", secondRoot, { name });
809+
expect(f.commands("Runtime.removeBinding").map((frame) => frame.params)).toEqual([{ name }]);
810+
f.event("Runtime.bindingCalled", params);
811+
expect(
812+
second.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
813+
).toHaveLength(1);
814+
},
815+
);
816+
817+
it.each([false, true])(
818+
"cleans up a native add admitted after logical close (peer owns same name=%s)",
819+
async (sameName) => {
820+
const f = fixture();
821+
const first = f.client();
822+
const firstRoot = await first.attach();
823+
const second = f.client();
824+
const secondRoot = await second.attach();
825+
const name = "pendingBinding";
826+
if (sameName) {
827+
await second.request("Runtime.addBinding", secondRoot, { name });
828+
}
829+
f.hold("Runtime.addBinding");
830+
const pending = first.send("Runtime.addBinding", firstRoot, { name });
831+
await flush();
832+
first.handlers.onClose();
833+
f.release();
834+
await flush();
835+
expect(first.socket.frames().find((frame) => frame.id === pending)).toBeUndefined();
836+
expect(f.commands("Runtime.removeBinding")).toHaveLength(sameName ? 0 : 1);
837+
const params = { name, payload: "after-close", executionContextId: 1 };
838+
f.event("Runtime.bindingCalled", params);
839+
expect(
840+
first.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
841+
).toEqual([]);
842+
expect(
843+
second.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
844+
).toHaveLength(sameName ? 1 : 0);
845+
},
846+
);
847+
848+
it.each(["Runtime.addBinding", "Runtime.removeBinding"])(
849+
"preserves acknowledged binding ownership when native %s fails",
850+
async (method) => {
851+
const f = fixture();
852+
const client = f.client();
853+
const root = await client.attach();
854+
const name = "failedBinding";
855+
if (method === "Runtime.removeBinding") {
856+
await client.request("Runtime.addBinding", root, { name });
857+
}
858+
f.hold(method);
859+
const pending = client.send(method, root, { name });
860+
await flush();
861+
f.extension.handlers.onMessage(
862+
JSON.stringify({
863+
type: "error",
864+
seq: f.commands(method).at(-1)!.seq,
865+
message: "native binding command failed",
866+
}),
867+
);
868+
await flush();
869+
expect(client.socket.frames().find((frame) => frame.id === pending)?.error).toBeDefined();
870+
f.release();
871+
f.event("Runtime.bindingCalled", { name, payload: "failure", executionContextId: 1 });
872+
expect(
873+
client.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
874+
).toHaveLength(method === "Runtime.removeBinding" ? 1 : 0);
875+
expect((await client.request(method, root, { name })).error).toBeUndefined();
876+
f.event("Runtime.bindingCalled", { name, payload: "retry", executionContextId: 1 });
877+
expect(
878+
client.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
879+
).toHaveLength(1);
880+
},
881+
);
882+
883+
it("keeps a replacement physical session independent of pending retired bindings", async () => {
884+
const f = fixture();
885+
const client = f.client();
886+
const root = await client.attach();
887+
const name = "retiredBinding";
888+
f.hold("Runtime.addBinding");
889+
client.send("Runtime.addBinding", root, { name });
890+
await flush();
891+
f.extension.handlers.onMessage(
892+
JSON.stringify({ type: "detached", tabId: 1, reason: "revoked" }),
893+
);
894+
f.release();
895+
await flush();
896+
const next = await client.attach();
897+
expect(next).not.toBe(root);
898+
const params = { name, payload: "replacement", executionContextId: 11 };
899+
f.event("Runtime.bindingCalled", params);
900+
expect(
901+
client.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
902+
).toEqual([]);
903+
await client.request("Runtime.addBinding", next, { name });
904+
f.event("Runtime.bindingCalled", params);
905+
expect(
906+
client.socket.frames().filter((frame) => frame.method === "Runtime.bindingCalled"),
907+
).toEqual([{ sessionId: next, method: "Runtime.bindingCalled", params }]);
908+
expect(f.commands("Runtime.removeBinding")).toHaveLength(0);
909+
});
910+
764911
it("keeps a concurrent admission alive when another enable for the same logical owner fails", async () => {
765912
const f = fixture();
766913
const c = f.client();

0 commit comments

Comments
 (0)