Capabilities and security
View SourceThis page is about what a component may reach and how much it may use, and what running it inside your node does and does not protect you from. Read it before you load a component you did not write, or before you expose tools to an MCP client.
A component starts with nothing: no directory, no environment variable, no network, a deadline of one second and erlang_wasm's untrusted limits. You grant what it needs when you load it, and nothing a component says about itself grants anything.
Grant capabilities
{ok, _} = application:ensure_all_started(wasm_tools),
Dir = filename:join("_build", "capabilities_doc"),
ok = filelib:ensure_path(Dir),
{ok, Env} = wasm_tools:load({file, "test/fixtures/component/erlang_wasm/wasienv.component.wasm"},
#{capabilities => #{env => #{<<"MODE">> => <<"test">>},
filesystem => [{<<"/data">>, Dir, read_only}]}}),
wasm_tools:call(Env, <<"env">>, #{}).
%% => {ok, [{<<"MODE">>, <<"test">>}]}| capability | value | the component gets |
|---|---|---|
filesystem | [{GuestPath, HostDir, read_only | read_write}] | those directories, and nothing outside them |
env | #{Name => Value} or [{Name, Value}], binaries | those variables, never the node's |
http | [Host | {Host, Port}] | outbound HTTP to those hosts, with no sockets and no name lookups |
network | an erlang_wasm network grant | sockets and HTTP to the addresses and ports it names |
Clocks and random numbers are always available: erlang_wasm gives them to every component. Standard output and error are discarded.
Grant HTTP by host name
{ok, Http} = wasm_tools:load({file, "test/fixtures/component/http_client.component.wasm"},
#{capabilities => #{http => [<<"api.example.com">>,
<<"*.example.org">>]}}),
{ok, {error, Denied}} = wasm_tools:call(Http, <<"fetch">>,
#{<<"url">> => <<"http://127.0.0.1:9/">>}),
Denied.
%% => <<"ErrorCode::HttpRequestDenied">>- A host is a name, a wildcard for its subdomains (
*.example.orgdoes not matchexample.org), or a literal address. Without a port, only the scheme's default port (80 or 443) is allowed. - A name that resolves to a loopback, private, link-local or metadata address is refused. Only a literal address you list reaches such an address.
- The host resolves a name once and connects to the address it checked, and does not follow redirects: a request to a new location is checked again.
The guest sees a refused request as HTTP-request-denied, an error value it can
handle; the call itself succeeds. Your observer sees it as
[wasm_tools, http, denied], with the host and port the component asked for
(Observability).
To see what a grant decides without running the component, ask
wasm_tools:authorize/2: with the request as the component makes it, then with
the address its name resolved to. The request goes out only when both answer
allow.
Request = #{scheme => <<"https">>, host => <<"api.example.com">>, port => 443},
allow = wasm_tools:authorize(Http, Request),
deny = wasm_tools:authorize(Http, Request#{port => 8443}),
wasm_tools:authorize(Http, Request#{address => {10, 0, 0, 1}}).
%% => denySet limits
{ok, Spin} = wasm_tools:load({file, "test/fixtures/component/limits.component.wasm"},
#{limits => #{fuel => 100000, max_memory_pages => 64}}),
{error, #{class := exhaustion, kind := out_of_fuel}} =
wasm_tools:call(Spin, <<"spin">>, #{<<"n">> => 100000000}),
wasm_tools:call(Spin, <<"grow">>, #{<<"mib">> => 8}).
%% => {error, #{class := exhaustion, kind := memory_limit}}| limit | what it bounds | untrusted |
|---|---|---|
fuel | instructions executed | 10,000,000 |
timeout | milliseconds per call | 1000 |
max_memory_pages | the component's memory, in 64 KiB pages | 256 (16 MiB) |
max_depth | call depth | 256 |
max_host_calls | calls into the host | 10,000 |
max_heap_words | the call process's Erlang heap | 8,388,608 |
min_heap_words | the heap the call process starts with (wasm_tools' own) | 16,384 |
limits is untrusted (the default), trusted (no fuel ceiling, no deadline,
no memory bound: for code you wrote) or a map merged over untrusted. A
timeout of infinity needs trusted. A call can lower any limit
(#{limits => ..., timeout => ...} in its options), never raise one.
Finite fuel keeps the component in erlang_wasm's interpreter: its compiled tier does not count fuel.
ok = wasm_tools:unload(Env), ok = wasm_tools:unload(Http), ok = wasm_tools:unload(Spin).What in-process execution gives you
A component runs in your node, in a process of its own per call. That process:
- can be stopped at any point: a deadline, a cancel, or the death of its caller kills it, and its memory, sockets and HTTP connections go with it;
- has its own Erlang heap bound, and erlang_wasm bounds the component's memory;
- cannot reach anything that was not granted: imports are its only way out, and wasm_tools gives only WASI with the capabilities above and the host interfaces you name;
- cannot turn anything it sends into an atom or a crash of your code: arguments and results are checked against their types, and errors are values.
What it does not give you
- An Erlang process is not an operating-system boundary. A component shares your node's memory allocator, schedulers and files with everything else. Timing and other side channels are not addressed.
- Fuel bounds work, not time: a component blocked in a host call uses none. The deadline is what bounds time.
- Host functions you write are the attack surface: they run with your application's rights. Check what they receive as you would check a request.
- Many components running at once can still load every scheduler: bound
concurrency with a registry's
max_concurrency. - A component of an unknown author should run on a node, or in a container, whose loss you can afford.
Notes
- Load components you did not build with a
sha256, so the bytes are the ones you reviewed. - A component's own description of itself (its WIT doc comments) is text a
model may read; for MCP, descriptions come from your
meta, not from the component.