From b54a6bc127b92ba76d7ba4fb6f515f4b88153020 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 01:03:02 +0000 Subject: [PATCH] Validate shell editor targets are directories Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.test.ts | 64 ++++++++++++++++++++++++++++ apps/t3/src/runtimeApiServer.ts | 15 ++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index 65072d5d10..08487c661c 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -870,6 +870,70 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it("returns structured errors when shell.openInEditor target is missing", async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const missingPath = path.join(process.cwd(), `missing-editor-target-${Date.now()}`); + const response = await sendRequest( + client.socket, + client.nextMessage, + "shell-missing-path-1", + "shell.openInEditor", + { + cwd: missingPath, + editor: "file-manager", + }, + ); + expect(response.ok).toBe(false); + if (response.ok) { + throw new Error("Expected missing shell path request to fail."); + } + expect(response.error?.code).toBe("request_failed"); + expect(response.error?.message).toContain("Editor target does not exist"); + + client.socket.close(); + }); + + it("returns structured errors when shell.openInEditor target is not a directory", async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const tempDir = mkdtempSync(path.join(os.tmpdir(), "t3-shell-target-file-")); + const filePath = path.join(tempDir, "target.txt"); + writeFileSync(filePath, "not-a-dir", "utf8"); + const response = await sendRequest( + client.socket, + client.nextMessage, + "shell-file-path-1", + "shell.openInEditor", + { + cwd: filePath, + editor: "cursor", + }, + ); + expect(response.ok).toBe(false); + if (response.ok) { + throw new Error("Expected file-path shell target request to fail."); + } + expect(response.error?.code).toBe("request_failed"); + expect(response.error?.message).toContain("Editor target is not a directory"); + + client.socket.close(); + }); + it("returns structured errors for invalid terminal.run payloads", async () => { const server = await startRuntimeApiServer({ port: 0, diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 1931cd94f9..46222298d7 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -562,17 +562,28 @@ export async function startRuntimeApiServer( if (method === "shell.openInEditor") { const parsed = shellOpenInEditorInputSchema.parse(params); + const targetPath = path.resolve(parsed.cwd); + let targetStats: fs.Stats; + try { + targetStats = fs.statSync(targetPath); + } catch { + throw new Error(`Editor target does not exist: ${targetPath}`); + } + if (!targetStats.isDirectory()) { + throw new Error(`Editor target is not a directory: ${targetPath}`); + } + const editor = EDITORS.find((entry) => entry.id === parsed.editor); if (!editor) { throw new Error(`Unknown editor: ${parsed.editor}`); } if (!editor.command) { - openPathInFileManager(parsed.cwd); + openPathInFileManager(targetPath); return null; } - const child = spawn(editor.command, [parsed.cwd], { + const child = spawn(editor.command, [targetPath], { detached: true, stdio: "ignore", });