Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(core-utils): tell the user only os.tmpdir() is writable when a /tmp permission denial fires (#16718)

Chaker Atallah83e421253032
diff --git a/packages/core/utils/src/lib/deno.ts b/packages/core/utils/src/lib/deno.ts
index aed476a8b15..2d7a9f46aef 100644
--- a/packages/core/utils/src/lib/deno.ts
+++ b/packages/core/utils/src/lib/deno.ts
@@ -10,8 +10,9 @@ export const deno = {
*/
async run({ body, permissions, cwd, memoryLimitMb = DEFAULT_MEMORY_LIMIT_MB, allowReadPaths = [], resolveNodeModules = false, env = {}, denoDirBase, timeoutMs }: DenoProgramParams): Promise<unknown> {
const marker = newResultMarker()
+ const { os } = await getNodeApis()
const { child, denoPath, denoDir } = await spawnDeno({ entry: '-', permissions, cwd, memoryLimitMb, allowReadPaths, resolveNodeModules, env, denoDirBase })
- child.stdin.end(buildRunProgram({ body, marker }))
+ child.stdin.end(buildRunProgram({ body, marker, tmpDir: os.tmpdir() }))
const killTimer = timeoutMs === undefined ? undefined : setTimeout(() => child.kill('SIGKILL'), timeoutMs)
return new Promise((resolve, reject) => {
@@ -85,8 +86,9 @@ export const deno = {
async createSession({ bootstrapBody, permissions, cwd, memoryLimitMb = DEFAULT_MEMORY_LIMIT_MB, env = {}, idleTimeoutMs = DEFAULT_SESSION_IDLE_TIMEOUT_MS }: DenoSessionParams): Promise<DenoSession> {
const marker = newResultMarker()
+ const { os } = await getNodeApis()
const { child, denoPath, denoDir } = await spawnDeno({
- entry: { body: buildSessionProgram({ bootstrapBody, marker }) },
+ entry: { body: buildSessionProgram({ bootstrapBody, marker, tmpDir: os.tmpdir() }) },
permissions,
cwd,
memoryLimitMb,
@@ -333,9 +335,9 @@ function toPermissionFlags({ permissions, tmpDir }: { permissions: DenoPermissio
})
}
-function buildRunProgram({ body, marker }: { body: string, marker: string }): string {
+function buildRunProgram({ body, marker, tmpDir }: { body: string, marker: string, tmpDir: string }): string {
return `
-${sandboxError.payloadSource}
+${sandboxError.payloadSourceFor({ tmpDir })}
let settled = false;
const emit = (payload) => {
if (settled) return;
@@ -370,9 +372,9 @@ catch (error) {
* stdin: {"kind":"run","script":"step_1.a + 1","id":"0"}
* stdout: MARKER{"id":"0","success":true,"result":42}
*/
-function buildSessionProgram({ bootstrapBody, marker }: { bootstrapBody: string, marker: string }): string {
+function buildSessionProgram({ bootstrapBody, marker, tmpDir }: { bootstrapBody: string, marker: string, tmpDir: string }): string {
return `
-${sandboxError.payloadSource}
+${sandboxError.payloadSourceFor({ tmpDir })}
${bootstrapBody}
${SESSION_REPLY_CHANNEL(marker)}
${SESSION_REJECTION_TRACKING}
diff --git a/packages/core/utils/src/lib/sandbox-error.ts b/packages/core/utils/src/lib/sandbox-error.ts
index f48a37e5d18..e8a258e63cf 100644
--- a/packages/core/utils/src/lib/sandbox-error.ts
+++ b/packages/core/utils/src/lib/sandbox-error.ts
@@ -11,10 +11,30 @@ export const sandboxError = {
return builtError
},
- payloadSource: `const toErrorPayload = (value) => {
+ payloadSourceFor({ tmpDir }: PayloadSourceParams = {}): string {
+ return `const __AP_TMP_DIR__ = ${JSON.stringify(tmpDir ?? null)};
+const toErrorPayload = (value) => {
try {
if (value instanceof Error) {
- return { message: value.message, name: value.name, stack: value.stack }
+ let message = value.message
+ const accessMatch = __AP_TMP_DIR__ !== null && typeof Deno !== 'undefined' && value.name === 'NotCapable'
+ ? message.match(/Requires (read|write) access/)
+ : null
+ if (accessMatch) {
+ const operation = accessMatch[1]
+ const tmpGranted = Deno.permissions.querySync({ name: operation, path: __AP_TMP_DIR__ }).state === 'granted'
+ if (operation === 'write') {
+ message += tmpGranted
+ ? '. Only os.tmpdir() is writable from a code step.'
+ : '. Writes are not available to a code step in this execution mode.'
+ }
+ else {
+ message += tmpGranted
+ ? ". Only os.tmpdir() and the step's own directory are readable from a code step."
+ : ". Only the step's own directory is readable by a code step in this execution mode."
+ }
+ }
+ return { message, name: value.name, stack: value.stack }
}
if (typeof value === 'string') {
return { message: value }
@@ -24,7 +44,8 @@ export const sandboxError = {
catch {
return { message: 'Code execution failed' }
}
-}`,
+}`
+ },
}
function formatCapturedOutput({ stdout, stderr }: { stdout: string, stderr: string }): string {
@@ -49,3 +70,7 @@ type BuildSandboxErrorParams = {
stdout: string
stderr: string
}
+
+type PayloadSourceParams = {
+ tmpDir?: string
+}
diff --git a/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts b/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts
index 4f61e172670..4503ccb36c8 100644
--- a/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts
+++ b/packages/server/engine/src/lib/core/code/no-op-code-sandbox.ts
@@ -6,7 +6,7 @@ const CODE_RUNNER_SCRIPT = `
process.once('message', async function(msg) {
let settled = false
- ${sandboxError.payloadSource}
+ ${sandboxError.payloadSourceFor({})}
process.on('unhandledRejection', (reason) => {
if (settled) return
diff --git a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts b/packages/server/engine/test/core/code/deno-code-sandbox.test.ts
index 4fb5e28b763..8b726262f7c 100644
--- a/packages/server/engine/test/core/code/deno-code-sandbox.test.ts
+++ b/packages/server/engine/test/core/code/deno-code-sandbox.test.ts
@@ -534,3 +534,54 @@ describe('post-result rejections', () => {
await expect(runModule(source)).resolves.toBe('ok')
})
})
+
+describe('permission-denied error hints', () => {
+ function tmpGrantedSandbox() {
+ return denoModule.denoCodeSandbox({
+ permissions: [denoModule.DenoPermission.WRITE_TMP, denoModule.DenoPermission.READ_TMP, denoModule.DenoPermission.ENV],
+ codeStepMemoryLimitMb: 128,
+ })
+ }
+
+ async function runInTmpGrantedSandbox(source: string): Promise<unknown> {
+ const codeFilePath = path.join(stepDir, 'index.ts')
+ await writeFile(codeFilePath, source)
+ return tmpGrantedSandbox().runCodeModule({ codeFilePath, inputs: {} })
+ }
+
+ it('tells the step only os.tmpdir() is writable, when tmp access is granted but the written path is not', async () => {
+ await expectRejection(
+ runInTmpGrantedSandbox(`export const code = async () => Deno.writeTextFile('/not-allowed/x.txt', 'x')`),
+ /Requires write access.*Only os\.tmpdir\(\) is writable from a code step\./,
+ )
+ })
+
+ it('tells the step tmp and the step directory are readable, when tmp access is granted but the read path is not', async () => {
+ await expectRejection(
+ runInTmpGrantedSandbox(`export const code = async () => Deno.readTextFile('/etc/hosts')`),
+ /Requires read access.*Only os\.tmpdir\(\) and the step's own directory are readable from a code step\./,
+ )
+ })
+
+ it('tells the step writes are unavailable, in the locked (code-only) profile', async () => {
+ await expectRejection(
+ runModule(`export const code = async () => Deno.writeTextFile('/tmp/ap-pwned.txt', 'hi')`),
+ /Requires write access.*Writes are not available to a code step in this execution mode\./,
+ )
+ })
+
+ it('tells the step only its own directory is readable, in the locked (code-only) profile', async () => {
+ await expectRejection(
+ runModule(`export const code = async () => Deno.readTextFile('/etc/hosts')`),
+ /Requires read access.*Only the step's own directory is readable by a code step in this execution mode\./,
+ )
+ })
+
+ it('leaves non-filesystem permission errors unchanged', async () => {
+ const thrown = await runModule(`export const code = async () => (await fetch('https://example.com')).status`)
+ .then(() => null, (error: Error) => error)
+ expect(thrown).toBeInstanceOf(Error)
+ expect(String(thrown)).toMatch(/Requires net access/)
+ expect(String(thrown)).not.toMatch(/os\.tmpdir\(\)|readable|writable|Writes are not available/)
+ })
+})