From e018d550324c6218223aa640ea42aba2e7a7b5fe Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 00:57:43 +0000 Subject: [PATCH] Validate terminal cwd exists before command execution Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.test.ts | 31 ++++++++++++++++++++++++++++ apps/t3/src/runtimeApiServer.ts | 23 ++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index 98bcdf68c4..fbca45b630 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -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, diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 124cc538b2..d316e482ca 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -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 { } async function runTerminalCommand(parsed: z.infer) { + 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((resolve, reject) => { const child = spawn(shellPath, args, { - cwd: parsed.cwd, + cwd: resolvedCwd, env: process.env, stdio: ["ignore", "pipe", "pipe"], });