Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/exec-cwd-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@perstack/base": patch
---

Fallback to workspace when exec cwd is outside allowed directories
13 changes: 13 additions & 0 deletions apps/base/src/tools/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,17 @@ describe("exec tool", () => {
})
expect(result.output).toBeTruthy()
})

it("falls back to workspace when cwd is outside allowed directories", async () => {
const result = await exec({
command: process.execPath,
args: ["-e", 'process.stdout.write("OK")'],
env: {},
cwd: "/home/user",
stdout: true,
stderr: false,
timeout: 2000,
})
expect(result.output).toBe("OK")
})
})
14 changes: 11 additions & 3 deletions apps/base/src/tools/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { promisify } from "node:util"
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
import { getFilteredEnv, truncateText } from "@perstack/core"
import { z } from "zod/v4"
import { validatePath } from "../lib/path.js"
import { validatePath, workspacePath } from "../lib/path.js"
import { successToolResult } from "../lib/tool-result.js"

const execFileAsync = promisify(execFile)
Expand All @@ -20,10 +20,18 @@ type ExecInput = {
stderr: boolean
timeout?: number
}
async function resolveCwd(cwd: string): Promise<string> {
try {
return await validatePath(cwd)
} catch {
return workspacePath
}
}

export async function exec(input: ExecInput) {
const validatedCwd = await validatePath(input.cwd)
const resolvedCwd = await resolveCwd(input.cwd)
const { stdout, stderr } = await execFileAsync(input.command, input.args, {
cwd: validatedCwd,
cwd: resolvedCwd,
env: getFilteredEnv(input.env),
timeout: input.timeout,
maxBuffer: 10 * 1024 * 1024,
Expand Down