test(pi-tools): share safeBins e2e setup and teardown

This commit is contained in:
Peter Steinberger
2026-02-21 23:50:22 +00:00
parent ba23d2b1fe
commit a97992fcf2

View File

@@ -118,17 +118,29 @@ async function createSafeBinsExecTool(params: {
return { tmpDir, execTool: execTool as ExecTool }; return { tmpDir, execTool: execTool as ExecTool };
} }
describe("createOpenClawCodingTools safeBins", () => { async function withSafeBinsExecTool(
it("threads tools.exec.safeBins into exec allowlist checks", async () => { params: Parameters<typeof createSafeBinsExecTool>[0],
run: (ctx: Awaited<ReturnType<typeof createSafeBinsExecTool>>) => Promise<void>,
) {
if (process.platform === "win32") { if (process.platform === "win32") {
return; return;
} }
const ctx = await createSafeBinsExecTool(params);
try {
await run(ctx);
} finally {
fs.rmSync(ctx.tmpDir, { recursive: true, force: true });
}
}
const { tmpDir, execTool } = await createSafeBinsExecTool({ describe("createOpenClawCodingTools safeBins", () => {
it("threads tools.exec.safeBins into exec allowlist checks", async () => {
await withSafeBinsExecTool(
{
tmpPrefix: "openclaw-safe-bins-", tmpPrefix: "openclaw-safe-bins-",
safeBins: ["echo"], safeBins: ["echo"],
}); },
async ({ tmpDir, execTool }) => {
const marker = `safe-bins-${Date.now()}`; const marker = `safe-bins-${Date.now()}`;
const result = await withEnvAsync( const result = await withEnvAsync(
{ OPENCLAW_SHELL_ENV_TIMEOUT_MS: "1000" }, { OPENCLAW_SHELL_ENV_TIMEOUT_MS: "1000" },
@@ -143,19 +155,18 @@ describe("createOpenClawCodingTools safeBins", () => {
const resultDetails = result.details as { status?: string }; const resultDetails = result.details as { status?: string };
expect(resultDetails.status).toBe("completed"); expect(resultDetails.status).toBe("completed");
expect(text).toContain(marker); expect(text).toContain(marker);
},
);
}); });
it("does not allow env var expansion to smuggle file args via safeBins", async () => { it("does not allow env var expansion to smuggle file args via safeBins", async () => {
if (process.platform === "win32") { await withSafeBinsExecTool(
return; {
}
const { tmpDir, execTool } = await createSafeBinsExecTool({
tmpPrefix: "openclaw-safe-bins-expand-", tmpPrefix: "openclaw-safe-bins-expand-",
safeBins: ["head", "wc"], safeBins: ["head", "wc"],
files: [{ name: "secret.txt", contents: "TOP_SECRET\n" }], files: [{ name: "secret.txt", contents: "TOP_SECRET\n" }],
}); },
async ({ tmpDir, execTool }) => {
await expect( await expect(
execTool.execute("call1", { execTool.execute("call1", {
command: "head $FOO ; wc -l", command: "head $FOO ; wc -l",
@@ -163,19 +174,18 @@ describe("createOpenClawCodingTools safeBins", () => {
env: { FOO: "secret.txt" }, env: { FOO: "secret.txt" },
}), }),
).rejects.toThrow("exec denied: allowlist miss"); ).rejects.toThrow("exec denied: allowlist miss");
},
);
}); });
it("does not leak file existence from sort output flags", async () => { it("does not leak file existence from sort output flags", async () => {
if (process.platform === "win32") { await withSafeBinsExecTool(
return; {
}
const { tmpDir, execTool } = await createSafeBinsExecTool({
tmpPrefix: "openclaw-safe-bins-oracle-", tmpPrefix: "openclaw-safe-bins-oracle-",
safeBins: ["sort"], safeBins: ["sort"],
files: [{ name: "existing.txt", contents: "x\n" }], files: [{ name: "existing.txt", contents: "x\n" }],
}); },
async ({ tmpDir, execTool }) => {
const run = async (command: string) => { const run = async (command: string) => {
try { try {
const result = await execTool.execute("call-oracle", { command, workdir: tmpDir }); const result = await execTool.execute("call-oracle", { command, workdir: tmpDir });
@@ -190,18 +200,17 @@ describe("createOpenClawCodingTools safeBins", () => {
const existing = await run("sort -o existing.txt"); const existing = await run("sort -o existing.txt");
const missing = await run("sort -o missing.txt"); const missing = await run("sort -o missing.txt");
expect(existing).toEqual(missing); expect(existing).toEqual(missing);
},
);
}); });
it("blocks sort output flags from writing files via safeBins", async () => { it("blocks sort output flags from writing files via safeBins", async () => {
if (process.platform === "win32") { await withSafeBinsExecTool(
return; {
}
const { tmpDir, execTool } = await createSafeBinsExecTool({
tmpPrefix: "openclaw-safe-bins-sort-", tmpPrefix: "openclaw-safe-bins-sort-",
safeBins: ["sort"], safeBins: ["sort"],
}); },
async ({ tmpDir, execTool }) => {
const cases = [ const cases = [
{ command: "sort -oblocked-short.txt", target: "blocked-short.txt" }, { command: "sort -oblocked-short.txt", target: "blocked-short.txt" },
{ command: "sort --output=blocked-long.txt", target: "blocked-long.txt" }, { command: "sort --output=blocked-long.txt", target: "blocked-long.txt" },
@@ -216,37 +225,35 @@ describe("createOpenClawCodingTools safeBins", () => {
).rejects.toThrow("exec denied: allowlist miss"); ).rejects.toThrow("exec denied: allowlist miss");
expect(fs.existsSync(path.join(tmpDir, testCase.target))).toBe(false); expect(fs.existsSync(path.join(tmpDir, testCase.target))).toBe(false);
} }
},
);
}); });
it("blocks sort --compress-program from bypassing safeBins", async () => { it("blocks sort --compress-program from bypassing safeBins", async () => {
if (process.platform === "win32") { await withSafeBinsExecTool(
return; {
}
const { tmpDir, execTool } = await createSafeBinsExecTool({
tmpPrefix: "openclaw-safe-bins-sort-compress-", tmpPrefix: "openclaw-safe-bins-sort-compress-",
safeBins: ["sort"], safeBins: ["sort"],
}); },
async ({ tmpDir, execTool }) => {
await expect( await expect(
execTool.execute("call1", { execTool.execute("call1", {
command: "sort --compress-program=sh", command: "sort --compress-program=sh",
workdir: tmpDir, workdir: tmpDir,
}), }),
).rejects.toThrow("exec denied: allowlist miss"); ).rejects.toThrow("exec denied: allowlist miss");
},
);
}); });
it("blocks shell redirection metacharacters in safeBins mode", async () => { it("blocks shell redirection metacharacters in safeBins mode", async () => {
if (process.platform === "win32") { await withSafeBinsExecTool(
return; {
}
const { tmpDir, execTool } = await createSafeBinsExecTool({
tmpPrefix: "openclaw-safe-bins-redirect-", tmpPrefix: "openclaw-safe-bins-redirect-",
safeBins: ["head"], safeBins: ["head"],
files: [{ name: "source.txt", contents: "line1\nline2\n" }], files: [{ name: "source.txt", contents: "line1\nline2\n" }],
}); },
async ({ tmpDir, execTool }) => {
await expect( await expect(
execTool.execute("call1", { execTool.execute("call1", {
command: "head -n 1 source.txt > blocked-redirect.txt", command: "head -n 1 source.txt > blocked-redirect.txt",
@@ -254,24 +261,25 @@ describe("createOpenClawCodingTools safeBins", () => {
}), }),
).rejects.toThrow("exec denied: allowlist miss"); ).rejects.toThrow("exec denied: allowlist miss");
expect(fs.existsSync(path.join(tmpDir, "blocked-redirect.txt"))).toBe(false); expect(fs.existsSync(path.join(tmpDir, "blocked-redirect.txt"))).toBe(false);
},
);
}); });
it("blocks grep recursive flags from reading cwd via safeBins", async () => { it("blocks grep recursive flags from reading cwd via safeBins", async () => {
if (process.platform === "win32") { await withSafeBinsExecTool(
return; {
}
const { tmpDir, execTool } = await createSafeBinsExecTool({
tmpPrefix: "openclaw-safe-bins-grep-", tmpPrefix: "openclaw-safe-bins-grep-",
safeBins: ["grep"], safeBins: ["grep"],
files: [{ name: "secret.txt", contents: "SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK\n" }], files: [{ name: "secret.txt", contents: "SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK\n" }],
}); },
async ({ tmpDir, execTool }) => {
await expect( await expect(
execTool.execute("call1", { execTool.execute("call1", {
command: "grep -R SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK", command: "grep -R SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK",
workdir: tmpDir, workdir: tmpDir,
}), }),
).rejects.toThrow("exec denied: allowlist miss"); ).rejects.toThrow("exec denied: allowlist miss");
},
);
}); });
}); });