mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 11:24:58 +00:00
test(pi-tools): share safeBins e2e setup and teardown
This commit is contained in:
@@ -118,160 +118,168 @@ async function createSafeBinsExecTool(params: {
|
|||||||
return { tmpDir, execTool: execTool as ExecTool };
|
return { tmpDir, execTool: execTool as ExecTool };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function withSafeBinsExecTool(
|
||||||
|
params: Parameters<typeof createSafeBinsExecTool>[0],
|
||||||
|
run: (ctx: Awaited<ReturnType<typeof createSafeBinsExecTool>>) => Promise<void>,
|
||||||
|
) {
|
||||||
|
if (process.platform === "win32") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const ctx = await createSafeBinsExecTool(params);
|
||||||
|
try {
|
||||||
|
await run(ctx);
|
||||||
|
} finally {
|
||||||
|
fs.rmSync(ctx.tmpDir, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("createOpenClawCodingTools safeBins", () => {
|
describe("createOpenClawCodingTools safeBins", () => {
|
||||||
it("threads tools.exec.safeBins into exec allowlist checks", async () => {
|
it("threads tools.exec.safeBins into exec allowlist checks", async () => {
|
||||||
if (process.platform === "win32") {
|
await withSafeBinsExecTool(
|
||||||
return;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-",
|
||||||
|
safeBins: ["echo"],
|
||||||
|
},
|
||||||
|
async ({ tmpDir, execTool }) => {
|
||||||
|
const marker = `safe-bins-${Date.now()}`;
|
||||||
|
const result = await withEnvAsync(
|
||||||
|
{ OPENCLAW_SHELL_ENV_TIMEOUT_MS: "1000" },
|
||||||
|
async () =>
|
||||||
|
await execTool.execute("call1", {
|
||||||
|
command: `echo ${marker}`,
|
||||||
|
workdir: tmpDir,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const text = result.content.find((content) => content.type === "text")?.text ?? "";
|
||||||
|
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
const resultDetails = result.details as { status?: string };
|
||||||
tmpPrefix: "openclaw-safe-bins-",
|
expect(resultDetails.status).toBe("completed");
|
||||||
safeBins: ["echo"],
|
expect(text).toContain(marker);
|
||||||
});
|
},
|
||||||
|
|
||||||
const marker = `safe-bins-${Date.now()}`;
|
|
||||||
const result = await withEnvAsync(
|
|
||||||
{ OPENCLAW_SHELL_ENV_TIMEOUT_MS: "1000" },
|
|
||||||
async () =>
|
|
||||||
await execTool.execute("call1", {
|
|
||||||
command: `echo ${marker}`,
|
|
||||||
workdir: tmpDir,
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
const text = result.content.find((content) => content.type === "text")?.text ?? "";
|
|
||||||
|
|
||||||
const resultDetails = result.details as { status?: string };
|
|
||||||
expect(resultDetails.status).toBe("completed");
|
|
||||||
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;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-expand-",
|
||||||
|
safeBins: ["head", "wc"],
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
files: [{ name: "secret.txt", contents: "TOP_SECRET\n" }],
|
||||||
tmpPrefix: "openclaw-safe-bins-expand-",
|
},
|
||||||
safeBins: ["head", "wc"],
|
async ({ tmpDir, execTool }) => {
|
||||||
files: [{ name: "secret.txt", contents: "TOP_SECRET\n" }],
|
await expect(
|
||||||
});
|
execTool.execute("call1", {
|
||||||
|
command: "head $FOO ; wc -l",
|
||||||
await expect(
|
workdir: tmpDir,
|
||||||
execTool.execute("call1", {
|
env: { FOO: "secret.txt" },
|
||||||
command: "head $FOO ; wc -l",
|
}),
|
||||||
workdir: tmpDir,
|
).rejects.toThrow("exec denied: allowlist miss");
|
||||||
env: { FOO: "secret.txt" },
|
},
|
||||||
}),
|
);
|
||||||
).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;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-oracle-",
|
||||||
|
safeBins: ["sort"],
|
||||||
|
files: [{ name: "existing.txt", contents: "x\n" }],
|
||||||
|
},
|
||||||
|
async ({ tmpDir, execTool }) => {
|
||||||
|
const run = async (command: string) => {
|
||||||
|
try {
|
||||||
|
const result = await execTool.execute("call-oracle", { command, workdir: tmpDir });
|
||||||
|
const text = result.content.find((content) => content.type === "text")?.text ?? "";
|
||||||
|
const resultDetails = result.details as { status?: string };
|
||||||
|
return { kind: "result" as const, status: resultDetails.status, text };
|
||||||
|
} catch (err) {
|
||||||
|
return { kind: "error" as const, message: String(err) };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
const existing = await run("sort -o existing.txt");
|
||||||
tmpPrefix: "openclaw-safe-bins-oracle-",
|
const missing = await run("sort -o missing.txt");
|
||||||
safeBins: ["sort"],
|
expect(existing).toEqual(missing);
|
||||||
files: [{ name: "existing.txt", contents: "x\n" }],
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
const run = async (command: string) => {
|
|
||||||
try {
|
|
||||||
const result = await execTool.execute("call-oracle", { command, workdir: tmpDir });
|
|
||||||
const text = result.content.find((content) => content.type === "text")?.text ?? "";
|
|
||||||
const resultDetails = result.details as { status?: string };
|
|
||||||
return { kind: "result" as const, status: resultDetails.status, text };
|
|
||||||
} catch (err) {
|
|
||||||
return { kind: "error" as const, message: String(err) };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const existing = await run("sort -o existing.txt");
|
|
||||||
const missing = await run("sort -o missing.txt");
|
|
||||||
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;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-sort-",
|
||||||
|
safeBins: ["sort"],
|
||||||
|
},
|
||||||
|
async ({ tmpDir, execTool }) => {
|
||||||
|
const cases = [
|
||||||
|
{ command: "sort -oblocked-short.txt", target: "blocked-short.txt" },
|
||||||
|
{ command: "sort --output=blocked-long.txt", target: "blocked-long.txt" },
|
||||||
|
] as const;
|
||||||
|
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
for (const [index, testCase] of cases.entries()) {
|
||||||
tmpPrefix: "openclaw-safe-bins-sort-",
|
await expect(
|
||||||
safeBins: ["sort"],
|
execTool.execute(`call${index + 1}`, {
|
||||||
});
|
command: testCase.command,
|
||||||
|
workdir: tmpDir,
|
||||||
const cases = [
|
}),
|
||||||
{ command: "sort -oblocked-short.txt", target: "blocked-short.txt" },
|
).rejects.toThrow("exec denied: allowlist miss");
|
||||||
{ command: "sort --output=blocked-long.txt", target: "blocked-long.txt" },
|
expect(fs.existsSync(path.join(tmpDir, testCase.target))).toBe(false);
|
||||||
] as const;
|
}
|
||||||
|
},
|
||||||
for (const [index, testCase] of cases.entries()) {
|
);
|
||||||
await expect(
|
|
||||||
execTool.execute(`call${index + 1}`, {
|
|
||||||
command: testCase.command,
|
|
||||||
workdir: tmpDir,
|
|
||||||
}),
|
|
||||||
).rejects.toThrow("exec denied: allowlist miss");
|
|
||||||
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;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-sort-compress-",
|
||||||
|
safeBins: ["sort"],
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
},
|
||||||
tmpPrefix: "openclaw-safe-bins-sort-compress-",
|
async ({ tmpDir, execTool }) => {
|
||||||
safeBins: ["sort"],
|
await expect(
|
||||||
});
|
execTool.execute("call1", {
|
||||||
|
command: "sort --compress-program=sh",
|
||||||
await expect(
|
workdir: tmpDir,
|
||||||
execTool.execute("call1", {
|
}),
|
||||||
command: "sort --compress-program=sh",
|
).rejects.toThrow("exec denied: allowlist miss");
|
||||||
workdir: tmpDir,
|
},
|
||||||
}),
|
);
|
||||||
).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;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-redirect-",
|
||||||
|
safeBins: ["head"],
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
files: [{ name: "source.txt", contents: "line1\nline2\n" }],
|
||||||
tmpPrefix: "openclaw-safe-bins-redirect-",
|
},
|
||||||
safeBins: ["head"],
|
async ({ tmpDir, execTool }) => {
|
||||||
files: [{ name: "source.txt", contents: "line1\nline2\n" }],
|
await expect(
|
||||||
});
|
execTool.execute("call1", {
|
||||||
|
command: "head -n 1 source.txt > blocked-redirect.txt",
|
||||||
await expect(
|
workdir: tmpDir,
|
||||||
execTool.execute("call1", {
|
}),
|
||||||
command: "head -n 1 source.txt > blocked-redirect.txt",
|
).rejects.toThrow("exec denied: allowlist miss");
|
||||||
workdir: tmpDir,
|
expect(fs.existsSync(path.join(tmpDir, "blocked-redirect.txt"))).toBe(false);
|
||||||
}),
|
},
|
||||||
).rejects.toThrow("exec denied: allowlist miss");
|
);
|
||||||
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;
|
{
|
||||||
}
|
tmpPrefix: "openclaw-safe-bins-grep-",
|
||||||
|
safeBins: ["grep"],
|
||||||
const { tmpDir, execTool } = await createSafeBinsExecTool({
|
files: [{ name: "secret.txt", contents: "SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK\n" }],
|
||||||
tmpPrefix: "openclaw-safe-bins-grep-",
|
},
|
||||||
safeBins: ["grep"],
|
async ({ tmpDir, execTool }) => {
|
||||||
files: [{ name: "secret.txt", contents: "SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK\n" }],
|
await expect(
|
||||||
});
|
execTool.execute("call1", {
|
||||||
|
command: "grep -R SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK",
|
||||||
await expect(
|
workdir: tmpDir,
|
||||||
execTool.execute("call1", {
|
}),
|
||||||
command: "grep -R SAFE_BINS_RECURSIVE_SHOULD_NOT_LEAK",
|
).rejects.toThrow("exec denied: allowlist miss");
|
||||||
workdir: tmpDir,
|
},
|
||||||
}),
|
);
|
||||||
).rejects.toThrow("exec denied: allowlist miss");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user