WebAssembly: Deploy and Secure a Wasm App
Deploying a Wasm app means serving it right and guarding it. Cache-busting is a new file name per build.
Serve it locally
A local server beats opening the file directly.
python3 -m http.server 8000Set the deploy headers
These headers label the module and guard its origin.
Content-Type: application/wasm
Cross-Origin-Resource-Policy: same-originExample
const B64 = "AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABags=";
(async () => {
const bytes = Uint8Array.from(atob(B64), c => c.charCodeAt(0));
const { instance } = await WebAssembly.instantiate(bytes, {});
console.log("2 + 3 =", instance.exports.add(2, 3));
})();Key points
- Deploying is the work of putting an app on a server.
- Serve .wasm as application/wasm on every deploy.
- Use content-hashed names so old files never mix with new ones.
- Use a local server for dev, since file paths block loading.
- Sandboxing keeps a module apart from the rest of the page.
- Memory safety is a Wasm guard against wild writes.
- Check the supply chain, since modules come from many sources.
- Safe deploys pair the right headers with the right names.
In this example, the code decodes the module bytes from B64 and instantiates the module. It calls add with 2 and 3 and logs the sum. The loader proves the module loads, while headers and hashed names are deploy steps around it.
Note
CORS is the set of rules for loading files from another site.
Try it Yourself
Edit the code, then press Run.
Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.
Exercise
Why use a content-hashed file name?