mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 02:01:25 +00:00
fix(agents): map container workdir paths in workspace guard
Co-authored-by: Explorer1092 <32663226+Explorer1092@users.noreply.github.com>
This commit is contained in:
76
src/agents/pi-tools.read.workspace-root-guard.test.ts
Normal file
76
src/agents/pi-tools.read.workspace-root-guard.test.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import path from "node:path";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { wrapToolWorkspaceRootGuardWithOptions } from "./pi-tools.read.js";
|
||||
import type { AnyAgentTool } from "./pi-tools.types.js";
|
||||
|
||||
const assertSandboxPath = vi.fn(async () => ({ resolved: "/tmp/root", relative: "" }));
|
||||
|
||||
vi.mock("./sandbox-paths.js", () => ({
|
||||
assertSandboxPath: (...args: unknown[]) => assertSandboxPath(...args),
|
||||
}));
|
||||
|
||||
function createToolHarness() {
|
||||
const execute = vi.fn(async () => ({
|
||||
content: [{ type: "text", text: "ok" }],
|
||||
}));
|
||||
const tool = {
|
||||
name: "read",
|
||||
description: "test tool",
|
||||
inputSchema: { type: "object", properties: {} },
|
||||
execute,
|
||||
} as unknown as AnyAgentTool;
|
||||
return { execute, tool };
|
||||
}
|
||||
|
||||
describe("wrapToolWorkspaceRootGuardWithOptions", () => {
|
||||
const root = "/tmp/root";
|
||||
|
||||
beforeEach(() => {
|
||||
assertSandboxPath.mockClear();
|
||||
});
|
||||
|
||||
it("maps container workspace paths to host workspace root", async () => {
|
||||
const { tool } = createToolHarness();
|
||||
const wrapped = wrapToolWorkspaceRootGuardWithOptions(tool, root, {
|
||||
containerWorkdir: "/workspace",
|
||||
});
|
||||
|
||||
await wrapped.execute("tc1", { path: "/workspace/docs/readme.md" });
|
||||
|
||||
expect(assertSandboxPath).toHaveBeenCalledWith({
|
||||
filePath: path.resolve(root, "docs", "readme.md"),
|
||||
cwd: root,
|
||||
root,
|
||||
});
|
||||
});
|
||||
|
||||
it("maps file:// container workspace paths to host workspace root", async () => {
|
||||
const { tool } = createToolHarness();
|
||||
const wrapped = wrapToolWorkspaceRootGuardWithOptions(tool, root, {
|
||||
containerWorkdir: "/workspace",
|
||||
});
|
||||
|
||||
await wrapped.execute("tc2", { path: "file:///workspace/docs/readme.md" });
|
||||
|
||||
expect(assertSandboxPath).toHaveBeenCalledWith({
|
||||
filePath: path.resolve(root, "docs", "readme.md"),
|
||||
cwd: root,
|
||||
root,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not remap absolute paths outside the configured container workdir", async () => {
|
||||
const { tool } = createToolHarness();
|
||||
const wrapped = wrapToolWorkspaceRootGuardWithOptions(tool, root, {
|
||||
containerWorkdir: "/workspace",
|
||||
});
|
||||
|
||||
await wrapped.execute("tc3", { path: "/workspace-two/secret.txt" });
|
||||
|
||||
expect(assertSandboxPath).toHaveBeenCalledWith({
|
||||
filePath: "/workspace-two/secret.txt",
|
||||
cwd: root,
|
||||
root,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user