|
1 | 1 | /** sessions_search visibility, bounds, redaction, and input tests. */ |
| 2 | +import path from "node:path"; |
2 | 3 | import { Value } from "typebox/value"; |
3 | | -import { describe, expect, it } from "vitest"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
| 6 | +import { |
| 7 | + applySessionStoreProjection, |
| 8 | + replaceSessionEntrySync, |
| 9 | +} from "../../config/sessions/session-accessor.js"; |
| 10 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
4 | 11 | import type { callGateway as gatewayCall } from "../../gateway/call.js"; |
| 12 | +import { createSessionVisibilityChecker } from "../../plugin-sdk/session-visibility.js"; |
5 | 13 | import { compactToolOutputHint } from "../tool-schema-hints.js"; |
6 | 14 | import { createSessionsSearchTool } from "./sessions-search-tool.js"; |
7 | 15 |
|
@@ -86,6 +94,8 @@ function createTool(params: { |
86 | 94 | } |
87 | 95 |
|
88 | 96 | describe("sessions_search tool", () => { |
| 97 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 98 | + |
89 | 99 | it("rejects a literal global target owned by another fixed-store agent", async () => { |
90 | 100 | const requests: CallGatewayRequest[] = []; |
91 | 101 | const tool = createTool({ |
@@ -419,4 +429,57 @@ describe("sessions_search tool", () => { |
419 | 429 | params: { agentId: "main", query: "text", sessionKeys: ["main"], limit: 25 }, |
420 | 430 | }); |
421 | 431 | }); |
| 432 | + |
| 433 | + it("rejects a scoped grant when the target incarnation changes before search", async () => { |
| 434 | + const requesterSessionKey = "agent:main:clickclack:discussion-race"; |
| 435 | + const targetSessionKey = "agent:main:main"; |
| 436 | + const expectedSessionId = "old-incarnation"; |
| 437 | + const storePath = path.join(tempDirs.make("openclaw-sessions-search-"), "sessions.sqlite"); |
| 438 | + await applySessionStoreProjection({ |
| 439 | + storePath, |
| 440 | + skipMaintenance: true, |
| 441 | + update: (store) => { |
| 442 | + store[targetSessionKey] = { sessionId: expectedSessionId, updatedAt: 1 }; |
| 443 | + return { persist: true, result: undefined }; |
| 444 | + }, |
| 445 | + }); |
| 446 | + const requests: CallGatewayRequest[] = []; |
| 447 | + const unregister = createSessionVisibilityChecker.registerScopedAccessProvider((request) => { |
| 448 | + if ( |
| 449 | + request.requesterSessionKey !== requesterSessionKey || |
| 450 | + request.targetSessionKey !== targetSessionKey |
| 451 | + ) { |
| 452 | + return undefined; |
| 453 | + } |
| 454 | + replaceSessionEntrySync( |
| 455 | + { storePath, sessionKey: targetSessionKey }, |
| 456 | + { sessionId: "replacement-incarnation", updatedAt: 2 }, |
| 457 | + ); |
| 458 | + return { expectedSessionId }; |
| 459 | + }); |
| 460 | + try { |
| 461 | + const tool = createSessionsSearchTool({ |
| 462 | + agentSessionKey: requesterSessionKey, |
| 463 | + sandboxed: true, |
| 464 | + config: { |
| 465 | + session: { store: storePath }, |
| 466 | + tools: { sessions: { visibility: "self" } }, |
| 467 | + agents: { defaults: { sandbox: { sessionToolsVisibility: "spawned" } } }, |
| 468 | + } as OpenClawConfig, |
| 469 | + callGateway: async <T = Record<string, unknown>>( |
| 470 | + request: CallGatewayRequest, |
| 471 | + ): Promise<T> => { |
| 472 | + requests.push(request); |
| 473 | + return { results: [hit({ sessionKey: targetSessionKey })] } as T; |
| 474 | + }, |
| 475 | + }); |
| 476 | + |
| 477 | + await expect( |
| 478 | + tool.execute("scoped-grant-race", { query: "text", sessionKey: targetSessionKey }), |
| 479 | + ).rejects.toThrow(`Session "${targetSessionKey}" changed after access was granted.`); |
| 480 | + expect(requests.some((request) => request.method === "sessions.search")).toBe(false); |
| 481 | + } finally { |
| 482 | + unregister(); |
| 483 | + } |
| 484 | + }); |
422 | 485 | }); |
0 commit comments