Skip to content

Commit d0f090e

Browse files
committed
test(agents): share exec preflight race helper
1 parent 93abf5e commit d0f090e

1 file changed

Lines changed: 42 additions & 58 deletions

File tree

‎src/agents/bash-tools.exec.script-preflight.test.ts‎

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,42 @@ afterEach(() => {
1515
__setFsSafeTestHooksForTest();
1616
});
1717

18+
async function expectSymlinkSwapDuringPreflightToAvoidErrors(params: {
19+
hookName: "afterPreOpenLstat" | "beforeOpen";
20+
callId: string;
21+
}) {
22+
await withTempDir("openclaw-exec-preflight-open-race-", async (parent) => {
23+
const workdir = path.join(parent, "workdir");
24+
const scriptPath = path.join(workdir, "script.js");
25+
const outsidePath = path.join(parent, "outside.js");
26+
await fs.mkdir(workdir, { recursive: true });
27+
await fs.writeFile(scriptPath, 'console.log("inside")', "utf-8");
28+
await fs.writeFile(outsidePath, 'console.log("$DM_JSON outside")', "utf-8");
29+
const scriptRealPath = await fs.realpath(scriptPath);
30+
31+
let swapped = false;
32+
__setFsSafeTestHooksForTest({
33+
[params.hookName]: async (target) => {
34+
if (swapped || path.resolve(target) !== scriptRealPath) {
35+
return;
36+
}
37+
await fs.rm(scriptPath, { force: true });
38+
await fs.symlink(outsidePath, scriptPath);
39+
swapped = true;
40+
},
41+
});
42+
43+
const tool = createExecTool({ host: "gateway", security: "full", ask: "off" });
44+
const result = await tool.execute(params.callId, {
45+
command: "node script.js",
46+
workdir,
47+
});
48+
const text = result.content.find((block) => block.type === "text")?.text ?? "";
49+
expect(swapped).toBe(true);
50+
expect(text).not.toMatch(/exec preflight:/);
51+
});
52+
}
53+
1854
describeNonWin("exec script preflight", () => {
1955
it("blocks shell env var injection tokens in python scripts before execution", async () => {
2056
await withTempDir("openclaw-exec-preflight-", async (tmp) => {
@@ -323,68 +359,16 @@ describeNonWin("exec script preflight", () => {
323359
});
324360

325361
it("does not trust a swapped script pathname between validation and read", async () => {
326-
await withTempDir("openclaw-exec-preflight-race-", async (parent) => {
327-
const workdir = path.join(parent, "workdir");
328-
const scriptPath = path.join(workdir, "script.js");
329-
const outsidePath = path.join(parent, "outside.js");
330-
await fs.mkdir(workdir, { recursive: true });
331-
await fs.writeFile(scriptPath, 'console.log("inside")', "utf-8");
332-
await fs.writeFile(outsidePath, 'console.log("$DM_JSON outside")', "utf-8");
333-
const scriptRealPath = await fs.realpath(scriptPath);
334-
335-
let swapped = false;
336-
__setFsSafeTestHooksForTest({
337-
afterPreOpenLstat: async (target) => {
338-
if (swapped || path.resolve(target) !== scriptRealPath) {
339-
return;
340-
}
341-
await fs.rm(scriptPath, { force: true });
342-
await fs.symlink(outsidePath, scriptPath);
343-
swapped = true;
344-
},
345-
});
346-
347-
const tool = createExecTool({ host: "gateway", security: "full", ask: "off" });
348-
const result = await tool.execute("call-swapped-pathname", {
349-
command: "node script.js",
350-
workdir,
351-
});
352-
const text = result.content.find((block) => block.type === "text")?.text ?? "";
353-
expect(swapped).toBe(true);
354-
expect(text).not.toMatch(/exec preflight:/);
362+
await expectSymlinkSwapDuringPreflightToAvoidErrors({
363+
hookName: "afterPreOpenLstat",
364+
callId: "call-swapped-pathname",
355365
});
356366
});
357367

358368
it("handles pre-open symlink swaps without surfacing preflight errors", async () => {
359-
await withTempDir("openclaw-exec-preflight-open-race-", async (parent) => {
360-
const workdir = path.join(parent, "workdir");
361-
const scriptPath = path.join(workdir, "script.js");
362-
const outsidePath = path.join(parent, "outside.js");
363-
await fs.mkdir(workdir, { recursive: true });
364-
await fs.writeFile(scriptPath, 'console.log("inside")', "utf-8");
365-
await fs.writeFile(outsidePath, 'console.log("$DM_JSON outside")', "utf-8");
366-
const scriptRealPath = await fs.realpath(scriptPath);
367-
368-
let swapped = false;
369-
__setFsSafeTestHooksForTest({
370-
beforeOpen: async (target) => {
371-
if (swapped || path.resolve(target) !== scriptRealPath) {
372-
return;
373-
}
374-
await fs.rm(scriptPath, { force: true });
375-
await fs.symlink(outsidePath, scriptPath);
376-
swapped = true;
377-
},
378-
});
379-
380-
const tool = createExecTool({ host: "gateway", security: "full", ask: "off" });
381-
const result = await tool.execute("call-pre-open-swapped-pathname", {
382-
command: "node script.js",
383-
workdir,
384-
});
385-
const text = result.content.find((block) => block.type === "text")?.text ?? "";
386-
expect(swapped).toBe(true);
387-
expect(text).not.toMatch(/exec preflight:/);
369+
await expectSymlinkSwapDuringPreflightToAvoidErrors({
370+
hookName: "beforeOpen",
371+
callId: "call-pre-open-swapped-pathname",
388372
});
389373
});
390374

0 commit comments

Comments
 (0)