Skip to main content

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
Creates a new WASM runtime with the specified configuration.
WasmRuntimeConfig
required
Runtime configuration including engine settings and security limits
Returns: Result<WasmToolRuntime, WasmError>

prepare

src/tools/wasm/runtime.rs
Validates and compiles a WASM component. This is done once at registration time.
&str
required
Tool name for error messages
&[u8]
required
Raw WASM component bytes
Option<ResourceLimits>
Optional resource limits (uses defaults if None)
Returns: Result<PreparedModule, WasmError> - Compiled module ready for execution

WasmToolWrapper

Wraps a WASM component as a Tool implementation.

Constructor

src/tools/wasm/wrapper.rs
Creates a new WASM tool wrapper.
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
Optional configuration overrides.

Capabilities

Defines what host functions a WASM tool can access.
src/tools/wasm/capabilities.rs

Constructor

src/tools/wasm/capabilities.rs
Creates a capabilities set with all features disabled (log and time are always available).

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
Example:

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
Restricts access to specific paths only.

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

Example: Register WASM Tool

Example: Store and Load