Overview
The WASM tool system provides secure sandboxed execution for untrusted tools using Wasmtime. It follows patterns from NEAR blockchain and modern WASM best practices:- Compile once, instantiate fresh: Tools are validated and compiled at registration time. Each execution creates a fresh instance.
- Fuel metering: CPU usage is limited via Wasmtime’s fuel system.
- Memory limits: Memory growth is bounded via ResourceLimiter.
- Capability-based security: Features are opt-in via Capabilities.
- Extended host API: Log, time, workspace, HTTP, tool invoke, secrets.
Architecture
Security Constraints
WasmToolRuntime
The runtime manages WASM compilation and execution.Constructor
src/tools/wasm/runtime.rs
WasmRuntimeConfig
required
Runtime configuration including engine settings and security limits
Result<WasmToolRuntime, WasmError>
prepare
src/tools/wasm/runtime.rs
&str
required
Tool name for error messages
&[u8]
required
Raw WASM component bytes
Option<ResourceLimits>
Optional resource limits (uses defaults if None)
Result<PreparedModule, WasmError> - Compiled module ready for execution
WasmToolWrapper
Wraps a WASM component as a Tool implementation.Constructor
src/tools/wasm/wrapper.rs
Arc<WasmToolRuntime>
required
Shared runtime for execution
PreparedModule
required
Pre-compiled module from runtime.prepare()
Capabilities
required
Security capabilities to grant the tool
Configuration Methods
src/tools/wasm/wrapper.rs
Capabilities
Defines what host functions a WASM tool can access.src/tools/wasm/capabilities.rs
Constructor
src/tools/wasm/capabilities.rs
Configuration Methods
src/tools/wasm/capabilities.rs
HttpCapability
Allows HTTP requests to specific endpoints.src/tools/wasm/capabilities.rs
Constructor
src/tools/wasm/capabilities.rs
Vec<EndpointPattern>
required
List of allowed URL patterns
EndpointPattern
Defines an allowed HTTP endpoint pattern.src/tools/wasm/capabilities.rs
Constructors
src/tools/wasm/capabilities.rs
WorkspaceCapability
Allows access to workspace files.src/tools/wasm/capabilities.rs
Constructor
src/tools/wasm/capabilities.rs
WorkspaceReader
required
Workspace reader for file access
Configuration
src/tools/wasm/capabilities.rs
ResourceLimits
Defines execution resource limits.src/tools/wasm/limits.rs
usize
default:"10MB"
Maximum memory the WASM instance can allocate
FuelConfig
Fuel-based CPU metering configuration
Duration
default:"30s"
Maximum execution time before timeout
Defaults
src/tools/wasm/limits.rs
Storage
Persist WASM tools to the database.WasmToolStore Trait
src/tools/wasm/storage.rs
StoreToolParams
src/tools/wasm/storage.rs
Error Types
WasmError
src/tools/wasm/error.rs
TrapInfo
src/tools/wasm/error.rs
