fix: harden lobster plugin tool (#1152) (thanks @vignesh07)

This commit is contained in:
Peter Steinberger committed 2026-01-22 02:33:26 +00:00
1 parent f582fd6b59
commit d75aa6cc43
8 files changed
+159 -17

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
{
"id": "lobster",
"configSchema": {
"type": "object",
"additionalProperties": false,
"properties": {}
}
}
+87 -2
View File
@@ -8,13 +8,26 @@ import type { ClawdbotPluginApi, ClawdbotPluginToolContext } from "../../../src/
import { createLobsterTool } from "./lobster-tool.js";
async function writeFakeLobster(params: {
payload: unknown;
payload?: unknown;
stdout?: string;
stderr?: string;
exitCode?: number;
delayMs?: number;
}) {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-lobster-plugin-"));
const binPath = path.join(dir, "lobster");
const payload = params.stdout ?? JSON.stringify(params.payload ?? null);
const delay = Math.max(0, params.delayMs ?? 0);
const exitCode = Number.isFinite(params.exitCode) ? params.exitCode : 0;
const stderr = params.stderr ? String(params.stderr) : "";
const file = `#!/usr/bin/env node\n` +
`process.stdout.write(JSON.stringify(${JSON.stringify(params.payload)}));\n`;
`setTimeout(() => {\n` +
` if (${JSON.stringify(stderr)}.length) process.stderr.write(${JSON.stringify(stderr)});\n` +
` process.stdout.write(${JSON.stringify(payload)});\n` +
` process.exit(${exitCode});\n` +
`}, ${delay});\n`;
await fs.writeFile(binPath, file, { encoding: "utf8", mode: 0o755 });
return { dir, binPath };
@@ -99,6 +112,78 @@ describe("lobster plugin tool", () => {
).rejects.toThrow(/invalid JSON/);
});
it("errors on timeout", async () => {
const fake = await writeFakeLobster({
payload: { ok: true, status: "ok", output: [], requiresApproval: null },
delayMs: 250,
});
const tool = createLobsterTool(fakeApi());
await expect(
tool.execute("call4", {
action: "run",
pipeline: "noop",
lobsterPath: fake.binPath,
timeoutMs: 50,
}),
).rejects.toThrow(/timed out/);
});
it("caps stdout", async () => {
const fake = await writeFakeLobster({
stdout: "x".repeat(2000),
});
const tool = createLobsterTool(fakeApi());
await expect(
tool.execute("call5", {
action: "run",
pipeline: "noop",
lobsterPath: fake.binPath,
maxStdoutBytes: 128,
}),
).rejects.toThrow(/maxStdoutBytes/);
});
it("returns stderr in non-zero exit errors", async () => {
const fake = await writeFakeLobster({
stdout: "",
stderr: "boom",
exitCode: 2,
});
const tool = createLobsterTool(fakeApi());
await expect(
tool.execute("call6", {
action: "run",
pipeline: "noop",
lobsterPath: fake.binPath,
}),
).rejects.toThrow(/boom/);
});
it("aborts via signal", async () => {
const fake = await writeFakeLobster({
payload: { ok: true, status: "ok", output: [], requiresApproval: null },
delayMs: 200,
});
const tool = createLobsterTool(fakeApi());
const controller = new AbortController();
const promise = tool.execute(
"call7",
{
action: "run",
pipeline: "noop",
lobsterPath: fake.binPath,
},
controller.signal,
);
controller.abort();
await expect(promise).rejects.toThrow(/aborted/);
});
it("can be gated off in sandboxed contexts", async () => {
const api = fakeApi();
const factoryTool = (ctx: ClawdbotPluginToolContext) => {
+34 -8
View File
@@ -35,12 +35,18 @@ async function runLobsterSubprocess(params: {
cwd: string;
timeoutMs: number;
maxStdoutBytes: number;
signal?: AbortSignal;
}) {
const { execPath, argv, cwd } = params;
const timeoutMs = Math.max(200, params.timeoutMs);
const maxStdoutBytes = Math.max(1024, params.maxStdoutBytes);
return await new Promise<{ stdout: string }>((resolve, reject) => {
if (params.signal?.aborted) {
reject(new Error("lobster subprocess aborted"));
return;
}
const child = spawn(execPath, argv, {
cwd,
stdio: ["ignore", "pipe", "pipe"],
@@ -53,6 +59,25 @@ async function runLobsterSubprocess(params: {
let stdout = "";
let stdoutBytes = 0;
let stderr = "";
let settled = false;
const finish = (fn: () => void) => {
if (settled) return;
settled = true;
clearTimeout(timer);
if (params.signal) params.signal.removeEventListener("abort", onAbort);
fn();
};
const onAbort = () => {
try {
child.kill("SIGKILL");
} finally {
finish(() => reject(new Error("lobster subprocess aborted")));
}
};
params.signal?.addEventListener("abort", onAbort);
child.stdout?.setEncoding("utf8");
child.stderr?.setEncoding("utf8");
@@ -64,7 +89,7 @@ async function runLobsterSubprocess(params: {
try {
child.kill("SIGKILL");
} finally {
reject(new Error("lobster output exceeded maxStdoutBytes"));
finish(() => reject(new Error("lobster output exceeded maxStdoutBytes")));
}
return;
}
@@ -79,22 +104,22 @@ async function runLobsterSubprocess(params: {
try {
child.kill("SIGKILL");
} finally {
reject(new Error("lobster subprocess timed out"));
finish(() => reject(new Error("lobster subprocess timed out")));
}
}, timeoutMs);
child.once("error", (err) => {
clearTimeout(timer);
reject(err);
finish(() => reject(err));
});
child.once("exit", (code) => {
clearTimeout(timer);
if (code !== 0) {
reject(new Error(`lobster failed (${code ?? "?"}): ${stderr.trim() || stdout.trim()}`));
finish(() =>
reject(new Error(`lobster failed (${code ?? "?"}): ${stderr.trim() || stdout.trim()}`)),
);
return;
}
resolve({ stdout });
finish(() => resolve({ stdout }));
});
});
}
@@ -135,7 +160,7 @@ export function createLobsterTool(api: ClawdbotPluginApi) {
timeoutMs: Type.Optional(Type.Number()),
maxStdoutBytes: Type.Optional(Type.Number()),
}),
async execute(_id: string, params: Record<string, unknown>) {
async execute(_id: string, params: Record<string, unknown>, signal?: AbortSignal) {
const action = String(params.action || "").trim();
if (!action) throw new Error("action required");
@@ -172,6 +197,7 @@ export function createLobsterTool(api: ClawdbotPluginApi) {
cwd,
timeoutMs,
maxStdoutBytes,
signal,
});
const envelope = parseEnvelope(stdout);