Sandbox Isolation
OpenFang runs untrusted code in two isolated sandbox environments:- WASM Sandbox — For WebAssembly modules (user skills, WASM tools)
- Subprocess Sandbox — For Python/Node.js script execution
WASM Dual Metering
The WASM sandbox uses Wasmtime with two independent metering systems running in parallel:1. Fuel Metering (Instruction Count)
Tracks the number of WASM instructions executed. Each instruction consumes “fuel.”- Execution immediately halts
- Returns
OpenFangError::ResourceExhausted
2. Epoch Interruption (Wall-Clock Time)
A watchdog thread increments an epoch counter every 100ms. The WASM store checks the epoch at regular intervals.- Execution immediately halts
- Returns
OpenFangError::Timeout
Why Dual Metering?
Fuel metering alone is insufficient:- A WASM module can execute a single slow instruction (e.g.,
sleep(), network I/O) that consumes little fuel but blocks indefinitely.
- A CPU-bound tight loop might complete in wall-clock time but consume unbounded CPU.
- CPU-bound loops → Fuel limit triggers
- I/O-bound hangs → Epoch limit triggers
- Mixed workloads → Whichever limit is hit first
WASM Host Functions
WASM modules cannot directly access the file system, network, or memory. They must call host functions provided by OpenFang. Each host function enforces capability checks before granting access.Example: host_file_read
Available Host Functions
| Function | Capability Required | Description |
|---|---|---|
host_file_read | ToolInvoke("file_read") | Read file contents |
host_file_write | ToolInvoke("file_write") | Write file contents |
host_file_list | ToolInvoke("file_list") | List directory |
host_net_fetch | NetConnect(host) | HTTP fetch |
host_memory_store | MemoryWrite(key) | Store memory KV |
host_memory_recall | MemoryRead(key) | Recall memory KV |
host_log | None | Write to agent log |
Path Traversal Prevention
All file operations go throughsafe_resolve_path() or safe_resolve_parent() before execution.
Implementation
What It Blocks
| Attack | Input | Result |
|---|---|---|
| Parent directory escape | ../../../etc/passwd | PathTraversal error |
| Symlink escape | link_to_root/etc/passwd | PathTraversal error |
| Absolute path escape | /etc/passwd | PathTraversal error |
| Encoded traversal | ..%2F..%2Fetc%2Fpasswd | PathTraversal error |
Enforcement Order
CRITICAL: Capability check runs BEFORE path resolution.SSRF Protection
Thehost_net_fetch function blocks requests to private IP ranges and cloud metadata endpoints.
Implementation
Blocked Targets
| Target | Reason |
|---|---|
http://10.0.0.1/admin | Private IP (internal network) |
http://192.168.1.1/config | Private IP (LAN) |
http://169.254.169.254/latest/meta-data/ | AWS metadata endpoint |
http://metadata.google.internal/ | GCP metadata endpoint |
http://localhost:6379/ | Loopback (Redis, etc.) |
http://[::1]:8080/ | IPv6 loopback |
DNS Rebinding Defense
DNS resolution happens at request time, not at capability check time. This prevents DNS rebinding attacks:- Attacker registers
evil.compointing to a public IP - Agent checks capability for
evil.com→ Allowed - DNS TTL expires, attacker changes
evil.comto169.254.169.254 - Agent makes request → Blocked by
is_ssrf_target()
Subprocess Sandbox
Python and Node.js skills run in isolated subprocesses with environment clearing.Environment Isolation
All subprocess invocations useenv_clear() followed by selective variable injection:
What’s Blocked
| Variable | Why It’s Cleared |
|---|---|
AWS_ACCESS_KEY_ID | Cloud credentials |
GOOGLE_APPLICATION_CREDENTIALS | GCP credentials |
AZURE_CLIENT_SECRET | Azure credentials |
ANTHROPIC_API_KEY | LLM API keys |
OPENAI_API_KEY | LLM API keys |
DATABASE_URL | Database connection strings |
REDIS_URL | Redis connection strings |
Restricted PATH
ThePATH environment variable is set to a minimal set of directories:
Secret Zeroization
All API keys and secrets useZeroizing<String> from the zeroize crate.
How It Works
When Zeroization Happens
- Driver drop: When an LLM driver is dropped (agent killed, kernel shutdown)
- Config reload: When config is reloaded with new API keys
- Key rotation: When API keys are rotated
Debug Redaction
All config structs implementDebug with secret redaction:
Resource Limits
Both sandboxes enforce strict resource limits:WASM Limits
| Resource | Limit | Enforced By |
|---|---|---|
| Instructions | 10M fuel | Fuel metering |
| Wall-clock time | 1 second | Epoch interruption |
| Memory | 64 MB | Wasmtime config |
| Stack depth | 1024 frames | Wasmtime config |
Subprocess Limits
| Resource | Limit | Enforced By |
|---|---|---|
| Wall-clock time | 60 seconds | tokio::time::timeout |
| Output size | 50,000 chars | truncate_tool_result() |
| Concurrent processes | 10 | BackgroundExecutor semaphore |
Sandbox Configuration
Sandbox limits can be configured inconfig.toml:
Testing Sandbox Isolation
Test 1: WASM Infinite Loop
- Fuel limit triggers after ~10M iterations
- Returns
OpenFangError::ResourceExhausted - Agent remains responsive
Test 2: WASM Sleep Attack
- Epoch limit triggers after ~1 second
- Returns
OpenFangError::Timeout - Watchdog thread kills the WASM instance
Test 3: Path Traversal
safe_resolve_path()detects traversal- Returns
OpenFangError::PathTraversal - File is never read
Test 4: SSRF Attack
is_ssrf_target()detects metadata endpoint- Returns
OpenFangError::SsrfBlocked - Request is never made
Test 5: Environment Variable Leak
- Output:
not found env_clear()removed all secrets from environment
Monitoring Sandbox Events
All sandbox violations are logged to the audit trail:Best Practices
1. Always Test Skills in Sandbox First
Never deploy untrusted skills directly to production:2. Set Conservative Limits
Start with tight limits, expand only if needed:3. Monitor Resource Usage
Track which skills consume the most resources:4. Review Skill Manifests
Before installing a skill, review its requested capabilities:network = ["*"]shell = ["*"]memory_read = ["*"]
Related Security Systems
Overview
All 16 security systems
Capabilities
Capability-based access control
Audit Trail
Merkle hash-chain logging
Architecture
How sandboxing integrates across subsystems
