We never run your page's code

MDX lets a docs page run JavaScript. On a platform that compiles thousands of strangers' pages, we decided the safest sandbox is the one you never need.

Sahil Gadimbayli

Founder & CTO

Engineering4 min read

MDX is Markdown that can run code. That's the point of it: you can drop a React component into a paragraph, import a snippet from another file, or compute a value inline. On your laptop, compiling your own docs, that's a feature.

On a hosting platform it's a different thing. We compile pages written by people we've never met, on the same servers, with the same compiler. A page that runs code there is remote code execution with nicer syntax.

What a page could do

Here's a page that looks harmless in a pull request:

---
title: Quickstart
---

export const key = process.env.DATABASE_URL

Your API key is {key}.

A standard MDX compiler turns that into a JavaScript module and evaluates it. The export runs. The {key} expression runs. If the compiler process can see the server's environment, the page prints a database password into the published site.

It doesn't have to be that obvious. An expression can loop forever, allocate memory until the process dies, or import a module from a URL. Any of those takes down compilation for every other customer waiting in the queue.

The two ways out

The first option is a sandbox: let the code run, but inside something that contains it. Node has a vm module, and there are isolate libraries that go further. We read the fine print. Node's own documentation says the vm module is not a security mechanism and must not be used to run untrusted code. Isolates are stronger, but every one of them is a large surface you have to keep patched forever.

The second option is to not run the code at all. Decide what a docs page is allowed to be, then remove everything else before the compiler sees it.

We took the second one.

What gets removed

The first step of our compile pipeline is a small plugin that walks the page's syntax tree and deletes every construct that can execute:

  • import and export statements;
  • {expressions}, on their own line or inline;
  • spread attributes, like <Card {...props} />;
  • attribute values that aren't literals. cols={3} and open={true} stay, cols={getCols()} goes.

What's left can only create elements. A callout, a set of tabs, a table, a code block. When the tree reaches the part of the compiler that evaluates things, there's nothing left to evaluate but "make this element with these strings".

The same pass removes <script>, <style>, <link>, <meta>, <object> and their relatives, and trims inline styles to layout and typography. A page can set a width or a colour. It can't position itself over the site's header, which is how a page would fake a login form.

What it costs

This has a price, and you should know it before you move docs here. Some MDX that works elsewhere won't work on our platform:

  • snippets imported from other files don't render;
  • components that compute their props from JavaScript lose those props;
  • anything that fetches data at build time is gone.

We decided that's the right trade for a hosted platform. Almost every docs page needs a handful of components and literal values, and we ship those components ourselves: callouts, tabs, steps, cards, accordions, code groups, diagrams and video embeds. If you need a page that runs arbitrary code, you need your own build server, and that's a fine choice for a team that wants it.

Belt and braces

Removing code from the page is the main defence. We assume it'll have a bug one day, so there are two more layers behind it.

The compiler runs in its own process, started with an empty environment. It never receives the application's secrets, and at startup it deletes any variable outside a short allow list. A page that somehow read process.env would find nothing worth stealing.

Each page compiles in a worker thread with a memory cap and a 10-second limit. A page that hangs gets its worker killed and replaced, and the next page in the queue carries on. One pathological file can't stall everyone else.

Diagrams follow the same rule. A Mermaid block compiles to its source text, and the reader's browser draws it. Our servers never run Mermaid on a customer's input.

If you're building something similar

If your product compiles MDX that other people wrote, here's what I'd take from this:

  1. Decide what the language is allowed to be before you decide how to sandbox it. A smaller language is easier to defend than a bigger box.
  2. Strip at the syntax tree, not with regular expressions. MDX has too many ways to write the same thing.
  3. Run the compiler with no secrets, a time limit and a memory limit, even after stripping.
  4. Write down what you removed and why, so the next engineer doesn't add export support back as a quick win.

Typemark has worked this way since October. It means a few imported pages need a small rewrite, and it means nobody's docs can reach anybody else's.

Engineering4 min read

Your docs belong in a Git repository

A docs database is convenient until you need history, review or a way out. Why we keep every page as a file in Git, and what that asks of the editor.

Sahil Gadimbayli

AI and agents4 min read

Your next docs reader is an AI agent

More of your readers ask an assistant instead of opening your docs. Here's what that assistant needs from your site, and how to check what it sees.

Sahil Gadimbayli