🔒 Secure code execution sandbox for OpenClaw agents.
bash
# Already in workspace skills directory
cd skills/code-sandbox
npm install
javascript
const { CodeSandbox } = require('./src/sandbox');
const sandbox = new CodeSandbox({
defaultTimeout: 30000, // 30 seconds
defaultMemoryLimit: 512, // 512 MB
});
// Execute Node.js code
const result = await sandbox.execute({
language: 'node',
code: 'console.log("Hello World!");',
});
console.log(result);
// {
// success: true,
// output: "Hello World!\n",
// error: "",
// exitCode: 0,
// executionTime: 45,
// memoryUsed: 0
// }
// Execute Python code
const pythonResult = await sandbox.execute({
language: 'python',
code: 'print("Hello from Python!")',
config: {
timeout: 5000, // 5 seconds
}
});
bash
# Run demo
npm run demo
# Run tests
npm test
LanguageVersionDescriptionnode20.xNode.js JavaScript runtimepython3.11Python interpretergo1.21Go compiler and runtimerust1.75Rust compiler (rustc)
javascript
const config = {
defaultTimeout: 30000, // Default timeout in ms
defaultMemoryLimit: 512, // Default memory limit in MB
defaultCpuLimit: 1, // Default CPU cores
tmpDir: '/path/to/tmp', // Temporary directory for execution
};
const sandbox = new CodeSandbox(config);
javascript
await sandbox.execute({
language: 'node',
code: '...',
config: {
timeout: 10000, // Override timeout
memoryLimit: 256, // Override memory limit
}
});
javascript
// Get last 100 executions
const history = sandbox.getHistory(100);
console.log(history);
// [
// {
// executionId: "exec_1710691200000_a1b2c3d4",
// timestamp: "2026-03-17T09:00:00.000Z",
// language: "node",
// codeLength: 42,
// success: true,
// executionTime: 45,
// memoryUsed: 0
// },
// ...
// ]
javascript
try {
const result = await sandbox.execute({
language: 'node',
code: 'throw new Error("Oops!");',
});
if (!result.success) {
console.error('Execution failed:', result.error);
}
} catch (error) {
console.error('Sandbox error:', error.message);
}
bash
# Run all tests
npm test
# Test specific language
node test/node-test.js
node test/python-test.js
⚠️ This sandbox provides basic isolation but is NOT suitable for:
For production use, implement:
MIT License - See LICENSE file for details
Contributions welcome! Please read CONTRIBUTING.md first.
Version: 0.1.0
Last Updated: 2026-03-17
Author: OpenClaw Team