Skip to content

Commit d0e99ed

Browse files
committed
refactor(dashboard): fold widget sandbox coverage
1 parent 6b30800 commit d0e99ed

4 files changed

Lines changed: 17 additions & 86 deletions

File tree

‎ui/src/components/board/board-view.test-support.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ export function callbacks(overrides: Partial<BoardViewCallbacks> = {}): BoardVie
6161
};
6262
}
6363

64-
export function gatewayContext(client: { request: ReturnType<typeof vi.fn> } | null) {
64+
export function gatewayContext(
65+
client: { request: ReturnType<typeof vi.fn> } | null,
66+
basePath = "",
67+
) {
6568
return {
69+
basePath,
6670
gateway: {
6771
connection: { gatewayUrl: "" },
6872
snapshot: { client },

‎ui/src/components/board/board-view.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ describe("openclaw-board-view", () => {
5858

5959
const frame = view.querySelector("iframe");
6060
expect(frame?.getAttribute("src")).toContain(":18790/mcp-app-sandbox");
61+
expect(frame?.getAttribute("sandbox")).toBe(
62+
"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox",
63+
);
6164
expect(frame?.getAttribute("loading")).toBe("eager");
6265
expect(view.querySelector('[data-test-id="board-widget-error"]')).toBeNull();
6366
});
@@ -94,7 +97,7 @@ describe("openclaw-board-view", () => {
9497
const fetchMock = vi.fn(async () => new Response("<!doctype html><p>weather</p>"));
9598
vi.stubGlobal("fetch", fetchMock);
9699
const view = await mount({
97-
context: gatewayContext({ request: firstRequest }),
100+
context: gatewayContext({ request: firstRequest }, "/control"),
98101
snapshot: snapshot({
99102
widgets: [
100103
boardWidget({
@@ -125,7 +128,7 @@ describe("openclaw-board-view", () => {
125128
});
126129
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
127130
const bridgeChannel = new MessageChannel();
128-
const initialized = new Promise<void>((resolve) => {
131+
const initialized = new Promise<{ controlUiBaseUrl?: string }>((resolve) => {
129132
bridgeChannel.port2.addEventListener("message", (event) => {
130133
if (event.data?.type !== "openclaw:widget-host-init") {
131134
return;
@@ -137,12 +140,13 @@ describe("openclaw-board-view", () => {
137140
},
138141
[],
139142
);
140-
resolve();
143+
resolve(event.data as { controlUiBaseUrl?: string });
141144
});
142145
});
143146
bridgeChannel.port2.start();
144147
send({ type: "openclaw:widget-bridge-port-offer" }, [bridgeChannel.port1]);
145-
await initialized;
148+
const hostInit = await initialized;
149+
expect(hostInit.controlUiBaseUrl).toBe(`${window.location.origin}/control`);
146150
bridgeChannel.port2.postMessage(
147151
{
148152
type: "openclaw:widget-bridge-request",
@@ -161,7 +165,7 @@ describe("openclaw-board-view", () => {
161165
);
162166

163167
const provider = view.parentElement as ReturnType<typeof createApplicationContextProvider>;
164-
provider.setContext(gatewayContext({ request: secondRequest }));
168+
provider.setContext(gatewayContext({ request: secondRequest }, "/control"));
165169
await cell.updateComplete;
166170
bridgeChannel.port2.postMessage(
167171
{

‎ui/src/components/board/board-widget-frame.test.ts‎

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* @vitest-environment jsdom */
22

3-
import { render } from "lit";
43
import { afterEach, describe, expect, it, vi } from "vitest";
5-
import type { ApplicationContext } from "../../app/context.ts";
64
import type { BoardWidget } from "../../lib/board/types.ts";
75
import { recordBoardWidgetTicketReceipt } from "../../lib/board/widget-ticket-lifetime.ts";
86
import { BoardWidgetFrameLifecycle } from "./board-widget-frame.ts";
@@ -17,10 +15,6 @@ type LifecycleInternals = {
1715
frameFailureKey: string;
1816
frameRefreshAttempts: number;
1917
refreshFailedFrame: (widget: BoardWidget) => void;
20-
sandboxHostOptions: (
21-
frame: HTMLIFrameElement,
22-
widget: BoardWidget,
23-
) => { controlUiBaseUrl?: string } | undefined;
2418
};
2519

2620
function createTicketRefreshLifecycle(
@@ -49,72 +43,6 @@ afterEach(() => {
4943
vi.useRealTimers();
5044
});
5145

52-
describe("board widget sandbox permissions", () => {
53-
it("allows rendered widgets to open new tabs while approval placeholders stay strict", () => {
54-
const renderSandbox = (grantState: BoardWidget["grantState"]) => {
55-
const widget = {
56-
name: "links",
57-
revision: 1,
58-
grantState,
59-
sandboxUrl: "/mcp-app-sandbox",
60-
sandboxPort: 19124,
61-
viewTicket: "ticket",
62-
} as BoardWidget;
63-
const lifecycle = new BoardWidgetFrameLifecycle({
64-
active: () => true,
65-
connected: () => true,
66-
context: () =>
67-
({
68-
gateway: { connection: { gatewayUrl: "https://gateway.example" } },
69-
}) as ApplicationContext,
70-
refreshFrame: () => undefined,
71-
reportContentHeight: () => {},
72-
requestUpdate: () => {},
73-
resolveFrameUrl: () => () => "/widget",
74-
root: () => document,
75-
widget: () => widget,
76-
});
77-
const container = document.createElement("div");
78-
render(lifecycle.render(widget), container);
79-
return container.querySelector("iframe")?.getAttribute("sandbox");
80-
};
81-
82-
const baseSandbox = "allow-scripts allow-same-origin allow-forms";
83-
const linkSandbox = `${baseSandbox} allow-popups allow-popups-to-escape-sandbox`;
84-
expect(renderSandbox("pending")).toBe(baseSandbox);
85-
expect(renderSandbox("rejected")).toBe(baseSandbox);
86-
expect(renderSandbox("none")).toBe(linkSandbox);
87-
expect(renderSandbox("granted")).toBe(linkSandbox);
88-
});
89-
90-
it("passes the Control UI origin and base path to the widget host", () => {
91-
const widget = { name: "links", revision: 1, grantState: "none" } as BoardWidget;
92-
const lifecycle = new BoardWidgetFrameLifecycle({
93-
active: () => true,
94-
connected: () => true,
95-
context: () =>
96-
({
97-
basePath: "/control",
98-
gateway: {
99-
connection: { gatewayUrl: "https://gateway.example" },
100-
snapshot: {},
101-
},
102-
}) as ApplicationContext,
103-
refreshFrame: () => undefined,
104-
reportContentHeight: () => {},
105-
requestUpdate: () => {},
106-
resolveFrameUrl: () => () => "/widget",
107-
root: () => document,
108-
widget: () => widget,
109-
});
110-
const frame = document.createElement("iframe");
111-
112-
const options = (lifecycle as unknown as LifecycleInternals).sandboxHostOptions(frame, widget);
113-
114-
expect(options?.controlUiBaseUrl).toBe(`${window.location.origin}/control`);
115-
});
116-
});
117-
11846
// Drives the private terminal-failure path directly: attempts are exhausted so
11947
// refreshFailedFrame surfaces the terminal message for the given sandbox origin.
12048
function terminalFailureError(params: {

‎ui/src/components/board/board-widget-frame.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ const TICKET_REFRESH_LEAD_MS = 15_000;
1717
const TICKET_REFRESH_MIN_DELAY_MS = 1_000;
1818
const TICKET_REFRESH_RETRY_MS = 1_000;
1919
const TICKET_REFRESH_MAX_RETRY_MS = 30_000;
20-
const BASE_WIDGET_SANDBOX = "allow-scripts allow-same-origin allow-forms";
21-
22-
function boardWidgetSandboxPermissions(widget: Pick<BoardWidget, "grantState">): string {
23-
return widget.grantState === "none" || widget.grantState === "granted"
24-
? `${BASE_WIDGET_SANDBOX} allow-popups allow-popups-to-escape-sandbox`
25-
: BASE_WIDGET_SANDBOX;
26-
}
20+
const WIDGET_SANDBOX =
21+
"allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox";
2722

2823
function documentHidden(): boolean {
2924
return typeof document !== "undefined" && document.visibilityState === "hidden";
@@ -247,7 +242,7 @@ export class BoardWidgetFrameLifecycle {
247242
return html`
248243
<iframe
249244
class="board-widget__frame"
250-
sandbox=${boardWidgetSandboxPermissions(widget)}
245+
sandbox=${WIDGET_SANDBOX}
251246
referrerpolicy="origin"
252247
loading="eager"
253248
title=${widget.title || widget.name}

0 commit comments

Comments
 (0)