DEV Community

Cover image for Your Go MJML and Liquid libraries fail on hostile templates. I tested them all.
Pierre
Pierre

Posted on

Your Go MJML and Liquid libraries fail on hostile templates. I tested them all.

While auditing the security of Notifuse, a self-hosted email platform I maintain in Go, I asked AI to generate hostile templates and ran them against every MJML library and every Liquid library I could find for the language.

Notifuse users write their own email templates, in MJML for the layout and Liquid for the personalization, and my server compiles whatever they paste in. Anyone with an account can paste in something written to hurt the server.

What broke

One MJML library crashes the whole process when two emails render at the same time. Another turns an escaped " back into a raw quote, so data that was escaped correctly still adds an onclick attribute to the HTML. With 1 MB of inline styles, the libraries that accept the template take between one and five minutes to render it.

For Liquid I used four templates: 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 has no limits at all, so each of the four runs it out of time or memory. The best of the others stopped three.

The Go MJML libraries didn't match MJML's output well either. The best produced the same HTML as MJML on 82.4% of my test documents, and Outlook punishes anything that drifts from it.

So I wrote two engines

I want both to be the best implementations in the world, in any language: safe-mjml for MJML and liquidgo for Liquid. That is a big claim, and the rest of this post is how I measure it.

safe-mjml is an MJML compiler in pure Go. It started from gomjml, Preslav Rachev's Go port, which gave me a big head start. Version 1.1 follows MJML 5.4.1 and writes identical HTML on 99.3% of 8,093 test documents. Most of the remaining differences are on purpose, such as MJML writing NaN as a width.

For hostile templates, a panic comes back as an error. Nesting depth is limited, the HTML output is capped at 16 MB, and attribute values are always escaped.

liquidgo is a port of Shopify's Ruby gem, done file by file so that each Shopify release can be carried over. It stops all four hostile templates, as Shopify's own gem does, and a template never reads your Go values by reflection. Its output is identical to Shopify Liquid's on 94.2% of 963 templates, and the rest are differences I chose and wrote down. A newsletter template that loops over 50 products renders in 53 µs, against 368 µs for Shopify Liquid on Ruby 3.3 with YJIT.

Try them

go get github.com/Notifuse/safe-mjml/mjml
go get github.com/Notifuse/liquidgo/v5
Enter fullscreen mode Exit fullscreen mode

Both are MIT licensed and pure Go.

Where this runs: Notifuse 44

Both engines render every email in Notifuse 44, which came out on November 19. It is the largest release so far, with 194 changelog entries. I rebuilt almost every screen of the console on a new design system, and the editor now writes MJML 5. The release post has the screenshots.

There is a live demo, and the code is on GitHub. If one of your templates makes safe-mjml or liquidgo disagree with the reference implementation, open an issue with it and I'll fix it.

Top comments (0)