waf-wasm 0.2.0

Proxy-Wasm runtime (on wasmi) for Light WAF: load .wasm filters as detection modules, with instance pooling and fuel/memory DoS caps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// SPDX-FileCopyrightText: 2026 0x00spor3
// SPDX-License-Identifier: Apache-2.0

//! The runtime: compile a Proxy-Wasm guest, drive its request-path lifecycle per request,
//! and expose it as a [`WafModule`]. See ARCHITECTURE §9 and the B3 plan.
//!
//! - **Linker**: the implemented subset ([`crate::host::add_to_linker`]) plus a dynamic
//!   stub for every other import the guest declares, so instantiation never fails on a
//!   missing import while the coverage gap stays explicit ([`crate::report`]).
//! - **Pool**: pre-instantiated `(Store, Instance)` pairs behind a `Mutex` + `Condvar`.
//!   Checkout blocks up to `checkout_timeout`; exhaustion fails closed (the
//!   `on_internal_error` binary). A `Store` is never shared across threads.
//! - **DoS**: fuel reset per request, a memory cap; any trap fails closed (`Reject{500}`).

use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};

use tracing::warn;
use waf_core::{Decision, Phase, RequestContext, WafModule};
use wasmi::{
    Config, Engine, Instance, Linker, Module, Store, StoreLimitsBuilder, Val, ValType,
};

use crate::abi::Status;
use crate::host::{add_to_linker, HostState, LocalResponse};
use crate::marshal::{header_value, RequestView};
use crate::report::{self, ImportReport};

const ROOT_CTX: i32 = 1;
const HTTP_CTX: i32 = 2;

/// Tunables for a plugin instance set.
#[derive(Debug, Clone)]
pub struct WasmOptions {
    pub pool_size: usize,
    pub fuel_per_request: u64,
    pub max_memory_bytes: usize,
    pub checkout_timeout: Duration,
}

impl Default for WasmOptions {
    fn default() -> Self {
        WasmOptions {
            pool_size: 4,
            fuel_per_request: 10_000_000,
            max_memory_bytes: 16 * 1024 * 1024,
            checkout_timeout: Duration::from_millis(100),
        }
    }
}

/// Errors building a plugin (load-time, before serving).
#[derive(Debug)]
pub enum WasmError {
    Compile(String),
    Instantiate(String),
    MissingMemory,
    MissingAllocator,
    VmStart(String),
}

impl std::fmt::Display for WasmError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WasmError::Compile(e) => write!(f, "wasm compile failed: {e}"),
            WasmError::Instantiate(e) => write!(f, "wasm instantiate failed: {e}"),
            WasmError::MissingMemory => write!(f, "guest exports no linear memory"),
            WasmError::MissingAllocator => {
                write!(f, "guest exports no allocator (proxy_on_memory_allocate/malloc)")
            }
            WasmError::VmStart(e) => write!(f, "guest vm/configure start failed: {e}"),
        }
    }
}

impl std::error::Error for WasmError {}

struct Pooled {
    store: Store<HostState>,
    instance: Instance,
}

struct Runtime {
    name: String,
    fuel: u64,
    checkout_timeout: Duration,
    pool: Mutex<Vec<Pooled>>,
    available: Condvar,
}

/// A Proxy-Wasm plugin exposed as a [`WafModule`]. Build with [`WasmModule::from_bytes`].
pub struct WasmModule {
    id: String,
    runtime: Arc<Runtime>,
}

impl WasmModule {
    /// Compile `wasm`, build a pool of `opts.pool_size` instances, run each guest's VM
    /// start/configure with `config`, and return the module plus its import report.
    pub fn from_bytes(
        name: &str,
        wasm: &[u8],
        config: &[u8],
        opts: &WasmOptions,
    ) -> Result<(WasmModule, ImportReport), WasmError> {
        let mut engine_cfg = Config::default();
        engine_cfg.consume_fuel(true);
        let engine = Engine::new(&engine_cfg);
        let module =
            Module::new(&engine, wasm).map_err(|e| WasmError::Compile(e.to_string()))?;

        let report = report::classify(&module);

        // One linker (store-independent) reused for every pool instance.
        let mut linker: Linker<HostState> = Linker::new(&engine);
        add_to_linker(&mut linker).map_err(|e| WasmError::Instantiate(e.to_string()))?;
        register_stubs(&mut linker, &module)
            .map_err(|e| WasmError::Instantiate(e.to_string()))?;

        let mut pool = Vec::with_capacity(opts.pool_size.max(1));
        for _ in 0..opts.pool_size.max(1) {
            pool.push(build_instance(&engine, &linker, &module, config, opts)?);
        }

        let runtime = Arc::new(Runtime {
            name: name.to_string(),
            fuel: opts.fuel_per_request,
            checkout_timeout: opts.checkout_timeout,
            pool: Mutex::new(pool),
            available: Condvar::new(),
        });
        Ok((WasmModule { id: format!("wasm:{name}"), runtime }, report))
    }

    fn checkout(&self) -> Option<Pooled> {
        let rt = &self.runtime;
        let mut guard = rt.pool.lock().unwrap();
        let deadline = Instant::now() + rt.checkout_timeout;
        loop {
            if let Some(p) = guard.pop() {
                return Some(p);
            }
            let now = Instant::now();
            if now >= deadline {
                return None;
            }
            let (g, _to) = rt.available.wait_timeout(guard, deadline - now).unwrap();
            guard = g;
        }
    }

    fn checkin(&self, p: Pooled) {
        self.runtime.pool.lock().unwrap().push(p);
        self.runtime.available.notify_one();
    }
}

impl WafModule for WasmModule {
    fn id(&self) -> &str {
        &self.id
    }

    fn phase(&self) -> Phase {
        Phase::Body
    }

    fn init(&mut self, _cfg: &waf_core::Config) {
        // Compilation + VM start happen in `from_bytes`, before serving.
    }

    fn inspect(&self, ctx: &RequestContext) -> Decision {
        let view = view_from(ctx);
        let mut pooled = match self.checkout() {
            Some(p) => p,
            // Pool exhausted within the timeout: fail closed (on_internal_error binary).
            None => {
                warn!(plugin = %self.runtime.name, "wasm pool exhausted -> fail closed");
                return reject_500(&self.runtime.name);
            }
        };
        let decision =
            run_request(&mut pooled.store, &pooled.instance, view, &ctx.request_id, self.runtime.fuel, &self.runtime.name);
        self.checkin(pooled);
        decision
    }

    /// CRS-like: the host cannot prove a WASM plugin inert, so it runs on every request.
    fn structural(&self) -> bool {
        true
    }
}

/// Instantiate one pool member and run its VM start/configure.
fn build_instance(
    engine: &Engine,
    linker: &Linker<HostState>,
    module: &Module,
    config: &[u8],
    opts: &WasmOptions,
) -> Result<Pooled, WasmError> {
    let limiter = StoreLimitsBuilder::new().memory_size(opts.max_memory_bytes).build();
    let mut store = Store::new(engine, HostState::new(limiter));
    store.limiter(|s| &mut s.limiter);
    store.data_mut().config = config.to_vec();
    // VM start can run guest code -> bound it with one request's fuel budget.
    store.set_fuel(opts.fuel_per_request).ok();

    let instance = linker
        .instantiate_and_start(&mut store, module)
        .map_err(|e| WasmError::Instantiate(e.to_string()))?;

    // Cache memory + allocator for the host functions.
    let mem = instance.get_memory(&store, "memory").ok_or(WasmError::MissingMemory)?;
    let alloc = crate::abi::ALLOC_EXPORTS
        .iter()
        .find_map(|n| instance.get_typed_func::<i32, i32>(&store, n).ok())
        .ok_or(WasmError::MissingAllocator)?;
    store.data_mut().mem = Some(mem);
    store.data_mut().alloc = Some(alloc);

    // Proxy-Wasm SDKs register their root-context factory in a WASI-reactor initializer
    // (`_initialize`, or `_start` from the older `main!` macro). wasmi does NOT call it
    // automatically (it only runs a wasm `start` section, which these modules lack), so the
    // SDK's context map would be empty and `proxy_on_context_create` would panic. Call it
    // once here, before the lifecycle.
    for init in ["_initialize", "_start"] {
        if instance.get_func(&store, init).is_some() {
            call_opt(&mut store, &instance, init, &[])
                .map_err(|e| WasmError::VmStart(format!("{init}: {e}")))?;
            break;
        }
    }

    // Root context + vm start + configure.
    call_opt(&mut store, &instance, "proxy_on_context_create", &[ROOT_CTX, 0])
        .map_err(|e| WasmError::VmStart(e.to_string()))?;
    if let Some(r) = call_opt(&mut store, &instance, "proxy_on_vm_start", &[ROOT_CTX, 0])
        .map_err(|e| WasmError::VmStart(e.to_string()))?
    {
        if r == 0 {
            return Err(WasmError::VmStart("proxy_on_vm_start returned failure".into()));
        }
    }
    if let Some(r) =
        call_opt(&mut store, &instance, "proxy_on_configure", &[ROOT_CTX, config.len() as i32])
            .map_err(|e| WasmError::VmStart(e.to_string()))?
    {
        if r == 0 {
            return Err(WasmError::VmStart("proxy_on_configure returned failure".into()));
        }
    }

    Ok(Pooled { store, instance })
}

/// Register a dynamic stub for every func import the runtime does not implement. The stub
/// returns `Status::Unimplemented` for single-`i32` results (the honest answer the SDK maps
/// to an error) and zeros otherwise.
fn register_stubs(linker: &mut Linker<HostState>, module: &Module) -> Result<(), wasmi::Error> {
    for import in module.imports() {
        let Some(func_ty) = import.ty().func().cloned() else { continue };
        let name = import.name();
        if report::IMPLEMENTED.contains(&name) {
            continue;
        }
        let name = name.to_string();
        let res_types: Vec<ValType> = func_ty.results().to_vec();
        let unimpl = res_types.len() == 1 && res_types[0] == ValType::I32;
        // Whether *invoking* this stub changes detection semantics (paletto #4). A real
        // SDK *declares* every host import, so the accurate "degraded" signal is a runtime
        // call, not a declared import — warn once, when actually hit.
        let semantic = report::SEMANTIC.contains(&name.as_str());
        let call_name = name.clone();
        linker.func_new(
            "env",
            &name,
            func_ty,
            move |mut caller, _params: &[Val], results: &mut [Val]| {
                if semantic && caller.data().semantic_stub_hit.is_none() {
                    caller.data_mut().semantic_stub_hit = Some(call_name.clone());
                    warn!(
                        host_call = %call_name,
                        "wasm plugin invoked a stubbed semantic host call -> Unimplemented (DEGRADED at runtime)"
                    );
                }
                for (slot, ty) in results.iter_mut().zip(res_types.iter()) {
                    *slot = zero_val(*ty);
                }
                if unimpl {
                    if let Some(s) = results.get_mut(0) {
                        *s = Val::I32(Status::Unimplemented.code());
                    }
                }
                Ok(())
            },
        )?;
    }
    Ok(())
}

fn zero_val(t: ValType) -> Val {
    match t {
        ValType::I32 => Val::I32(0),
        ValType::I64 => Val::I64(0),
        ValType::F32 => Val::F32(0.0f32.into()),
        ValType::F64 => Val::F64(0.0f64.into()),
        _ => Val::I32(0),
    }
}

/// Build the request view the host exposes to the guest (header names lowercased so
/// property/value lookups are case-insensitive). The HTTP/2-style **pseudo-headers**
/// (`:method`/`:path`/`:authority`/`:scheme`) are injected into the header map because
/// real Proxy-Wasm filters routinely read them there (Envoy convention) — in addition to
/// the `get_property` path.
fn view_from(ctx: &RequestContext) -> RequestView {
    let real: Vec<(String, String)> = ctx
        .headers
        .iter()
        .map(|(k, v)| (k.to_ascii_lowercase(), v.clone()))
        .collect();
    let host = header_value(&real, "host").cloned().unwrap_or_default();
    let path = match &ctx.query {
        Some(q) => format!("{}?{}", ctx.path, q),
        None => ctx.path.clone(),
    };
    let mut headers = vec![
        (":method".to_string(), ctx.method.clone()),
        (":path".to_string(), path.clone()),
        (":authority".to_string(), host.clone()),
        (":scheme".to_string(), "http".to_string()),
    ];
    headers.extend(real);
    RequestView {
        method: ctx.method.clone(),
        path,
        url_path: ctx.path.clone(),
        protocol: ctx.http_version.clone(),
        scheme: "http".to_string(),
        host,
        source_addr: ctx.client_ip.to_string(),
        headers,
        body: ctx.body.clone(),
    }
}

/// Drive one request's callback sequence and map the captured disposition to a `Decision`.
/// Any trap/error fails closed.
fn run_request(
    store: &mut Store<HostState>,
    inst: &Instance,
    view: RequestView,
    request_id: &str,
    fuel: u64,
    name: &str,
) -> Decision {
    store.data_mut().reset_for(view, request_id.to_string());
    if store.set_fuel(fuel).is_err() {
        return reject_500(name);
    }
    match run_callbacks(store, inst) {
        Ok(()) => map_decision(store.data().captured.clone(), name),
        Err(e) => {
            warn!(plugin = %name, error = %e, "wasm request failed (fail-closed)");
            reject_500(name)
        }
    }
}

fn run_callbacks(store: &mut Store<HostState>, inst: &Instance) -> Result<(), wasmi::Error> {
    let num_headers = store.data().req.headers.len() as i32;
    let body_len = store.data().req.body.len() as i32;
    call_opt(store, inst, "proxy_on_context_create", &[HTTP_CTX, ROOT_CTX])?;
    let action = call_opt(store, inst, "proxy_on_request_headers", &[HTTP_CTX, num_headers, 1])?
        .unwrap_or(0);
    if action == 0 {
        // Continue
        call_opt(store, inst, "proxy_on_request_body", &[HTTP_CTX, body_len, 1])?;
    }
    call_opt(store, inst, "proxy_on_done", &[HTTP_CTX])?;
    call_opt(store, inst, "proxy_on_delete", &[HTTP_CTX])?;
    Ok(())
}

fn map_decision(captured: Option<LocalResponse>, name: &str) -> Decision {
    match captured {
        None => Decision::Allow,
        Some(lr) => {
            let rule_id = format!("wasm-{name}");
            let reason = if lr.detail.is_empty() {
                "wasm plugin local response".to_string()
            } else {
                lr.detail
            };
            if lr.status == 403 {
                Decision::Block { rule_id, reason }
            } else {
                Decision::Reject { rule_id, reason, status: lr.status, retry_after: None }
            }
        }
    }
}

fn reject_500(name: &str) -> Decision {
    Decision::Reject {
        rule_id: format!("wasm-{name}"),
        reason: "wasm runtime error (fail-closed)".to_string(),
        status: 500,
        retry_after: None,
    }
}

/// Call an exported function by name if it exists, tolerating ABI arity differences
/// (0.2.0 vs 0.2.1) by padding/truncating `args` to the export's actual param count.
/// Returns the first result as `i32` (or `Some(0)` for a void export, `None` if absent).
fn call_opt(
    store: &mut Store<HostState>,
    inst: &Instance,
    name: &str,
    args: &[i32],
) -> Result<Option<i32>, wasmi::Error> {
    let Some(func) = inst.get_func(&*store, name) else { return Ok(None) };
    let ty = func.ty(&*store);
    let nparams = ty.params().len();
    let params: Vec<Val> = (0..nparams).map(|i| Val::I32(args.get(i).copied().unwrap_or(0))).collect();
    let mut results: Vec<Val> = ty.results().iter().map(|t| zero_val(*t)).collect();
    func.call(&mut *store, &params, &mut results)?;
    Ok(Some(results.first().and_then(|v| v.i32()).unwrap_or(0)))
}

#[cfg(test)]
mod tests {
    use super::*;

    // A guest that imports a SEMANTIC host call (`proxy_http_call`, 10 params) and invokes
    // it — exercising the runtime degradation signal (paletto #4).
    const HTTP_CALL_WAT: &str = r#"
    (module
      (import "env" "proxy_http_call"
        (func $http (param i32 i32 i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))
      (memory (export "memory") 1)
      (func (export "proxy_on_memory_allocate") (param i32) (result i32) (i32.const 16))
      (func (export "run") (result i32)
        (call $http (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0)
                    (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0))))
    "#;

    #[test]
    fn semantic_stub_invocation_returns_unimplemented_and_is_flagged() {
        let mut cfg = Config::default();
        cfg.consume_fuel(true);
        let engine = Engine::new(&cfg);
        let module = Module::new(&engine, wat::parse_str(HTTP_CALL_WAT).unwrap().as_slice()).unwrap();

        // The stub must be classified semantic (so a runtime call flags it).
        let report = report::classify(&module);
        assert_eq!(report.semantic_stubs(), vec!["proxy_http_call"]);

        let mut linker = Linker::new(&engine);
        add_to_linker(&mut linker).unwrap();
        register_stubs(&mut linker, &module).unwrap();

        let limiter = StoreLimitsBuilder::new().memory_size(64 * 1024).build();
        let mut store = Store::new(&engine, HostState::new(limiter));
        store.limiter(|s| &mut s.limiter);
        store.set_fuel(1_000_000).unwrap();
        let inst = linker.instantiate_and_start(&mut store, &module).unwrap();

        let run = inst.get_typed_func::<(), i32>(&store, "run").unwrap();
        let ret = run.call(&mut store, ()).unwrap();
        assert_eq!(ret, Status::Unimplemented.code(), "semantic stub must answer Unimplemented");
        assert_eq!(
            store.data().semantic_stub_hit.as_deref(),
            Some("proxy_http_call"),
            "invoking a semantic stub must flag the instance as degraded at runtime"
        );
    }
}