WebAssembly Sandbox (wasm-sandbox)
A secure WebAssembly sandbox for running untrusted code with flexible host-guest communication, comprehensive resource limits, and capability-based security.
Key Features
- ð Security First: Isolate untrusted code in WebAssembly sandboxes with fine-grained capability controls
- ð High Performance: Efficient host-guest communication with minimal overhead
- ð§ Flexible APIs: High-level convenience APIs and low-level control for advanced use cases
- ðĶ Multiple Runtimes: Support for Wasmtime and Wasmer WebAssembly runtimes
- ð Application Wrappers: Built-in support for HTTP servers, MCP servers, and CLI tools
- ð Resource Control: Memory, CPU, network, and filesystem limits with monitoring
- ð Async/Await: Full async support for non-blocking operations
Usage Examples
Basic Sandbox Usage
use ;
async
Sandboxing an HTTP Server
use ;
async
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
For all features including Wasmer runtime support:
[]
= { = "0.2.0", = ["all-runtimes"] }
Architecture Overview
The crate features a trait-based architecture with two main patterns:
- Dyn-Compatible Core Traits:
WasmRuntime,WasmInstance,WasmModule- can be used as trait objects - Extension Traits:
WasmRuntimeExt,WasmInstanceExt- provide async and generic operations
This design allows for maximum flexibility while maintaining type safety. See TRAIT_DESIGN.md for detailed information.
Quick Start
use WasmSandbox;
async
Advanced Features
Security Configuration
use ;
use ;
let mut capabilities = minimal;
capabilities.network = Loopback; // Only localhost
capabilities.filesystem = ReadOnly;
let config = InstanceConfig ;
let instance_id = sandbox.create_instance?;
Resource Monitoring
use ResourceLimits;
let limits = ResourceLimits ;
// Monitor resource usage
let usage = sandbox.get_resource_usage?;
println!;
HTTP Server Wrapping
use HttpServerWrapper;
let wrapper = new?;
let server_spec = wrapper.create_server_spec?;
// Start the HTTP server in a sandbox
let server_id = wrapper.start_server.await?;
println!;
Building from Source
# Clone the repository
# Build the project
# Run tests
# Run benchmarks
# Build examples
# Run an example
Examples
The repository includes several examples:
- HTTP Server:
cargo run --example http_server - MCP Server:
cargo run --example mcp_server - CLI Tool:
cargo run --example cli_wrapper
Architecture
The crate is organized into several key modules:
- Runtime: WebAssembly runtime abstraction (Wasmtime, Wasmer)
- Security: Capability-based security and resource limits
- Communication: Host-guest communication channels and RPC
- Wrappers: Application-specific wrappers and templates
- Compiler: WebAssembly compilation utilities
Performance
Benchmarks show excellent performance characteristics:
- Function calls: < 1Ξs overhead for simple function calls
- Memory communication: > 1GB/s throughput for large data transfers
- Startup time: < 10ms for typical modules
- Resource monitoring: < 0.1% CPU overhead
Run cargo bench to see detailed performance metrics.
Contributing
Contributions are welcome! Please see our contributing guidelines for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Safety and Security
This crate uses WebAssembly's sandboxing capabilities to provide security isolation. However:
- Always validate input to guest functions
- Set appropriate resource limits for your use case
- Review WebAssembly modules before execution
- Consider additional security measures for production use
For security-sensitive applications, consider using additional sandboxing layers such as containers or process isolation.