Spaces:
Runtime error
Runtime error
File size: 7,002 Bytes
e92be04 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | import { spawn } from 'node:child_process';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import vm from 'node:vm';
/**
* IsolateSandbox
* Provides a secure, containerized environment for executing untrusted agent code.
* Primary: Docker containers.
* Fallback: Node.js `vm` module (lightweight isolation).
*/
export class IsolateSandbox {
constructor(workDir = './temp_sandbox') {
this.workDir = path.resolve(workDir);
this.dockerAvailable = null; // null = unchecked
}
async init() {
await fs.mkdir(this.workDir, { recursive: true });
// Pre-check Docker availability
try {
await new Promise((resolve, reject) => {
const p = spawn('docker', ['version'], { stdio: 'ignore' });
p.on('close', code => code === 0 ? resolve() : reject());
p.on('error', reject);
});
this.dockerAvailable = true;
console.log('[SANDBOX] Docker available. Using containerized execution.');
} catch {
this.dockerAvailable = false;
console.warn('[SANDBOX] Docker unavailable. Using vm fallback (lower isolation).');
}
}
/**
* Executes arbitrary code in a Docker container (or vm fallback).
* Supports JavaScript and Python.
*/
async execute(code, options = {}) {
const lang = options.language || this._detectLanguage(code);
options._lang = lang;
if (this.dockerAvailable) {
return this._executeDocker(code, options);
}
if (lang === 'python') {
return this._executePythonFallback(code, options);
}
return this._executeVm(code, options);
}
/**
* Detect language from code content.
*/
_detectLanguage(code) {
const trimmed = code.trim();
if (trimmed.startsWith('#!/usr/bin/env python') || trimmed.startsWith('#!/usr/bin/python')) return 'python';
if (trimmed.match(/^(import |from |def |class |print\()/m)) return 'python';
return 'javascript';
}
/**
* Docker-based execution (high isolation). Supports JS + Python.
*/
async _executeDocker(code, options = {}) {
const runId = crypto.randomBytes(8).toString('hex');
const runFolder = path.join(this.workDir, runId);
await fs.mkdir(runFolder);
const lang = options._lang || 'javascript';
const ext = lang === 'python' ? '.py' : '.js';
const image = lang === 'python' ? 'python:3.11-slim' : 'node:18-slim';
const cmd = lang === 'python' ? ['python', `script${ext}`] : ['node', `script${ext}`];
const scriptPath = path.join(runFolder, `script${ext}`);
await fs.writeFile(scriptPath, code);
const memoryLimit = options.memory || '128m';
const cpuLimit = options.cpus || '0.5';
const timeout = options.timeout || 10000;
return new Promise((resolve) => {
const dockerArgs = [
'run', '--rm',
'--name', `p2pclaw-sandbox-${runId}`,
'--memory', memoryLimit,
'--cpus', cpuLimit,
'--network', 'none',
'-v', `${path.resolve(runFolder)}:/app`,
'-w', '/app',
image, ...cmd
];
const proc = spawn('docker', dockerArgs);
let stdout = '';
let stderr = '';
const timer = setTimeout(() => {
spawn('docker', ['stop', `p2pclaw-sandbox-${runId}`]);
resolve({ success: false, error: 'TIMEOUT', language: lang, stdout, stderr: stderr + '\nExecution timed out.' });
}, timeout);
proc.stdout.on('data', d => stdout += d.toString());
proc.stderr.on('data', d => stderr += d.toString());
proc.on('close', async (code) => {
clearTimeout(timer);
try { await fs.rm(runFolder, { recursive: true, force: true }); } catch {}
resolve({ success: code === 0, exitCode: code, language: lang, stdout, stderr });
});
});
}
/**
* vm module-based execution (processes fallback for local dev).
*/
async _executeVm(code, options = {}) {
const timeout = options.timeout || 5000;
const logs = [];
try {
const sandbox = {
console: {
log: (...args) => logs.push(args.join(' ')),
error: (...args) => logs.push('[ERR] ' + args.join(' '))
},
Math, Date, JSON, Array, Object, Number, String, Boolean
};
vm.createContext(sandbox);
vm.runInContext(code, sandbox, { timeout });
return {
success: true,
exitCode: 0,
stdout: logs.join('\n'),
stderr: ''
};
} catch (err) {
return {
success: false,
exitCode: 1,
stdout: logs.join('\n'),
stderr: err.message
};
}
}
/**
* Python fallback — try local Python interpreter if Docker unavailable.
*/
async _executePythonFallback(code, options = {}) {
const timeout = options.timeout || 10000;
const runId = crypto.randomBytes(8).toString('hex');
const tmpFile = path.join(this.workDir, `py_${runId}.py`);
try {
await fs.writeFile(tmpFile, code);
return new Promise(resolve => {
const proc = spawn('python3', [tmpFile], { timeout });
let stdout = '', stderr = '';
proc.stdout.on('data', d => stdout += d.toString());
proc.stderr.on('data', d => stderr += d.toString());
const timer = setTimeout(() => {
proc.kill();
resolve({ success: false, error: 'TIMEOUT', language: 'python', stdout, stderr });
}, timeout);
proc.on('close', async exitCode => {
clearTimeout(timer);
try { await fs.unlink(tmpFile); } catch {}
resolve({ success: exitCode === 0, exitCode, language: 'python', stdout, stderr });
});
proc.on('error', async () => {
clearTimeout(timer);
try { await fs.unlink(tmpFile); } catch {}
resolve({ success: false, exitCode: 1, language: 'python', stdout: '', stderr: 'Python not available on this system. Use Docker for Python execution.' });
});
});
} catch (e) {
return { success: false, error: e.message, language: 'python', stdout: '', stderr: e.message };
}
}
}
export const sandbox = new IsolateSandbox();
await sandbox.init();
|