Fix "Wasm code generation disallowed by embedder" in a Chrome extension
7 min readModerok team
Why WebAssembly throws "Wasm code generation disallowed by embedder" in an MV3 extension, the wasm-unsafe-eval manifest fix, and what still breaks after it.
If compiling WebAssembly in your extension fails with CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder, add 'wasm-unsafe-eval' to the extension_pages policy in your manifest:
{
"manifest_version": 3,
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';"
}
}
Reload the extension from chrome://extensions so the new manifest is read, and the same .wasm file compiles. Your module, your toolchain and your fetch call were never the problem. WebAssembly is off by default in extension pages and the background service worker, and the 'wasm-unsafe-eval' source is what turns it back on. The message names whichever entry point you called, so instantiateStreaming() prints WebAssembly.instantiateStreaming(): Wasm code generation disallowed by embedder instead. Content scripts are the exception, covered further down.
Why "Wasm code generation disallowed by embedder" happens
The refusal is not really about your module. The "embedder" is the browser hosting the WebAssembly engine, and the complaint is that this context may not generate wasm code at all. What decides that is a Content Security Policy you never wrote. Chrome's Content Security Policy manifest reference gives the MV3 default as script-src 'self'; object-src 'self'; and states the consequence outright: "the extension will only load local scripts and objects from its own packaged resources. WebAssembly will be disabled, and the extension won't run inline Javascript or be able to evaluate strings as executable code."
So the default is not "wasm from remote origins is blocked" but "wasm is disabled", including a .wasm inside your own package: compiling it counts as code generation in the same family as eval(), and the default forbids the family.
Which contexts does that cover? The same page says the "extension pages" policy "applies to page and worker contexts in the extension. This would include the extension popup, background worker, and tabs with HTML pages or iframes that were opened by the extension." A popup and the service worker therefore fail identically, and one key fixes both.
The manifest key, in full
Two details in that snippet matter more than they look.
Keep object-src. The browsers differ here, per MDN's content_security_policy reference: Chrome requires the directive and falls back to its own object-src 'self' with a logged warning if it is missing or judged insecure, while Firefox has treated it as optional since Firefox 106.
Do not reach for 'unsafe-eval' as well. Chrome enforces a minimum policy for extension pages, and that minimum is exactly script-src 'self' 'wasm-unsafe-eval'; object-src 'self';. The reference is blunt about the ceiling: "The extension_pages policy cannot be relaxed beyond this minimum value. In other words, you cannot add other script sources to directives, such as adding 'unsafe-eval' to script-src." A manifest that tries anyway does not fail quietly at runtime, it fails at load. The reference: "If you add a disallowed source to your extension's policy, Chrome will throw an error like this at install time", followed by the error itself:
'content_security_policy.extension_pages': Insecure CSP value "'unsafe-eval'" in directive 'script-src'.
For wasm, 'wasm-unsafe-eval' is the entire budget. Chrome's Improving security in Manifest V3 guide gives the values script-src, object-src and worker-src may take inside extension_pages: self, none, wasm-unsafe-eval, and "Unpacked extensions only: any localhost source", which does not survive packaging. Sandboxed pages run under a separate policy and are not restricted this way. If you are also fighting Refused to execute inline script, that is the same policy from the other side: the inline-script post covers why no manifest key relaxes it.
Loading the .wasm file after the policy is fixed
The policy decides whether a module compiles. Getting the bytes is a separate problem with its own MV3 rule, and the same security guide puts it in one line: "Move all external code (JS, Wasm, CSS) into your extension bundle." A .wasm fetched from your CDN is remotely hosted code however well your CSP is configured.
Ship the file and build its URL at runtime:
// background.js (the service worker)
const url = chrome.runtime.getURL("crypto.wasm");
async function loadModule() {
const response = await fetch(url);
const bytes = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(bytes, {});
return instance.exports;
}
It is the compile step the policy gates, so changing how you fetch the bytes will not move the embedder error. If the module arrives through generated glue JavaScript you import, the service worker also has to be a module for that statement to parse: "background": { "service_worker": "background.js", "type": "module" }.
What still breaks after the fix
Glue code that calls eval or new Function. The most common second failure is not a wasm failure. Toolchain glue sometimes builds functions from strings, 'wasm-unsafe-eval' does not cover that, and the minimum policy is the ceiling, so there is no source left to add.
The documented escape hatch is a sandboxed page. Chrome's sandbox reference says such a page "is not subject to the Content Security Policy (CSP) used by the rest of the extension (it has its own separate CSP value). This means that, for example, it can use inline script and eval()", and, one line above, the price: it "won't have access to extension APIs". It takes more than a CSP key, though. The file goes under the "sandbox": { "pages": ["sandbox.html"] } manifest key, and something has to host it: the sandboxing eval guide loads "this sandboxed page into our extension via an iframe", and a service worker has no document to put an iframe in. From a worker you need an offscreen document for that, which means the "offscreen" permission and a chrome.offscreen.createDocument() call with a url, the IFRAME_SCRIPTING reason and a justification, per the offscreen API reference. Every call into the module is then a postMessage round trip.
Content scripts have their own CSP. If the code that needs the module runs in a content script rather than in the worker or a popup, the extension_pages key is not what governs it, and an embedder error there means something else. Chrome's content scripts guide prints the policy they get: "Content scripts running in isolated worlds have the following Content Security Policy (CSP)", and that policy already contains 'wasm-unsafe-eval' in its script-src, whatever your manifest says. The case that does fail is the other injection target: "When a content script is injected into the main world, the CSP of the page applies", so a page with a strict policy of its own blocks your wasm and nothing in your manifest overrides it.
A content script still cannot read the file. Compiling and fetching are separate questions. The same guide, on reaching packaged files: "You can also access other files in your extension from a content script, using APIs like fetch(). To do this, you need to declare them as web-accessible resources. Note that this also exposes the resources to any first-party or third-party scripts running on the same site." The entry is what makes your extension detectable, a trade-off covered in the web_accessible_resources post.
The compiled instance does not survive the worker. A module instantiated in a service worker lives in that worker's memory, and the worker is disposable. When Chrome shuts it down the instance goes too, and the next event instantiates again before it can call your exports. Cache it in a module-scope variable so repeat events in one worker lifetime reuse it, keep the instantiation in the handler's async path rather than at top level, and expect first-call latency after every cold start. Why the service worker keeps stopping covers what resets that clock.
Firefox differences
Firefox lands in the same place by a different route. MDN's content_security_policy reference states the MV3 restriction in the same terms: "In Manifest V3, all CSP sources that refer to external or non-static content are forbidden in CSP directives covering script content. The only permitted values are 'none', 'self', and 'wasm-unsafe-eval'." The policy at the top of this post therefore satisfies both browsers, object-src included for Chrome's benefit, and MDN notes the same localhost exception for development.
The trap is testing against an older Firefox build of the same extension. MDN: "For backward compatibility, Manifest V2 extensions in Firefox can use WebAssembly without the use of 'wasm-unsafe-eval'. However, this behavior isn't guaranteed." An MV2 Firefox build that has always compiled wasm with no CSP key is on that compatibility path, not proof your manifest is right. MDN: "Extensions using WebAssembly are therefore encouraged to declare 'wasm-unsafe-eval' in their CSP."
Catching it in a published build
The error is easy to fix and easy to miss, because it usually fires in a service worker with no DevTools window attached. Locally you see it as soon as you open the inspector. In a shipped build, a rejected compile in a worker nobody is watching looks like a feature that quietly does nothing.
Moderok's SDK records uncaught exceptions and unhandled promise rejections as __error events in every context where you call Moderok.init(), so a CompileError that rejects after init() has run in that worker reaches your dashboard, not just a console nobody opens. Repeats are deduplicated and rate limited, so you get the signal rather than one event per cold start. Capture starts when init() does, so a module instantiated at the top of the file is the case you have to wrap yourself. See the error tracking docs for what is captured automatically and what needs an explicit captureError call.