diff --git a/src/agent/index.ts b/src/agent/index.ts index 90ae238c..d1a1ecb0 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -5,6 +5,7 @@ import { getModel } from '@earendil-works/pi-ai' import { Type, type TSchema } from '@sinclair/typebox' import * as fs from 'fs' import * as path from 'path' +import * as os from 'os' import { DatabaseSessionStorage } from './database-session-storage' import { analyzeAssetMediaTool } from './tools/analyze-asset-media' import { createCreateFileTool } from './tools/create-file' @@ -109,7 +110,7 @@ export async function createAgentSession(params: CreateAgentSessionParams) { const piDir = path.join(process.cwd(), '.pi') if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true }) - const allowWrite = [piDir, '/tmp'] + const allowWrite = [piDir] // SandboxManager.initialize is a global operation that applies to the entire process. await SandboxManager.initialize({ @@ -118,9 +119,10 @@ export async function createAgentSession(params: CreateAgentSessionParams) { deniedDomains: [], }, filesystem: { + allowRead: [piDir], + denyRead: [os.homedir(), '.env', '.env.*', '*.pem', '*.key'], allowWrite, denyWrite: ['.env', '.env.*', '*.pem', '*.key'], - denyRead: ['~/.ssh', '~/.aws', '~/.gnupg'], }, }) @@ -148,6 +150,18 @@ export async function createAgentSession(params: CreateAgentSessionParams) { model, systemPrompt: async () => { let prompt = systemPrompt + + // Sandbox environment restrictions + prompt += + '\n\n' + + [ + '# Sandbox Environment Restrictions', + 'Your shell environment (the `bash` tool) is highly sandboxed to protect the host system:', + '1. **Filesystem Isolation**: You only have read and write permissions to the `.pi` folder in the project root directory.', + '2. **Read/Write Restrictions**: All reading and writing to directories outside `.pi` (e.g. your home directory `~/`, `/tmp`, `/etc`, or the rest of the workspace) are strictly denied by the sandbox security policy.', + "3. **Avoid System Temp Directory Writes**: You must strictly avoid any commands or shell constructs that attempt to write to the system temporary directory `/tmp` or `/var/tmp`. For example, do not use Bash here documents (`< 0) { prompt += formatSkillsForPrompt(teamSkills) }