|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { BoardSnapshot } from "../../../packages/gateway-protocol/src/index.js"; |
| 3 | +import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
| 4 | +import { resetBoardEventNoticeStateForTest } from "../../boards/board-notices.js"; |
| 5 | +import { SqliteBoardStore } from "../../boards/sqlite-board-store.js"; |
| 6 | +import { replaceSessionEntrySync } from "../../config/sessions/session-accessor.entry.js"; |
| 7 | +import { peekSystemEvents, resetSystemEventsForTest } from "../../infra/system-events.js"; |
| 8 | +import { |
| 9 | + closeOpenClawAgentDatabasesForTest, |
| 10 | + openOpenClawAgentDatabase, |
| 11 | +} from "../../state/openclaw-agent-db.js"; |
| 12 | +import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js"; |
| 13 | +import { createBoardHarness as createHarness } from "./board.test-support.js"; |
| 14 | +import { sessionMutationHandlers } from "./sessions-mutations.js"; |
| 15 | +import type { GatewayRequestContext, RespondFn } from "./types.js"; |
| 16 | + |
| 17 | +vi.mock("./sessions.runtime.js", () => ({ |
| 18 | + performGatewaySessionReset: vi.fn(async ({ key, reason }: { key: string; reason: string }) => ({ |
| 19 | + ok: true, |
| 20 | + key, |
| 21 | + agentId: "main", |
| 22 | + entry: { sessionId: `reset-${reason}` }, |
| 23 | + resolved: {}, |
| 24 | + })), |
| 25 | +})); |
| 26 | + |
| 27 | +describe("board gateway runtime boundaries", () => { |
| 28 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + resetBoardEventNoticeStateForTest(); |
| 32 | + resetSystemEventsForTest(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + closeOpenClawAgentDatabasesForTest(); |
| 37 | + closeOpenClawStateDatabaseForTest(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("enforces data bindings against the granted tool set", async () => { |
| 41 | + const readDataBinding = vi.fn(async () => ({ sessions: ["one"] })); |
| 42 | + const { invoke, store } = createHarness(undefined, { readDataBinding }); |
| 43 | + await invoke("board.widget.put", { |
| 44 | + sessionKey: "session", |
| 45 | + name: "reader", |
| 46 | + content: { kind: "html", html: "reader" }, |
| 47 | + }); |
| 48 | + let board = await invoke("board.get", { sessionKey: "session" }); |
| 49 | + let snapshot = board.mock.calls[0]?.[1] as BoardSnapshot; |
| 50 | + const denied = await invoke("board.data.read", { |
| 51 | + ticket: snapshot.widgets[0]?.viewTicket, |
| 52 | + bindingId: "sessions.list", |
| 53 | + params: { limit: 2 }, |
| 54 | + }); |
| 55 | + expect(denied.mock.calls[0]?.[0]).toBe(false); |
| 56 | + expect(readDataBinding).not.toHaveBeenCalled(); |
| 57 | + |
| 58 | + await invoke("board.widget.put", { |
| 59 | + sessionKey: "session", |
| 60 | + name: "reader", |
| 61 | + content: { kind: "html", html: "reader" }, |
| 62 | + declared: { tools: ["sessions.list"] }, |
| 63 | + }); |
| 64 | + await invoke("board.widget.grant", { |
| 65 | + sessionKey: "session", |
| 66 | + name: "reader", |
| 67 | + decision: "granted", |
| 68 | + revision: 2, |
| 69 | + instanceId: store.getSnapshot("session").widgets[0]?.instanceId, |
| 70 | + }); |
| 71 | + board = await invoke("board.get", { sessionKey: "session" }); |
| 72 | + snapshot = board.mock.calls[0]?.[1] as BoardSnapshot; |
| 73 | + const allowed = await invoke("board.data.read", { |
| 74 | + ticket: snapshot.widgets[0]?.viewTicket, |
| 75 | + bindingId: "sessions.list", |
| 76 | + params: { limit: 2 }, |
| 77 | + }); |
| 78 | + expect(allowed.mock.calls[0]?.[1]).toEqual({ sessions: ["one"] }); |
| 79 | + expect(readDataBinding).toHaveBeenCalledWith( |
| 80 | + "sessions.list", |
| 81 | + { limit: 2 }, |
| 82 | + expect.objectContaining({ params: expect.any(Object) }), |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("rejects unknown data bindings inside the gateway allowlist boundary", async () => { |
| 87 | + const { invoke, store } = createHarness(); |
| 88 | + await invoke("board.widget.put", { |
| 89 | + sessionKey: "session", |
| 90 | + name: "reader", |
| 91 | + content: { kind: "html", html: "reader" }, |
| 92 | + declared: { tools: ["secrets.dump"] }, |
| 93 | + }); |
| 94 | + await invoke("board.widget.grant", { |
| 95 | + sessionKey: "session", |
| 96 | + name: "reader", |
| 97 | + decision: "granted", |
| 98 | + revision: 1, |
| 99 | + instanceId: store.getSnapshot("session").widgets[0]?.instanceId, |
| 100 | + }); |
| 101 | + const board = await invoke("board.get", { sessionKey: "session" }); |
| 102 | + const snapshot = board.mock.calls[0]?.[1] as BoardSnapshot; |
| 103 | + const response = await invoke("board.data.read", { |
| 104 | + ticket: snapshot.widgets[0]?.viewTicket, |
| 105 | + bindingId: "secrets.dump", |
| 106 | + }); |
| 107 | + expect(response).toHaveBeenCalledWith( |
| 108 | + false, |
| 109 | + undefined, |
| 110 | + expect.objectContaining({ message: expect.stringContaining("not allowed") }), |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it("runs only the exact granted cron job capability", async () => { |
| 115 | + const triggerCronJob = vi.fn(async (jobId: string) => ({ ok: true, jobId })); |
| 116 | + const { invoke, store } = createHarness(undefined, { triggerCronJob }); |
| 117 | + await invoke("board.widget.put", { |
| 118 | + sessionKey: "session", |
| 119 | + name: "runner", |
| 120 | + content: { kind: "html", html: "runner" }, |
| 121 | + declared: { tools: ["cron.trigger:job-1"] }, |
| 122 | + }); |
| 123 | + await invoke("board.widget.grant", { |
| 124 | + sessionKey: "session", |
| 125 | + name: "runner", |
| 126 | + decision: "granted", |
| 127 | + revision: 1, |
| 128 | + instanceId: store.getSnapshot("session").widgets[0]?.instanceId, |
| 129 | + }); |
| 130 | + const board = await invoke("board.get", { sessionKey: "session" }); |
| 131 | + const snapshot = board.mock.calls[0]?.[1] as BoardSnapshot; |
| 132 | + const ticket = snapshot.widgets[0]?.viewTicket; |
| 133 | + |
| 134 | + const denied = await invoke("board.action", { |
| 135 | + ticket, |
| 136 | + action: "cron.trigger", |
| 137 | + jobId: "job-2", |
| 138 | + }); |
| 139 | + expect(denied.mock.calls[0]?.[0]).toBe(false); |
| 140 | + expect(triggerCronJob).not.toHaveBeenCalled(); |
| 141 | + |
| 142 | + const allowed = await invoke("board.action", { |
| 143 | + ticket, |
| 144 | + action: "cron.trigger", |
| 145 | + jobId: "job-1", |
| 146 | + }); |
| 147 | + expect(allowed.mock.calls[0]?.[1]).toEqual({ ok: true, jobId: "job-1" }); |
| 148 | + expect(triggerCronJob).toHaveBeenCalledWith("job-1", expect.any(Object)); |
| 149 | + }); |
| 150 | + |
| 151 | + it("caps board.event payloads and preserves Unicode at the notice boundary", async () => { |
| 152 | + const { invoke } = createHarness(); |
| 153 | + await invoke("board.widget.put", { |
| 154 | + sessionKey: "session", |
| 155 | + name: "counter", |
| 156 | + content: { kind: "html", html: "ok" }, |
| 157 | + }); |
| 158 | + const clippedCodeUnits = 500 - "[dashboard] ".length - " on widget counter".length - 1; |
| 159 | + // JSON's opening quote places the emoji across the legacy slice boundary. |
| 160 | + const payload = `${"x".repeat(clippedCodeUnits - 2)}😀tail`; |
| 161 | + await invoke("board.event", { sessionKey: "session", widget: "counter", payload }); |
| 162 | + const unicodeNotice = peekSystemEvents("agent:main:session")[0] ?? ""; |
| 163 | + expect(unicodeNotice.length).toBeLessThanOrEqual(500); |
| 164 | + expect(unicodeNotice).not.toContain(String.fromCharCode(0xd83d)); |
| 165 | + expect(unicodeNotice).toMatch(/… on widget counter$/u); |
| 166 | + await invoke("board.event", { |
| 167 | + sessionKey: "session", |
| 168 | + widget: "counter", |
| 169 | + payload: "x".repeat(1_000), |
| 170 | + }); |
| 171 | + expect(peekSystemEvents("agent:main:session")[1]).toHaveLength(500); |
| 172 | + const oversized = await invoke("board.event", { |
| 173 | + sessionKey: "session", |
| 174 | + widget: "counter", |
| 175 | + payload: "x".repeat(8_193), |
| 176 | + }); |
| 177 | + expect(oversized.mock.calls[0]?.[0]).toBe(false); |
| 178 | + }); |
| 179 | + |
| 180 | + it("keeps board state across the real sessions.reset handler", async () => { |
| 181 | + const sessionKey = "agent:main:board-reset-proof"; |
| 182 | + const stateDir = tempDirs.make("openclaw-board-reset-"); |
| 183 | + const env = { OPENCLAW_STATE_DIR: stateDir }; |
| 184 | + const database = openOpenClawAgentDatabase({ agentId: "main", env }); |
| 185 | + replaceSessionEntrySync( |
| 186 | + { agentId: "main", sessionKey, storePath: database.path }, |
| 187 | + { sessionId: "board-reset-proof", updatedAt: Date.now() }, |
| 188 | + ); |
| 189 | + const boardStore = new SqliteBoardStore({ |
| 190 | + resolveSession: () => ({ agentId: "main", sessionKey }), |
| 191 | + env, |
| 192 | + }); |
| 193 | + boardStore.putWidget({ |
| 194 | + sessionKey, |
| 195 | + name: "status", |
| 196 | + content: { kind: "html", html: "ok" }, |
| 197 | + }); |
| 198 | + const respond = vi.fn<RespondFn>(); |
| 199 | + await sessionMutationHandlers["sessions.reset"]!({ |
| 200 | + req: { type: "req", id: "reset", method: "sessions.reset", params: {} }, |
| 201 | + params: { key: sessionKey, reason: "reset" }, |
| 202 | + client: null, |
| 203 | + isWebchatConnect: () => false, |
| 204 | + respond, |
| 205 | + context: { |
| 206 | + broadcast: vi.fn(), |
| 207 | + getSessionEventSubscriberConnIds: () => new Set<string>(), |
| 208 | + } as unknown as GatewayRequestContext, |
| 209 | + }); |
| 210 | + expect(respond.mock.calls[0]?.[0]).toBe(true); |
| 211 | + expect(boardStore.getSnapshot(sessionKey).widgets).toHaveLength(1); |
| 212 | + }); |
| 213 | +}); |
0 commit comments