Sandboxed templates are not a guarantee

A sandbox flag is a claim about an engine. A template you did not write, compiled from caller text, is still code you chose to run.

Lesson 66 of 69 · 15 min read · practice only with authorization

Diagram

From caller text to a template the server compiles

Follow the path. One example at a time.

Step 1 of 4

Field

A greeting, a theme, or a notification body arrives from a customer or an admin.

Illustration: the field is a variable
display_name = "Avery"
# The name is data for a template file you wrote.
What changes the outcome
Source

The field is the template.

Engine

A sandbox flag may or may not disable the operations you fear.

Render

The output is whatever the engine did.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. A sandbox is a bet

    What fails
    Sandboxes miss features, change between versions, and get turned off for one template that “needed” more. A file you wrote does not depend on that bet.
    Who is affected
    Users whose data the render can see, and the host if the engine can reach it.
    What you would notice
    A configuration flag as the only control, plus an editor that stores template source.
    What you change
    Delete user-supplied template source. Render files from the deployment artifact.
  2. Admins are callers too

    What fails
    An admin theme editor is still untrusted input if a stolen admin session or a malicious admin is in your model. The template runs with the server’s power, not the admin’s browser power.
    Who is affected
    Everyone the application can act as.
    What you would notice
    A CMS that stores templates in the database and compiles them.
    What you change
    Ship templates in the release. Let admins edit data, not source.
  3. What the engine can see

    What fails
    The render context often includes helpers, configuration, and other users’ fields if you put them there. Minimize that object.
    Who is affected
    People whose records were placed on the context “in case the template wants them.”
    What you would notice
    A render call that passes the whole request or the whole database row set.
    What you change
    Pass the few fields the template prints. Nothing else.
  4. Escaping is the default

    What fails
    Auto-escape has to be on for the format you emit. Marking a variable safe is a review item, not a habit.
    Who is affected
    The next person who views the rendered page.
    What you would notice
    A filter that marks all output safe so the designer can write HTML.
    What you change
    Escape by default. Allow formatted HTML only through the sanitizer path, not through the template language.

Template source is code

A template engine compiles a string into a program that runs while you render a page, an email, or a PDF. If that string comes from a customer, a content editor, or a row in the database, you are running code you did not review, inside the server. That is server-side template injection, including when the product calls the feature “personalization.”

Sandbox modes promise to disable the dangerous operations. Some do, for a version, for a subset of operations, until someone enables a helper. You should not bet the host on that promise when the alternative is obvious: the template is a file in the repository, reviewed like the rest of the code, and the personalized part is a variable.

An admin editor is still a compiler

It is tempting to allow template editing for staff because staff are trusted. The template does not run as staff. It runs as the application, with the application’s data and often the application’s operating-system user. A stolen staff session, or a staff account that should not have had that power, becomes server code execution in the worst engines and data exposure in the milder ones.

Let staff edit content that is data: a sentence, a color token from a list, a feature flag. Ship layout changes as a deploy. If you truly need per-tenant layout, give them a constrained set of slots, not a template language.

The context object is the data boundary

Engines print whatever you place on the context, and some will call methods on those objects. Pass the name, the order id, and the already-authorized fields. Do not pass the request, the database handle, or every tenant’s rows “so the template can choose.” Auto-escape the format you render. A mark-as-safe on the user’s string puts you back in the stored XSS problem as well.

You would notice a render call whose first argument is a string from the database, a sandbox setting with no test, and a context that includes a large object graph. The people affected are the owners of that data and, if the engine can touch the process, everyone on the host. The finding says to stop compiling that string. It does not include a template that performs the failure.

What you change

Move templates into the artifact. Replace the editor with fields. Turn auto-escape on. Delete the sandbox exception path so a later change cannot “just enable” user templates. Review helpers registered on the engine. A helper that shells out or reads files is a capability the template language did not need.

Add a test that a variable containing punctuation is rendered as those characters, and that there is no code path that compiles a request body. The second test is the one that keeps this bug from returning under a new product name.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

Customers can edit the notification body, and that body is compiled as the template. A sandbox flag is enabled. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapServer-side template injection: user text compiled as a templateHow SSTI happens when user input becomes template source instead of template data, how to recognize it with a harmless arithmetic probe in an authorized test, and why passing data to a fixed template is the fix.
  1. 01Templates in email and PDFA message body or a PDF layout is still a template.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.

Glossary

  1. CardServer-side template injectionUser input is evaluated by the server's template engine, which can leak configuration or run template-side code.