From 33edb9235deb77fabccf17c0334b684d4f3560f8 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 03:32:13 +0000 Subject: [PATCH] Reject whitespace cwd values for runtime path APIs Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.test.ts | 60 ++++++++++++++++++++++++++++ apps/t3/src/runtimeApiServer.ts | 25 +++--------- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index d2bf1c4d07..b79078e9f8 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -1175,6 +1175,36 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it("returns structured errors when shell.openInEditor target is whitespace", async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const response = await sendRequest( + client.socket, + client.nextMessage, + "shell-whitespace-path-1", + "shell.openInEditor", + { + cwd: " ", + editor: "cursor", + }, + ); + expect(response.ok).toBe(false); + if (response.ok) { + throw new Error("Expected whitespace shell target request to fail."); + } + expect(response.error?.code).toBe("request_failed"); + expect(response.error?.message).toContain("Editor target must be a non-empty path"); + + client.socket.close(); + }); + it("returns structured errors for invalid terminal.run payloads", async () => { const server = await startRuntimeApiServer({ port: 0, @@ -1751,6 +1781,36 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it("returns structured errors when terminal cwd is whitespace", async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const response = await sendRequest( + client.socket, + client.nextMessage, + "terminal-whitespace-cwd-1", + "terminal.run", + { + command: "pwd", + cwd: " ", + }, + ); + expect(response.ok).toBe(false); + if (response.ok) { + throw new Error("Expected terminal whitespace cwd response to fail."); + } + expect(response.error?.code).toBe("request_failed"); + expect(response.error?.message).toContain("Working directory must be a non-empty path"); + + 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 031f786194..09a3232a73 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -316,29 +316,14 @@ function resolveExistingDirectoryFromBase( label: string, baseDirectory: string, ): string { + if (typeof targetPath !== "string" || targetPath.trim().length === 0) { + throw new Error(`${label} must be a non-empty path.`); + } + const candidate = path.isAbsolute(targetPath) ? path.resolve(targetPath) : path.resolve(baseDirectory, targetPath); - let stats: fs.Stats; - try { - stats = fs.statSync(candidate); - } catch (error) { - const code = (error as NodeJS.ErrnoException | undefined)?.code; - if (code && code !== "ENOENT") { - throw new Error(`Failed to access ${label.toLowerCase()}: ${candidate} (${code})`, { - cause: error, - }); - } - throw new Error(`${label} does not exist: ${candidate}`, { - cause: error, - }); - } - - if (!stats.isDirectory()) { - throw new Error(`${label} is not a directory: ${candidate}`); - } - - return candidate; + return resolveExistingDirectory(candidate, label); } export async function startRuntimeApiServer(