The Go template engine that matched Shopify best had no limits at all

An email template is a compiler behind a login page

Notifuse is a self-hosted email platform written in Go. Its users write their own templates, MJML for layout and Liquid for personalization, and the server compiles whatever gets pasted into the editor. That makes the template stack a compiler with a login page in front of it.

The maintainer’s audit method was blunt. He had AI generate templates designed to hurt the server, then ran the corpus against every MJML library and every Liquid library he could find for Go. No threat model document first, attacks second. The write-up lists what came back.

What hostile templates actually did

The first failure is concurrency. Two emails rendered at once in the same process brought one MJML library down. A panicking goroutine takes the whole process with it, so the blast radius is every request in flight, not the one that tripped it. If the triggering template is still queued, the restart repeats.

The second failure is an escaping bug with teeth. One library turns an escaped quote entity back into a raw quote on the way out, and the render writes an onclick attribute into HTML that the template author never wrote. Escape on input, un-escape on output, and the escaping did nothing. A quote character is enough to exploit it.

Neither attack needs a clever payload.

Liquid made a cleaner experiment, because the failure modes are countable. Four templates went in: a loop of a billion iterations, a string doubled 40 times, a billion bytes of output, and 100,000 nested blocks. The Go library whose output is closest to Shopify’s own gem has no iteration cap, no output cap and no depth cap, so each of the four runs it out of time or memory. The best of the others stopped three.

The faithfulness was the bug. A close port of Shopify’s gem inherits Shopify’s assumption that the template author is a paying customer, not a stranger with an account.

Correctness is the cheap failure next to wall-clock time. Hand the MJML libraries a template with 1 MB of inline styles and the ones that accept it take one to five minutes to return. A few dozen of those in the queue retire the instance without a single crash.

Output quality was mediocre too. The best existing Go MJML library produced HTML identical to reference MJML on 82.4% of the test documents. For email, that drift is not cosmetic: Outlook is why anyone still writes MJML instead of table soup by hand.

What the two rewrites measure

So he wrote both engines. safe-mjml started as a fork of Preslav Rachev’s gomjml and now follows MJML 5.4.1; version 1.1 writes identical HTML on 99.3% of 8,093 documents, with the rest deliberate, including MJML’s habit of emitting NaN as a width. liquidgo is a file-by-file port of Shopify’s Ruby gem, so each upstream release can be carried across. It matches Shopify’s output on 94.2% of 963 templates. Both are MIT licensed and pure Go, which matters when the engine has to ship inside a single static binary instead of a container with a Ruby runtime.

go get github.com/Notifuse/safe-mjml/mjml
go get github.com/Notifuse/liquidgo/v5

The safety rules are cheap, which is the point. A panic comes back as an error, nesting depth is bounded, output is capped at 16 MB, and attribute values are always escaped. None of that shows up in a benchmark on a normal template.

If a template can panic the renderer, the person who wrote the template controls the process. Error returns are how you take that control back, and an output cap is the same decision applied to memory.

liquidgo also refuses to read Go values by reflection. I care about that more than the benchmarks. Reflective engines reach fields and methods the caller never meant to publish, and they dispatch on method names the template supplies. A renderer that only sees the data you handed it is easier to reason about at 3 a.m.

The speed number deserves a caveat. A newsletter loop over 50 products renders in 53 microseconds, against 368 for Shopify Liquid on Ruby 3.3 with YJIT. Most of that gap is Go versus Ruby, and quoting it as an engine comparison would be dishonest.

The percentages are where I would keep watching. A 5.8% gap over 963 templates means the port diverges at the edges, and “the differences are deliberate and written down” is the only defense that survives a real customer template. The MJML side carries the same exposure at 0.7%: the reference output is the spec, so every diff needs a reason on record.

More templates would prove nothing new. The test I would run is 100 concurrent renders of the template that used to kill the old library, with p99 latency and peak RSS measured. The original bug was concurrency, and a single-threaded fidelity score cannot see it.

Press ⌘K to search & switch themes