envoy-guide
Module 7 · Going Deeper·16 min·advanced

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

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

rust
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):

Shell
tinygo build -o plugin.wasm -scheduler=none -target=wasi ./main.go

The three pitfalls

fail_open: false is the dangerous default

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.

Wrong WASM target = silent abort

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.

Body buffering eats RAM

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

Hands-on · Build, load, break, observe
  1. Clone any proxy-wasm-rust-sdk example, build it, mount the .wasm next to your Envoy config:
Shell
docker run --rm -v $PWD:/etc/envoy/plugins \
  -p 10000:10000 \
  envoyproxy/envoy:v1.32-latest -c /etc/envoy/envoy.yaml
  1. curl -i localhost:10000/get — confirm the x-wasm header appears on the response.

  2. Add a panic!("boom") inside on_http_response_headers, rebuild. With fail_open: false the next request returns 503; flip to true and confirm requests pass through with no header added.

  3. Watch http://localhost:9901/stats?filter=wasm_vm — you'll see the failure counter increment every time the panic fires.

Real-world example · Ledgerly (B2B fintech)
Problem
Each enterprise customer has contractual requirements to redact different PII fields from response logs before they leave the cluster. They run Envoy as their north-south gateway and don't want to patch upstream services or write a custom Lua-per-customer mess.
Envoy solution
One Rust WASM plugin (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 pipelineLua
Heavy logic, language choice, sandboxWASM
AI/ML libs, Python/non-WASM toolchain, full body mutationext_proc
Stateful logic that must scale across podsext_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."

Quick check

Which combination is required to load a TinyGo proxy-wasm plugin in Envoy?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3