feat(agent): finalize sandbox integration with strict isolation and tests

This commit is contained in:
yiling
2026-05-20 21:49:42 +08:00
parent 9b4f68dff2
commit 1d00de51b0
4 changed files with 27 additions and 61 deletions
+12 -28
View File
@@ -1,16 +1,18 @@
import { prisma } from '@/db'
import { logger } from '@/logger'
import { Usage } from '@/services/ai/provider/provider'
import { s3Service } from '@/services/s3/s3'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
import { getModel, type ImageContent, type TextContent } from '@mariozechner/pi-ai'
import {
AuthStorage,
createAgentSession,
DefaultResourceLoader,
ModelRegistry,
SessionManager,
SettingsManager,
defineTool,
type ToolDefinition,
AuthStorage,
createAgentSession,
DefaultResourceLoader,
defineTool,
ModelRegistry,
SessionManager,
SettingsManager,
type ToolDefinition,
} from '@mariozechner/pi-coding-agent'
import { Type, type TSchema } from '@sinclair/typebox'
import * as fs from 'fs'
@@ -19,8 +21,6 @@ import { DatabaseSessionManager } from './database-session-manager'
import { analyzeAssetMediaTool } from './tools/analyze-asset-media'
import { createReadSkillTool } from './tools/read-skill'
import { createSandboxedBashTool } from './tools/sandboxed-bash'
import { Usage } from '@/services/ai/provider/provider'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
export interface AutofillField {
id: string
@@ -176,21 +176,6 @@ export class AgentExecutor {
if (!fs.existsSync(piDir)) fs.mkdirSync(piDir, { recursive: true })
const allowWrite = [piDir, '/tmp']
const allowRead = [
piDir,
'/tmp',
'/usr/bin', // Allow common binaries
'/bin',
'/usr/lib',
'/lib',
'/usr/share/zoneinfo', // Essential for time
]
// On macOS, /tmp is a symlink to /private/tmp, and other system paths might need expansion
if (process.platform === 'darwin') {
allowWrite.push('/private/tmp')
allowRead.push('/private/tmp', '/private/var/folders')
}
// NOTE: SandboxManager.initialize is a global operation that applies to the entire process.
// anthropic-experimental/sandbox-runtime currently does not support separate managers per session/team.
@@ -204,9 +189,8 @@ export class AgentExecutor {
},
filesystem: {
allowWrite,
denyRead: ['/', '~'], // Deny all roots
denyWrite: ['/', '~'],
allowRead,
denyWrite: ['.env', '.env.*', '*.pem', '*.key'],
denyRead: ['~/.ssh', '~/.aws', '~/.gnupg'],
},
})
+3 -1
View File
@@ -66,7 +66,9 @@ describe('createSandboxedBashTool', () => {
'bash',
['-c', 'sandboxed-echo hello'],
expect.objectContaining({
env: { TEST_ENV: 'test-value' },
env: expect.objectContaining({
TEST_ENV: 'test-value',
}),
}),
)
+1 -1
View File
@@ -1,5 +1,5 @@
import { createBashTool } from '@mariozechner/pi-coding-agent'
import { SandboxManager } from '@anthropic-ai/sandbox-runtime'
import { createBashTool } from '@mariozechner/pi-coding-agent'
import { spawn } from 'node:child_process'
import type { DatabaseSessionManager } from '../database-session-manager'
+11 -31
View File
@@ -31,8 +31,6 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '@/ui/components/ui/alert-dialog'
// @ts-ignore - Import browser distribution to avoid Node.js built-ins in the bundle
import JSZip from 'jszip/dist/jszip.min.js'
import { SkillInfo } from '@/dtos/skill'
interface SkillsConfigCardProps {
@@ -305,31 +303,17 @@ const AddSkillDialog = ({
return
}
const fileToUpload = files[0]
if (!fileToUpload.name.toLowerCase().endsWith('.zip')) {
alert('Only .zip files are supported.')
return
}
setIsProcessing(true)
try {
let fileToUpload: File | Blob
let fileName: string
let contentType = 'application/zip'
// Check if it's a ZIP file or a collection of files (folder)
const isZip = files.length === 1 && files[0].name.toLowerCase().endsWith('.zip')
if (isZip) {
fileToUpload = files[0]
fileName = files[0].name
contentType = files[0].type || 'application/zip'
} else {
const zip = new JSZip()
for (let i = 0; i < files.length; i++) {
const f = files[i]
// Use relative path for folder structure if available
const path = f.webkitRelativePath || f.name
zip.file(path, f)
}
fileToUpload = await zip.generateAsync({ type: 'blob' })
fileName = 'skill_package.zip'
}
const fileName = fileToUpload.name
const contentType = fileToUpload.type || 'application/zip'
// 1. Get Presigned URL and Asset ID
const attachRes = await client.api.projects[':projectId'].attachments.$post({
@@ -373,12 +357,12 @@ const AddSkillDialog = ({
<DialogHeader>
<DialogTitle>Add New Skill</DialogTitle>
<DialogDescription>
Import a skill from a local ZIP/folder or a GitHub repository.
Import a skill from a local ZIP archive or a GitHub repository.
</DialogDescription>
</DialogHeader>
<div className="space-y-6 pt-4">
{/* File/Folder Upload Section */}
{/* File Upload Section */}
<div className="space-y-3">
<Label>Package Upload</Label>
<div
@@ -395,10 +379,6 @@ const AddSkillDialog = ({
ref={fileInputRef}
className="hidden"
accept=".zip"
// @ts-ignore
webkitdirectory=""
directory=""
multiple
onChange={(e) => handleFileUpload(e.target.files)}
/>
<div className="p-3 bg-slate-100 dark:bg-slate-800 rounded-full group-hover:bg-blue-100 dark:group-hover:bg-blue-900/30 transition-colors">
@@ -406,7 +386,7 @@ const AddSkillDialog = ({
</div>
<div className="mt-3 text-center">
<p className="text-sm font-medium text-slate-700 dark:text-slate-300">
Select ZIP file or folder
Select ZIP file
</p>
<p className="text-[10px] text-slate-500 dark:text-slate-400 mt-1 uppercase tracking-wider font-semibold">
Packages must contain a SKILL.md