WASM filters
Write custom Envoy filters in Rust or Go without forking the proxy.
Why WASM filters exist
Pre-WASM, customizing Envoy meant one of three painful options:
- Lua — easy, but slow and limited to one file with no real ecosystem.
ext_authz/ext_proc— flexible, but every request crosses a network hop.- C++ filter built into Envoy — fast and powerful, but you fork Envoy and inherit a build system.
WASM filters give you the fourth option: write a small module in
Rust, Go (via TinyGo), AssemblyScript, or C++, compile to a .wasm
file, and Envoy loads it into a sandboxed VM at runtime. Near-native
speed, language choice, no fork, no network hop, and a sandbox so a
buggy plugin can't crash the proxy.
The contract is proxy-wasm, an ABI defined jointly by Envoy, Istio,
MOSN, and APISIX. Same .wasm binary, multiple hosts.
Minimal envoy.yaml
http_filters:
- name: envoy.filters.http.wasm
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
config:
name: "add_header"
root_id: "add_header_root"
fail_open: true # if VM panics, pass traffic through
vm_config:
runtime: "envoy.wasm.runtime.v8"
vm_id: "add_header_vm"
code:
local: { filename: "/etc/envoy/plugins/add_header.wasm" }
allow_precompiled: true
configuration:
"@type": type.googleapis.com/google.protobuf.StringValue
value: '"header":"x-wasm","value":"hello"'
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
A Rust skeleton
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Info);
proxy_wasm::set_http_context(|_, _| Box::new(AddHeader));
}}
struct AddHeader;
impl Context for AddHeader {}
impl HttpContext for AddHeader {
fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action {
self.set_http_response_header("x-wasm", Some("hello"));
Action::Continue
}
}
Build: cargo build --target wasm32-wasip1 --release. The resulting
.wasm is what you mount into Envoy.
For Go, use TinyGo (not standard Go — see pitfalls):
tinygo build -o plugin.wasm -scheduler=none -target=wasi ./main.go
The three pitfalls
A panicking VM kills requests by default. During development always set
fail_open: true. In production, only flip to
false after you have alerts on
wasm_vm.*.envoy_wasm_runtime_v8.failure, or a bad rollout
becomes a 503 storm.
TinyGo plugins built with -target=wasm (the browser target)
load fine but immediately abort because they expect the JS host ABI.
You MUST use -target=wasi. Rust must use
wasm32-wasip1; the older wasm32-wasi target is
deprecated.
Each HTTP context allocates inside the VM. Long-lived buffers in
on_http_request_body with end_stream=false
balloon RSS under load. Handle the streaming case: only buffer until
end_stream when you need the full body, and call
set_buffer to release.
Hands-on: panic-safety experiment
- Clone any proxy-wasm-rust-sdk example, build it, mount the
.wasmnext to your Envoy config:
docker run --rm -v $PWD:/etc/envoy/plugins \
-p 10000:10000 \
envoyproxy/envoy:v1.32-latest -c /etc/envoy/envoy.yaml
-
curl -i localhost:10000/get— confirm thex-wasmheader appears on the response. -
Add a
panic!("boom")insideon_http_response_headers, rebuild. Withfail_open: falsethe next request returns 503; flip totrueand confirm requests pass through with no header added. -
Watch
http://localhost:9901/stats?filter=wasm_vm— you'll see thefailurecounter increment every time the panic fires.
pii-redact.wasm) reads a per-route JSON config ({fields: [ssn, dob]}) from the configuration field and rewrites response bodies on the fly. The same binary deploys to staging, prod-us, prod-eu — only the per-route config changes via xDS ECDS. Adding a new tenant = one config push, not a new code release.When to reach for WASM (vs Lua, vs ext_proc)
| You need… | Reach for |
|---|---|
| Quick header/body tweak, no build pipeline | Lua |
| Heavy logic, language choice, sandbox | WASM |
| AI/ML libs, Python/non-WASM toolchain, full body mutation | ext_proc |
| Stateful logic that must scale across pods | ext_proc (centralised) |
WASM is the sweet spot for most custom filter logic. ext_proc is the escape hatch for "I need pandas" or "I need to share state across replicas."
Which combination is required to load a TinyGo proxy-wasm plugin in Envoy?