Protect page code with a signing key

v0.0.3

Pages, layouts, components and error pages in Mishka CMS can hold Elixir, and that code compiles into the module that serves your site. A code signing key makes the CMS sign each snippet it saves and run only snippets whose signature matches. Code that reaches the database any other way, through an injection bug, a wrong permission or a tampered backup, compiles to a step that does nothing. A write to your database can then no longer become code running on your server.

Signing is off until you set a key. This guide turns it on, checks the result and signs stored code again after you restore a database.

How code signing works

With a key set, every save signs the code with HMAC-SHA256 and stores the signature beside it. Before a snippet is compiled, the CMS signs its code again and compares the two in constant time. Line endings and trailing spaces are normalized first, so a paste from another editor keeps its signature.

RecordFields that are signed
Pageevents, infos, state, helpers
Layouthelpers
Componenthelpers
Error pagehelpers

A snippet with no signature or a wrong one compiles to a clause that does nothing, and the rest of the page compiles and serves as usual. The log names it:

bash
[CodeSignature] handler "sign_in" is not signed for this deployment and will do nothing.

Signing does not decide who may write code. Permissions do that, as How permissions work explains. Signing decides whether stored code was written by this application at all.

Before you begin

  • A deployment set up as in Deploy Mishka CMS to production, where you can set environment variables and restart the node.
  • For a database that already holds pages, time to sign the stored code once, as the last step below says.

Turn on code signing

  1. Make a key:
    bash
    openssl rand -base64 32
  2. Set it as the CODE_SIGNING_KEY environment variable of the deployment. The host reads it in config/runtime.exs, in every environment:
    elixir
    config :mishka_cms_core, code_signing_key: System.get_env("CODE_SIGNING_KEY")
  3. Keep the same key on every deploy, and keep it out of version control. A new key leaves every stored snippet unverified.
  4. Restart the node. On a database that already holds code, sign it now, as Sign stored code again shows.

Check the boot log

At boot the CMS counts the stored snippets this key cannot verify. When the count is 0 it says nothing. Otherwise it logs one warning with the total and the command that signs them:

bash
[CodeSignature] 12 stored snippet(s) carry no signature this deployment can verify, and every one of them compiles to a clause that does nothing

The lines above it name each snippet. Read them before you sign anything: a count you did not expect can mean someone changed code in the database.

Sign stored code again

Sign stored code when you turn signing on for a database that already holds code, or after you restore a database from another environment, whose signatures were made with its own key.

A release has no Mix, so call it on the running node:

bash
/app/bin/mishka_cms rpc 'MishkaCmsCore.Runtime.Authoring.CodeAudit.resign()'

It answers {:ok, count} with the number of rows it wrote, or {:error, :signing_off} when no key is set. With the node stopped, start the application inside eval first:

bash
/app/bin/mishka_cms eval 'Application.ensure_all_started(:mishka_cms); MishkaCmsCore.Runtime.Authoring.CodeAudit.resign()'

In development, the Mix task does the same:

bash
CODE_SIGNING_KEY=... mix mishka.resign_code

Restart the node afterwards, so the pages compile with their new signatures.

Stop code written by hand with PAGE_CODE

Signing keeps foreign code from running. To stop people writing server code at all, set PAGE_CODE=off in production. Nobody can then store Elixir or hand-written HEEx in a page, layout, component or error page, master users included.

PAGE_CODE=off stops new code from being stored. It does not stop code already in the database from compiling, so keep the signing key set as well. JS hooks and stylesheets run in the browser, not on the server, and stay editable.

Next steps