| Crates.io | wasm-sandbox |
| lib.rs | wasm-sandbox |
| version | 0.4.1 |
| created_at | 2025-07-10 21:12:45.609644+00 |
| updated_at | 2025-07-16 16:57:40.383684+00 |
| description | A secure WebAssembly sandbox with dead-simple ease of use, progressive complexity APIs, and comprehensive safety controls |
| homepage | https://github.com/ciresnave/wasm-sandbox |
| repository | https://github.com/ciresnave/wasm-sandbox |
| max_upload_size | |
| id | 1747090 |
| size | 1,666,213 |
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.
We've completely reimagined the developer experience with progressive complexity APIs:
// Auto-compile and run in one line!
let result: i32 = wasm_sandbox::run("calculator.rs", "add", &(5, 3)).await?;
let result: String = wasm_sandbox::run_with_timeout(
"processor.py", "process", &"data", Duration::from_secs(30)
).await?;
let sandbox = WasmSandbox::builder()
.source("my_program.rs")
.timeout_duration(Duration::from_secs(60))
.memory_limit(64 * 1024 * 1024)
.enable_file_access(false)
.build().await?;
ð Complete Documentation | ð API Reference | ð§ Examples | ðŽ Discussions
Support for the next evolution of WebAssembly with interface-driven development, multi-language components, and type-safe interactions. Learn more
Use wasm-sandbox from Python applications with a familiar API and seamless integration. Learn more
Process datasets larger than memory with efficient streaming APIs for real-time data handling. Learn more
// One line: auto-compile and run
let result: i32 = wasm_sandbox::run("./calculator.rs", "add", &(5, 3))?;
// Auto-compile project and reuse
let sandbox = WasmSandbox::from_source("./my_project/")?;
let result = sandbox.call("process_data", &input)?;
use wasm_sandbox::{WasmSandbox, SandboxConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the sandbox with default configuration
let mut sandbox = WasmSandbox::new()?;
// Load a WebAssembly module
let wasm_bytes = std::fs::read("my_module.wasm")?;
let module_id = sandbox.load_module(&wasm_bytes)?;
// Create an instance of the module
let instance_id = sandbox.create_instance(module_id, None)?;
// Call a function in the module
let result: String = sandbox.call_function(instance_id, "greet", &"World").await?;
println!("Result: {}", result);
Ok(())
}
use wasm_sandbox::{
WasmSandbox, InstanceConfig,
security::{Capabilities, NetworkCapability}
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sandbox = WasmSandbox::new()?;
// Load module
let wasm_bytes = std::fs::read("http_server.wasm")?;
let module_id = sandbox.load_module(&wasm_bytes)?;
// Configure instance with network capabilities
let mut capabilities = Capabilities::minimal();
capabilities.network = NetworkCapability::Loopback;
let instance_config = InstanceConfig {
capabilities,
..InstanceConfig::default()
};
// Create instance
let instance_id = sandbox.create_instance(module_id, Some(instance_config))?;
// Start the HTTP server
let port: u16 = sandbox.call_function(instance_id, "start", &8080).await?;
println!("HTTP server running on port {}", port);
// Wait for user to press Enter
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
// Stop the server
let _: () = sandbox.call_function(instance_id, "stop", &()).await?;
Ok(())
}
Add this to your Cargo.toml:
[dependencies]
wasm-sandbox = "0.2.0"
For all features including Wasmer runtime support:
[dependencies]
wasm-sandbox = { version = "0.2.0", features = ["all-runtimes"] }
The crate features a trait-based architecture with two main patterns:
WasmRuntime, WasmInstance, WasmModule - can be used as trait objectsWasmRuntimeExt, WasmInstanceExt - provide async and generic operationsThis design allows for maximum flexibility while maintaining type safety. See docs/design/TRAIT_DESIGN.md for detailed information.
use wasm_sandbox::WasmSandbox;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new sandbox
let mut sandbox = WasmSandbox::new()?;
// Load a WebAssembly module
let wasm_bytes = std::fs::read("module.wasm")?;
let module_id = sandbox.load_module(&wasm_bytes)?;
// Create an instance with default security settings
let instance_id = sandbox.create_instance(module_id, None)?;
// Call a function
let result: i32 = sandbox.call_function(instance_id, "add", &(5, 3)).await?;
println!("5 + 3 = {}", result);
Ok(())
}
use wasm_sandbox::{WasmSandbox, InstanceConfig};
use wasm_sandbox::security::{Capabilities, NetworkCapability, FilesystemCapability};
let mut capabilities = Capabilities::minimal();
capabilities.network = NetworkCapability::Loopback; // Only localhost
capabilities.filesystem = FilesystemCapability::ReadOnly(vec!["./data".into()]);
let config = InstanceConfig {
capabilities,
max_memory: Some(64 * 1024 * 1024), // 64MB limit
max_execution_time: Some(std::time::Duration::from_secs(30)),
..Default::default()
};
let instance_id = sandbox.create_instance(module_id, Some(config))?;
use wasm_sandbox::security::ResourceLimits;
let limits = ResourceLimits {
max_memory: 128 * 1024 * 1024, // 128MB
max_cpu_time: std::time::Duration::from_secs(60),
max_file_descriptors: 10,
max_network_connections: 5,
..Default::default()
};
// Monitor resource usage
let usage = sandbox.get_resource_usage(instance_id)?;
println!("Memory used: {} bytes", usage.memory_used);
use wasm_sandbox::wrappers::HttpServerWrapper;
let wrapper = HttpServerWrapper::new()?;
let server_spec = wrapper.create_server_spec(
"./my_server.wasm",
8080,
Some("./static".into()),
)?;
// Start the HTTP server in a sandbox
let server_id = wrapper.start_server(server_spec).await?;
println!("Server running on http://localhost:8080");
# Clone the repository
git clone https://github.com/username/wasm-sandbox.git
cd wasm-sandbox
# Build the project
cargo build --release
# Run tests
cargo test --all-features
# Run benchmarks
cargo bench
# Build examples
cargo build --examples
# Run an example
cargo run --example http_server
The repository includes several examples demonstrating different use cases:
Run examples:
cargo run --example basic_usage
cargo run --example http_server
cargo run --example file_processor
See examples/README.md for detailed descriptions and usage instructions.
ð Browse All Documentation - Organized by category with detailed navigation
The crate is organized into several key modules:
Benchmarks show excellent performance characteristics:
Run cargo bench to see detailed performance metrics.
We welcome contributions! Please see:
For questions and discussions, use GitHub Discussions.
This project is licensed under the MIT License - see the LICENSE file for details.
This crate uses WebAssembly's sandboxing capabilities to provide security isolation. However:
For security-sensitive applications, consider using additional sandboxing layers such as containers or process isolation.