TExcelTAD
Home › WebAssembly › Security Model

Security Model

Running code downloaded from anywhere is only acceptable if that code can't hurt you. WebAssembly was designed around this from day one. Its guarantees come from a few simple rules, all enforced by the engine.

The guarantees

Property How it's enforced
Memory safety for the host each access is bounds-checked against the instance's own memory; there are no raw pointers to host memory
Control-flow integrity structured control flow, no computed jumps; return addresses live in the engine's stack, invisible to Wasm
Type-safe calls direct calls are validated; call_indirect checks the callee's signature at runtime
No ambient authority a module can only reach the outside world through the imports it was given
Deterministic validation malformed or ill-typed code is rejected before it runs

Capabilities = imports

A module's entire power over the outside world is its import list. The host can inspect it before instantiating anything and refuse modules that ask for too much:

js · try it yourself
// a module that imports "env" "fetch_url" and "env" "log" (bytes built from its WAT)
const bytes = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
  0x01, 0x05, 0x01, 0x60, 0x01, 0x7f, 0x00,                                     // type: (i32) -> ()
  0x02, 0x1b, 0x02,
  0x03, 0x65, 0x6e, 0x76, 0x09, 0x66, 0x65, 0x74, 0x63, 0x68, 0x5f, 0x75, 0x72, 0x6c, 0x00, 0x00,  // env.fetch_url
  0x03, 0x65, 0x6e, 0x76, 0x03, 0x6c, 0x6f, 0x67, 0x00, 0x00,                   // env.log
]);
const module = await WebAssembly.compile(bytes);
const wanted = WebAssembly.Module.imports(module).map((i) => `${i.module}.${i.name}`);
console.log('module asks for:', wanted);

const allowed = new Set(['env.log']);
const denied = wanted.filter((name) => !allowed.has(name));
console.log(denied.length ? `refusing: ${denied.join(', ')}` : 'ok');
module asks for: [ 'env.fetch_url', 'env.log' ]
refusing: env.fetch_url

Plug-in hosts (Envoy, Shopify Functions, database UDFs) use exactly this reasoning: give each module the smallest import set that lets it do its job.

Memory isolation in action

A buggy or malicious module can scribble over its own memory, but nothing else. Addresses past the end trap instead of reading neighbouring data:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (memory 1 1)                                   ;; exactly 64 KiB, cannot grow
  (func $read (param $addr i32) (result i32) (i32.load8_u (local.get $addr)))
  (func (export "main")
    (call $log (call $read (i32.const 65535)))   ;; last byte: fine
    (call $log (call $read (i32.const -1)))))    ;; "address" 0xFFFFFFFF: trap, not a host read
0
RuntimeError: memory access out of bounds

Engines often implement these checks with guard pages: they reserve a large virtual address range for each memory so out-of-bounds accesses hit unmapped pages, making the checks almost free on 64-bit systems.

What Wasm does NOT protect

The sandbox protects the host from the module. It does not make the code inside the module correct:

  • Memory bugs inside the module still exist. A C buffer overflow can corrupt that module's own heap and data, change its behaviour, or leak its secrets. There's no stack canary or ASLR in linear memory by default.
  • The glue code is ordinary JavaScript. If it passes untrusted strings into innerHTML, you still get XSS.
  • Imports are trust boundaries. Exposing a generic eval-like import gives the module everything that import can do.
  • Side channels. Like JavaScript, Wasm can be used for timing attacks (Spectre). That's why SharedArrayBuffer and shared memory require cross-origin isolation (COOP/COEP headers).

This example shows an in-module overflow: writing 12 bytes into an 8-byte "buffer" silently overwrites the adjacent "is_admin" flag, because inside linear memory nothing separates them:

wat · try it yourself
(module
  (import "console" "log" (func $log2 (param i32 i32)))
  (memory 1)
  ;; layout: name buffer [100..107], is_admin flag [108..111]
  (func $set_name (param $src i32) (param $len i32)       ;; no length check: a bug!
    (memory.copy (i32.const 100) (local.get $src) (local.get $len)))
  (data (i32.const 0) "guest")
  (data (i32.const 16) "AAAAAAAA\01\00\00\00")             ;; 12 attacker-controlled bytes
  (func (export "main")
    (i32.store (i32.const 108) (i32.const 0))              ;; is_admin = 0
    (call $set_name (i32.const 0) (i32.const 5))
    (call $log2 (i32.load8_u (i32.const 100)) (i32.load (i32.const 108)))
    (call $set_name (i32.const 16) (i32.const 12))         ;; overflow into the flag
    (call $log2 (i32.load8_u (i32.const 100)) (i32.load (i32.const 108)))))
103 0
65 1

The host is unharmed, but the module's own logic is compromised. Memory-safe source languages (Rust, safe AssemblyScript, managed languages via Wasm GC) and careful bounds checks remain essential.

Hardening checklist

  • Serve .wasm from your own origin (or with correct CORS) and use Subresource Integrity where possible.
  • Keep import surfaces minimal; validate every argument a module passes to an import (pointers and lengths too).
  • Treat data read from Wasm memory as untrusted input in your JS.
  • Use Content Security Policy 'wasm-unsafe-eval' to allow Wasm compilation without allowing JS eval.
  • On servers, use runtime limits: fuel/epoch interruption (Wasmtime), memory caps and timeouts per instance.

Test yourself

What determines what a Wasm module can do outside its sandbox?

  1. Its file size
  2. The imports the host provides
  3. The order of its sections
  4. Its start function

Can a buffer overflow happen inside a Wasm module compiled from C?

  1. No, Wasm prevents all overflows
  2. Yes, it can corrupt the module's own linear memory, but not the host's memory
  3. Yes, and it can overwrite the browser's memory
  4. Only with SIMD

Why do shared Wasm memories require cross-origin isolation?

  1. To save bandwidth
  2. Because shared memory enables high-resolution timers useful for Spectre-style side-channel attacks
  3. Because workers can't run Wasm otherwise
  4. It's only a recommendation