Skip to main content

Module wasm_plugin

Module wasm_plugin 

Source
Available on crate feature wasm-plugins only.
Expand description

WebAssembly middleware (18.10.0, #40, feature wasm-plugins): a WasmLayer runs a core WebAssembly module on each request and, optionally, each response. The module may let the request pass, change its headers or target, answer it itself, or change the response’s status and headers.

Isolation. Plugins get no imports at all — no WASI, no clock, no files, no network: a module that imports anything is refused at load. They see only what the host hands them. CPU per call is bounded by fuel, memory by a limit on the instance’s linear memory. A trap, running out of fuel or memory, or output the host can’t read answers 500 and counts in rws_wasm_plugin_errors_total; it never takes the server down. Each request gets a fresh instance, so nothing leaks from one request to the next.

ABI (version 1). The module exports:

ExportSignature
memorymemoryrequired
rws_alloc(len: i32) -> i32required: a buffer of len bytes the host writes input into
rws_on_request(ptr: i32, len: i32) -> i64required
rws_on_response(ptr: i32, len: i32) -> i64optional
rws_init(ptr: i32, len: i32)optional: called once per instance with the config string

A hook returns 0 to change nothing, or (ptr << 32) | len of its output in its memory. The request a hook gets:

GET /path?query HTTP/1.1
203.0.113.7                  ← the client address
host: example.com            ← every header, "name: value"
user-agent: curl/8.11.1
                             ← blank line; the body isn't passed

The response: its status code on the first line, then its headers and a blank line. Output is one directive per line:

DirectiveInDoes
set <name>: <value>bothreplace the header
add <name>: <value>bothadd a header
remove <name>bothremove the header
uri <target>requestchange the request target (origin form, /…)
status <code>responsechange the status
respond <code>requestanswer now: header lines (name: value) follow, then a blank line and the body

Anything else is an error (fail closed). Header names and values with CR, LF or NUL are refused.

Structs§

ResponseAction
A response hook’s decision.
WasmLayer
WebAssembly middleware. See the module docs.

Enums§

Edit
PluginError
What went wrong in a plugin call (the metric’s kind label).
RequestAction
A request hook’s decision.

Constants§

DEFAULT_FUEL
Fuel per call when not configured: roughly tens of milliseconds of CPU.
DEFAULT_MEMORY_MB
Linear memory limit when not configured, in MiB.
MAX_OUTPUT
The most output a hook may return.

Functions§

parse_request_output
A request hook’s output, decoded. Public for the fuzz target.
parse_response_output
A response hook’s output, decoded. Public for the fuzz target.