Reject whitespace cwd values for runtime path APIs

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 03:32:13 +00:00
1 parent dcb44cb729
commit 33edb9235d
2 files changed
+65 -20

No files matched your search

+60
View File
@@ -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,
+5 -20
View File
@@ -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(