Validate terminal cwd exists before command execution
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
ead26b4c26
commit
e018d55032
2 files changed
+53
-1
No files matched your search
@@ -1212,6 +1212,37 @@ describe("runtimeApiServer", () => {
|
||||
client.socket.close();
|
||||
});
|
||||
|
||||
it("returns structured errors when terminal cwd does not exist", async () => {
|
||||
const server = await startRuntimeApiServer({
|
||||
port: 0,
|
||||
launchCwd: process.cwd(),
|
||||
});
|
||||
servers.push(server);
|
||||
|
||||
const client = await connectClient(server.wsUrl);
|
||||
await client.nextMessage();
|
||||
|
||||
const missingCwd = path.join(process.cwd(), `missing-cwd-${Date.now()}`);
|
||||
const response = await sendRequest(
|
||||
client.socket,
|
||||
client.nextMessage,
|
||||
"terminal-missing-cwd-1",
|
||||
"terminal.run",
|
||||
{
|
||||
command: "pwd",
|
||||
cwd: missingCwd,
|
||||
},
|
||||
);
|
||||
expect(response.ok).toBe(false);
|
||||
if (response.ok) {
|
||||
throw new Error("Expected terminal missing cwd response to fail.");
|
||||
}
|
||||
expect(response.error?.code).toBe("request_failed");
|
||||
expect(response.error?.message).toContain("Working directory does not exist");
|
||||
|
||||
client.socket.close();
|
||||
});
|
||||
|
||||
it("returns non-zero terminal exit codes without request failure", async () => {
|
||||
const server = await startRuntimeApiServer({
|
||||
port: 0,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { z } from "zod";
|
||||
@@ -249,6 +250,26 @@ async function pickFolder(): Promise<string | null> {
|
||||
}
|
||||
|
||||
async function runTerminalCommand(parsed: z.infer<typeof terminalCommandInputSchema>) {
|
||||
const resolvedCwd = (() => {
|
||||
if (!parsed.cwd) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const candidate = path.resolve(parsed.cwd);
|
||||
let stats: fs.Stats;
|
||||
try {
|
||||
stats = fs.statSync(candidate);
|
||||
} catch {
|
||||
throw new Error(`Working directory does not exist: ${candidate}`);
|
||||
}
|
||||
|
||||
if (!stats.isDirectory()) {
|
||||
throw new Error(`Working directory is not a directory: ${candidate}`);
|
||||
}
|
||||
|
||||
return candidate;
|
||||
})();
|
||||
|
||||
const shellPath =
|
||||
process.platform === "win32"
|
||||
? (process.env.ComSpec ?? "cmd.exe")
|
||||
@@ -264,7 +285,7 @@ async function runTerminalCommand(parsed: z.infer<typeof terminalCommandInputSch
|
||||
timedOut: boolean;
|
||||
}>((resolve, reject) => {
|
||||
const child = spawn(shellPath, args, {
|
||||
cwd: parsed.cwd,
|
||||
cwd: resolvedCwd,
|
||||
env: process.env,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user