Code Sandbox
by @yuyonghao-123
Secure sandbox for executing Node.js, Python, Go, and Rust code with timeout, CPU, and isolated temporary directory constraints.
clawhub install yuyonghao-code-sandboxπ About This Skill
Code Sandbox Skill
π Secure code execution sandbox for OpenClaw agents.
Features
Installation
# Already in workspace skills directory
cd skills/code-sandbox
npm install
Usage
JavaScript API
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
}
});
CLI Demo
# Run demo
npm run demoRun tests
npm test
Supported Languages
| Language | Version | Description | |----------|---------|-------------| | node | 20.x | Node.js JavaScript runtime | | python | 3.11 | Python interpreter | | go | 1.21 | Go compiler and runtime | | rust | 1.75 | Rust compiler (rustc) |
Configuration
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);
Per-execution Config
await sandbox.execute({
language: 'node',
code: '...',
config: {
timeout: 10000, // Override timeout
memoryLimit: 256, // Override memory limit
}
});
Security Features
Current Implementation (v0.1.0)
Planned (v0.2.0+)
Execution History
// 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
// },
// ...
// ]
Error Handling
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);
}
Testing
# Run all tests
npm testTest specific language
node test/node-test.js
node test/python-test.js
Limitations
Current Version (v0.1.0)
Known Issues
Roadmap
v0.2.0 (Next Release)
v0.3.0
v0.4.0
Safety Warnings
β οΈ This sandbox provides basic isolation but is NOT suitable for:
For production use, implement:
License
MIT License - See LICENSE file for details
Contributing
Contributions welcome! Please read CONTRIBUTING.md first.
Version: 0.1.0 Last Updated: 2026-03-17 Author: OpenClaw Team
π‘ Examples
JavaScript API
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
}
});
CLI Demo
# Run demo
npm run demoRun tests
npm test
βοΈ Configuration
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);
Per-execution Config
await sandbox.execute({
language: 'node',
code: '...',
config: {
timeout: 10000, // Override timeout
memoryLimit: 256, // Override memory limit
}
});