wasm-sandbox
A secure WebAssembly sandbox for running untrusted code with dead-simple ease of use, flexible host-guest communication, comprehensive resource limits, and capability-based security.
âĻ NEW in v0.3.0: Ease-of-Use Revolution
We've completely reimagined the developer experience with progressive complexity APIs:
ð One-Line Execution (Dead Simple)
// Auto-compile and run in one line!
let result: i32 = run.await?;
âąïļ With Timeout (Simple + Safe)
let result: String = run_with_timeout.await?;
ðïļ Builder Pattern (More Control)
let sandbox = builder
.source
.timeout_duration
.memory_limit
.enable_file_access
.build.await?;
Quick Links
ð Complete Documentation | ð API Reference | ð§ Examples | ðŽ Discussions
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
âĻ NEW Major Additions in v0.4.0
ð§Đ WebAssembly Component Model
Support for the next evolution of WebAssembly with interface-driven development, multi-language components, and type-safe interactions. Learn more
ð Python Language Bindings
Use wasm-sandbox from Python applications with a familiar API and seamless integration. Learn more
ð Streaming APIs for Large Data
Process datasets larger than memory with efficient streaming APIs for real-time data handling. Learn more
Usage Examples
Ultra-Simple Usage (Coming in v0.3.0)
// One line: auto-compile and run
let result: i32 = run?;
// Auto-compile project and reuse
let sandbox = from_source?;
let result = sandbox.call?;
Current API (v0.2.0)
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 docs/design/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 demonstrating different use cases:
- Basic Usage - Simple function calling and sandbox setup
- File Processor - Secure file processing with filesystem limits
- HTTP Server - Web server running in sandbox with network controls
- MCP Server - Model Context Protocol server implementation
- CLI Tool - Command-line tool wrapper with I/O redirection
- Plugin Ecosystem - Generic plugin system with hot reload
Run examples:
See examples/README.md for detailed descriptions and usage instructions.
Documentation
For New Users
- Quick Start - Get up and running in minutes
- Installation Guide - Detailed installation and setup
- Basic Tutorial - Step-by-step first application (planned)
- Examples - Working code examples
API Reference
- docs.rs - Complete API documentation (always up-to-date)
- API Overview - Core concepts and usage patterns
- Planned Improvements - Upcoming API changes
Advanced Topics
- Trait Design - Architecture and trait patterns
- Generic Plugin System - Plugin development framework
- Migration Guide - Upgrading between versions
- Security Configuration - Capability and resource management (planned)
Complete Documentation Index
ð Browse All Documentation - Organized by category with detailed navigation
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
We welcome contributions! Please see:
- Contributing Guidelines - How to contribute to the project
- Documentation Index - Complete documentation for developers
- API Improvements - Priority development areas
- Design Documents - Architecture and design decisions
For questions and discussions, use GitHub Discussions.
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.