Skip to content

HomeBlog

Running untrusted CMS plugins in WebAssembly

How Hatamoto runs third-party plugins in Extism and wazero: always-on timeouts, throwaway instances after a trap, per-plugin Postgres roles, a guarded fetch, and details of the plugin ecosystem.

5 min read
𝕏in

A WordPress plugin is PHP running in the same process, with the same database user and the same filesystem as the site which most of you would know is a security problem. When a plugin has a bug, every site that installed it has the bug. That is the tradeoff WordPress made knowingly or unknowingly.

I am building Hatamoto, a CMS written in Go where I wanted third-party plugins without that trade. This post covers what I built and what WebAssembly does not fix. (The content side, where plugins write only through reviewable changesets)

Attempt one: compile plugins in

The first design made plugins Go packages. You ran hatamoto build, it generated a module that blank-imported your plugins, and you got a custom binary. It was fast and type-safe. It was also wrong. Installing a plugin meant rebuilding the server, and a plugin could call anything in the process. "Untrusted" was not on the table.

Attempt two: Extism on wazero

Plugins are now WebAssembly modules run by Extism on top of wazero. wazero is a pure-Go runtime, so the server is still a single static binary with no cgo. Authors can use any Extism PDK language. In practice most write Go.

By default a guest gets:

  • an empty filesystem

  • no network (Extism's http_request is never granted)

  • a linear-memory cap from its manifest

  • a timeout on every call: 250 ms for filters, 10 s for actions

Everything else goes through one import, hm_call. The host checks it against capabilities the owner granted at install time. A capability the manifest did not request cannot be granted.

Things I learned the hard way

Timeouts have to be always on. If the caller has no deadline, nothing stops a guest stuck in for {}. Every call gets its own context with the plugin's timeout, even when the caller passed none.

Time spent waiting for a free instance and starting a cold one is bounded separately, so a page that calls five filters under load cannot queue for five full timeouts.

A trapped Go guest is poisoned. After a panic or a trap, the Go runtime inside the module is in an unknown state. I did not reuse it. Any instance that saw any error is thrown away. Three traps or timeouts in a row on the same export trip a circuit breaker, and that hook is skipped for 30 seconds.

Pools should be stacks. Instances live in a pool capped by an admission limit. It is LIFO: the next call takes the most recently returned instance. Under light traffic the older instances stay untouched, and a sweeper closes anything idle for 5 minutes, down to one warm instance per plugin. With a FIFO pool, round-robin keeps every instance warm forever.

Compilation is slow, so cache it, and then guard the cache. Compiling a Go guest takes about a second. wazero can cache the compiled native code on disk. That directory now holds machine code the server executes, so it is created 0700 and documented as being as sensitive as the binary.

No locks on the request path. The plugin registry is an immutable map behind an atomic pointer. Loading or unloading a plugin swaps the pointer. Requests never take a host-wide lock.

The sandbox ends at the host call

WASM isolation means little if a host call can do anything. Two host calls carry most of the risk.

Database. Each plugin that asks for db.* gets its own Postgres login role. That role owns one schema and nothing else. There is also an SQL guard in the host, but the integration tests turn the guard off and attack the database directly, because the role is the real boundary. I call it the genius in the system but if you were building a CMS this should have been the most obviouos thing to do.

http.fetch. Only hosts listed in the manifest are allowed. Private, loopback and link-local addresses are refused at dial time. DNS is resolved once, so rebinding cannot swap the address. Every redirect hop goes through the same guard. Proxy environment variables are ignored. If an update widens the host list, the owner has to confirm it again.

What WASM does not fix

  • The visitor's browser. A plugin with no grants can still ship CSS and markup. <img src="https://tracker"> and CSS url() run in the visitor's browser, not in our sandbox. A plugin can track your visitors. CSS attribute selectors can read attributes on the page and leak them through such a request. We limit the damage by keeping per-user secrets out of public HTML (no session, CSRF or other tokens, and the same markup signed in or out). But the real answer is: install plugins from publishers you trust.

  • Script injection through templates. Plugin templates without the site.scripts grant are checked by a scanner. It replaces every template action with an inert token and runs each branch through x/net/html's tokenizer. The scanner is new and nobody outside the project has tried to break it. Treat the grant as the boundary, not the scanner. ( Please - try to break it for security reasons )

  • Themes. Themes can do anything the template engine can. They are trusted code, and the docs say so.

  • Side channels. Spectre-class attacks against the runtime are out of scope beyond what wazero mitigates.

Try to break it

The code and the security model are on GitHub. Reports of sandbox escapes and capability bypasses are very welcome.

Run it on your own server.

Open source, one Go binary and Postgres. AI analytics start counting from the first request.

curl -fsSL https://hatamoto.dev/install.sh | sudo sh